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.
16 #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];
91 G_DEFINE_BOXED_TYPE (SoupDate, soup_date, soup_date_copy, soup_date_free)
94 soup_date_fixup (SoupDate *date)
96 /* We only correct date->second if it's negative or too high
97 * to be a leap second.
99 if (date->second < 0 || date->second > 61) {
100 date->minute += date->second / 60;
102 if (date->second < 0)
106 if (date->minute < 0 || date->minute > 59) {
107 date->hour += date->minute / 60;
109 if (date->minute < 0)
113 if (date->hour < 0 || date->hour > 23) {
114 date->day += date->hour / 24;
120 /* Have to make sure month is valid before we can look at the
123 if (date->month < 1 || date->month > 12) {
124 date->year += ((date->month - 1) / 12) + 1;
125 date->month = ((date->month - 1) % 12) + 1;
131 while (date->day < 0) {
132 if (date->month == 1) {
137 date->day += days_in_month (date->month, date->year);
140 while (date->day > days_in_month (date->month, date->year)) {
141 date->day -= days_in_month (date->month, date->year);
142 if (date->month == 12) {
153 * @year: the year (1-9999)
154 * @month: the month (1-12)
155 * @day: the day of the month (1-31, as appropriate for @month)
156 * @hour: the hour (0-23)
157 * @minute: the minute (0-59)
158 * @second: the second (0-59, or up to 61 for leap seconds)
160 * Creates a #SoupDate representing the indicated time, UTC.
162 * Return value: a new #SoupDate
165 soup_date_new (int year, int month, int day,
166 int hour, int minute, int second)
168 SoupDate *date = g_slice_new (SoupDate);
174 date->minute = minute;
175 date->second = second;
183 * soup_date_new_from_now:
184 * @offset_seconds: offset from current time
186 * Creates a #SoupDate representing a time @offset_seconds after the
187 * current time (or before it, if @offset_seconds is negative). If
188 * offset_seconds is 0, returns the current time.
190 * If @offset_seconds would indicate a time not expressible as a
191 * <type>time_t</type>, the return value will be clamped into range.
193 * Return value: a new #SoupDate
196 soup_date_new_from_now (int offset_seconds)
198 time_t now = time (NULL);
199 time_t then = now + offset_seconds;
201 if (sizeof (time_t) == 4) {
202 if (offset_seconds < 0 && then > now)
203 return soup_date_new_from_time_t (-G_MAXINT);
204 else if (offset_seconds > 0 && then < now)
205 return soup_date_new_from_time_t (G_MAXINT);
207 return soup_date_new_from_time_t (then);
211 parse_iso8601_date (SoupDate *date, const char *date_string)
215 if (strlen (date_string) < 15)
217 if (date_string[4] == '-' &&
218 date_string[7] == '-' &&
219 date_string[10] == 'T') {
221 date->year = atoi (date_string);
222 date->month = atoi (date_string + 5);
223 date->day = atoi (date_string + 8);
225 } else if (date_string[8] == 'T') {
227 val = atoi (date_string);
228 date->year = val / 10000;
229 date->month = (val % 10000) / 100;
230 date->day = val % 100;
235 if (strlen (date_string) >= 8 &&
236 date_string[2] == ':' && date_string[5] == ':') {
238 date->hour = atoi (date_string);
239 date->minute = atoi (date_string + 3);
240 date->second = atoi (date_string + 6);
242 } else if (strlen (date_string) >= 6) {
244 val = strtoul (date_string, (char **)&date_string, 10);
245 date->hour = val / 10000;
246 date->minute = (val % 10000) / 100;
247 date->second = val % 100;
251 if (*date_string == '.' || *date_string == ',')
252 (void) strtoul (date_string + 1, (char **)&date_string, 10);
254 if (*date_string == 'Z') {
258 } else if (*date_string == '+' || *date_string == '-') {
259 int sign = (*date_string == '+') ? -1 : 1;
260 val = strtoul (date_string + 1, (char **)&date_string, 10);
261 if (*date_string == ':')
262 val = 60 * val + strtoul (date_string + 1, (char **)&date_string, 10);
264 val = 60 * (val / 100) + (val % 100);
265 date->offset = sign * val;
272 return !*date_string;
275 static inline gboolean
276 parse_day (SoupDate *date, const char **date_string)
280 date->day = strtoul (*date_string, &end, 10);
281 if (end == (char *)*date_string)
284 while (*end == ' ' || *end == '-')
290 static inline gboolean
291 parse_month (SoupDate *date, const char **date_string)
295 for (i = 0; i < G_N_ELEMENTS (months); i++) {
296 if (!g_ascii_strncasecmp (*date_string, months[i], 3)) {
299 while (**date_string == ' ' || **date_string == '-')
307 static inline gboolean
308 parse_year (SoupDate *date, const char **date_string)
312 date->year = strtoul (*date_string, &end, 10);
313 if (end == (char *)*date_string)
316 if (end == (char *)*date_string + 2) {
321 } else if (end == (char *)*date_string + 3)
324 while (*end == ' ' || *end == '-')
330 static inline gboolean
331 parse_time (SoupDate *date, const char **date_string)
335 date->hour = strtoul (*date_string, &end, 10);
336 if (end == (char *)*date_string || *end++ != ':')
339 date->minute = strtoul (p, &end, 10);
340 if (end == p || *end++ != ':')
343 date->second = strtoul (p, &end, 10);
354 static inline gboolean
355 parse_timezone (SoupDate *date, const char **date_string)
357 if (!**date_string) {
360 } else if (**date_string == '+' || **date_string == '-') {
362 int sign = (**date_string == '+') ? -1 : 1;
363 val = strtoul (*date_string + 1, (char **)date_string, 10);
364 if (**date_string == ':')
365 val = 60 * val + strtoul (*date_string + 1, (char **)date_string, 10);
367 val = 60 * (val / 100) + (val % 100);
368 date->offset = sign * val;
369 date->utc = (sign == -1) && !val;
370 } else if (**date_string == 'Z') {
374 } else if (!strcmp (*date_string, "GMT") ||
375 !strcmp (*date_string, "UTC")) {
379 } else if (strchr ("ECMP", **date_string) &&
380 ((*date_string)[1] == 'D' || (*date_string)[1] == 'S') &&
381 (*date_string)[2] == 'T') {
382 date->offset = -60 * (5 * strcspn ("ECMP", *date_string));
383 if ((*date_string)[1] == 'D')
392 parse_textual_date (SoupDate *date, const char *date_string)
394 /* If it starts with a word, it must be a weekday, which we skip */
395 if (g_ascii_isalpha (*date_string)) {
396 while (g_ascii_isalpha (*date_string))
398 if (*date_string == ',')
400 while (g_ascii_isspace (*date_string))
404 /* If there's now another word, this must be an asctime-date */
405 if (g_ascii_isalpha (*date_string)) {
406 /* (Sun) Nov 6 08:49:37 1994 */
407 if (!parse_month (date, &date_string) ||
408 !parse_day (date, &date_string) ||
409 !parse_time (date, &date_string) ||
410 !parse_year (date, &date_string))
413 /* There shouldn't be a timezone, but check anyway */
414 parse_timezone (date, &date_string);
416 /* Non-asctime date, so some variation of
417 * (Sun,) 06 Nov 1994 08:49:37 GMT
419 if (!parse_day (date, &date_string) ||
420 !parse_month (date, &date_string) ||
421 !parse_year (date, &date_string) ||
422 !parse_time (date, &date_string))
425 /* This time there *should* be a timezone, but we
426 * survive if there isn't.
428 parse_timezone (date, &date_string);
435 * @SOUP_DATE_HTTP: RFC 1123 format, used by the HTTP "Date" header. Eg
436 * "Sun, 06 Nov 1994 08:49:37 GMT"
437 * @SOUP_DATE_COOKIE: The format for the "Expires" timestamp in the
438 * Netscape cookie specification. Eg, "Sun, 06-Nov-1994 08:49:37 GMT".
439 * @SOUP_DATE_RFC2822: RFC 2822 format, eg "Sun, 6 Nov 1994 09:49:37 -0100"
440 * @SOUP_DATE_ISO8601_COMPACT: ISO 8601 date/time with no optional
441 * punctuation. Eg, "19941106T094937-0100".
442 * @SOUP_DATE_ISO8601_FULL: ISO 8601 date/time with all optional
443 * punctuation. Eg, "1994-11-06T09:49:37-01:00".
444 * @SOUP_DATE_ISO8601_XMLRPC: ISO 8601 date/time as used by XML-RPC.
445 * Eg, "19941106T09:49:37".
446 * @SOUP_DATE_ISO8601: An alias for @SOUP_DATE_ISO8601_FULL.
448 * Date formats that soup_date_to_string() can use.
450 * @SOUP_DATE_HTTP and @SOUP_DATE_COOKIE always coerce the time to
451 * UTC. @SOUP_DATE_ISO8601_XMLRPC uses the time as given, ignoring the
452 * offset completely. @SOUP_DATE_RFC2822 and the other ISO 8601
453 * variants use the local time, appending the offset information if
456 * This enum may be extended with more values in future releases.
460 * soup_date_new_from_string:
461 * @date_string: the date in some plausible format
463 * Parses @date_string and tries to extract a date from it. This
464 * recognizes all of the "HTTP-date" formats from RFC 2616, all ISO
465 * 8601 formats containing both a time and a date, RFC 2822 dates,
466 * and reasonable approximations thereof. (Eg, it is lenient about
467 * whitespace, leading "0"s, etc.)
469 * Return value: (nullable): a new #SoupDate, or %NULL if @date_string
470 * could not be parsed.
473 soup_date_new_from_string (const char *date_string)
478 g_return_val_if_fail (date_string != NULL, NULL);
480 date = g_slice_new (SoupDate);
482 while (g_ascii_isspace (*date_string))
485 /* If it starts with a digit, it's either an ISO 8601 date, or
486 * an RFC2822 date without the optional weekday; in the later
487 * case, there will be a month name later on, so look for one
488 * of the month-start letters.
490 if (g_ascii_isdigit (*date_string) &&
491 !strpbrk (date_string, "JFMASOND"))
492 success = parse_iso8601_date (date, date_string);
494 success = parse_textual_date (date, date_string);
497 g_slice_free (SoupDate, date);
501 if (date->year < 1 || date->year > 9999 ||
502 date->month < 1 || date->month > 12 ||
504 date->day > days_in_month (date->month, date->year) ||
505 date->hour < 0 || date->hour > 24 ||
506 date->minute < 0 || date->minute > 59 ||
507 date->second < 0 || date->second > 61) {
508 soup_date_free (date);
511 if (date->hour == 24) {
512 /* ISO8601 allows this explicitly. We allow it for
513 * other types as well just for simplicity.
515 if (date->minute == 0 && date->second == 0)
516 soup_date_fixup (date);
518 soup_date_free (date);
527 * soup_date_new_from_time_t:
528 * @when: a <type>time_t</type>
530 * Creates a #SoupDate corresponding to @when
532 * Return value: a new #SoupDate
535 soup_date_new_from_time_t (time_t when)
540 gmtime_r (&when, &tm);
542 tm = *gmtime (&when);
545 return soup_date_new (tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
546 tm.tm_hour, tm.tm_min, tm.tm_sec);
550 soup_date_weekday (SoupDate *date)
552 /* Proleptic Gregorian 0001-01-01 was a Monday, which
553 * corresponds to 1 in the days[] array.
555 return days[rata_die_day (date) % 7];
559 * soup_date_to_string:
561 * @format: the format to generate the date in
563 * Converts @date to a string in the format described by @format.
565 * Return value: @date as a string
568 soup_date_to_string (SoupDate *date, SoupDateFormat format)
570 g_return_val_if_fail (date != NULL, NULL);
572 if (format == SOUP_DATE_HTTP || format == SOUP_DATE_COOKIE) {
573 /* HTTP and COOKIE formats require UTC timestamp, so coerce
574 * @date if it's non-UTC.
578 if (date->offset != 0) {
579 memcpy (&utcdate, date, sizeof (SoupDate));
580 utcdate.minute += utcdate.offset;
583 soup_date_fixup (&utcdate);
589 /* "Sun, 06 Nov 1994 08:49:37 GMT" */
590 return g_strdup_printf (
591 "%s, %02d %s %04d %02d:%02d:%02d GMT",
592 soup_date_weekday (date), date->day,
593 months[date->month - 1], date->year,
594 date->hour, date->minute, date->second);
596 case SOUP_DATE_COOKIE:
597 /* "Sun, 06-Nov-1994 08:49:37 GMT" */
598 return g_strdup_printf (
599 "%s, %02d-%s-%04d %02d:%02d:%02d GMT",
600 soup_date_weekday (date), date->day,
601 months[date->month - 1], date->year,
602 date->hour, date->minute, date->second);
605 g_return_val_if_reached (NULL);
607 } else if (format == SOUP_DATE_ISO8601_XMLRPC) {
608 /* Always "floating", ignore offset */
609 return g_strdup_printf ("%04d%02d%02dT%02d:%02d:%02d",
610 date->year, date->month, date->day,
611 date->hour, date->minute, date->second);
613 int hour_offset, minute_offset;
616 /* For other ISO8601 formats or RFC2822, use the
617 * offset given in @date. For ISO8601 formats, use "Z"
618 * for UTC, +-offset for non-UTC, and nothing for
619 * floating. For RFC2822, use +-offset for UTC or
620 * non-UTC, and -0000 for floating.
622 hour_offset = abs (date->offset) / 60;
623 minute_offset = abs (date->offset) - hour_offset * 60;
626 case SOUP_DATE_ISO8601_COMPACT:
627 /* "19941106T084937[zone]" */
630 else if (date->offset) {
631 g_snprintf (zone, sizeof (zone), "%c%02d%02d",
632 date->offset > 0 ? '-' : '+',
633 hour_offset, minute_offset);
637 return g_strdup_printf (
638 "%04d%02d%02dT%02d%02d%02d%s",
639 date->year, date->month, date->day,
640 date->hour, date->minute, date->second,
643 case SOUP_DATE_ISO8601_FULL:
644 /* "1994-11-06T08:49:37[zone]" */
647 else if (date->offset) {
648 g_snprintf (zone, sizeof (zone), "%c%02d:%02d",
649 date->offset > 0 ? '-' : '+',
650 hour_offset, minute_offset);
654 return g_strdup_printf (
655 "%04d-%02d-%02dT%02d:%02d:%02d%s",
656 date->year, date->month, date->day,
657 date->hour, date->minute, date->second,
660 case SOUP_DATE_RFC2822:
661 /* "Sun, 6 Nov 1994 09:49:37 -0100" */
663 sign = (date->offset > 0) ? '-' : '+';
665 sign = date->utc ? '+' : '-';
666 return g_strdup_printf (
667 "%s, %d %s %04d %02d:%02d:%02d %c%02d%02d",
668 soup_date_weekday (date), date->day,
669 months[date->month - 1], date->year,
670 date->hour, date->minute, date->second,
671 sign, hour_offset, minute_offset);
680 * soup_date_to_time_t:
683 * Converts @date to a <type>time_t</type>, assumming it to be in
686 * If @date is not representable as a <type>time_t</type>, it will be
687 * clamped into range. (In particular, some HTTP cookies have
688 * expiration dates after "Y2.038k" (2038-01-19T03:14:07Z).)
690 * Return value: @date as a <type>time_t</type>
693 soup_date_to_time_t (SoupDate *date)
698 g_return_val_if_fail (date != NULL, 0);
700 if (date->year < 1970)
703 /* If the year is later than 2038, we're guaranteed to
704 * overflow a 32-bit time_t. (If it's exactly 2038, we'll
705 * *probably* overflow, but only by a little, and it's easiest
706 * to just clamp down the value if it's above G_MAXINT32.
708 if (sizeof (time_t) == 4 && date->year > 2038)
709 return (time_t)G_MAXINT32;
711 datetime = g_date_time_new_utc (date->year,
718 seconds = g_date_time_to_unix (datetime);
720 g_date_time_unref (datetime);
722 return (time_t) (sizeof (time_t) == 4 ? MIN(seconds, G_MAXINT32) : seconds);
725 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
727 * soup_date_to_timeval:
729 * @time: (out): a #GTimeVal structure in which to store the converted time.
731 * Converts @date to a #GTimeVal.
733 * Deprecated: Do not use #GTimeVal, as it's not Y2038-safe.
738 soup_date_to_timeval (SoupDate *date, GTimeVal *time)
740 g_return_if_fail (date != NULL);
741 g_return_if_fail (time != NULL);
743 /* FIXME: offset, etc */
745 time->tv_sec = rata_die_day (date) - TIME_T_EPOCH_RATA_DIE_DAY;
746 time->tv_sec = ((((time->tv_sec * 24) + date->hour) * 60) + date->minute) * 60 + date->second;
749 G_GNUC_END_IGNORE_DEPRECATIONS
755 * Determines if @date is in the past.
757 * Return value: %TRUE if @date is in the past
762 soup_date_is_past (SoupDate *date)
764 g_return_val_if_fail (date != NULL, TRUE);
767 if (date->year < 2020)
770 return soup_date_to_time_t (date) < time (NULL);
774 * soup_date_get_year:
779 * Return value: @date's year
784 soup_date_get_year (SoupDate *date)
790 * soup_date_get_month:
793 * Gets @date's month.
795 * Return value: @date's month
800 soup_date_get_month (SoupDate *date)
811 * Return value: @date's day
816 soup_date_get_day (SoupDate *date)
822 * soup_date_get_hour:
827 * Return value: @date's hour
832 soup_date_get_hour (SoupDate *date)
838 * soup_date_get_minute:
841 * Gets @date's minute.
843 * Return value: @date's minute
848 soup_date_get_minute (SoupDate *date)
854 * soup_date_get_second:
857 * Gets @date's second.
859 * Return value: @date's second
864 soup_date_get_second (SoupDate *date)
873 * Gets @date's UTC flag
875 * Return value: %TRUE if @date is UTC.
880 soup_date_get_utc (SoupDate *date)
886 * soup_date_get_offset:
889 * Gets @date's offset from UTC.
891 * Return value: @date's offset from UTC. If soup_date_get_utc()
892 * returns %FALSE but soup_date_get_offset() returns 0, that means the
893 * date is a "floating" time with no associated offset information.
898 soup_date_get_offset (SoupDate *date)
912 soup_date_copy (SoupDate *date)
916 g_return_val_if_fail (date != NULL, NULL);
918 copy = g_slice_new (SoupDate);
919 memcpy (copy, date, sizeof (SoupDate));
932 soup_date_free (SoupDate *date)
934 g_return_if_fail (date != NULL);
936 g_slice_free (SoupDate, date);