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 ; */
48 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
54 g_date_new_dmy (GDateDay day, GDateMonth m, GDateYear y)
57 g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
68 g_assert (g_date_valid (d));
74 g_date_new_julian (guint32 j)
77 g_return_val_if_fail (g_date_valid_julian (j), NULL);
86 g_assert (g_date_valid (d));
92 g_date_free (GDate *d)
94 g_return_if_fail (d != NULL);
100 g_date_valid (const GDate *d)
102 g_return_val_if_fail (d != NULL, FALSE);
104 return (d->julian || d->dmy);
107 static const guint8 days_in_months[2][13] =
108 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
109 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
110 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
113 static const guint16 days_in_year[2][14] =
114 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
115 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
116 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
120 g_date_valid_month (GDateMonth m)
122 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
126 g_date_valid_year (GDateYear y)
128 return ( y > G_DATE_BAD_YEAR );
132 g_date_valid_day (GDateDay d)
134 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
138 g_date_valid_weekday (GDateWeekday w)
140 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
144 g_date_valid_julian (guint32 j)
146 return (j > G_DATE_BAD_JULIAN);
150 g_date_valid_dmy (GDateDay d,
154 return ( (m > G_DATE_BAD_MONTH) &&
156 (d > G_DATE_BAD_DAY) &&
157 (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
158 (d <= (g_date_is_leap_year (y) ?
159 days_in_months[1][m] : days_in_months[0][m])) );
163 /* "Julian days" just means an absolute number of days, where Day 1 ==
167 g_date_update_julian (const GDate *const_d)
169 GDate *d = (GDate *) const_d;
173 g_return_if_fail (d != NULL);
174 g_return_if_fail (d->dmy);
175 g_return_if_fail (!d->julian);
176 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
178 /* What we actually do is: multiply years * 365 days in the year,
179 * add the number of years divided by 4, subtract the number of
180 * years divided by 100 and add the number of years divided by 400,
181 * which accounts for leap year stuff. Code from Steffen Beyer's
185 year = d->year - 1; /* we know d->year > 0 since it's valid */
187 d->julian_days = year * 365U;
188 d->julian_days += (year >>= 2); /* divide by 4 and add */
189 d->julian_days -= (year /= 25); /* divides original # years by 100 */
190 d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
192 index = g_date_is_leap_year (d->year) ? 1 : 0;
194 d->julian_days += days_in_year[index][d->month] + d->day;
196 g_return_if_fail (g_date_valid_julian (d->julian_days));
202 g_date_update_dmy (const GDate *const_d)
204 GDate *d = (GDate *) const_d;
209 guint32 A, B, C, D, E, M;
211 g_return_if_fail (d != NULL);
212 g_return_if_fail (d->julian);
213 g_return_if_fail (!d->dmy);
214 g_return_if_fail (g_date_valid_julian (d->julian_days));
216 /* Formula taken from the Calendar FAQ; the formula was for the
217 * Julian Period which starts on 1 January 4713 BC, so we add
218 * 1,721,425 to the number of days before doing the formula.
220 * I'm sure this can be simplified for our 1 January 1 AD period
221 * start, but I can't figure out how to unpack the formula.
224 A = d->julian_days + 1721425 + 32045;
225 B = ( 4 *(A + 36524) )/ 146097 - 1;
226 C = A - (146097 * B)/4;
227 D = ( 4 * (C + 365) ) / 1461 - 1;
228 E = C - ((1461*D) / 4);
229 M = (5 * (E - 1) + 2)/153;
231 m = M + 3 - (12*(M/10));
232 day = E - (153*M + 2)/5;
233 y = 100 * B + D - 4800 + (M/10);
235 #ifdef G_ENABLE_DEBUG
236 if (!g_date_valid_dmy (day, m, y))
238 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
239 d->julian_days, day, m, y);
251 g_date_get_weekday (const GDate *d)
253 g_return_val_if_fail (d != NULL, G_DATE_BAD_WEEKDAY);
254 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
258 g_date_update_julian (d);
260 g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
262 return ((d->julian_days - 1) % 7) + 1;
266 g_date_get_month (const GDate *d)
268 g_return_val_if_fail (d != NULL, G_DATE_BAD_MONTH);
269 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
273 g_date_update_dmy (d);
275 g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
281 g_date_get_year (const GDate *d)
283 g_return_val_if_fail (d != NULL, G_DATE_BAD_YEAR);
284 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
288 g_date_update_dmy (d);
290 g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
296 g_date_get_day (const GDate *d)
298 g_return_val_if_fail (d != NULL, G_DATE_BAD_DAY);
299 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
303 g_date_update_dmy (d);
305 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
311 g_date_get_julian (const GDate *d)
313 g_return_val_if_fail (d != NULL, G_DATE_BAD_JULIAN);
314 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
318 g_date_update_julian (d);
320 g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
322 return d->julian_days;
326 g_date_get_day_of_year (const GDate *d)
330 g_return_val_if_fail (d != NULL, 0);
331 g_return_val_if_fail (g_date_valid (d), 0);
335 g_date_update_dmy (d);
337 g_return_val_if_fail (d->dmy, 0);
339 index = g_date_is_leap_year (d->year) ? 1 : 0;
341 return (days_in_year[index][d->month] + d->day);
345 g_date_get_monday_week_of_year (const GDate *d)
351 g_return_val_if_fail (d != NULL, 0);
352 g_return_val_if_fail (g_date_valid (d), 0);
356 g_date_update_dmy (d);
358 g_return_val_if_fail (d->dmy, 0);
360 g_date_clear (&first, 1);
362 g_date_set_dmy (&first, 1, 1, d->year);
364 wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
365 day = g_date_get_day_of_year (d) - 1;
367 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
371 g_date_get_sunday_week_of_year (const GDate *d)
377 g_return_val_if_fail (d != NULL, 0);
378 g_return_val_if_fail (g_date_valid (d), 0);
382 g_date_update_dmy (d);
384 g_return_val_if_fail (d->dmy, 0);
386 g_date_clear (&first, 1);
388 g_date_set_dmy (&first, 1, 1, d->year);
390 wd = g_date_get_weekday (&first);
391 if (wd == 7) wd = 0; /* make Sunday day 0 */
392 day = g_date_get_day_of_year (d) - 1;
394 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
398 * g_date_get_iso8601_week_of_year:
399 * @date: a valid #GDate
401 * Returns the week of the year, where weeks are interpreted according
404 * Returns: ISO 8601 week number of the year.
409 g_date_get_iso8601_week_of_year (const GDate *d)
411 guint j, d4, L, d1, w;
413 g_return_val_if_fail (d != NULL, 0);
414 g_return_val_if_fail (g_date_valid (d), 0);
417 g_date_update_julian (d);
418 g_return_val_if_fail (d->julian, 0);
420 /* Formula taken from the Calendar FAQ; the formula was for the
421 * Julian Period which starts on 1 January 4713 BC, so we add
422 * 1,721,425 to the number of days before doing the formula.
424 j = d->julian + 1721425;
425 d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
427 d1 = ((d4 - L) % 365) + L;
434 g_date_days_between (const GDate *d1,
437 g_return_val_if_fail (d1 != NULL, 0);
438 g_return_val_if_fail (d2 != NULL, 0);
440 g_return_val_if_fail (g_date_valid (d1), 0);
441 g_return_val_if_fail (g_date_valid (d2), 0);
443 return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
447 g_date_clear (GDate *d, guint ndates)
449 g_return_if_fail (d != NULL);
450 g_return_if_fail (ndates != 0);
452 memset (d, 0x0, ndates*sizeof (GDate));
455 G_LOCK_DEFINE_STATIC (g_date_global);
457 /* These are for the parser, output to the user should use *
458 * g_date_strftime () - this creates more never-freed memory to annoy
459 * all those memory debugger users. :-)
462 static gchar *long_month_names[13] =
464 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
467 static gchar *short_month_names[13] =
469 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
472 /* This tells us if we need to update the parse info */
473 static gchar *current_locale = NULL;
475 /* order of these in the current locale */
476 static GDateDMY dmy_order[3] =
478 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
481 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
482 * 29 and below are counted as in the year 2000, numbers 30 and above
483 * are counted as in the year 1900.
486 static GDateYear twodigit_start_year = 1930;
488 /* It is impossible to enter a year between 1 AD and 99 AD with this
491 static gboolean using_twodigit_years = FALSE;
493 /* Adjustment of locale era to AD, non-zero means using locale era
495 static gint locale_era_adjust = 0;
497 struct _GDateParseTokens {
503 typedef struct _GDateParseTokens GDateParseTokens;
507 /* HOLDS: g_date_global_lock */
509 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
511 gchar num[4][NUM_LEN+1];
515 /* We count 4, but store 3; so we can give an error
518 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
520 s = (const guchar *) str;
522 while (*s && pt->num_ints < 4)
526 while (*s && g_ascii_isdigit (*s) && i <= NUM_LEN)
528 num[pt->num_ints][i] = *s;
535 num[pt->num_ints][i] = '\0';
539 if (*s == '\0') break;
544 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
545 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
546 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
548 pt->month = G_DATE_BAD_MONTH;
550 if (pt->num_ints < 3)
555 casefold = g_utf8_casefold (str, -1);
556 normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
562 if (long_month_names[i] != NULL)
564 const gchar *found = strstr (normalized, long_month_names[i]);
573 if (short_month_names[i] != NULL)
575 const gchar *found = strstr (normalized, short_month_names[i]);
591 /* HOLDS: g_date_global_lock */
593 g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
595 const gchar *locale = setlocale (LC_TIME, NULL);
596 gboolean recompute_localeinfo = FALSE;
599 g_return_if_fail (locale != NULL); /* should not happen */
601 g_date_clear (&d, 1); /* clear for scratch use */
603 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
605 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
608 if (recompute_localeinfo)
611 GDateParseTokens testpt;
614 g_free (current_locale); /* still works if current_locale == NULL */
616 current_locale = g_strdup (locale);
622 g_date_set_dmy (&d, 1, i, 1);
624 g_return_if_fail (g_date_valid (&d));
626 g_date_strftime (buf, 127, "%b", &d);
628 casefold = g_utf8_casefold (buf, -1);
629 g_free (short_month_names[i]);
630 short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
633 g_date_strftime (buf, 127, "%B", &d);
634 casefold = g_utf8_casefold (buf, -1);
635 g_free (long_month_names[i]);
636 long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
642 /* Determine DMY order */
644 /* had to pick a random day - don't change this, some strftimes
645 * are broken on some days, and this one is good so far. */
646 g_date_set_dmy (&d, 4, 7, 1976);
648 g_date_strftime (buf, 127, "%x", &d);
650 g_date_fill_parse_tokens (buf, &testpt);
653 while (i < testpt.num_ints)
658 dmy_order[i] = G_DATE_MONTH;
661 dmy_order[i] = G_DATE_DAY;
664 using_twodigit_years = TRUE; /* FALL THRU */
666 dmy_order[i] = G_DATE_YEAR;
669 /* assume locale era */
670 locale_era_adjust = 1976 - testpt.n[i];
671 dmy_order[i] = G_DATE_YEAR;
677 #ifdef G_ENABLE_DEBUG
678 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
682 DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
685 if (using_twodigit_years)
686 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
692 switch (dmy_order[i])
695 strings[i] = "Month";
709 DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
710 DEBUG_MSG (("**Sample date in this locale: `%s'", buf));
715 g_date_fill_parse_tokens (str, pt);
719 g_date_set_parse (GDate *d,
723 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
725 g_return_if_fail (d != NULL);
730 G_LOCK (g_date_global);
732 g_date_prepare_to_parse (str, &pt);
734 DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d",
735 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
738 if (pt.num_ints == 4)
740 G_UNLOCK (g_date_global);
741 return; /* presumably a typo; bail out. */
749 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
751 while (i < pt.num_ints && j < 3)
753 switch (dmy_order[j])
757 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
760 ++j; /* skip months, but don't skip this number */
769 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
772 ++j; /* skip days, since we may have month/year */
782 if (locale_era_adjust != 0)
784 y += locale_era_adjust;
786 else if (using_twodigit_years && y < 100)
788 guint two = twodigit_start_year % 100;
789 guint century = (twodigit_start_year / 100) * 100;
807 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
814 if (using_twodigit_years && y < 100)
815 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
817 else if (pt.num_ints == 2)
819 if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
825 else if (pt.num_ints == 1)
827 if (pt.month != G_DATE_BAD_MONTH)
829 /* Month name and year? */
836 /* Try yyyymmdd and yymmdd */
838 m = (pt.n[0]/100) % 100;
842 /* FIXME move this into a separate function */
843 if (using_twodigit_years && y < 100)
845 guint two = twodigit_start_year % 100;
846 guint century = (twodigit_start_year / 100) * 100;
856 /* See if we got anything valid out of all this. */
857 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
858 if (y < 8000 && g_date_valid_dmy (day, m, y))
865 #ifdef G_ENABLE_DEBUG
867 DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
869 G_UNLOCK (g_date_global);
873 g_date_set_time (GDate *d,
879 g_return_if_fail (d != NULL);
881 #ifdef HAVE_LOCALTIME_R
882 localtime_r (&t, &tm);
885 struct tm *ptm = localtime (&t);
887 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
893 d->month = tm.tm_mon + 1;
895 d->year = tm.tm_year + 1900;
897 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
903 g_date_set_month (GDate *d,
906 g_return_if_fail (d != NULL);
907 g_return_if_fail (g_date_valid_month (m));
909 if (d->julian && !d->dmy) g_date_update_dmy(d);
914 if (g_date_valid_dmy (d->day, d->month, d->year))
921 g_date_set_day (GDate *d,
924 g_return_if_fail (d != NULL);
925 g_return_if_fail (g_date_valid_day (day));
927 if (d->julian && !d->dmy) g_date_update_dmy(d);
932 if (g_date_valid_dmy (d->day, d->month, d->year))
939 g_date_set_year (GDate *d,
942 g_return_if_fail (d != NULL);
943 g_return_if_fail (g_date_valid_year (y));
945 if (d->julian && !d->dmy) g_date_update_dmy(d);
950 if (g_date_valid_dmy (d->day, d->month, d->year))
957 g_date_set_dmy (GDate *d,
962 g_return_if_fail (d != NULL);
963 g_return_if_fail (g_date_valid_dmy (day, m, y));
975 g_date_set_julian (GDate *d, guint32 j)
977 g_return_if_fail (d != NULL);
978 g_return_if_fail (g_date_valid_julian (j));
987 g_date_is_first_of_month (const GDate *d)
989 g_return_val_if_fail (d != NULL, FALSE);
990 g_return_val_if_fail (g_date_valid (d), FALSE);
994 g_date_update_dmy (d);
996 g_return_val_if_fail (d->dmy, FALSE);
998 if (d->day == 1) return TRUE;
1003 g_date_is_last_of_month (const GDate *d)
1007 g_return_val_if_fail (d != NULL, FALSE);
1008 g_return_val_if_fail (g_date_valid (d), FALSE);
1012 g_date_update_dmy (d);
1014 g_return_val_if_fail (d->dmy, FALSE);
1016 index = g_date_is_leap_year (d->year) ? 1 : 0;
1018 if (d->day == days_in_months[index][d->month]) return TRUE;
1023 g_date_add_days (GDate *d, guint ndays)
1025 g_return_if_fail (d != NULL);
1026 g_return_if_fail (g_date_valid (d));
1030 g_date_update_julian (d);
1032 g_return_if_fail (d->julian);
1034 d->julian_days += ndays;
1039 g_date_subtract_days (GDate *d, guint ndays)
1041 g_return_if_fail (d != NULL);
1042 g_return_if_fail (g_date_valid (d));
1046 g_date_update_julian (d);
1048 g_return_if_fail (d->julian);
1049 g_return_if_fail (d->julian_days > ndays);
1051 d->julian_days -= ndays;
1056 g_date_add_months (GDate *d,
1059 guint years, months;
1062 g_return_if_fail (d != NULL);
1063 g_return_if_fail (g_date_valid (d));
1067 g_date_update_dmy (d);
1069 g_return_if_fail (d->dmy);
1071 nmonths += d->month - 1;
1074 months = nmonths%12;
1076 d->month = months + 1;
1079 index = g_date_is_leap_year (d->year) ? 1 : 0;
1081 if (d->day > days_in_months[index][d->month])
1082 d->day = days_in_months[index][d->month];
1086 g_return_if_fail (g_date_valid (d));
1090 g_date_subtract_months (GDate *d,
1093 guint years, months;
1096 g_return_if_fail (d != NULL);
1097 g_return_if_fail (g_date_valid (d));
1101 g_date_update_dmy (d);
1103 g_return_if_fail (d->dmy);
1106 months = nmonths%12;
1108 g_return_if_fail (d->year > years);
1112 if (d->month > months) d->month -= months;
1116 d->month = 12 - months;
1120 index = g_date_is_leap_year (d->year) ? 1 : 0;
1122 if (d->day > days_in_months[index][d->month])
1123 d->day = days_in_months[index][d->month];
1127 g_return_if_fail (g_date_valid (d));
1131 g_date_add_years (GDate *d,
1134 g_return_if_fail (d != NULL);
1135 g_return_if_fail (g_date_valid (d));
1139 g_date_update_dmy (d);
1141 g_return_if_fail (d->dmy);
1145 if (d->month == 2 && d->day == 29)
1147 if (!g_date_is_leap_year (d->year))
1157 g_date_subtract_years (GDate *d,
1160 g_return_if_fail (d != NULL);
1161 g_return_if_fail (g_date_valid (d));
1165 g_date_update_dmy (d);
1167 g_return_if_fail (d->dmy);
1168 g_return_if_fail (d->year > nyears);
1172 if (d->month == 2 && d->day == 29)
1174 if (!g_date_is_leap_year (d->year))
1185 g_date_is_leap_year (GDateYear year)
1187 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1189 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1190 (year % 400) == 0 );
1194 g_date_get_days_in_month (GDateMonth month,
1199 g_return_val_if_fail (g_date_valid_year (year), 0);
1200 g_return_val_if_fail (g_date_valid_month (month), 0);
1202 index = g_date_is_leap_year (year) ? 1 : 0;
1204 return days_in_months[index][month];
1208 g_date_get_monday_weeks_in_year (GDateYear year)
1212 g_return_val_if_fail (g_date_valid_year (year), 0);
1214 g_date_clear (&d, 1);
1215 g_date_set_dmy (&d, 1, 1, year);
1216 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1217 g_date_set_dmy (&d, 31, 12, year);
1218 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1219 if (g_date_is_leap_year (year))
1221 g_date_set_dmy (&d, 2, 1, year);
1222 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1223 g_date_set_dmy (&d, 30, 12, year);
1224 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1230 g_date_get_sunday_weeks_in_year (GDateYear year)
1234 g_return_val_if_fail (g_date_valid_year (year), 0);
1236 g_date_clear (&d, 1);
1237 g_date_set_dmy (&d, 1, 1, year);
1238 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1239 g_date_set_dmy (&d, 31, 12, year);
1240 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1241 if (g_date_is_leap_year (year))
1243 g_date_set_dmy (&d, 2, 1, year);
1244 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1245 g_date_set_dmy (&d, 30, 12, year);
1246 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1252 g_date_compare (const GDate *lhs,
1255 g_return_val_if_fail (lhs != NULL, 0);
1256 g_return_val_if_fail (rhs != NULL, 0);
1257 g_return_val_if_fail (g_date_valid (lhs), 0);
1258 g_return_val_if_fail (g_date_valid (rhs), 0);
1260 /* Remember the self-comparison case! I think it works right now. */
1265 if (lhs->julian && rhs->julian)
1267 if (lhs->julian_days < rhs->julian_days) return -1;
1268 else if (lhs->julian_days > rhs->julian_days) return 1;
1271 else if (lhs->dmy && rhs->dmy)
1273 if (lhs->year < rhs->year) return -1;
1274 else if (lhs->year > rhs->year) return 1;
1277 if (lhs->month < rhs->month) return -1;
1278 else if (lhs->month > rhs->month) return 1;
1281 if (lhs->day < rhs->day) return -1;
1282 else if (lhs->day > rhs->day) return 1;
1291 if (!lhs->julian) g_date_update_julian (lhs);
1292 if (!rhs->julian) g_date_update_julian (rhs);
1293 g_return_val_if_fail (lhs->julian, 0);
1294 g_return_val_if_fail (rhs->julian, 0);
1298 return 0; /* warnings */
1303 g_date_to_struct_tm (const GDate *d,
1308 g_return_if_fail (d != NULL);
1309 g_return_if_fail (g_date_valid (d));
1310 g_return_if_fail (tm != NULL);
1314 g_date_update_dmy (d);
1316 g_return_if_fail (d->dmy);
1318 /* zero all the irrelevant fields to be sure they're valid */
1320 /* On Linux and maybe other systems, there are weird non-POSIX
1321 * fields on the end of struct tm that choke strftime if they
1322 * contain garbage. So we need to 0 the entire struct, not just the
1323 * fields we know to exist.
1326 memset (tm, 0x0, sizeof (struct tm));
1328 tm->tm_mday = d->day;
1329 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1330 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1332 day = g_date_get_weekday (d);
1333 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1335 tm->tm_wday = (int)day;
1337 tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
1338 tm->tm_isdst = -1; /* -1 means "information not available" */
1342 g_date_clamp (GDate *date,
1343 const GDate *min_date,
1344 const GDate *max_date)
1346 g_return_if_fail (date);
1347 g_return_if_fail (g_date_valid (date));
1348 if (min_date != NULL)
1349 g_return_if_fail (g_date_valid (min_date));
1350 if (max_date != NULL)
1351 g_return_if_fail (g_date_valid (max_date));
1352 if (min_date != NULL && max_date != NULL)
1353 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
1355 if (min_date && g_date_compare (date, min_date) < 0)
1358 if (max_date && g_date_compare (max_date, date) < 0)
1363 g_date_order (GDate *date1,
1366 g_return_if_fail (date1 != NULL);
1367 g_return_if_fail (date2 != NULL);
1368 g_return_if_fail (g_date_valid (date1));
1369 g_return_if_fail (g_date_valid (date2));
1371 if (g_date_compare (date1, date2) > 0)
1380 g_date_strftime (gchar *s,
1382 const gchar *format,
1386 gsize locale_format_len = 0;
1387 gchar *locale_format;
1393 GError *error = NULL;
1396 g_return_val_if_fail (d != NULL, 0);
1397 g_return_val_if_fail (g_date_valid (d), 0);
1398 g_return_val_if_fail (slen > 0, 0);
1399 g_return_val_if_fail (format != 0, 0);
1400 g_return_val_if_fail (s != 0, 0);
1402 g_date_to_struct_tm (d, &tm);
1404 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
1408 g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
1409 g_error_free (error);
1415 tmpbufsize = MAX (128, locale_format_len * 2);
1418 tmpbuf = g_malloc (tmpbufsize);
1420 /* Set the first byte to something other than '\0', to be able to
1421 * recognize whether strftime actually failed or just returned "".
1424 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
1426 if (tmplen == 0 && tmpbuf[0] != '\0')
1431 if (tmpbufsize > 65536)
1433 g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
1434 g_free (locale_format);
1443 g_free (locale_format);
1445 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
1450 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
1451 g_error_free (error);
1457 if (slen <= convlen)
1459 /* Ensure only whole characters are copied into the buffer.
1461 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1462 g_assert (end != NULL);
1463 convlen = end - convbuf;
1465 /* Return 0 because the buffer isn't large enough.
1472 memcpy (s, convbuf, convlen);