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 ; */
53 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
59 g_date_new_dmy (GDateDay day,
64 g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
75 g_assert (g_date_valid (d));
81 g_date_new_julian (guint32 j)
84 g_return_val_if_fail (g_date_valid_julian (j), NULL);
93 g_assert (g_date_valid (d));
99 g_date_free (GDate *d)
101 g_return_if_fail (d != NULL);
107 g_date_valid (const GDate *d)
109 g_return_val_if_fail (d != NULL, FALSE);
111 return (d->julian || d->dmy);
114 static const guint8 days_in_months[2][13] =
115 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
116 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
117 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
120 static const guint16 days_in_year[2][14] =
121 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
122 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
123 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
127 g_date_valid_month (GDateMonth m)
129 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
133 g_date_valid_year (GDateYear y)
135 return ( y > G_DATE_BAD_YEAR );
139 g_date_valid_day (GDateDay d)
141 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
145 g_date_valid_weekday (GDateWeekday w)
147 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
151 g_date_valid_julian (guint32 j)
153 return (j > G_DATE_BAD_JULIAN);
157 g_date_valid_dmy (GDateDay d,
161 return ( (m > G_DATE_BAD_MONTH) &&
163 (d > G_DATE_BAD_DAY) &&
164 (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
165 (d <= (g_date_is_leap_year (y) ?
166 days_in_months[1][m] : days_in_months[0][m])) );
170 /* "Julian days" just means an absolute number of days, where Day 1 ==
174 g_date_update_julian (const GDate *const_d)
176 GDate *d = (GDate *) const_d;
180 g_return_if_fail (d != NULL);
181 g_return_if_fail (d->dmy);
182 g_return_if_fail (!d->julian);
183 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
185 /* What we actually do is: multiply years * 365 days in the year,
186 * add the number of years divided by 4, subtract the number of
187 * years divided by 100 and add the number of years divided by 400,
188 * which accounts for leap year stuff. Code from Steffen Beyer's
192 year = d->year - 1; /* we know d->year > 0 since it's valid */
194 d->julian_days = year * 365U;
195 d->julian_days += (year >>= 2); /* divide by 4 and add */
196 d->julian_days -= (year /= 25); /* divides original # years by 100 */
197 d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
199 idx = g_date_is_leap_year (d->year) ? 1 : 0;
201 d->julian_days += days_in_year[idx][d->month] + d->day;
203 g_return_if_fail (g_date_valid_julian (d->julian_days));
209 g_date_update_dmy (const GDate *const_d)
211 GDate *d = (GDate *) const_d;
216 guint32 A, B, C, D, E, M;
218 g_return_if_fail (d != NULL);
219 g_return_if_fail (d->julian);
220 g_return_if_fail (!d->dmy);
221 g_return_if_fail (g_date_valid_julian (d->julian_days));
223 /* Formula taken from the Calendar FAQ; the formula was for the
224 * Julian Period which starts on 1 January 4713 BC, so we add
225 * 1,721,425 to the number of days before doing the formula.
227 * I'm sure this can be simplified for our 1 January 1 AD period
228 * start, but I can't figure out how to unpack the formula.
231 A = d->julian_days + 1721425 + 32045;
232 B = ( 4 *(A + 36524) )/ 146097 - 1;
233 C = A - (146097 * B)/4;
234 D = ( 4 * (C + 365) ) / 1461 - 1;
235 E = C - ((1461*D) / 4);
236 M = (5 * (E - 1) + 2)/153;
238 m = M + 3 - (12*(M/10));
239 day = E - (153*M + 2)/5;
240 y = 100 * B + D - 4800 + (M/10);
242 #ifdef G_ENABLE_DEBUG
243 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);
256 g_date_get_weekday (const GDate *d)
258 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
261 g_date_update_julian (d);
263 g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
265 return ((d->julian_days - 1) % 7) + 1;
269 g_date_get_month (const GDate *d)
271 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
274 g_date_update_dmy (d);
276 g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
282 g_date_get_year (const GDate *d)
284 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
287 g_date_update_dmy (d);
289 g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
295 g_date_get_day (const GDate *d)
297 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
300 g_date_update_dmy (d);
302 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
308 g_date_get_julian (const GDate *d)
310 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
313 g_date_update_julian (d);
315 g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
317 return d->julian_days;
321 g_date_get_day_of_year (const GDate *d)
325 g_return_val_if_fail (g_date_valid (d), 0);
328 g_date_update_dmy (d);
330 g_return_val_if_fail (d->dmy, 0);
332 idx = g_date_is_leap_year (d->year) ? 1 : 0;
334 return (days_in_year[idx][d->month] + d->day);
338 g_date_get_monday_week_of_year (const GDate *d)
344 g_return_val_if_fail (g_date_valid (d), 0);
347 g_date_update_dmy (d);
349 g_return_val_if_fail (d->dmy, 0);
351 g_date_clear (&first, 1);
353 g_date_set_dmy (&first, 1, 1, d->year);
355 wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
356 day = g_date_get_day_of_year (d) - 1;
358 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
362 g_date_get_sunday_week_of_year (const GDate *d)
368 g_return_val_if_fail (g_date_valid (d), 0);
371 g_date_update_dmy (d);
373 g_return_val_if_fail (d->dmy, 0);
375 g_date_clear (&first, 1);
377 g_date_set_dmy (&first, 1, 1, d->year);
379 wd = g_date_get_weekday (&first);
380 if (wd == 7) wd = 0; /* make Sunday day 0 */
381 day = g_date_get_day_of_year (d) - 1;
383 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
387 * g_date_get_iso8601_week_of_year:
388 * @date: a valid #GDate
390 * Returns the week of the year, where weeks are interpreted according
393 * Returns: ISO 8601 week number of the year.
398 g_date_get_iso8601_week_of_year (const GDate *d)
400 guint j, d4, L, d1, w;
402 g_return_val_if_fail (g_date_valid (d), 0);
405 g_date_update_julian (d);
407 g_return_val_if_fail (d->julian, 0);
409 /* Formula taken from the Calendar FAQ; the formula was for the
410 * Julian Period which starts on 1 January 4713 BC, so we add
411 * 1,721,425 to the number of days before doing the formula.
413 j = d->julian_days + 1721425;
414 d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
416 d1 = ((d4 - L) % 365) + L;
423 g_date_days_between (const GDate *d1,
426 g_return_val_if_fail (g_date_valid (d1), 0);
427 g_return_val_if_fail (g_date_valid (d2), 0);
429 return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
433 g_date_clear (GDate *d, guint ndates)
435 g_return_if_fail (d != NULL);
436 g_return_if_fail (ndates != 0);
438 memset (d, 0x0, ndates*sizeof (GDate));
441 G_LOCK_DEFINE_STATIC (g_date_global);
443 /* These are for the parser, output to the user should use *
444 * g_date_strftime () - this creates more never-freed memory to annoy
445 * all those memory debugger users. :-)
448 static gchar *long_month_names[13] =
453 static gchar *short_month_names[13] =
458 /* This tells us if we need to update the parse info */
459 static gchar *current_locale = NULL;
461 /* order of these in the current locale */
462 static GDateDMY dmy_order[3] =
464 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
467 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
468 * 29 and below are counted as in the year 2000, numbers 30 and above
469 * are counted as in the year 1900.
472 static const GDateYear twodigit_start_year = 1930;
474 /* It is impossible to enter a year between 1 AD and 99 AD with this
477 static gboolean using_twodigit_years = FALSE;
479 /* Adjustment of locale era to AD, non-zero means using locale era
481 static gint locale_era_adjust = 0;
483 struct _GDateParseTokens {
489 typedef struct _GDateParseTokens GDateParseTokens;
493 /* HOLDS: g_date_global_lock */
495 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
497 gchar num[4][NUM_LEN+1];
501 /* We count 4, but store 3; so we can give an error
504 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
506 s = (const guchar *) str;
508 while (*s && pt->num_ints < 4)
512 while (*s && g_ascii_isdigit (*s) && i < NUM_LEN)
514 num[pt->num_ints][i] = *s;
521 num[pt->num_ints][i] = '\0';
525 if (*s == '\0') break;
530 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
531 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
532 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
534 pt->month = G_DATE_BAD_MONTH;
536 if (pt->num_ints < 3)
541 casefold = g_utf8_casefold (str, -1);
542 normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
548 if (long_month_names[i] != NULL)
550 const gchar *found = strstr (normalized, long_month_names[i]);
559 if (short_month_names[i] != NULL)
561 const gchar *found = strstr (normalized, short_month_names[i]);
577 /* HOLDS: g_date_global_lock */
579 g_date_prepare_to_parse (const gchar *str,
580 GDateParseTokens *pt)
582 const gchar *locale = setlocale (LC_TIME, NULL);
583 gboolean recompute_localeinfo = FALSE;
586 g_return_if_fail (locale != NULL); /* should not happen */
588 g_date_clear (&d, 1); /* clear for scratch use */
590 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
591 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
593 if (recompute_localeinfo)
596 GDateParseTokens testpt;
599 g_free (current_locale); /* still works if current_locale == NULL */
601 current_locale = g_strdup (locale);
603 short_month_names[0] = "Error";
604 long_month_names[0] = "Error";
610 g_date_set_dmy (&d, 1, i, 1);
612 g_return_if_fail (g_date_valid (&d));
614 g_date_strftime (buf, 127, "%b", &d);
616 casefold = g_utf8_casefold (buf, -1);
617 g_free (short_month_names[i]);
618 short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
621 g_date_strftime (buf, 127, "%B", &d);
622 casefold = g_utf8_casefold (buf, -1);
623 g_free (long_month_names[i]);
624 long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
630 /* Determine DMY order */
632 /* had to pick a random day - don't change this, some strftimes
633 * are broken on some days, and this one is good so far. */
634 g_date_set_dmy (&d, 4, 7, 1976);
636 g_date_strftime (buf, 127, "%x", &d);
638 g_date_fill_parse_tokens (buf, &testpt);
641 while (i < testpt.num_ints)
646 dmy_order[i] = G_DATE_MONTH;
649 dmy_order[i] = G_DATE_DAY;
652 using_twodigit_years = TRUE; /* FALL THRU */
654 dmy_order[i] = G_DATE_YEAR;
657 /* assume locale era */
658 locale_era_adjust = 1976 - testpt.n[i];
659 dmy_order[i] = G_DATE_YEAR;
665 #ifdef G_ENABLE_DEBUG
666 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
670 DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
673 if (using_twodigit_years)
675 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
682 switch (dmy_order[i])
685 strings[i] = "Month";
699 DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
700 DEBUG_MSG (("**Sample date in this locale: `%s'", buf));
705 g_date_fill_parse_tokens (str, pt);
709 g_date_set_parse (GDate *d,
713 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
715 g_return_if_fail (d != NULL);
720 G_LOCK (g_date_global);
722 g_date_prepare_to_parse (str, &pt);
724 DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d",
725 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
728 if (pt.num_ints == 4)
730 G_UNLOCK (g_date_global);
731 return; /* presumably a typo; bail out. */
739 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
741 while (i < pt.num_ints && j < 3)
743 switch (dmy_order[j])
747 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
750 ++j; /* skip months, but don't skip this number */
759 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
762 ++j; /* skip days, since we may have month/year */
772 if (locale_era_adjust != 0)
774 y += locale_era_adjust;
776 else if (using_twodigit_years && y < 100)
778 guint two = twodigit_start_year % 100;
779 guint century = (twodigit_start_year / 100) * 100;
797 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
804 if (using_twodigit_years && y < 100)
805 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
807 else if (pt.num_ints == 2)
809 if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
813 else if (pt.num_ints == 1)
815 if (pt.month != G_DATE_BAD_MONTH)
817 /* Month name and year? */
824 /* Try yyyymmdd and yymmdd */
826 m = (pt.n[0]/100) % 100;
830 /* FIXME move this into a separate function */
831 if (using_twodigit_years && y < 100)
833 guint two = twodigit_start_year % 100;
834 guint century = (twodigit_start_year / 100) * 100;
844 /* See if we got anything valid out of all this. */
845 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
846 if (y < 8000 && g_date_valid_dmy (day, m, y))
853 #ifdef G_ENABLE_DEBUG
856 DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
859 G_UNLOCK (g_date_global);
865 * @timet: <type>time_t</type> value to set
867 * Sets the value of a date to the date corresponding to a time
868 * specified as a time_t. The time to date conversion is done using
869 * the user's current timezone.
871 * To set the value of a date to the current day, you could write:
873 * g_date_set_time_t (date, time (NULL));
879 g_date_set_time_t (GDate *date,
884 g_return_if_fail (date != NULL);
886 #ifdef HAVE_LOCALTIME_R
887 localtime_r (&timet, &tm);
890 struct tm *ptm = localtime (&timet);
894 /* Happens at least in Microsoft's C library if you pass a
895 * negative time_t. Use 2000-01-01 as default date.
897 #ifndef G_DISABLE_CHECKS
898 g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
906 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
910 date->julian = FALSE;
912 date->month = tm.tm_mon + 1;
913 date->day = tm.tm_mday;
914 date->year = tm.tm_year + 1900;
916 g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
925 * @time_: #GTime value to set.
927 * Sets the value of a date from a #GTime value.
928 * The time to date conversion is done using the user's current timezone.
930 * Deprecated: 2.10: Use g_date_set_time_t() instead.
933 g_date_set_time (GDate *date,
936 g_date_set_time_t (date, (time_t) time_);
940 * g_date_set_time_val:
942 * @timeval: #GTimeVal value to set
944 * Sets the value of a date from a #GTimeVal value. Note that the
945 * @tv_usec member is ignored, because #GDate can't make use of the
946 * additional precision.
948 * The time to date conversion is done using the user's current timezone.
953 g_date_set_time_val (GDate *date,
956 g_date_set_time_t (date, (time_t) timeval->tv_sec);
960 g_date_set_month (GDate *d,
963 g_return_if_fail (d != NULL);
964 g_return_if_fail (g_date_valid_month (m));
966 if (d->julian && !d->dmy) g_date_update_dmy(d);
971 if (g_date_valid_dmy (d->day, d->month, d->year))
978 g_date_set_day (GDate *d,
981 g_return_if_fail (d != NULL);
982 g_return_if_fail (g_date_valid_day (day));
984 if (d->julian && !d->dmy) g_date_update_dmy(d);
989 if (g_date_valid_dmy (d->day, d->month, d->year))
996 g_date_set_year (GDate *d,
999 g_return_if_fail (d != NULL);
1000 g_return_if_fail (g_date_valid_year (y));
1002 if (d->julian && !d->dmy) g_date_update_dmy(d);
1007 if (g_date_valid_dmy (d->day, d->month, d->year))
1014 g_date_set_dmy (GDate *d,
1019 g_return_if_fail (d != NULL);
1020 g_return_if_fail (g_date_valid_dmy (day, m, y));
1032 g_date_set_julian (GDate *d,
1035 g_return_if_fail (d != NULL);
1036 g_return_if_fail (g_date_valid_julian (j));
1045 g_date_is_first_of_month (const GDate *d)
1047 g_return_val_if_fail (g_date_valid (d), FALSE);
1050 g_date_update_dmy (d);
1052 g_return_val_if_fail (d->dmy, FALSE);
1054 if (d->day == 1) return TRUE;
1059 g_date_is_last_of_month (const GDate *d)
1063 g_return_val_if_fail (g_date_valid (d), FALSE);
1066 g_date_update_dmy (d);
1068 g_return_val_if_fail (d->dmy, FALSE);
1070 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1072 if (d->day == days_in_months[idx][d->month]) return TRUE;
1077 g_date_add_days (GDate *d,
1080 g_return_if_fail (g_date_valid (d));
1083 g_date_update_julian (d);
1085 g_return_if_fail (d->julian);
1087 d->julian_days += ndays;
1092 g_date_subtract_days (GDate *d,
1095 g_return_if_fail (g_date_valid (d));
1098 g_date_update_julian (d);
1100 g_return_if_fail (d->julian);
1101 g_return_if_fail (d->julian_days > ndays);
1103 d->julian_days -= ndays;
1108 g_date_add_months (GDate *d,
1111 guint years, months;
1114 g_return_if_fail (g_date_valid (d));
1117 g_date_update_dmy (d);
1119 g_return_if_fail (d->dmy);
1121 nmonths += d->month - 1;
1124 months = nmonths%12;
1126 d->month = months + 1;
1129 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1131 if (d->day > days_in_months[idx][d->month])
1132 d->day = days_in_months[idx][d->month];
1136 g_return_if_fail (g_date_valid (d));
1140 g_date_subtract_months (GDate *d,
1143 guint years, months;
1146 g_return_if_fail (g_date_valid (d));
1149 g_date_update_dmy (d);
1151 g_return_if_fail (d->dmy);
1154 months = nmonths%12;
1156 g_return_if_fail (d->year > years);
1160 if (d->month > months) d->month -= months;
1164 d->month = 12 - months;
1168 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1170 if (d->day > days_in_months[idx][d->month])
1171 d->day = days_in_months[idx][d->month];
1175 g_return_if_fail (g_date_valid (d));
1179 g_date_add_years (GDate *d,
1182 g_return_if_fail (g_date_valid (d));
1185 g_date_update_dmy (d);
1187 g_return_if_fail (d->dmy);
1191 if (d->month == 2 && d->day == 29)
1193 if (!g_date_is_leap_year (d->year))
1201 g_date_subtract_years (GDate *d,
1204 g_return_if_fail (g_date_valid (d));
1207 g_date_update_dmy (d);
1209 g_return_if_fail (d->dmy);
1210 g_return_if_fail (d->year > nyears);
1214 if (d->month == 2 && d->day == 29)
1216 if (!g_date_is_leap_year (d->year))
1224 g_date_is_leap_year (GDateYear year)
1226 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1228 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1229 (year % 400) == 0 );
1233 g_date_get_days_in_month (GDateMonth month,
1238 g_return_val_if_fail (g_date_valid_year (year), 0);
1239 g_return_val_if_fail (g_date_valid_month (month), 0);
1241 idx = g_date_is_leap_year (year) ? 1 : 0;
1243 return days_in_months[idx][month];
1247 g_date_get_monday_weeks_in_year (GDateYear year)
1251 g_return_val_if_fail (g_date_valid_year (year), 0);
1253 g_date_clear (&d, 1);
1254 g_date_set_dmy (&d, 1, 1, year);
1255 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1256 g_date_set_dmy (&d, 31, 12, year);
1257 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1258 if (g_date_is_leap_year (year))
1260 g_date_set_dmy (&d, 2, 1, year);
1261 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1262 g_date_set_dmy (&d, 30, 12, year);
1263 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1269 g_date_get_sunday_weeks_in_year (GDateYear year)
1273 g_return_val_if_fail (g_date_valid_year (year), 0);
1275 g_date_clear (&d, 1);
1276 g_date_set_dmy (&d, 1, 1, year);
1277 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1278 g_date_set_dmy (&d, 31, 12, year);
1279 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1280 if (g_date_is_leap_year (year))
1282 g_date_set_dmy (&d, 2, 1, year);
1283 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1284 g_date_set_dmy (&d, 30, 12, year);
1285 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1291 g_date_compare (const GDate *lhs,
1294 g_return_val_if_fail (lhs != NULL, 0);
1295 g_return_val_if_fail (rhs != NULL, 0);
1296 g_return_val_if_fail (g_date_valid (lhs), 0);
1297 g_return_val_if_fail (g_date_valid (rhs), 0);
1299 /* Remember the self-comparison case! I think it works right now. */
1303 if (lhs->julian && rhs->julian)
1305 if (lhs->julian_days < rhs->julian_days) return -1;
1306 else if (lhs->julian_days > rhs->julian_days) return 1;
1309 else if (lhs->dmy && rhs->dmy)
1311 if (lhs->year < rhs->year) return -1;
1312 else if (lhs->year > rhs->year) return 1;
1315 if (lhs->month < rhs->month) return -1;
1316 else if (lhs->month > rhs->month) return 1;
1319 if (lhs->day < rhs->day) return -1;
1320 else if (lhs->day > rhs->day) return 1;
1329 if (!lhs->julian) g_date_update_julian (lhs);
1330 if (!rhs->julian) g_date_update_julian (rhs);
1331 g_return_val_if_fail (lhs->julian, 0);
1332 g_return_val_if_fail (rhs->julian, 0);
1336 return 0; /* warnings */
1341 g_date_to_struct_tm (const GDate *d,
1346 g_return_if_fail (g_date_valid (d));
1347 g_return_if_fail (tm != NULL);
1350 g_date_update_dmy (d);
1352 g_return_if_fail (d->dmy);
1354 /* zero all the irrelevant fields to be sure they're valid */
1356 /* On Linux and maybe other systems, there are weird non-POSIX
1357 * fields on the end of struct tm that choke strftime if they
1358 * contain garbage. So we need to 0 the entire struct, not just the
1359 * fields we know to exist.
1362 memset (tm, 0x0, sizeof (struct tm));
1364 tm->tm_mday = d->day;
1365 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1366 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1368 day = g_date_get_weekday (d);
1369 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1371 tm->tm_wday = (int)day;
1373 tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
1374 tm->tm_isdst = -1; /* -1 means "information not available" */
1378 g_date_clamp (GDate *date,
1379 const GDate *min_date,
1380 const GDate *max_date)
1382 g_return_if_fail (g_date_valid (date));
1384 if (min_date != NULL)
1385 g_return_if_fail (g_date_valid (min_date));
1387 if (max_date != NULL)
1388 g_return_if_fail (g_date_valid (max_date));
1390 if (min_date != NULL && max_date != NULL)
1391 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
1393 if (min_date && g_date_compare (date, min_date) < 0)
1396 if (max_date && g_date_compare (max_date, date) < 0)
1401 g_date_order (GDate *date1,
1404 g_return_if_fail (g_date_valid (date1));
1405 g_return_if_fail (g_date_valid (date2));
1407 if (g_date_compare (date1, date2) > 0)
1417 win32_strftime_helper (const GDate *d,
1418 const gchar *format,
1419 const struct tm *tm,
1423 SYSTEMTIME systemtime;
1424 TIME_ZONE_INFORMATION tzinfo;
1430 const wchar_t digits[] = L"0123456789";
1435 systemtime.wYear = tm->tm_year + 1900;
1436 systemtime.wMonth = tm->tm_mon + 1;
1437 systemtime.wDayOfWeek = tm->tm_wday;
1438 systemtime.wDay = tm->tm_mday;
1439 systemtime.wHour = tm->tm_hour;
1440 systemtime.wMinute = tm->tm_min;
1441 systemtime.wSecond = tm->tm_sec;
1442 systemtime.wMilliseconds = 0;
1444 lcid = GetThreadLocale ();
1445 result = g_array_sized_new (FALSE, FALSE, sizeof (wchar_t), MAX (128, strlen (format) * 2));
1450 c = g_utf8_get_char (p);
1453 p = g_utf8_next_char (p);
1457 g_array_free (result, TRUE);
1462 c = g_utf8_get_char (p);
1463 if (c == 'E' || c == 'O')
1465 /* Ignore modified conversion specifiers for now. */
1466 p = g_utf8_next_char (p);
1470 g_array_free (result, TRUE);
1475 c = g_utf8_get_char (p);
1481 if (systemtime.wDayOfWeek == 0)
1484 k = systemtime.wDayOfWeek - 1;
1485 n = GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, NULL, 0);
1486 g_array_set_size (result, result->len + n);
1487 GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
1488 g_array_set_size (result, result->len - 1);
1491 if (systemtime.wDayOfWeek == 0)
1494 k = systemtime.wDayOfWeek - 1;
1495 n = GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, NULL, 0);
1496 g_array_set_size (result, result->len + n);
1497 GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
1498 g_array_set_size (result, result->len - 1);
1502 n = GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, NULL, 0);
1503 g_array_set_size (result, result->len + n);
1504 GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
1505 g_array_set_size (result, result->len - 1);
1508 n = GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, NULL, 0);
1509 g_array_set_size (result, result->len + n);
1510 GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
1511 g_array_set_size (result, result->len - 1);
1514 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1517 g_array_set_size (result, result->len + n);
1518 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1519 g_array_set_size (result, result->len - 1);
1521 g_array_append_vals (result, L" ", 1);
1522 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1525 g_array_set_size (result, result->len + n);
1526 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1527 g_array_set_size (result, result->len - 1);
1531 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
1532 g_array_append_vals (result, digits + (systemtime.wYear/1000)%10, 1);
1535 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1536 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1539 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
1540 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
1541 g_array_append_vals (result, L"/", 1);
1542 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1543 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1544 g_array_append_vals (result, L"/", 1);
1545 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
1546 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
1549 if (systemtime.wDay >= 10)
1550 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1552 g_array_append_vals (result, L" ", 1);
1553 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1556 /* A GDate has no time fields, so for now we can
1557 * hardcode all time conversions into zeros (or 12 for
1558 * %I). The alternative code snippets in the #else
1559 * branches are here ready to be taken into use when
1560 * needed by a g_strftime() or g_date_and_time_format()
1565 g_array_append_vals (result, L"00", 2);
1567 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1568 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1573 g_array_append_vals (result, L"12", 2);
1575 if (systemtime.wHour == 0)
1576 g_array_append_vals (result, L"12", 2);
1579 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
1580 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
1585 g_array_append_vals (result, digits + (tm->tm_yday+1)/100, 1);
1586 g_array_append_vals (result, digits + ((tm->tm_yday+1)/10)%10, 1);
1587 g_array_append_vals (result, digits + (tm->tm_yday+1)%10, 1);
1590 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
1591 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
1595 g_array_append_vals (result, L"00", 2);
1597 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1598 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1602 g_array_append_vals (result, L"\n", 1);
1605 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
1608 g_array_set_size (result, result->len + n);
1609 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
1610 g_array_set_size (result, result->len - 1);
1614 /* This is a rather odd format. Hard to say what to do.
1615 * Let's always use the POSIX %I:%M:%S %p
1618 g_array_append_vals (result, L"12:00:00", 8);
1620 if (systemtime.wHour == 0)
1621 g_array_append_vals (result, L"12", 2);
1624 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
1625 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
1627 g_array_append_vals (result, L":", 1);
1628 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1629 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1630 g_array_append_vals (result, L":", 1);
1631 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1632 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1633 g_array_append_vals (result, L" ", 1);
1635 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
1638 g_array_set_size (result, result->len + n);
1639 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
1640 g_array_set_size (result, result->len - 1);
1645 g_array_append_vals (result, L"00:00", 5);
1647 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1648 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1649 g_array_append_vals (result, L":", 1);
1650 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1651 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1656 g_array_append_vals (result, L"00", 2);
1658 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1659 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1663 g_array_append_vals (result, L"\t", 1);
1667 g_array_append_vals (result, L"00:00:00", 8);
1669 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1670 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1671 g_array_append_vals (result, L":", 1);
1672 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1673 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1674 g_array_append_vals (result, L":", 1);
1675 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1676 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1680 if (systemtime.wDayOfWeek == 0)
1681 g_array_append_vals (result, L"7", 1);
1683 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
1686 n = g_date_get_sunday_week_of_year (d);
1687 g_array_append_vals (result, digits + n/10, 1);
1688 g_array_append_vals (result, digits + n%10, 1);
1691 n = g_date_get_iso8601_week_of_year (d);
1692 g_array_append_vals (result, digits + n/10, 1);
1693 g_array_append_vals (result, digits + n%10, 1);
1696 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
1699 n = g_date_get_monday_week_of_year (d);
1700 g_array_append_vals (result, digits + n/10, 1);
1701 g_array_append_vals (result, digits + n%10, 1);
1704 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1707 g_array_set_size (result, result->len + n);
1708 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1709 g_array_set_size (result, result->len - 1);
1713 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1716 g_array_set_size (result, result->len + n);
1717 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1718 g_array_set_size (result, result->len - 1);
1722 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
1723 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
1726 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
1727 g_array_append_vals (result, digits + (systemtime.wYear/100)%10, 1);
1728 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
1729 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
1732 n = GetTimeZoneInformation (&tzinfo);
1733 if (n == TIME_ZONE_ID_UNKNOWN)
1735 else if (n == TIME_ZONE_ID_STANDARD)
1736 g_array_append_vals (result, tzinfo.StandardName, wcslen (tzinfo.StandardName));
1737 else if (n == TIME_ZONE_ID_DAYLIGHT)
1738 g_array_append_vals (result, tzinfo.DaylightName, wcslen (tzinfo.DaylightName));
1741 g_array_append_vals (result, L"%", 1);
1745 else if (c <= 0xFFFF)
1748 g_array_append_vals (result, &wc, 1);
1755 ws = g_ucs4_to_utf16 (&c, 1, NULL, &nwc, NULL);
1756 g_array_append_vals (result, ws, nwc);
1759 p = g_utf8_next_char (p);
1762 convbuf = g_utf16_to_utf8 ((wchar_t *) result->data, result->len, NULL, &convlen, NULL);
1763 g_array_free (result, TRUE);
1771 if (slen <= convlen)
1773 /* Ensure only whole characters are copied into the buffer. */
1774 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1775 g_assert (end != NULL);
1776 convlen = end - convbuf;
1778 /* Return 0 because the buffer isn't large enough. */
1784 memcpy (s, convbuf, convlen);
1794 g_date_strftime (gchar *s,
1796 const gchar *format,
1801 gsize locale_format_len = 0;
1802 gchar *locale_format;
1808 GError *error = NULL;
1812 g_return_val_if_fail (g_date_valid (d), 0);
1813 g_return_val_if_fail (slen > 0, 0);
1814 g_return_val_if_fail (format != NULL, 0);
1815 g_return_val_if_fail (s != NULL, 0);
1817 g_date_to_struct_tm (d, &tm);
1820 if (!g_utf8_validate (format, -1, NULL))
1825 return win32_strftime_helper (d, format, &tm, s, slen);
1828 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
1832 g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
1833 g_error_free (error);
1839 tmpbufsize = MAX (128, locale_format_len * 2);
1842 tmpbuf = g_malloc (tmpbufsize);
1844 /* Set the first byte to something other than '\0', to be able to
1845 * recognize whether strftime actually failed or just returned "".
1848 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
1850 if (tmplen == 0 && tmpbuf[0] != '\0')
1855 if (tmpbufsize > 65536)
1857 g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
1858 g_free (locale_format);
1867 g_free (locale_format);
1869 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
1874 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
1875 g_error_free (error);
1881 if (slen <= convlen)
1883 /* Ensure only whole characters are copied into the buffer.
1885 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1886 g_assert (end != NULL);
1887 convlen = end - convbuf;
1889 /* Return 0 because the buffer isn't large enough.
1896 memcpy (s, convbuf, convlen);