1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
33 #define DEBUG_MSG(x) /* */
35 /* #define DEBUG_MSG(args) g_message args ; */
54 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
60 g_date_new_dmy (GDateDay day, GDateMonth m, GDateYear y)
63 g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
74 g_assert (g_date_valid (d));
80 g_date_new_julian (guint32 j)
83 g_return_val_if_fail (g_date_valid_julian (j), NULL);
92 g_assert (g_date_valid (d));
98 g_date_free (GDate *d)
100 g_return_if_fail (d != NULL);
106 g_date_valid (const GDate *d)
108 g_return_val_if_fail (d != NULL, FALSE);
110 return (d->julian || d->dmy);
113 static const guint8 days_in_months[2][13] =
114 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
115 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
116 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
119 static const guint16 days_in_year[2][14] =
120 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
121 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
122 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
126 g_date_valid_month (GDateMonth m)
128 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
132 g_date_valid_year (GDateYear y)
134 return ( y > G_DATE_BAD_YEAR );
138 g_date_valid_day (GDateDay d)
140 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
144 g_date_valid_weekday (GDateWeekday w)
146 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
150 g_date_valid_julian (guint32 j)
152 return (j > G_DATE_BAD_JULIAN);
156 g_date_valid_dmy (GDateDay d,
160 return ( (m > G_DATE_BAD_MONTH) &&
162 (d > G_DATE_BAD_DAY) &&
163 (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
164 (d <= (g_date_is_leap_year (y) ?
165 days_in_months[1][m] : days_in_months[0][m])) );
169 /* "Julian days" just means an absolute number of days, where Day 1 ==
173 g_date_update_julian (const GDate *const_d)
175 GDate *d = (GDate *) const_d;
179 g_return_if_fail (d != NULL);
180 g_return_if_fail (d->dmy);
181 g_return_if_fail (!d->julian);
182 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
184 /* What we actually do is: multiply years * 365 days in the year,
185 * add the number of years divided by 4, subtract the number of
186 * years divided by 100 and add the number of years divided by 400,
187 * which accounts for leap year stuff. Code from Steffen Beyer's
191 year = d->year - 1; /* we know d->year > 0 since it's valid */
193 d->julian_days = year * 365U;
194 d->julian_days += (year >>= 2); /* divide by 4 and add */
195 d->julian_days -= (year /= 25); /* divides original # years by 100 */
196 d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
198 index = g_date_is_leap_year (d->year) ? 1 : 0;
200 d->julian_days += days_in_year[index][d->month] + d->day;
202 g_return_if_fail (g_date_valid_julian (d->julian_days));
208 g_date_update_dmy (const GDate *const_d)
210 GDate *d = (GDate *) const_d;
215 guint32 A, B, C, D, E, M;
217 g_return_if_fail (d != NULL);
218 g_return_if_fail (d->julian);
219 g_return_if_fail (!d->dmy);
220 g_return_if_fail (g_date_valid_julian (d->julian_days));
222 /* Formula taken from the Calendar FAQ; the formula was for the
223 * Julian Period which starts on 1 January 4713 BC, so we add
224 * 1,721,425 to the number of days before doing the formula.
226 * I'm sure this can be simplified for our 1 January 1 AD period
227 * start, but I can't figure out how to unpack the formula.
230 A = d->julian_days + 1721425 + 32045;
231 B = ( 4 *(A + 36524) )/ 146097 - 1;
232 C = A - (146097 * B)/4;
233 D = ( 4 * (C + 365) ) / 1461 - 1;
234 E = C - ((1461*D) / 4);
235 M = (5 * (E - 1) + 2)/153;
237 m = M + 3 - (12*(M/10));
238 day = E - (153*M + 2)/5;
239 y = 100 * B + D - 4800 + (M/10);
241 #ifdef G_ENABLE_DEBUG
242 if (!g_date_valid_dmy (day, m, y))
244 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
245 d->julian_days, day, m, y);
257 g_date_get_weekday (const GDate *d)
259 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
263 g_date_update_julian (d);
265 g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
267 return ((d->julian_days - 1) % 7) + 1;
271 g_date_get_month (const GDate *d)
273 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
277 g_date_update_dmy (d);
279 g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
285 g_date_get_year (const GDate *d)
287 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
291 g_date_update_dmy (d);
293 g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
299 g_date_get_day (const GDate *d)
301 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
305 g_date_update_dmy (d);
307 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
313 g_date_get_julian (const GDate *d)
315 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
319 g_date_update_julian (d);
321 g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
323 return d->julian_days;
327 g_date_get_day_of_year (const GDate *d)
331 g_return_val_if_fail (g_date_valid (d), 0);
335 g_date_update_dmy (d);
337 g_return_val_if_fail (d->dmy, 0);
339 index = g_date_is_leap_year (d->year) ? 1 : 0;
341 return (days_in_year[index][d->month] + d->day);
345 g_date_get_monday_week_of_year (const GDate *d)
351 g_return_val_if_fail (g_date_valid (d), 0);
355 g_date_update_dmy (d);
357 g_return_val_if_fail (d->dmy, 0);
359 g_date_clear (&first, 1);
361 g_date_set_dmy (&first, 1, 1, d->year);
363 wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
364 day = g_date_get_day_of_year (d) - 1;
366 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
370 g_date_get_sunday_week_of_year (const GDate *d)
376 g_return_val_if_fail (g_date_valid (d), 0);
380 g_date_update_dmy (d);
382 g_return_val_if_fail (d->dmy, 0);
384 g_date_clear (&first, 1);
386 g_date_set_dmy (&first, 1, 1, d->year);
388 wd = g_date_get_weekday (&first);
389 if (wd == 7) wd = 0; /* make Sunday day 0 */
390 day = g_date_get_day_of_year (d) - 1;
392 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
396 * g_date_get_iso8601_week_of_year:
397 * @date: a valid #GDate
399 * Returns the week of the year, where weeks are interpreted according
402 * Returns: ISO 8601 week number of the year.
407 g_date_get_iso8601_week_of_year (const GDate *d)
409 guint j, d4, L, d1, w;
411 g_return_val_if_fail (g_date_valid (d), 0);
414 g_date_update_julian (d);
415 g_return_val_if_fail (d->julian, 0);
417 /* Formula taken from the Calendar FAQ; the formula was for the
418 * Julian Period which starts on 1 January 4713 BC, so we add
419 * 1,721,425 to the number of days before doing the formula.
421 j = d->julian_days + 1721425;
422 d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
424 d1 = ((d4 - L) % 365) + L;
431 g_date_days_between (const GDate *d1,
434 g_return_val_if_fail (g_date_valid (d1), 0);
435 g_return_val_if_fail (g_date_valid (d2), 0);
437 return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
441 g_date_clear (GDate *d, guint ndates)
443 g_return_if_fail (d != NULL);
444 g_return_if_fail (ndates != 0);
446 memset (d, 0x0, ndates*sizeof (GDate));
449 G_LOCK_DEFINE_STATIC (g_date_global);
451 /* These are for the parser, output to the user should use *
452 * g_date_strftime () - this creates more never-freed memory to annoy
453 * all those memory debugger users. :-)
456 static gchar *long_month_names[13] =
461 static gchar *short_month_names[13] =
466 /* This tells us if we need to update the parse info */
467 static gchar *current_locale = NULL;
469 /* order of these in the current locale */
470 static GDateDMY dmy_order[3] =
472 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
475 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
476 * 29 and below are counted as in the year 2000, numbers 30 and above
477 * are counted as in the year 1900.
480 static const GDateYear twodigit_start_year = 1930;
482 /* It is impossible to enter a year between 1 AD and 99 AD with this
485 static gboolean using_twodigit_years = FALSE;
487 /* Adjustment of locale era to AD, non-zero means using locale era
489 static gint locale_era_adjust = 0;
491 struct _GDateParseTokens {
497 typedef struct _GDateParseTokens GDateParseTokens;
501 /* HOLDS: g_date_global_lock */
503 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
505 gchar num[4][NUM_LEN+1];
509 /* We count 4, but store 3; so we can give an error
512 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
514 s = (const guchar *) str;
516 while (*s && pt->num_ints < 4)
520 while (*s && g_ascii_isdigit (*s) && i < NUM_LEN)
522 num[pt->num_ints][i] = *s;
529 num[pt->num_ints][i] = '\0';
533 if (*s == '\0') break;
538 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
539 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
540 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
542 pt->month = G_DATE_BAD_MONTH;
544 if (pt->num_ints < 3)
549 casefold = g_utf8_casefold (str, -1);
550 normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
556 if (long_month_names[i] != NULL)
558 const gchar *found = strstr (normalized, long_month_names[i]);
567 if (short_month_names[i] != NULL)
569 const gchar *found = strstr (normalized, short_month_names[i]);
585 /* HOLDS: g_date_global_lock */
587 g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
589 const gchar *locale = setlocale (LC_TIME, NULL);
590 gboolean recompute_localeinfo = FALSE;
593 g_return_if_fail (locale != NULL); /* should not happen */
595 g_date_clear (&d, 1); /* clear for scratch use */
597 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
599 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
602 if (recompute_localeinfo)
605 GDateParseTokens testpt;
608 g_free (current_locale); /* still works if current_locale == NULL */
610 current_locale = g_strdup (locale);
612 short_month_names[0] = "Error";
613 long_month_names[0] = "Error";
619 g_date_set_dmy (&d, 1, i, 1);
621 g_return_if_fail (g_date_valid (&d));
623 g_date_strftime (buf, 127, "%b", &d);
625 casefold = g_utf8_casefold (buf, -1);
626 g_free (short_month_names[i]);
627 short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
630 g_date_strftime (buf, 127, "%B", &d);
631 casefold = g_utf8_casefold (buf, -1);
632 g_free (long_month_names[i]);
633 long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
639 /* Determine DMY order */
641 /* had to pick a random day - don't change this, some strftimes
642 * are broken on some days, and this one is good so far. */
643 g_date_set_dmy (&d, 4, 7, 1976);
645 g_date_strftime (buf, 127, "%x", &d);
647 g_date_fill_parse_tokens (buf, &testpt);
650 while (i < testpt.num_ints)
655 dmy_order[i] = G_DATE_MONTH;
658 dmy_order[i] = G_DATE_DAY;
661 using_twodigit_years = TRUE; /* FALL THRU */
663 dmy_order[i] = G_DATE_YEAR;
666 /* assume locale era */
667 locale_era_adjust = 1976 - testpt.n[i];
668 dmy_order[i] = G_DATE_YEAR;
674 #ifdef G_ENABLE_DEBUG
675 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
679 DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
682 if (using_twodigit_years)
683 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
689 switch (dmy_order[i])
692 strings[i] = "Month";
706 DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
707 DEBUG_MSG (("**Sample date in this locale: `%s'", buf));
712 g_date_fill_parse_tokens (str, pt);
716 g_date_set_parse (GDate *d,
720 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
722 g_return_if_fail (d != NULL);
727 G_LOCK (g_date_global);
729 g_date_prepare_to_parse (str, &pt);
731 DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d",
732 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
735 if (pt.num_ints == 4)
737 G_UNLOCK (g_date_global);
738 return; /* presumably a typo; bail out. */
746 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
748 while (i < pt.num_ints && j < 3)
750 switch (dmy_order[j])
754 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
757 ++j; /* skip months, but don't skip this number */
766 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
769 ++j; /* skip days, since we may have month/year */
779 if (locale_era_adjust != 0)
781 y += locale_era_adjust;
783 else if (using_twodigit_years && y < 100)
785 guint two = twodigit_start_year % 100;
786 guint century = (twodigit_start_year / 100) * 100;
804 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
811 if (using_twodigit_years && y < 100)
812 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
814 else if (pt.num_ints == 2)
816 if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
822 else if (pt.num_ints == 1)
824 if (pt.month != G_DATE_BAD_MONTH)
826 /* Month name and year? */
833 /* Try yyyymmdd and yymmdd */
835 m = (pt.n[0]/100) % 100;
839 /* FIXME move this into a separate function */
840 if (using_twodigit_years && y < 100)
842 guint two = twodigit_start_year % 100;
843 guint century = (twodigit_start_year / 100) * 100;
853 /* See if we got anything valid out of all this. */
854 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
855 if (y < 8000 && g_date_valid_dmy (day, m, y))
862 #ifdef G_ENABLE_DEBUG
864 DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
866 G_UNLOCK (g_date_global);
872 * @timet: <type>time_t</type> value to set
874 * Sets the value of a date from a <type>time_t</type> value.
876 * To set the value of a date to the current day, you could write:
877 * <informalexample><programlisting>
878 * g_date_set_time_t (date, time (NULL));
879 * </programlisting></informalexample>
884 g_date_set_time_t (GDate *date,
889 g_return_if_fail (date != NULL);
891 #ifdef HAVE_LOCALTIME_R
892 localtime_r (&timet, &tm);
895 struct tm *ptm = localtime (&timet);
899 /* Happens at least in Microsoft's C library if you pass a
900 * negative time_t. Use 2000-01-01 as default date.
902 #ifndef G_DISABLE_CHECKS
903 g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
911 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
915 date->julian = FALSE;
917 date->month = tm.tm_mon + 1;
918 date->day = tm.tm_mday;
919 date->year = tm.tm_year + 1900;
921 g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
930 * @time_: #GTime value to set.
932 * Sets the value of a date from a #GTime value.
934 * @Deprecated:2.10: Use g_date_set_time_t() instead.
937 g_date_set_time (GDate *date,
940 g_date_set_time_t (date, (time_t) time_);
944 * g_date_set_time_val:
946 * @timeval: #GTimeVal value to set
948 * Sets the value of a date from a #GTimeVal value. Note that the
949 * @tv_usec member is ignored, because #GDate can't make use of the
950 * additional precision.
955 g_date_set_time_val (GDate *date,
958 g_date_set_time_t (date, (time_t) timeval->tv_sec);
962 g_date_set_month (GDate *d,
965 g_return_if_fail (d != NULL);
966 g_return_if_fail (g_date_valid_month (m));
968 if (d->julian && !d->dmy) g_date_update_dmy(d);
973 if (g_date_valid_dmy (d->day, d->month, d->year))
980 g_date_set_day (GDate *d,
983 g_return_if_fail (d != NULL);
984 g_return_if_fail (g_date_valid_day (day));
986 if (d->julian && !d->dmy) g_date_update_dmy(d);
991 if (g_date_valid_dmy (d->day, d->month, d->year))
998 g_date_set_year (GDate *d,
1001 g_return_if_fail (d != NULL);
1002 g_return_if_fail (g_date_valid_year (y));
1004 if (d->julian && !d->dmy) g_date_update_dmy(d);
1009 if (g_date_valid_dmy (d->day, d->month, d->year))
1016 g_date_set_dmy (GDate *d,
1021 g_return_if_fail (d != NULL);
1022 g_return_if_fail (g_date_valid_dmy (day, m, y));
1034 g_date_set_julian (GDate *d, guint32 j)
1036 g_return_if_fail (d != NULL);
1037 g_return_if_fail (g_date_valid_julian (j));
1046 g_date_is_first_of_month (const GDate *d)
1048 g_return_val_if_fail (g_date_valid (d), FALSE);
1052 g_date_update_dmy (d);
1054 g_return_val_if_fail (d->dmy, FALSE);
1056 if (d->day == 1) return TRUE;
1061 g_date_is_last_of_month (const GDate *d)
1065 g_return_val_if_fail (g_date_valid (d), FALSE);
1069 g_date_update_dmy (d);
1071 g_return_val_if_fail (d->dmy, FALSE);
1073 index = g_date_is_leap_year (d->year) ? 1 : 0;
1075 if (d->day == days_in_months[index][d->month]) return TRUE;
1080 g_date_add_days (GDate *d, guint ndays)
1082 g_return_if_fail (g_date_valid (d));
1086 g_date_update_julian (d);
1088 g_return_if_fail (d->julian);
1090 d->julian_days += ndays;
1095 g_date_subtract_days (GDate *d, guint ndays)
1097 g_return_if_fail (g_date_valid (d));
1101 g_date_update_julian (d);
1103 g_return_if_fail (d->julian);
1104 g_return_if_fail (d->julian_days > ndays);
1106 d->julian_days -= ndays;
1111 g_date_add_months (GDate *d,
1114 guint years, months;
1117 g_return_if_fail (g_date_valid (d));
1121 g_date_update_dmy (d);
1123 g_return_if_fail (d->dmy);
1125 nmonths += d->month - 1;
1128 months = nmonths%12;
1130 d->month = months + 1;
1133 index = g_date_is_leap_year (d->year) ? 1 : 0;
1135 if (d->day > days_in_months[index][d->month])
1136 d->day = days_in_months[index][d->month];
1140 g_return_if_fail (g_date_valid (d));
1144 g_date_subtract_months (GDate *d,
1147 guint years, months;
1150 g_return_if_fail (g_date_valid (d));
1154 g_date_update_dmy (d);
1156 g_return_if_fail (d->dmy);
1159 months = nmonths%12;
1161 g_return_if_fail (d->year > years);
1165 if (d->month > months) d->month -= months;
1169 d->month = 12 - months;
1173 index = g_date_is_leap_year (d->year) ? 1 : 0;
1175 if (d->day > days_in_months[index][d->month])
1176 d->day = days_in_months[index][d->month];
1180 g_return_if_fail (g_date_valid (d));
1184 g_date_add_years (GDate *d,
1187 g_return_if_fail (g_date_valid (d));
1191 g_date_update_dmy (d);
1193 g_return_if_fail (d->dmy);
1197 if (d->month == 2 && d->day == 29)
1199 if (!g_date_is_leap_year (d->year))
1209 g_date_subtract_years (GDate *d,
1212 g_return_if_fail (g_date_valid (d));
1216 g_date_update_dmy (d);
1218 g_return_if_fail (d->dmy);
1219 g_return_if_fail (d->year > nyears);
1223 if (d->month == 2 && d->day == 29)
1225 if (!g_date_is_leap_year (d->year))
1236 g_date_is_leap_year (GDateYear year)
1238 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1240 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1241 (year % 400) == 0 );
1245 g_date_get_days_in_month (GDateMonth month,
1250 g_return_val_if_fail (g_date_valid_year (year), 0);
1251 g_return_val_if_fail (g_date_valid_month (month), 0);
1253 index = g_date_is_leap_year (year) ? 1 : 0;
1255 return days_in_months[index][month];
1259 g_date_get_monday_weeks_in_year (GDateYear year)
1263 g_return_val_if_fail (g_date_valid_year (year), 0);
1265 g_date_clear (&d, 1);
1266 g_date_set_dmy (&d, 1, 1, year);
1267 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1268 g_date_set_dmy (&d, 31, 12, year);
1269 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1270 if (g_date_is_leap_year (year))
1272 g_date_set_dmy (&d, 2, 1, year);
1273 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1274 g_date_set_dmy (&d, 30, 12, year);
1275 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1281 g_date_get_sunday_weeks_in_year (GDateYear year)
1285 g_return_val_if_fail (g_date_valid_year (year), 0);
1287 g_date_clear (&d, 1);
1288 g_date_set_dmy (&d, 1, 1, year);
1289 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1290 g_date_set_dmy (&d, 31, 12, year);
1291 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1292 if (g_date_is_leap_year (year))
1294 g_date_set_dmy (&d, 2, 1, year);
1295 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1296 g_date_set_dmy (&d, 30, 12, year);
1297 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1303 g_date_compare (const GDate *lhs,
1306 g_return_val_if_fail (lhs != NULL, 0);
1307 g_return_val_if_fail (rhs != NULL, 0);
1308 g_return_val_if_fail (g_date_valid (lhs), 0);
1309 g_return_val_if_fail (g_date_valid (rhs), 0);
1311 /* Remember the self-comparison case! I think it works right now. */
1316 if (lhs->julian && rhs->julian)
1318 if (lhs->julian_days < rhs->julian_days) return -1;
1319 else if (lhs->julian_days > rhs->julian_days) return 1;
1322 else if (lhs->dmy && rhs->dmy)
1324 if (lhs->year < rhs->year) return -1;
1325 else if (lhs->year > rhs->year) return 1;
1328 if (lhs->month < rhs->month) return -1;
1329 else if (lhs->month > rhs->month) return 1;
1332 if (lhs->day < rhs->day) return -1;
1333 else if (lhs->day > rhs->day) return 1;
1342 if (!lhs->julian) g_date_update_julian (lhs);
1343 if (!rhs->julian) g_date_update_julian (rhs);
1344 g_return_val_if_fail (lhs->julian, 0);
1345 g_return_val_if_fail (rhs->julian, 0);
1349 return 0; /* warnings */
1354 g_date_to_struct_tm (const GDate *d,
1359 g_return_if_fail (g_date_valid (d));
1360 g_return_if_fail (tm != NULL);
1364 g_date_update_dmy (d);
1366 g_return_if_fail (d->dmy);
1368 /* zero all the irrelevant fields to be sure they're valid */
1370 /* On Linux and maybe other systems, there are weird non-POSIX
1371 * fields on the end of struct tm that choke strftime if they
1372 * contain garbage. So we need to 0 the entire struct, not just the
1373 * fields we know to exist.
1376 memset (tm, 0x0, sizeof (struct tm));
1378 tm->tm_mday = d->day;
1379 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1380 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1382 day = g_date_get_weekday (d);
1383 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1385 tm->tm_wday = (int)day;
1387 tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
1388 tm->tm_isdst = -1; /* -1 means "information not available" */
1392 g_date_clamp (GDate *date,
1393 const GDate *min_date,
1394 const GDate *max_date)
1396 g_return_if_fail (g_date_valid (date));
1398 if (min_date != NULL)
1399 g_return_if_fail (g_date_valid (min_date));
1400 if (max_date != NULL)
1401 g_return_if_fail (g_date_valid (max_date));
1402 if (min_date != NULL && max_date != NULL)
1403 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
1405 if (min_date && g_date_compare (date, min_date) < 0)
1408 if (max_date && g_date_compare (max_date, date) < 0)
1413 g_date_order (GDate *date1,
1416 g_return_if_fail (g_date_valid (date1));
1417 g_return_if_fail (g_date_valid (date2));
1419 if (g_date_compare (date1, date2) > 0)
1429 win32_strftime_helper (const GDate *d,
1430 const gchar *format,
1431 const struct tm *tm,
1435 SYSTEMTIME systemtime;
1436 TIME_ZONE_INFORMATION tzinfo;
1442 const wchar_t digits[] = L"0123456789";
1447 systemtime.wYear = tm->tm_year + 1900;
1448 systemtime.wMonth = tm->tm_mon + 1;
1449 systemtime.wDayOfWeek = tm->tm_wday;
1450 systemtime.wDay = tm->tm_mday;
1451 systemtime.wHour = tm->tm_hour;
1452 systemtime.wMinute = tm->tm_min;
1453 systemtime.wSecond = tm->tm_sec;
1454 systemtime.wMilliseconds = 0;
1456 lcid = GetThreadLocale ();
1457 result = g_array_sized_new (FALSE, FALSE, sizeof (wchar_t), MAX (128, strlen (format) * 2));
1462 c = g_utf8_get_char (p);
1465 p = g_utf8_next_char (p);
1469 g_array_free (result, TRUE);
1474 c = g_utf8_get_char (p);
1475 if (c == 'E' || c == 'O')
1477 /* Ignore modified conversion specifiers for now. */
1478 p = g_utf8_next_char (p);
1482 g_array_free (result, TRUE);
1487 c = g_utf8_get_char (p);
1493 if (systemtime.wDayOfWeek == 0)
1496 k = systemtime.wDayOfWeek - 1;
1497 n = GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, NULL, 0);
1498 g_array_set_size (result, result->len + n);
1499 GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
1500 g_array_set_size (result, result->len - 1);
1503 if (systemtime.wDayOfWeek == 0)
1506 k = systemtime.wDayOfWeek - 1;
1507 n = GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, NULL, 0);
1508 g_array_set_size (result, result->len + n);
1509 GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
1510 g_array_set_size (result, result->len - 1);
1514 n = GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, NULL, 0);
1515 g_array_set_size (result, result->len + n);
1516 GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
1517 g_array_set_size (result, result->len - 1);
1520 n = GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, NULL, 0);
1521 g_array_set_size (result, result->len + n);
1522 GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
1523 g_array_set_size (result, result->len - 1);
1526 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1529 g_array_set_size (result, result->len + n);
1530 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1531 g_array_set_size (result, result->len - 1);
1533 g_array_append_vals (result, L" ", 1);
1534 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1537 g_array_set_size (result, result->len + n);
1538 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1539 g_array_set_size (result, result->len - 1);
1543 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
1544 g_array_append_vals (result, digits + (systemtime.wYear/1000)%10, 1);
1547 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1548 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1551 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
1552 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
1553 g_array_append_vals (result, L"/", 1);
1554 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1555 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1556 g_array_append_vals (result, L"/", 1);
1557 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
1558 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
1561 if (systemtime.wDay >= 10)
1562 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1564 g_array_append_vals (result, L" ", 1);
1565 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1568 /* A GDate has no time fields, so for now we can
1569 * hardcode all time conversions into zeros (or 12 for
1570 * %I). The alternative code snippets in the #else
1571 * branches are here ready to be taken into use when
1572 * needed by a g_strftime() or g_date_and_time_format()
1577 g_array_append_vals (result, L"00", 2);
1579 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1580 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1585 g_array_append_vals (result, L"12", 2);
1587 if (systemtime.wHour == 0)
1588 g_array_append_vals (result, L"12", 2);
1591 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
1592 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
1597 g_array_append_vals (result, digits + (tm->tm_yday+1)/100, 1);
1598 g_array_append_vals (result, digits + ((tm->tm_yday+1)/10)%10, 1);
1599 g_array_append_vals (result, digits + (tm->tm_yday+1)%10, 1);
1602 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
1603 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
1607 g_array_append_vals (result, L"00", 2);
1609 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1610 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1614 g_array_append_vals (result, L"\n", 1);
1617 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
1620 g_array_set_size (result, result->len + n);
1621 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
1622 g_array_set_size (result, result->len - 1);
1626 /* This is a rather odd format. Hard to say what to do.
1627 * Let's always use the POSIX %I:%M:%S %p
1630 g_array_append_vals (result, L"12:00:00", 8);
1632 if (systemtime.wHour == 0)
1633 g_array_append_vals (result, L"12", 2);
1636 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
1637 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
1639 g_array_append_vals (result, L":", 1);
1640 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1641 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1642 g_array_append_vals (result, L":", 1);
1643 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1644 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1645 g_array_append_vals (result, L" ", 1);
1647 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
1650 g_array_set_size (result, result->len + n);
1651 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
1652 g_array_set_size (result, result->len - 1);
1657 g_array_append_vals (result, L"00:00", 5);
1659 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1660 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1661 g_array_append_vals (result, L":", 1);
1662 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1663 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1668 g_array_append_vals (result, L"00", 2);
1670 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1671 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1675 g_array_append_vals (result, L"\t", 1);
1679 g_array_append_vals (result, L"00:00:00", 8);
1681 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1682 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1683 g_array_append_vals (result, L":", 1);
1684 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1685 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1686 g_array_append_vals (result, L":", 1);
1687 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1688 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1692 if (systemtime.wDayOfWeek == 0)
1693 g_array_append_vals (result, L"7", 1);
1695 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
1698 n = g_date_get_sunday_week_of_year (d);
1699 g_array_append_vals (result, digits + n/10, 1);
1700 g_array_append_vals (result, digits + n%10, 1);
1703 n = g_date_get_iso8601_week_of_year (d);
1704 g_array_append_vals (result, digits + n/10, 1);
1705 g_array_append_vals (result, digits + n%10, 1);
1708 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
1711 n = g_date_get_monday_week_of_year (d);
1712 g_array_append_vals (result, digits + n/10, 1);
1713 g_array_append_vals (result, digits + n%10, 1);
1716 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1719 g_array_set_size (result, result->len + n);
1720 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1721 g_array_set_size (result, result->len - 1);
1725 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1728 g_array_set_size (result, result->len + n);
1729 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1730 g_array_set_size (result, result->len - 1);
1734 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
1735 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
1738 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
1739 g_array_append_vals (result, digits + (systemtime.wYear/100)%10, 1);
1740 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
1741 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
1744 n = GetTimeZoneInformation (&tzinfo);
1745 if (n == TIME_ZONE_ID_UNKNOWN)
1747 else if (n == TIME_ZONE_ID_STANDARD)
1748 g_array_append_vals (result, tzinfo.StandardName, wcslen (tzinfo.StandardName));
1749 else if (n == TIME_ZONE_ID_DAYLIGHT)
1750 g_array_append_vals (result, tzinfo.DaylightName, wcslen (tzinfo.DaylightName));
1753 g_array_append_vals (result, L"%", 1);
1757 else if (c <= 0xFFFF)
1760 g_array_append_vals (result, &wc, 1);
1767 ws = g_ucs4_to_utf16 (&c, 1, NULL, &nwc, NULL);
1768 g_array_append_vals (result, ws, nwc);
1771 p = g_utf8_next_char (p);
1774 convbuf = g_utf16_to_utf8 ((wchar_t *) result->data, result->len, NULL, &convlen, NULL);
1775 g_array_free (result, TRUE);
1783 if (slen <= convlen)
1785 /* Ensure only whole characters are copied into the buffer. */
1786 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1787 g_assert (end != NULL);
1788 convlen = end - convbuf;
1790 /* Return 0 because the buffer isn't large enough. */
1796 memcpy (s, convbuf, convlen);
1806 g_date_strftime (gchar *s,
1808 const gchar *format,
1813 gsize locale_format_len = 0;
1814 gchar *locale_format;
1820 GError *error = NULL;
1824 g_return_val_if_fail (g_date_valid (d), 0);
1825 g_return_val_if_fail (slen > 0, 0);
1826 g_return_val_if_fail (format != 0, 0);
1827 g_return_val_if_fail (s != 0, 0);
1829 g_date_to_struct_tm (d, &tm);
1832 if (!g_utf8_validate (format, -1, NULL))
1837 return win32_strftime_helper (d, format, &tm, s, slen);
1840 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
1844 g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
1845 g_error_free (error);
1851 tmpbufsize = MAX (128, locale_format_len * 2);
1854 tmpbuf = g_malloc (tmpbufsize);
1856 /* Set the first byte to something other than '\0', to be able to
1857 * recognize whether strftime actually failed or just returned "".
1860 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
1862 if (tmplen == 0 && tmpbuf[0] != '\0')
1867 if (tmpbufsize > 65536)
1869 g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
1870 g_free (locale_format);
1879 g_free (locale_format);
1881 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
1886 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
1887 g_error_free (error);
1893 if (slen <= convlen)
1895 /* Ensure only whole characters are copied into the buffer.
1897 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1898 g_assert (end != NULL);
1899 convlen = end - convbuf;
1901 /* Return 0 because the buffer isn't large enough.
1908 memcpy (s, convbuf, convlen);
1916 #define __G_DATE_C__
1917 #include "galiasdef.c"