1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * soup-date.c: Date/time handling
5 * Copyright (C) 2005, Novell, Inc.
6 * Copyright (C) 2007, Red Hat, Inc.
17 #include "soup-date.h"
21 * @year: the year, 1 to 9999
22 * @month: the month, 1 to 12
23 * @day: day of the month, 1 to 31
24 * @hour: hour of the day, 0 to 23
25 * @minute: minute, 0 to 59
26 * @second: second, 0 to 59 (or up to 61 in the case of leap seconds)
27 * @utc: %TRUE if the date is in UTC
28 * @offset: offset from UTC
30 * A date and time. The date is assumed to be in the (proleptic)
31 * Gregorian calendar. The time is in UTC if @utc is %TRUE. Otherwise,
32 * the time is a local time, and @offset gives the offset from UTC in
33 * minutes (such that adding @offset to the time would give the
34 * correct UTC time). If @utc is %FALSE and @offset is 0, then the
35 * %SoupDate represents a "floating" time with no associated timezone
39 /* Do not internationalize */
40 static const char *const months[] = {
41 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
42 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
45 /* Do not internationalize */
46 static const char *const days[] = {
47 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
50 static const int nonleap_days_in_month[] = {
51 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
54 static const int nonleap_days_before[] = {
55 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
58 static inline gboolean
59 is_leap_year (int year)
61 return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
64 /* Computes the number of days since proleptic Gregorian 0000-12-31.
65 * (That is, 0001-01-01 is "1", and 1970-01-01 is 719163.
68 rata_die_day (SoupDate *date)
72 day = (date->year - 1) * 365 + ((date->year - 1) / 4) -
73 ((date->year - 1) / 100) + ((date->year - 1) / 400);
74 day += nonleap_days_before[date->month] + date->day;
75 if (is_leap_year (date->year) && date->month > 2)
80 #define TIME_T_EPOCH_RATA_DIE_DAY 719163
83 days_in_month (int month, int year)
85 if (month == 2 && is_leap_year (year))
88 return nonleap_days_in_month[month];
92 soup_date_get_type (void)
94 static volatile gsize type_volatile = 0;
96 if (g_once_init_enter (&type_volatile)) {
97 GType type = g_boxed_type_register_static (
98 g_intern_static_string ("SoupDate"),
99 (GBoxedCopyFunc) soup_date_copy,
100 (GBoxedFreeFunc) soup_date_free);
101 g_once_init_leave (&type_volatile, type);
103 return type_volatile;
107 soup_date_fixup (SoupDate *date)
109 /* We only correct date->second if it's negative or too high
110 * to be a leap second.
112 if (date->second < 0 || date->second > 61) {
113 date->minute += date->second / 60;
115 if (date->second < 0)
119 if (date->minute < 0 || date->minute > 59) {
120 date->hour += date->minute / 60;
122 if (date->minute < 0)
126 if (date->hour < 0 || date->hour > 23) {
127 date->day += date->hour / 24;
133 /* Have to make sure month is valid before we can look at the
136 if (date->month < 1 || date->month > 12) {
137 date->year += ((date->month - 1) / 12) + 1;
138 date->month = ((date->month - 1) % 12) + 1;
144 while (date->day < 0) {
145 if (date->month == 1) {
150 date->day += days_in_month (date->month, date->year);
153 while (date->day > days_in_month (date->month, date->year)) {
154 date->day -= days_in_month (date->month, date->year);
155 if (date->month == 12) {
166 * @year: the year (1-9999)
167 * @month: the month (1-12)
168 * @day: the day of the month (1-31, as appropriate for @month)
169 * @hour: the hour (0-23)
170 * @minute: the minute (0-59)
171 * @second: the second (0-59, or up to 61 for leap seconds)
173 * Creates a #SoupDate representing the indicated time, UTC.
175 * Return value: a new #SoupDate
178 soup_date_new (int year, int month, int day,
179 int hour, int minute, int second)
181 SoupDate *date = g_slice_new (SoupDate);
187 date->minute = minute;
188 date->second = second;
196 * soup_date_new_from_now:
197 * @offset_seconds: offset from current time
199 * Creates a #SoupDate representing a time @offset_seconds after the
200 * current time (or before it, if @offset_seconds is negative). If
201 * offset_seconds is 0, returns the current time.
203 * If @offset_seconds would indicate a time not expressible as a
204 * #time_t, the return value will be clamped into range.
206 * Return value: a new #SoupDate
209 soup_date_new_from_now (int offset_seconds)
211 time_t now = time (NULL);
212 time_t then = now + offset_seconds;
214 if (sizeof (time_t) == 4) {
215 if (offset_seconds < 0 && then > now)
216 return soup_date_new_from_time_t (-G_MAXINT);
217 else if (offset_seconds > 0 && then < now)
218 return soup_date_new_from_time_t (G_MAXINT);
220 return soup_date_new_from_time_t (then);
224 parse_iso8601_date (SoupDate *date, const char *date_string)
228 if (strlen (date_string) < 15)
230 if (date_string[4] == '-' &&
231 date_string[7] == '-' &&
232 date_string[10] == 'T') {
234 date->year = atoi (date_string);
235 date->month = atoi (date_string + 5);
236 date->day = atoi (date_string + 8);
238 } else if (date_string[8] == 'T') {
240 val = atoi (date_string);
241 date->year = val / 10000;
242 date->month = (val % 10000) / 100;
243 date->day = val % 100;
248 if (strlen (date_string) >= 8 &&
249 date_string[2] == ':' && date_string[5] == ':') {
251 date->hour = atoi (date_string);
252 date->minute = atoi (date_string + 3);
253 date->second = atoi (date_string + 6);
255 } else if (strlen (date_string) >= 6) {
257 val = strtoul (date_string, (char **)&date_string, 10);
258 date->hour = val / 10000;
259 date->minute = (val % 10000) / 100;
260 date->second = val % 100;
264 if (*date_string == '.' || *date_string == ',')
265 (void) strtoul (date_string + 1, (char **)&date_string, 10);
267 if (*date_string == 'Z') {
271 } else if (*date_string == '+' || *date_string == '-') {
272 int sign = (*date_string == '+') ? -1 : 1;
273 val = strtoul (date_string + 1, (char **)&date_string, 10);
274 if (*date_string == ':')
275 val = 60 * val + strtoul (date_string + 1, (char **)&date_string, 10);
277 val = 60 * (val / 100) + (val % 100);
278 date->offset = sign * val;
285 return !*date_string;
288 static inline gboolean
289 parse_day (SoupDate *date, const char **date_string)
293 date->day = strtoul (*date_string, &end, 10);
294 if (end == (char *)*date_string)
297 while (*end == ' ' || *end == '-')
303 static inline gboolean
304 parse_month (SoupDate *date, const char **date_string)
308 for (i = 0; i < G_N_ELEMENTS (months); i++) {
309 if (!g_ascii_strncasecmp (*date_string, months[i], 3)) {
312 while (**date_string == ' ' || **date_string == '-')
320 static inline gboolean
321 parse_year (SoupDate *date, const char **date_string)
325 date->year = strtoul (*date_string, &end, 10);
326 if (end == (char *)*date_string)
329 if (end == (char *)*date_string + 2) {
334 } else if (end == (char *)*date_string + 3)
337 while (*end == ' ' || *end == '-')
343 static inline gboolean
344 parse_time (SoupDate *date, const char **date_string)
348 date->hour = strtoul (*date_string, &end, 10);
349 if (end == (char *)*date_string || *end++ != ':')
352 date->minute = strtoul (p, &end, 10);
353 if (end == p || *end++ != ':')
356 date->second = strtoul (p, &end, 10);
367 static inline gboolean
368 parse_timezone (SoupDate *date, const char **date_string)
370 if (!**date_string) {
373 } else if (**date_string == '+' || **date_string == '-') {
375 int sign = (**date_string == '+') ? -1 : 1;
376 val = strtoul (*date_string + 1, (char **)date_string, 10);
377 if (**date_string == ':')
378 val = 60 * val + strtoul (*date_string + 1, (char **)date_string, 10);
380 val = 60 * (val / 100) + (val % 100);
381 date->offset = sign * val;
382 date->utc = (sign == -1) && !val;
383 } else if (**date_string == 'Z') {
387 } else if (!strcmp (*date_string, "GMT") ||
388 !strcmp (*date_string, "UTC")) {
392 } else if (strchr ("ECMP", **date_string) &&
393 ((*date_string)[1] == 'D' || (*date_string)[1] == 'S') &&
394 (*date_string)[2] == 'T') {
395 date->offset = -60 * (5 * strcspn ("ECMP", *date_string));
396 if ((*date_string)[1] == 'D')
405 parse_textual_date (SoupDate *date, const char *date_string)
407 /* If it starts with a word, it must be a weekday, which we skip */
408 if (g_ascii_isalpha (*date_string)) {
409 while (g_ascii_isalpha (*date_string))
411 if (*date_string == ',')
413 while (g_ascii_isspace (*date_string))
417 /* If there's now another word, this must be an asctime-date */
418 if (g_ascii_isalpha (*date_string)) {
419 /* (Sun) Nov 6 08:49:37 1994 */
420 if (!parse_month (date, &date_string) ||
421 !parse_day (date, &date_string) ||
422 !parse_time (date, &date_string) ||
423 !parse_year (date, &date_string))
426 /* There shouldn't be a timezone, but check anyway */
427 parse_timezone (date, &date_string);
429 /* Non-asctime date, so some variation of
430 * (Sun,) 06 Nov 1994 08:49:37 GMT
432 if (!parse_day (date, &date_string) ||
433 !parse_month (date, &date_string) ||
434 !parse_year (date, &date_string) ||
435 !parse_time (date, &date_string))
438 /* This time there *should* be a timezone, but we
439 * survive if there isn't.
441 parse_timezone (date, &date_string);
448 * @SOUP_DATE_HTTP: RFC 1123 format, used by the HTTP "Date" header. Eg
449 * "Sun, 06 Nov 1994 08:49:37 GMT"
450 * @SOUP_DATE_COOKIE: The format for the "Expires" timestamp in the
451 * Netscape cookie specification. Eg, "Sun, 06-Nov-1994 08:49:37 GMT".
452 * @SOUP_DATE_RFC2822: RFC 2822 format, eg "Sun, 6 Nov 1994 09:49:37 -0100"
453 * @SOUP_DATE_ISO8601_COMPACT: ISO 8601 date/time with no optional
454 * punctuation. Eg, "19941106T094937-0100".
455 * @SOUP_DATE_ISO8601_FULL: ISO 8601 date/time with all optional
456 * punctuation. Eg, "1994-11-06T09:49:37-01:00".
457 * @SOUP_DATE_ISO8601_XMLRPC: ISO 8601 date/time as used by XML-RPC.
458 * Eg, "19941106T09:49:37".
459 * @SOUP_DATE_ISO8601: An alias for @SOUP_DATE_ISO8601_FULL.
461 * Date formats that soup_date_to_string() can use.
463 * @SOUP_DATE_HTTP and @SOUP_DATE_COOKIE always coerce the time to
464 * UTC. @SOUP_DATE_ISO8601_XMLRPC uses the time as given, ignoring the
465 * offset completely. @SOUP_DATE_RFC2822 and the other ISO 8601
466 * variants use the local time, appending the offset information if
469 * This enum may be extended with more values in future releases.
473 * soup_date_new_from_string:
474 * @date_string: the date in some plausible format
476 * Parses @date_string and tries to extract a date from it. This
477 * recognizes all of the "HTTP-date" formats from RFC 2616, all ISO
478 * 8601 formats containing both a time and a date, RFC 2822 dates,
479 * and reasonable approximations thereof. (Eg, it is lenient about
480 * whitespace, leading "0"s, etc.)
482 * Return value: a new #SoupDate, or %NULL if @date_string could not
486 soup_date_new_from_string (const char *date_string)
491 g_return_val_if_fail (date_string != NULL, NULL);
493 date = g_slice_new (SoupDate);
495 while (g_ascii_isspace (*date_string))
498 /* If it starts with a digit, it's either an ISO 8601 date, or
499 * an RFC2822 date without the optional weekday; in the later
500 * case, there will be a month name later on, so look for one
501 * of the month-start letters.
503 if (g_ascii_isdigit (*date_string) &&
504 !strpbrk (date_string, "JFMASOND"))
505 success = parse_iso8601_date (date, date_string);
507 success = parse_textual_date (date, date_string);
510 g_slice_free (SoupDate, date);
514 if (date->year < 1 || date->year > 9999 ||
515 date->month < 1 || date->month > 12 ||
517 date->day > days_in_month (date->month, date->year) ||
518 date->hour < 0 || date->hour > 24 ||
519 date->minute < 0 || date->minute > 59 ||
520 date->second < 0 || date->second > 61) {
521 soup_date_free (date);
524 if (date->hour == 24) {
525 /* ISO8601 allows this explicitly. We allow it for
526 * other types as well just for simplicity.
528 if (date->minute == 0 && date->second == 0)
529 soup_date_fixup (date);
531 soup_date_free (date);
540 * soup_date_new_from_time_t:
543 * Creates a #SoupDate corresponding to @when
545 * Return value: a new #SoupDate
548 soup_date_new_from_time_t (time_t when)
553 gmtime_r (&when, &tm);
555 tm = *gmtime (&when);
558 return soup_date_new (tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
559 tm.tm_hour, tm.tm_min, tm.tm_sec);
563 soup_date_weekday (SoupDate *date)
565 /* Proleptic Gregorian 0001-01-01 was a Monday, which
566 * corresponds to 1 in the days[] array.
568 return days[rata_die_day (date) % 7];
572 * soup_date_to_string:
574 * @format: the format to generate the date in
576 * Converts @date to a string in the format described by @format.
578 * Return value: @date as a string
581 soup_date_to_string (SoupDate *date, SoupDateFormat format)
583 g_return_val_if_fail (date != NULL, NULL);
585 if (format == SOUP_DATE_HTTP || format == SOUP_DATE_COOKIE) {
586 /* HTTP and COOKIE formats require UTC timestamp, so coerce
587 * @date if it's non-UTC.
591 if (date->offset != 0) {
592 memcpy (&utcdate, date, sizeof (SoupDate));
593 utcdate.minute += utcdate.offset;
596 soup_date_fixup (&utcdate);
602 /* "Sun, 06 Nov 1994 08:49:37 GMT" */
603 return g_strdup_printf (
604 "%s, %02d %s %04d %02d:%02d:%02d GMT",
605 soup_date_weekday (date), date->day,
606 months[date->month - 1], date->year,
607 date->hour, date->minute, date->second);
609 case SOUP_DATE_COOKIE:
610 /* "Sun, 06-Nov-1994 08:49:37 GMT" */
611 return g_strdup_printf (
612 "%s, %02d-%s-%04d %02d:%02d:%02d GMT",
613 soup_date_weekday (date), date->day,
614 months[date->month - 1], date->year,
615 date->hour, date->minute, date->second);
618 g_return_val_if_reached (NULL);
620 } else if (format == SOUP_DATE_ISO8601_XMLRPC) {
621 /* Always "floating", ignore offset */
622 return g_strdup_printf ("%04d%02d%02dT%02d:%02d:%02d",
623 date->year, date->month, date->day,
624 date->hour, date->minute, date->second);
626 int hour_offset, minute_offset;
629 /* For other ISO8601 formats or RFC2822, use the
630 * offset given in @date. For ISO8601 formats, use "Z"
631 * for UTC, +-offset for non-UTC, and nothing for
632 * floating. For RFC2822, use +-offset for UTC or
633 * non-UTC, and -0000 for floating.
635 hour_offset = abs (date->offset) / 60;
636 minute_offset = abs (date->offset) - hour_offset * 60;
639 case SOUP_DATE_ISO8601_COMPACT:
640 /* "19941106T084937[zone]" */
643 else if (date->offset) {
644 g_snprintf (zone, sizeof (zone), "%c%02d%02d",
645 date->offset > 0 ? '-' : '+',
646 hour_offset, minute_offset);
650 return g_strdup_printf (
651 "%04d%02d%02dT%02d%02d%02d%s",
652 date->year, date->month, date->day,
653 date->hour, date->minute, date->second,
656 case SOUP_DATE_ISO8601_FULL:
657 /* "1994-11-06T08:49:37[zone]" */
660 else if (date->offset) {
661 g_snprintf (zone, sizeof (zone), "%c%02d:%02d",
662 date->offset > 0 ? '-' : '+',
663 hour_offset, minute_offset);
667 return g_strdup_printf (
668 "%04d-%02d-%02dT%02d:%02d:%02d%s",
669 date->year, date->month, date->day,
670 date->hour, date->minute, date->second,
673 case SOUP_DATE_RFC2822:
674 /* "Sun, 6 Nov 1994 09:49:37 -0100" */
676 sign = (date->offset > 0) ? '-' : '+';
678 sign = date->utc ? '+' : '-';
679 return g_strdup_printf (
680 "%s, %d %s %04d %02d:%02d:%02d %c%02d%02d",
681 soup_date_weekday (date), date->day,
682 months[date->month - 1], date->year,
683 date->hour, date->minute, date->second,
684 sign, hour_offset, minute_offset);
693 * soup_date_to_time_t:
696 * Converts @date to a %time_t.
698 * If @date is not representable as a %time_t, it will be clamped into
699 * range. (In particular, some HTTP cookies have expiration dates
700 * after "Y2.038k" (2038-01-19T03:14:07Z).)
702 * Return value: @date as a %time_t
705 soup_date_to_time_t (SoupDate *date)
710 g_return_val_if_fail (date != NULL, 0);
712 /* FIXME: offset, etc */
714 if (date->year < 1970)
717 /* If the year is later than 2038, we're guaranteed to
718 * overflow a 32-bit time_t. (If it's exactly 2038, we'll
719 * *probably* overflow, but only by a little, and it's easiest
720 * to test that at the end by seeing if the result has turned
723 if (sizeof (time_t) == 4 && date->year > 2038)
724 return (time_t)0x7fffffff;
726 soup_date_to_timeval (date, &val);
729 if (sizeof (time_t) == 4 && tt < 0)
730 return (time_t)0x7fffffff;
735 * soup_date_to_timeval:
737 * @time: (out): a #GTimeVal structure in which to store the converted time.
739 * Converts @date to a #GTimeVal.
744 soup_date_to_timeval (SoupDate *date, GTimeVal *time)
746 g_return_if_fail (date != NULL);
747 g_return_if_fail (time != NULL);
749 /* FIXME: offset, etc */
751 time->tv_sec = rata_die_day (date) - TIME_T_EPOCH_RATA_DIE_DAY;
752 time->tv_sec = ((((time->tv_sec * 24) + date->hour) * 60) + date->minute) * 60 + date->second;
760 * Determines if @date is in the past.
762 * Return value: %TRUE if @date is in the past
767 soup_date_is_past (SoupDate *date)
769 g_return_val_if_fail (date != NULL, TRUE);
772 if (date->year < 2010)
775 return soup_date_to_time_t (date) < time (NULL);
779 * soup_date_get_year:
784 * Return value: @date's year
789 soup_date_get_year (SoupDate *date)
795 * soup_date_get_month:
798 * Gets @date's month.
800 * Return value: @date's month
805 soup_date_get_month (SoupDate *date)
816 * Return value: @date's day
821 soup_date_get_day (SoupDate *date)
827 * soup_date_get_hour:
832 * Return value: @date's hour
837 soup_date_get_hour (SoupDate *date)
843 * soup_date_get_minute:
846 * Gets @date's minute.
848 * Return value: @date's minute
853 soup_date_get_minute (SoupDate *date)
859 * soup_date_get_second:
862 * Gets @date's second.
864 * Return value: @date's second
869 soup_date_get_second (SoupDate *date)
878 * Gets @date's UTC flag
880 * Return value: %TRUE if @date is UTC.
885 soup_date_get_utc (SoupDate *date)
891 * soup_date_get_offset:
894 * Gets @date's offset from UTC.
896 * Return value: @date's offset from UTC. If soup_date_get_utc()
897 * returns %FALSE but soup_date_get_offset() returns 0, that means the
898 * date is a "floating" time with no associated offset information.
903 soup_date_get_offset (SoupDate *date)
915 soup_date_copy (SoupDate *date)
919 g_return_val_if_fail (date != NULL, NULL);
921 copy = g_slice_new (SoupDate);
922 memcpy (copy, date, sizeof (SoupDate));
933 soup_date_free (SoupDate *date)
935 g_return_if_fail (date != NULL);
937 g_slice_free (SoupDate, date);