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 ; */
51 #include "gstrfuncs.h"
52 #include "gtestutils.h"
59 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
65 g_date_new_dmy (GDateDay day,
70 g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
81 g_assert (g_date_valid (d));
87 g_date_new_julian (guint32 j)
90 g_return_val_if_fail (g_date_valid_julian (j), NULL);
99 g_assert (g_date_valid (d));
105 g_date_free (GDate *d)
107 g_return_if_fail (d != NULL);
113 g_date_valid (const GDate *d)
115 g_return_val_if_fail (d != NULL, FALSE);
117 return (d->julian || d->dmy);
120 static const guint8 days_in_months[2][13] =
121 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
122 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
123 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
126 static const guint16 days_in_year[2][14] =
127 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
128 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
129 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
133 g_date_valid_month (GDateMonth m)
135 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
139 g_date_valid_year (GDateYear y)
141 return ( y > G_DATE_BAD_YEAR );
145 g_date_valid_day (GDateDay d)
147 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
151 g_date_valid_weekday (GDateWeekday w)
153 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
157 g_date_valid_julian (guint32 j)
159 return (j > G_DATE_BAD_JULIAN);
163 g_date_valid_dmy (GDateDay d,
167 return ( (m > G_DATE_BAD_MONTH) &&
169 (d > G_DATE_BAD_DAY) &&
170 (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
171 (d <= (g_date_is_leap_year (y) ?
172 days_in_months[1][m] : days_in_months[0][m])) );
176 /* "Julian days" just means an absolute number of days, where Day 1 ==
180 g_date_update_julian (const GDate *const_d)
182 GDate *d = (GDate *) const_d;
186 g_return_if_fail (d != NULL);
187 g_return_if_fail (d->dmy);
188 g_return_if_fail (!d->julian);
189 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
191 /* What we actually do is: multiply years * 365 days in the year,
192 * add the number of years divided by 4, subtract the number of
193 * years divided by 100 and add the number of years divided by 400,
194 * which accounts for leap year stuff. Code from Steffen Beyer's
198 year = d->year - 1; /* we know d->year > 0 since it's valid */
200 d->julian_days = year * 365U;
201 d->julian_days += (year >>= 2); /* divide by 4 and add */
202 d->julian_days -= (year /= 25); /* divides original # years by 100 */
203 d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
205 idx = g_date_is_leap_year (d->year) ? 1 : 0;
207 d->julian_days += days_in_year[idx][d->month] + d->day;
209 g_return_if_fail (g_date_valid_julian (d->julian_days));
215 g_date_update_dmy (const GDate *const_d)
217 GDate *d = (GDate *) const_d;
222 guint32 A, B, C, D, E, M;
224 g_return_if_fail (d != NULL);
225 g_return_if_fail (d->julian);
226 g_return_if_fail (!d->dmy);
227 g_return_if_fail (g_date_valid_julian (d->julian_days));
229 /* Formula taken from the Calendar FAQ; the formula was for the
230 * Julian Period which starts on 1 January 4713 BC, so we add
231 * 1,721,425 to the number of days before doing the formula.
233 * I'm sure this can be simplified for our 1 January 1 AD period
234 * start, but I can't figure out how to unpack the formula.
237 A = d->julian_days + 1721425 + 32045;
238 B = ( 4 *(A + 36524) )/ 146097 - 1;
239 C = A - (146097 * B)/4;
240 D = ( 4 * (C + 365) ) / 1461 - 1;
241 E = C - ((1461*D) / 4);
242 M = (5 * (E - 1) + 2)/153;
244 m = M + 3 - (12*(M/10));
245 day = E - (153*M + 2)/5;
246 y = 100 * B + D - 4800 + (M/10);
248 #ifdef G_ENABLE_DEBUG
249 if (!g_date_valid_dmy (day, m, y))
250 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
251 d->julian_days, day, m, y);
262 g_date_get_weekday (const GDate *d)
264 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
267 g_date_update_julian (d);
269 g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
271 return ((d->julian_days - 1) % 7) + 1;
275 g_date_get_month (const GDate *d)
277 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
280 g_date_update_dmy (d);
282 g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
288 g_date_get_year (const GDate *d)
290 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
293 g_date_update_dmy (d);
295 g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
301 g_date_get_day (const GDate *d)
303 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
306 g_date_update_dmy (d);
308 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
314 g_date_get_julian (const GDate *d)
316 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);
334 g_date_update_dmy (d);
336 g_return_val_if_fail (d->dmy, 0);
338 idx = g_date_is_leap_year (d->year) ? 1 : 0;
340 return (days_in_year[idx][d->month] + d->day);
344 g_date_get_monday_week_of_year (const GDate *d)
350 g_return_val_if_fail (g_date_valid (d), 0);
353 g_date_update_dmy (d);
355 g_return_val_if_fail (d->dmy, 0);
357 g_date_clear (&first, 1);
359 g_date_set_dmy (&first, 1, 1, d->year);
361 wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
362 day = g_date_get_day_of_year (d) - 1;
364 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
368 g_date_get_sunday_week_of_year (const GDate *d)
374 g_return_val_if_fail (g_date_valid (d), 0);
377 g_date_update_dmy (d);
379 g_return_val_if_fail (d->dmy, 0);
381 g_date_clear (&first, 1);
383 g_date_set_dmy (&first, 1, 1, d->year);
385 wd = g_date_get_weekday (&first);
386 if (wd == 7) wd = 0; /* make Sunday day 0 */
387 day = g_date_get_day_of_year (d) - 1;
389 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
393 * g_date_get_iso8601_week_of_year:
394 * @date: a valid #GDate
396 * Returns the week of the year, where weeks are interpreted according
399 * Returns: ISO 8601 week number of the year.
404 g_date_get_iso8601_week_of_year (const GDate *d)
406 guint j, d4, L, d1, w;
408 g_return_val_if_fail (g_date_valid (d), 0);
411 g_date_update_julian (d);
413 g_return_val_if_fail (d->julian, 0);
415 /* Formula taken from the Calendar FAQ; the formula was for the
416 * Julian Period which starts on 1 January 4713 BC, so we add
417 * 1,721,425 to the number of days before doing the formula.
419 j = d->julian_days + 1721425;
420 d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
422 d1 = ((d4 - L) % 365) + L;
429 g_date_days_between (const GDate *d1,
432 g_return_val_if_fail (g_date_valid (d1), 0);
433 g_return_val_if_fail (g_date_valid (d2), 0);
435 return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
439 g_date_clear (GDate *d, guint ndates)
441 g_return_if_fail (d != NULL);
442 g_return_if_fail (ndates != 0);
444 memset (d, 0x0, ndates*sizeof (GDate));
447 G_LOCK_DEFINE_STATIC (g_date_global);
449 /* These are for the parser, output to the user should use *
450 * g_date_strftime () - this creates more never-freed memory to annoy
451 * all those memory debugger users. :-)
454 static gchar *long_month_names[13] =
459 static gchar *short_month_names[13] =
464 /* This tells us if we need to update the parse info */
465 static gchar *current_locale = NULL;
467 /* order of these in the current locale */
468 static GDateDMY dmy_order[3] =
470 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
473 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
474 * 29 and below are counted as in the year 2000, numbers 30 and above
475 * are counted as in the year 1900.
478 static const GDateYear twodigit_start_year = 1930;
480 /* It is impossible to enter a year between 1 AD and 99 AD with this
483 static gboolean using_twodigit_years = FALSE;
485 /* Adjustment of locale era to AD, non-zero means using locale era
487 static gint locale_era_adjust = 0;
489 struct _GDateParseTokens {
495 typedef struct _GDateParseTokens GDateParseTokens;
499 /* HOLDS: g_date_global_lock */
501 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
503 gchar num[4][NUM_LEN+1];
507 /* We count 4, but store 3; so we can give an error
510 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
512 s = (const guchar *) str;
514 while (*s && pt->num_ints < 4)
518 while (*s && g_ascii_isdigit (*s) && i < NUM_LEN)
520 num[pt->num_ints][i] = *s;
527 num[pt->num_ints][i] = '\0';
531 if (*s == '\0') break;
536 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
537 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
538 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
540 pt->month = G_DATE_BAD_MONTH;
542 if (pt->num_ints < 3)
547 casefold = g_utf8_casefold (str, -1);
548 normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
554 if (long_month_names[i] != NULL)
556 const gchar *found = strstr (normalized, long_month_names[i]);
565 if (short_month_names[i] != NULL)
567 const gchar *found = strstr (normalized, short_month_names[i]);
583 /* HOLDS: g_date_global_lock */
585 g_date_prepare_to_parse (const gchar *str,
586 GDateParseTokens *pt)
588 const gchar *locale = setlocale (LC_TIME, NULL);
589 gboolean recompute_localeinfo = FALSE;
592 g_return_if_fail (locale != NULL); /* should not happen */
594 g_date_clear (&d, 1); /* clear for scratch use */
596 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
597 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
599 if (recompute_localeinfo)
602 GDateParseTokens testpt;
605 g_free (current_locale); /* still works if current_locale == NULL */
607 current_locale = g_strdup (locale);
609 short_month_names[0] = "Error";
610 long_month_names[0] = "Error";
616 g_date_set_dmy (&d, 1, i, 1);
618 g_return_if_fail (g_date_valid (&d));
620 g_date_strftime (buf, 127, "%b", &d);
622 casefold = g_utf8_casefold (buf, -1);
623 g_free (short_month_names[i]);
624 short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
627 g_date_strftime (buf, 127, "%B", &d);
628 casefold = g_utf8_casefold (buf, -1);
629 g_free (long_month_names[i]);
630 long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
636 /* Determine DMY order */
638 /* had to pick a random day - don't change this, some strftimes
639 * are broken on some days, and this one is good so far. */
640 g_date_set_dmy (&d, 4, 7, 1976);
642 g_date_strftime (buf, 127, "%x", &d);
644 g_date_fill_parse_tokens (buf, &testpt);
647 while (i < testpt.num_ints)
652 dmy_order[i] = G_DATE_MONTH;
655 dmy_order[i] = G_DATE_DAY;
658 using_twodigit_years = TRUE; /* FALL THRU */
660 dmy_order[i] = G_DATE_YEAR;
663 /* assume locale era */
664 locale_era_adjust = 1976 - testpt.n[i];
665 dmy_order[i] = G_DATE_YEAR;
671 #ifdef G_ENABLE_DEBUG
672 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
676 DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
679 if (using_twodigit_years)
681 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
688 switch (dmy_order[i])
691 strings[i] = "Month";
705 DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
706 DEBUG_MSG (("**Sample date in this locale: `%s'", buf));
711 g_date_fill_parse_tokens (str, pt);
715 g_date_set_parse (GDate *d,
719 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
721 g_return_if_fail (d != NULL);
726 G_LOCK (g_date_global);
728 g_date_prepare_to_parse (str, &pt);
730 DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d",
731 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
734 if (pt.num_ints == 4)
736 G_UNLOCK (g_date_global);
737 return; /* presumably a typo; bail out. */
745 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
747 while (i < pt.num_ints && j < 3)
749 switch (dmy_order[j])
753 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
756 ++j; /* skip months, but don't skip this number */
765 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
768 ++j; /* skip days, since we may have month/year */
778 if (locale_era_adjust != 0)
780 y += locale_era_adjust;
782 else if (using_twodigit_years && y < 100)
784 guint two = twodigit_start_year % 100;
785 guint century = (twodigit_start_year / 100) * 100;
803 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
810 if (using_twodigit_years && y < 100)
811 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
813 else if (pt.num_ints == 2)
815 if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
819 else if (pt.num_ints == 1)
821 if (pt.month != G_DATE_BAD_MONTH)
823 /* Month name and year? */
830 /* Try yyyymmdd and yymmdd */
832 m = (pt.n[0]/100) % 100;
836 /* FIXME move this into a separate function */
837 if (using_twodigit_years && y < 100)
839 guint two = twodigit_start_year % 100;
840 guint century = (twodigit_start_year / 100) * 100;
850 /* See if we got anything valid out of all this. */
851 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
852 if (y < 8000 && g_date_valid_dmy (day, m, y))
859 #ifdef G_ENABLE_DEBUG
862 DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
865 G_UNLOCK (g_date_global);
871 * @timet: <type>time_t</type> value to set
873 * Sets the value of a date to the date corresponding to a time
874 * specified as a time_t. The time to date conversion is done using
875 * the user's current timezone.
877 * To set the value of a date to the current day, you could write:
879 * g_date_set_time_t (date, time (NULL));
885 g_date_set_time_t (GDate *date,
890 g_return_if_fail (date != NULL);
892 #ifdef HAVE_LOCALTIME_R
893 localtime_r (&timet, &tm);
896 struct tm *ptm = localtime (&timet);
900 /* Happens at least in Microsoft's C library if you pass a
901 * negative time_t. Use 2000-01-01 as default date.
903 #ifndef G_DISABLE_CHECKS
904 g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
912 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
916 date->julian = FALSE;
918 date->month = tm.tm_mon + 1;
919 date->day = tm.tm_mday;
920 date->year = tm.tm_year + 1900;
922 g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
931 * @time_: #GTime value to set.
933 * Sets the value of a date from a #GTime value.
934 * The time to date conversion is done using the user's current timezone.
936 * Deprecated: 2.10: Use g_date_set_time_t() instead.
939 g_date_set_time (GDate *date,
942 g_date_set_time_t (date, (time_t) time_);
946 * g_date_set_time_val:
948 * @timeval: #GTimeVal value to set
950 * Sets the value of a date from a #GTimeVal value. Note that the
951 * @tv_usec member is ignored, because #GDate can't make use of the
952 * additional precision.
954 * The time to date conversion is done using the user's current timezone.
959 g_date_set_time_val (GDate *date,
962 g_date_set_time_t (date, (time_t) timeval->tv_sec);
966 g_date_set_month (GDate *d,
969 g_return_if_fail (d != NULL);
970 g_return_if_fail (g_date_valid_month (m));
972 if (d->julian && !d->dmy) g_date_update_dmy(d);
977 if (g_date_valid_dmy (d->day, d->month, d->year))
984 g_date_set_day (GDate *d,
987 g_return_if_fail (d != NULL);
988 g_return_if_fail (g_date_valid_day (day));
990 if (d->julian && !d->dmy) g_date_update_dmy(d);
995 if (g_date_valid_dmy (d->day, d->month, d->year))
1002 g_date_set_year (GDate *d,
1005 g_return_if_fail (d != NULL);
1006 g_return_if_fail (g_date_valid_year (y));
1008 if (d->julian && !d->dmy) g_date_update_dmy(d);
1013 if (g_date_valid_dmy (d->day, d->month, d->year))
1020 g_date_set_dmy (GDate *d,
1025 g_return_if_fail (d != NULL);
1026 g_return_if_fail (g_date_valid_dmy (day, m, y));
1038 g_date_set_julian (GDate *d,
1041 g_return_if_fail (d != NULL);
1042 g_return_if_fail (g_date_valid_julian (j));
1051 g_date_is_first_of_month (const GDate *d)
1053 g_return_val_if_fail (g_date_valid (d), FALSE);
1056 g_date_update_dmy (d);
1058 g_return_val_if_fail (d->dmy, FALSE);
1060 if (d->day == 1) return TRUE;
1065 g_date_is_last_of_month (const GDate *d)
1069 g_return_val_if_fail (g_date_valid (d), FALSE);
1072 g_date_update_dmy (d);
1074 g_return_val_if_fail (d->dmy, FALSE);
1076 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1078 if (d->day == days_in_months[idx][d->month]) return TRUE;
1083 g_date_add_days (GDate *d,
1086 g_return_if_fail (g_date_valid (d));
1089 g_date_update_julian (d);
1091 g_return_if_fail (d->julian);
1093 d->julian_days += ndays;
1098 g_date_subtract_days (GDate *d,
1101 g_return_if_fail (g_date_valid (d));
1104 g_date_update_julian (d);
1106 g_return_if_fail (d->julian);
1107 g_return_if_fail (d->julian_days > ndays);
1109 d->julian_days -= ndays;
1114 g_date_add_months (GDate *d,
1117 guint years, months;
1120 g_return_if_fail (g_date_valid (d));
1123 g_date_update_dmy (d);
1125 g_return_if_fail (d->dmy);
1127 nmonths += d->month - 1;
1130 months = nmonths%12;
1132 d->month = months + 1;
1135 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1137 if (d->day > days_in_months[idx][d->month])
1138 d->day = days_in_months[idx][d->month];
1142 g_return_if_fail (g_date_valid (d));
1146 g_date_subtract_months (GDate *d,
1149 guint years, months;
1152 g_return_if_fail (g_date_valid (d));
1155 g_date_update_dmy (d);
1157 g_return_if_fail (d->dmy);
1160 months = nmonths%12;
1162 g_return_if_fail (d->year > years);
1166 if (d->month > months) d->month -= months;
1170 d->month = 12 - months;
1174 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1176 if (d->day > days_in_months[idx][d->month])
1177 d->day = days_in_months[idx][d->month];
1181 g_return_if_fail (g_date_valid (d));
1185 g_date_add_years (GDate *d,
1188 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))
1207 g_date_subtract_years (GDate *d,
1210 g_return_if_fail (g_date_valid (d));
1213 g_date_update_dmy (d);
1215 g_return_if_fail (d->dmy);
1216 g_return_if_fail (d->year > nyears);
1220 if (d->month == 2 && d->day == 29)
1222 if (!g_date_is_leap_year (d->year))
1230 g_date_is_leap_year (GDateYear year)
1232 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1234 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1235 (year % 400) == 0 );
1239 g_date_get_days_in_month (GDateMonth month,
1244 g_return_val_if_fail (g_date_valid_year (year), 0);
1245 g_return_val_if_fail (g_date_valid_month (month), 0);
1247 idx = g_date_is_leap_year (year) ? 1 : 0;
1249 return days_in_months[idx][month];
1253 g_date_get_monday_weeks_in_year (GDateYear year)
1257 g_return_val_if_fail (g_date_valid_year (year), 0);
1259 g_date_clear (&d, 1);
1260 g_date_set_dmy (&d, 1, 1, year);
1261 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1262 g_date_set_dmy (&d, 31, 12, year);
1263 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1264 if (g_date_is_leap_year (year))
1266 g_date_set_dmy (&d, 2, 1, year);
1267 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1268 g_date_set_dmy (&d, 30, 12, year);
1269 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1275 g_date_get_sunday_weeks_in_year (GDateYear year)
1279 g_return_val_if_fail (g_date_valid_year (year), 0);
1281 g_date_clear (&d, 1);
1282 g_date_set_dmy (&d, 1, 1, year);
1283 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1284 g_date_set_dmy (&d, 31, 12, year);
1285 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1286 if (g_date_is_leap_year (year))
1288 g_date_set_dmy (&d, 2, 1, year);
1289 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1290 g_date_set_dmy (&d, 30, 12, year);
1291 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1297 g_date_compare (const GDate *lhs,
1300 g_return_val_if_fail (lhs != NULL, 0);
1301 g_return_val_if_fail (rhs != NULL, 0);
1302 g_return_val_if_fail (g_date_valid (lhs), 0);
1303 g_return_val_if_fail (g_date_valid (rhs), 0);
1305 /* Remember the self-comparison case! I think it works right now. */
1309 if (lhs->julian && rhs->julian)
1311 if (lhs->julian_days < rhs->julian_days) return -1;
1312 else if (lhs->julian_days > rhs->julian_days) return 1;
1315 else if (lhs->dmy && rhs->dmy)
1317 if (lhs->year < rhs->year) return -1;
1318 else if (lhs->year > rhs->year) return 1;
1321 if (lhs->month < rhs->month) return -1;
1322 else if (lhs->month > rhs->month) return 1;
1325 if (lhs->day < rhs->day) return -1;
1326 else if (lhs->day > rhs->day) return 1;
1335 if (!lhs->julian) g_date_update_julian (lhs);
1336 if (!rhs->julian) g_date_update_julian (rhs);
1337 g_return_val_if_fail (lhs->julian, 0);
1338 g_return_val_if_fail (rhs->julian, 0);
1342 return 0; /* warnings */
1347 g_date_to_struct_tm (const GDate *d,
1352 g_return_if_fail (g_date_valid (d));
1353 g_return_if_fail (tm != NULL);
1356 g_date_update_dmy (d);
1358 g_return_if_fail (d->dmy);
1360 /* zero all the irrelevant fields to be sure they're valid */
1362 /* On Linux and maybe other systems, there are weird non-POSIX
1363 * fields on the end of struct tm that choke strftime if they
1364 * contain garbage. So we need to 0 the entire struct, not just the
1365 * fields we know to exist.
1368 memset (tm, 0x0, sizeof (struct tm));
1370 tm->tm_mday = d->day;
1371 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1372 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1374 day = g_date_get_weekday (d);
1375 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1377 tm->tm_wday = (int)day;
1379 tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
1380 tm->tm_isdst = -1; /* -1 means "information not available" */
1384 g_date_clamp (GDate *date,
1385 const GDate *min_date,
1386 const GDate *max_date)
1388 g_return_if_fail (g_date_valid (date));
1390 if (min_date != NULL)
1391 g_return_if_fail (g_date_valid (min_date));
1393 if (max_date != NULL)
1394 g_return_if_fail (g_date_valid (max_date));
1396 if (min_date != NULL && max_date != NULL)
1397 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
1399 if (min_date && g_date_compare (date, min_date) < 0)
1402 if (max_date && g_date_compare (max_date, date) < 0)
1407 g_date_order (GDate *date1,
1410 g_return_if_fail (g_date_valid (date1));
1411 g_return_if_fail (g_date_valid (date2));
1413 if (g_date_compare (date1, date2) > 0)
1423 win32_strftime_helper (const GDate *d,
1424 const gchar *format,
1425 const struct tm *tm,
1429 SYSTEMTIME systemtime;
1430 TIME_ZONE_INFORMATION tzinfo;
1436 const wchar_t digits[] = L"0123456789";
1441 systemtime.wYear = tm->tm_year + 1900;
1442 systemtime.wMonth = tm->tm_mon + 1;
1443 systemtime.wDayOfWeek = tm->tm_wday;
1444 systemtime.wDay = tm->tm_mday;
1445 systemtime.wHour = tm->tm_hour;
1446 systemtime.wMinute = tm->tm_min;
1447 systemtime.wSecond = tm->tm_sec;
1448 systemtime.wMilliseconds = 0;
1450 lcid = GetThreadLocale ();
1451 result = g_array_sized_new (FALSE, FALSE, sizeof (wchar_t), MAX (128, strlen (format) * 2));
1456 c = g_utf8_get_char (p);
1459 p = g_utf8_next_char (p);
1463 g_array_free (result, TRUE);
1468 c = g_utf8_get_char (p);
1469 if (c == 'E' || c == 'O')
1471 /* Ignore modified conversion specifiers for now. */
1472 p = g_utf8_next_char (p);
1476 g_array_free (result, TRUE);
1481 c = g_utf8_get_char (p);
1487 if (systemtime.wDayOfWeek == 0)
1490 k = systemtime.wDayOfWeek - 1;
1491 n = GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, NULL, 0);
1492 g_array_set_size (result, result->len + n);
1493 GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
1494 g_array_set_size (result, result->len - 1);
1497 if (systemtime.wDayOfWeek == 0)
1500 k = systemtime.wDayOfWeek - 1;
1501 n = GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, NULL, 0);
1502 g_array_set_size (result, result->len + n);
1503 GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
1504 g_array_set_size (result, result->len - 1);
1508 n = GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, NULL, 0);
1509 g_array_set_size (result, result->len + n);
1510 GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
1511 g_array_set_size (result, result->len - 1);
1514 n = GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, NULL, 0);
1515 g_array_set_size (result, result->len + n);
1516 GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
1517 g_array_set_size (result, result->len - 1);
1520 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1523 g_array_set_size (result, result->len + n);
1524 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1525 g_array_set_size (result, result->len - 1);
1527 g_array_append_vals (result, L" ", 1);
1528 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1531 g_array_set_size (result, result->len + n);
1532 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1533 g_array_set_size (result, result->len - 1);
1537 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
1538 g_array_append_vals (result, digits + (systemtime.wYear/1000)%10, 1);
1541 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1542 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1545 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
1546 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
1547 g_array_append_vals (result, L"/", 1);
1548 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1549 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1550 g_array_append_vals (result, L"/", 1);
1551 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
1552 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
1555 if (systemtime.wDay >= 10)
1556 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1558 g_array_append_vals (result, L" ", 1);
1559 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1562 /* A GDate has no time fields, so for now we can
1563 * hardcode all time conversions into zeros (or 12 for
1564 * %I). The alternative code snippets in the #else
1565 * branches are here ready to be taken into use when
1566 * needed by a g_strftime() or g_date_and_time_format()
1571 g_array_append_vals (result, L"00", 2);
1573 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1574 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1579 g_array_append_vals (result, L"12", 2);
1581 if (systemtime.wHour == 0)
1582 g_array_append_vals (result, L"12", 2);
1585 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
1586 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
1591 g_array_append_vals (result, digits + (tm->tm_yday+1)/100, 1);
1592 g_array_append_vals (result, digits + ((tm->tm_yday+1)/10)%10, 1);
1593 g_array_append_vals (result, digits + (tm->tm_yday+1)%10, 1);
1596 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
1597 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
1601 g_array_append_vals (result, L"00", 2);
1603 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1604 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1608 g_array_append_vals (result, L"\n", 1);
1611 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
1614 g_array_set_size (result, result->len + n);
1615 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
1616 g_array_set_size (result, result->len - 1);
1620 /* This is a rather odd format. Hard to say what to do.
1621 * Let's always use the POSIX %I:%M:%S %p
1624 g_array_append_vals (result, L"12:00:00", 8);
1626 if (systemtime.wHour == 0)
1627 g_array_append_vals (result, L"12", 2);
1630 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
1631 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
1633 g_array_append_vals (result, L":", 1);
1634 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1635 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1636 g_array_append_vals (result, L":", 1);
1637 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1638 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1639 g_array_append_vals (result, L" ", 1);
1641 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
1644 g_array_set_size (result, result->len + n);
1645 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
1646 g_array_set_size (result, result->len - 1);
1651 g_array_append_vals (result, L"00:00", 5);
1653 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1654 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1655 g_array_append_vals (result, L":", 1);
1656 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1657 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1662 g_array_append_vals (result, L"00", 2);
1664 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1665 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1669 g_array_append_vals (result, L"\t", 1);
1673 g_array_append_vals (result, L"00:00:00", 8);
1675 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1676 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1677 g_array_append_vals (result, L":", 1);
1678 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1679 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1680 g_array_append_vals (result, L":", 1);
1681 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1682 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1686 if (systemtime.wDayOfWeek == 0)
1687 g_array_append_vals (result, L"7", 1);
1689 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
1692 n = g_date_get_sunday_week_of_year (d);
1693 g_array_append_vals (result, digits + n/10, 1);
1694 g_array_append_vals (result, digits + n%10, 1);
1697 n = g_date_get_iso8601_week_of_year (d);
1698 g_array_append_vals (result, digits + n/10, 1);
1699 g_array_append_vals (result, digits + n%10, 1);
1702 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
1705 n = g_date_get_monday_week_of_year (d);
1706 g_array_append_vals (result, digits + n/10, 1);
1707 g_array_append_vals (result, digits + n%10, 1);
1710 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1713 g_array_set_size (result, result->len + n);
1714 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1715 g_array_set_size (result, result->len - 1);
1719 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1722 g_array_set_size (result, result->len + n);
1723 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1724 g_array_set_size (result, result->len - 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 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
1733 g_array_append_vals (result, digits + (systemtime.wYear/100)%10, 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 n = GetTimeZoneInformation (&tzinfo);
1739 if (n == TIME_ZONE_ID_UNKNOWN)
1741 else if (n == TIME_ZONE_ID_STANDARD)
1742 g_array_append_vals (result, tzinfo.StandardName, wcslen (tzinfo.StandardName));
1743 else if (n == TIME_ZONE_ID_DAYLIGHT)
1744 g_array_append_vals (result, tzinfo.DaylightName, wcslen (tzinfo.DaylightName));
1747 g_array_append_vals (result, L"%", 1);
1751 else if (c <= 0xFFFF)
1754 g_array_append_vals (result, &wc, 1);
1761 ws = g_ucs4_to_utf16 (&c, 1, NULL, &nwc, NULL);
1762 g_array_append_vals (result, ws, nwc);
1765 p = g_utf8_next_char (p);
1768 convbuf = g_utf16_to_utf8 ((wchar_t *) result->data, result->len, NULL, &convlen, NULL);
1769 g_array_free (result, TRUE);
1777 if (slen <= convlen)
1779 /* Ensure only whole characters are copied into the buffer. */
1780 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1781 g_assert (end != NULL);
1782 convlen = end - convbuf;
1784 /* Return 0 because the buffer isn't large enough. */
1790 memcpy (s, convbuf, convlen);
1800 g_date_strftime (gchar *s,
1802 const gchar *format,
1807 gsize locale_format_len = 0;
1808 gchar *locale_format;
1814 GError *error = NULL;
1818 g_return_val_if_fail (g_date_valid (d), 0);
1819 g_return_val_if_fail (slen > 0, 0);
1820 g_return_val_if_fail (format != NULL, 0);
1821 g_return_val_if_fail (s != NULL, 0);
1823 g_date_to_struct_tm (d, &tm);
1826 if (!g_utf8_validate (format, -1, NULL))
1831 return win32_strftime_helper (d, format, &tm, s, slen);
1834 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
1838 g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
1839 g_error_free (error);
1845 tmpbufsize = MAX (128, locale_format_len * 2);
1848 tmpbuf = g_malloc (tmpbufsize);
1850 /* Set the first byte to something other than '\0', to be able to
1851 * recognize whether strftime actually failed or just returned "".
1854 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
1856 if (tmplen == 0 && tmpbuf[0] != '\0')
1861 if (tmpbufsize > 65536)
1863 g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
1864 g_free (locale_format);
1873 g_free (locale_format);
1875 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
1880 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
1881 g_error_free (error);
1887 if (slen <= convlen)
1889 /* Ensure only whole characters are copied into the buffer.
1891 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1892 g_assert (end != NULL);
1893 convlen = end - convbuf;
1895 /* Return 0 because the buffer isn't large enough.
1902 memcpy (s, convbuf, convlen);