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.
951 g_date_set_time_val (GDate *date,
954 g_date_set_time_t (date, (time_t) timeval->tv_sec);
958 g_date_set_month (GDate *d,
961 g_return_if_fail (d != NULL);
962 g_return_if_fail (g_date_valid_month (m));
964 if (d->julian && !d->dmy) g_date_update_dmy(d);
969 if (g_date_valid_dmy (d->day, d->month, d->year))
976 g_date_set_day (GDate *d,
979 g_return_if_fail (d != NULL);
980 g_return_if_fail (g_date_valid_day (day));
982 if (d->julian && !d->dmy) g_date_update_dmy(d);
987 if (g_date_valid_dmy (d->day, d->month, d->year))
994 g_date_set_year (GDate *d,
997 g_return_if_fail (d != NULL);
998 g_return_if_fail (g_date_valid_year (y));
1000 if (d->julian && !d->dmy) g_date_update_dmy(d);
1005 if (g_date_valid_dmy (d->day, d->month, d->year))
1012 g_date_set_dmy (GDate *d,
1017 g_return_if_fail (d != NULL);
1018 g_return_if_fail (g_date_valid_dmy (day, m, y));
1030 g_date_set_julian (GDate *d,
1033 g_return_if_fail (d != NULL);
1034 g_return_if_fail (g_date_valid_julian (j));
1043 g_date_is_first_of_month (const GDate *d)
1045 g_return_val_if_fail (g_date_valid (d), FALSE);
1048 g_date_update_dmy (d);
1050 g_return_val_if_fail (d->dmy, FALSE);
1052 if (d->day == 1) return TRUE;
1057 g_date_is_last_of_month (const GDate *d)
1061 g_return_val_if_fail (g_date_valid (d), FALSE);
1064 g_date_update_dmy (d);
1066 g_return_val_if_fail (d->dmy, FALSE);
1068 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1070 if (d->day == days_in_months[idx][d->month]) return TRUE;
1075 g_date_add_days (GDate *d,
1078 g_return_if_fail (g_date_valid (d));
1081 g_date_update_julian (d);
1083 g_return_if_fail (d->julian);
1085 d->julian_days += ndays;
1090 g_date_subtract_days (GDate *d,
1093 g_return_if_fail (g_date_valid (d));
1096 g_date_update_julian (d);
1098 g_return_if_fail (d->julian);
1099 g_return_if_fail (d->julian_days > ndays);
1101 d->julian_days -= ndays;
1106 g_date_add_months (GDate *d,
1109 guint years, months;
1112 g_return_if_fail (g_date_valid (d));
1115 g_date_update_dmy (d);
1117 g_return_if_fail (d->dmy);
1119 nmonths += d->month - 1;
1122 months = nmonths%12;
1124 d->month = months + 1;
1127 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1129 if (d->day > days_in_months[idx][d->month])
1130 d->day = days_in_months[idx][d->month];
1134 g_return_if_fail (g_date_valid (d));
1138 g_date_subtract_months (GDate *d,
1141 guint years, months;
1144 g_return_if_fail (g_date_valid (d));
1147 g_date_update_dmy (d);
1149 g_return_if_fail (d->dmy);
1152 months = nmonths%12;
1154 g_return_if_fail (d->year > years);
1158 if (d->month > months) d->month -= months;
1162 d->month = 12 - months;
1166 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1168 if (d->day > days_in_months[idx][d->month])
1169 d->day = days_in_months[idx][d->month];
1173 g_return_if_fail (g_date_valid (d));
1177 g_date_add_years (GDate *d,
1180 g_return_if_fail (g_date_valid (d));
1183 g_date_update_dmy (d);
1185 g_return_if_fail (d->dmy);
1189 if (d->month == 2 && d->day == 29)
1191 if (!g_date_is_leap_year (d->year))
1199 g_date_subtract_years (GDate *d,
1202 g_return_if_fail (g_date_valid (d));
1205 g_date_update_dmy (d);
1207 g_return_if_fail (d->dmy);
1208 g_return_if_fail (d->year > nyears);
1212 if (d->month == 2 && d->day == 29)
1214 if (!g_date_is_leap_year (d->year))
1222 g_date_is_leap_year (GDateYear year)
1224 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1226 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1227 (year % 400) == 0 );
1231 g_date_get_days_in_month (GDateMonth month,
1236 g_return_val_if_fail (g_date_valid_year (year), 0);
1237 g_return_val_if_fail (g_date_valid_month (month), 0);
1239 idx = g_date_is_leap_year (year) ? 1 : 0;
1241 return days_in_months[idx][month];
1245 g_date_get_monday_weeks_in_year (GDateYear year)
1249 g_return_val_if_fail (g_date_valid_year (year), 0);
1251 g_date_clear (&d, 1);
1252 g_date_set_dmy (&d, 1, 1, year);
1253 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1254 g_date_set_dmy (&d, 31, 12, year);
1255 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1256 if (g_date_is_leap_year (year))
1258 g_date_set_dmy (&d, 2, 1, year);
1259 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1260 g_date_set_dmy (&d, 30, 12, year);
1261 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1267 g_date_get_sunday_weeks_in_year (GDateYear year)
1271 g_return_val_if_fail (g_date_valid_year (year), 0);
1273 g_date_clear (&d, 1);
1274 g_date_set_dmy (&d, 1, 1, year);
1275 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1276 g_date_set_dmy (&d, 31, 12, year);
1277 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1278 if (g_date_is_leap_year (year))
1280 g_date_set_dmy (&d, 2, 1, year);
1281 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1282 g_date_set_dmy (&d, 30, 12, year);
1283 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1289 g_date_compare (const GDate *lhs,
1292 g_return_val_if_fail (lhs != NULL, 0);
1293 g_return_val_if_fail (rhs != NULL, 0);
1294 g_return_val_if_fail (g_date_valid (lhs), 0);
1295 g_return_val_if_fail (g_date_valid (rhs), 0);
1297 /* Remember the self-comparison case! I think it works right now. */
1301 if (lhs->julian && rhs->julian)
1303 if (lhs->julian_days < rhs->julian_days) return -1;
1304 else if (lhs->julian_days > rhs->julian_days) return 1;
1307 else if (lhs->dmy && rhs->dmy)
1309 if (lhs->year < rhs->year) return -1;
1310 else if (lhs->year > rhs->year) return 1;
1313 if (lhs->month < rhs->month) return -1;
1314 else if (lhs->month > rhs->month) return 1;
1317 if (lhs->day < rhs->day) return -1;
1318 else if (lhs->day > rhs->day) return 1;
1327 if (!lhs->julian) g_date_update_julian (lhs);
1328 if (!rhs->julian) g_date_update_julian (rhs);
1329 g_return_val_if_fail (lhs->julian, 0);
1330 g_return_val_if_fail (rhs->julian, 0);
1334 return 0; /* warnings */
1339 g_date_to_struct_tm (const GDate *d,
1344 g_return_if_fail (g_date_valid (d));
1345 g_return_if_fail (tm != NULL);
1348 g_date_update_dmy (d);
1350 g_return_if_fail (d->dmy);
1352 /* zero all the irrelevant fields to be sure they're valid */
1354 /* On Linux and maybe other systems, there are weird non-POSIX
1355 * fields on the end of struct tm that choke strftime if they
1356 * contain garbage. So we need to 0 the entire struct, not just the
1357 * fields we know to exist.
1360 memset (tm, 0x0, sizeof (struct tm));
1362 tm->tm_mday = d->day;
1363 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1364 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1366 day = g_date_get_weekday (d);
1367 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1369 tm->tm_wday = (int)day;
1371 tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
1372 tm->tm_isdst = -1; /* -1 means "information not available" */
1376 g_date_clamp (GDate *date,
1377 const GDate *min_date,
1378 const GDate *max_date)
1380 g_return_if_fail (g_date_valid (date));
1382 if (min_date != NULL)
1383 g_return_if_fail (g_date_valid (min_date));
1385 if (max_date != NULL)
1386 g_return_if_fail (g_date_valid (max_date));
1388 if (min_date != NULL && max_date != NULL)
1389 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
1391 if (min_date && g_date_compare (date, min_date) < 0)
1394 if (max_date && g_date_compare (max_date, date) < 0)
1399 g_date_order (GDate *date1,
1402 g_return_if_fail (g_date_valid (date1));
1403 g_return_if_fail (g_date_valid (date2));
1405 if (g_date_compare (date1, date2) > 0)
1415 win32_strftime_helper (const GDate *d,
1416 const gchar *format,
1417 const struct tm *tm,
1421 SYSTEMTIME systemtime;
1422 TIME_ZONE_INFORMATION tzinfo;
1428 const wchar_t digits[] = L"0123456789";
1433 systemtime.wYear = tm->tm_year + 1900;
1434 systemtime.wMonth = tm->tm_mon + 1;
1435 systemtime.wDayOfWeek = tm->tm_wday;
1436 systemtime.wDay = tm->tm_mday;
1437 systemtime.wHour = tm->tm_hour;
1438 systemtime.wMinute = tm->tm_min;
1439 systemtime.wSecond = tm->tm_sec;
1440 systemtime.wMilliseconds = 0;
1442 lcid = GetThreadLocale ();
1443 result = g_array_sized_new (FALSE, FALSE, sizeof (wchar_t), MAX (128, strlen (format) * 2));
1448 c = g_utf8_get_char (p);
1451 p = g_utf8_next_char (p);
1455 g_array_free (result, TRUE);
1460 c = g_utf8_get_char (p);
1461 if (c == 'E' || c == 'O')
1463 /* Ignore modified conversion specifiers for now. */
1464 p = g_utf8_next_char (p);
1468 g_array_free (result, TRUE);
1473 c = g_utf8_get_char (p);
1479 if (systemtime.wDayOfWeek == 0)
1482 k = systemtime.wDayOfWeek - 1;
1483 n = GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, NULL, 0);
1484 g_array_set_size (result, result->len + n);
1485 GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
1486 g_array_set_size (result, result->len - 1);
1489 if (systemtime.wDayOfWeek == 0)
1492 k = systemtime.wDayOfWeek - 1;
1493 n = GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, NULL, 0);
1494 g_array_set_size (result, result->len + n);
1495 GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
1496 g_array_set_size (result, result->len - 1);
1500 n = GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, NULL, 0);
1501 g_array_set_size (result, result->len + n);
1502 GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
1503 g_array_set_size (result, result->len - 1);
1506 n = GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, NULL, 0);
1507 g_array_set_size (result, result->len + n);
1508 GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
1509 g_array_set_size (result, result->len - 1);
1512 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1515 g_array_set_size (result, result->len + n);
1516 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1517 g_array_set_size (result, result->len - 1);
1519 g_array_append_vals (result, L" ", 1);
1520 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1523 g_array_set_size (result, result->len + n);
1524 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1525 g_array_set_size (result, result->len - 1);
1529 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
1530 g_array_append_vals (result, digits + (systemtime.wYear/1000)%10, 1);
1533 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1534 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1537 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
1538 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
1539 g_array_append_vals (result, L"/", 1);
1540 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1541 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1542 g_array_append_vals (result, L"/", 1);
1543 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
1544 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
1547 if (systemtime.wDay >= 10)
1548 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.wDay%10, 1);
1554 /* A GDate has no time fields, so for now we can
1555 * hardcode all time conversions into zeros (or 12 for
1556 * %I). The alternative code snippets in the #else
1557 * branches are here ready to be taken into use when
1558 * needed by a g_strftime() or g_date_and_time_format()
1563 g_array_append_vals (result, L"00", 2);
1565 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1566 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1571 g_array_append_vals (result, L"12", 2);
1573 if (systemtime.wHour == 0)
1574 g_array_append_vals (result, L"12", 2);
1577 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
1578 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
1583 g_array_append_vals (result, digits + (tm->tm_yday+1)/100, 1);
1584 g_array_append_vals (result, digits + ((tm->tm_yday+1)/10)%10, 1);
1585 g_array_append_vals (result, digits + (tm->tm_yday+1)%10, 1);
1588 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
1589 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
1593 g_array_append_vals (result, L"00", 2);
1595 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1596 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1600 g_array_append_vals (result, L"\n", 1);
1603 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
1606 g_array_set_size (result, result->len + n);
1607 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
1608 g_array_set_size (result, result->len - 1);
1612 /* This is a rather odd format. Hard to say what to do.
1613 * Let's always use the POSIX %I:%M:%S %p
1616 g_array_append_vals (result, L"12:00:00", 8);
1618 if (systemtime.wHour == 0)
1619 g_array_append_vals (result, L"12", 2);
1622 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
1623 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
1625 g_array_append_vals (result, L":", 1);
1626 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1627 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1628 g_array_append_vals (result, L":", 1);
1629 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1630 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1631 g_array_append_vals (result, L" ", 1);
1633 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
1636 g_array_set_size (result, result->len + n);
1637 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
1638 g_array_set_size (result, result->len - 1);
1643 g_array_append_vals (result, L"00:00", 5);
1645 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1646 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1647 g_array_append_vals (result, L":", 1);
1648 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1649 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1654 g_array_append_vals (result, L"00", 2);
1656 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1657 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1661 g_array_append_vals (result, L"\t", 1);
1665 g_array_append_vals (result, L"00:00:00", 8);
1667 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1668 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1669 g_array_append_vals (result, L":", 1);
1670 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1671 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1672 g_array_append_vals (result, L":", 1);
1673 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1674 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1678 if (systemtime.wDayOfWeek == 0)
1679 g_array_append_vals (result, L"7", 1);
1681 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
1684 n = g_date_get_sunday_week_of_year (d);
1685 g_array_append_vals (result, digits + n/10, 1);
1686 g_array_append_vals (result, digits + n%10, 1);
1689 n = g_date_get_iso8601_week_of_year (d);
1690 g_array_append_vals (result, digits + n/10, 1);
1691 g_array_append_vals (result, digits + n%10, 1);
1694 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
1697 n = g_date_get_monday_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 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1705 g_array_set_size (result, result->len + n);
1706 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1707 g_array_set_size (result, result->len - 1);
1711 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1714 g_array_set_size (result, result->len + n);
1715 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1716 g_array_set_size (result, result->len - 1);
1720 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
1721 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
1724 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
1725 g_array_append_vals (result, digits + (systemtime.wYear/100)%10, 1);
1726 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
1727 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
1730 n = GetTimeZoneInformation (&tzinfo);
1731 if (n == TIME_ZONE_ID_UNKNOWN)
1733 else if (n == TIME_ZONE_ID_STANDARD)
1734 g_array_append_vals (result, tzinfo.StandardName, wcslen (tzinfo.StandardName));
1735 else if (n == TIME_ZONE_ID_DAYLIGHT)
1736 g_array_append_vals (result, tzinfo.DaylightName, wcslen (tzinfo.DaylightName));
1739 g_array_append_vals (result, L"%", 1);
1743 else if (c <= 0xFFFF)
1746 g_array_append_vals (result, &wc, 1);
1753 ws = g_ucs4_to_utf16 (&c, 1, NULL, &nwc, NULL);
1754 g_array_append_vals (result, ws, nwc);
1757 p = g_utf8_next_char (p);
1760 convbuf = g_utf16_to_utf8 ((wchar_t *) result->data, result->len, NULL, &convlen, NULL);
1761 g_array_free (result, TRUE);
1769 if (slen <= convlen)
1771 /* Ensure only whole characters are copied into the buffer. */
1772 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1773 g_assert (end != NULL);
1774 convlen = end - convbuf;
1776 /* Return 0 because the buffer isn't large enough. */
1782 memcpy (s, convbuf, convlen);
1792 g_date_strftime (gchar *s,
1794 const gchar *format,
1799 gsize locale_format_len = 0;
1800 gchar *locale_format;
1806 GError *error = NULL;
1810 g_return_val_if_fail (g_date_valid (d), 0);
1811 g_return_val_if_fail (slen > 0, 0);
1812 g_return_val_if_fail (format != NULL, 0);
1813 g_return_val_if_fail (s != NULL, 0);
1815 g_date_to_struct_tm (d, &tm);
1818 if (!g_utf8_validate (format, -1, NULL))
1823 return win32_strftime_helper (d, format, &tm, s, slen);
1826 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
1830 g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
1831 g_error_free (error);
1837 tmpbufsize = MAX (128, locale_format_len * 2);
1840 tmpbuf = g_malloc (tmpbufsize);
1842 /* Set the first byte to something other than '\0', to be able to
1843 * recognize whether strftime actually failed or just returned "".
1846 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
1848 if (tmplen == 0 && tmpbuf[0] != '\0')
1853 if (tmpbufsize > 65536)
1855 g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
1856 g_free (locale_format);
1865 g_free (locale_format);
1867 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
1872 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
1873 g_error_free (error);
1879 if (slen <= convlen)
1881 /* Ensure only whole characters are copied into the buffer.
1883 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1884 g_assert (end != NULL);
1885 convlen = end - convbuf;
1887 /* Return 0 because the buffer isn't large enough.
1894 memcpy (s, convbuf, convlen);