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_days_between (const GDate *d1,
401 g_return_val_if_fail (d1 != NULL, 0);
402 g_return_val_if_fail (d2 != NULL, 0);
404 g_return_val_if_fail (g_date_valid (d1), 0);
405 g_return_val_if_fail (g_date_valid (d2), 0);
407 return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
411 g_date_clear (GDate *d, guint ndates)
413 g_return_if_fail (d != NULL);
414 g_return_if_fail (ndates != 0);
416 memset (d, 0x0, ndates*sizeof (GDate));
419 G_LOCK_DEFINE_STATIC (g_date_global);
421 /* These are for the parser, output to the user should use *
422 * g_date_strftime () - this creates more never-freed memory to annoy
423 * all those memory debugger users. :-)
426 static gchar *long_month_names[13] =
428 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
431 static gchar *short_month_names[13] =
433 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
436 /* This tells us if we need to update the parse info */
437 static gchar *current_locale = NULL;
439 /* order of these in the current locale */
440 static GDateDMY dmy_order[3] =
442 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
445 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
446 * 29 and below are counted as in the year 2000, numbers 30 and above
447 * are counted as in the year 1900.
450 static GDateYear twodigit_start_year = 1930;
452 /* It is impossible to enter a year between 1 AD and 99 AD with this
455 static gboolean using_twodigit_years = FALSE;
457 /* Adjustment of locale era to AD, non-zero means using locale era
459 static gint locale_era_adjust = 0;
461 struct _GDateParseTokens {
467 typedef struct _GDateParseTokens GDateParseTokens;
471 /* HOLDS: g_date_global_lock */
473 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
475 gchar num[4][NUM_LEN+1];
479 /* We count 4, but store 3; so we can give an error
482 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
484 s = (const guchar *) str;
486 while (*s && pt->num_ints < 4)
490 while (*s && g_ascii_isdigit (*s) && i <= NUM_LEN)
492 num[pt->num_ints][i] = *s;
499 num[pt->num_ints][i] = '\0';
503 if (*s == '\0') break;
508 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
509 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
510 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
512 pt->month = G_DATE_BAD_MONTH;
514 if (pt->num_ints < 3)
519 casefold = g_utf8_casefold (str, -1);
520 normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
526 if (long_month_names[i] != NULL)
528 const gchar *found = strstr (normalized, long_month_names[i]);
537 if (short_month_names[i] != NULL)
539 const gchar *found = strstr (normalized, short_month_names[i]);
555 /* HOLDS: g_date_global_lock */
557 g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
559 const gchar *locale = setlocale (LC_TIME, NULL);
560 gboolean recompute_localeinfo = FALSE;
563 g_return_if_fail (locale != NULL); /* should not happen */
565 g_date_clear (&d, 1); /* clear for scratch use */
567 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
569 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
572 if (recompute_localeinfo)
575 GDateParseTokens testpt;
578 g_free (current_locale); /* still works if current_locale == NULL */
580 current_locale = g_strdup (locale);
586 g_date_set_dmy (&d, 1, i, 1);
588 g_return_if_fail (g_date_valid (&d));
590 g_date_strftime (buf, 127, "%b", &d);
592 casefold = g_utf8_casefold (buf, -1);
593 g_free (short_month_names[i]);
594 short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
597 g_date_strftime (buf, 127, "%B", &d);
598 casefold = g_utf8_casefold (buf, -1);
599 g_free (long_month_names[i]);
600 long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
606 /* Determine DMY order */
608 /* had to pick a random day - don't change this, some strftimes
609 * are broken on some days, and this one is good so far. */
610 g_date_set_dmy (&d, 4, 7, 1976);
612 g_date_strftime (buf, 127, "%x", &d);
614 g_date_fill_parse_tokens (buf, &testpt);
617 while (i < testpt.num_ints)
622 dmy_order[i] = G_DATE_MONTH;
625 dmy_order[i] = G_DATE_DAY;
628 using_twodigit_years = TRUE; /* FALL THRU */
630 dmy_order[i] = G_DATE_YEAR;
633 /* assume locale era */
634 locale_era_adjust = 1976 - testpt.n[i];
635 dmy_order[i] = G_DATE_YEAR;
641 #ifdef G_ENABLE_DEBUG
642 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
646 DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
649 if (using_twodigit_years)
650 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
656 switch (dmy_order[i])
659 strings[i] = "Month";
673 DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
674 DEBUG_MSG (("**Sample date in this locale: `%s'", buf));
679 g_date_fill_parse_tokens (str, pt);
683 g_date_set_parse (GDate *d,
687 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
689 g_return_if_fail (d != NULL);
694 G_LOCK (g_date_global);
696 g_date_prepare_to_parse (str, &pt);
698 DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d",
699 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
702 if (pt.num_ints == 4)
704 G_UNLOCK (g_date_global);
705 return; /* presumably a typo; bail out. */
713 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
715 while (i < pt.num_ints && j < 3)
717 switch (dmy_order[j])
721 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
724 ++j; /* skip months, but don't skip this number */
733 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
736 ++j; /* skip days, since we may have month/year */
746 if (locale_era_adjust != 0)
748 y += locale_era_adjust;
750 else if (using_twodigit_years && y < 100)
752 guint two = twodigit_start_year % 100;
753 guint century = (twodigit_start_year / 100) * 100;
771 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
778 if (using_twodigit_years && y < 100)
779 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
781 else if (pt.num_ints == 2)
783 if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
789 else if (pt.num_ints == 1)
791 if (pt.month != G_DATE_BAD_MONTH)
793 /* Month name and year? */
800 /* Try yyyymmdd and yymmdd */
802 m = (pt.n[0]/100) % 100;
806 /* FIXME move this into a separate function */
807 if (using_twodigit_years && y < 100)
809 guint two = twodigit_start_year % 100;
810 guint century = (twodigit_start_year / 100) * 100;
820 /* See if we got anything valid out of all this. */
821 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
822 if (y < 8000 && g_date_valid_dmy (day, m, y))
829 #ifdef G_ENABLE_DEBUG
831 DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
833 G_UNLOCK (g_date_global);
837 g_date_set_time (GDate *d,
843 g_return_if_fail (d != NULL);
845 #ifdef HAVE_LOCALTIME_R
846 localtime_r (&t, &tm);
849 struct tm *ptm = localtime (&t);
851 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
857 d->month = tm.tm_mon + 1;
859 d->year = tm.tm_year + 1900;
861 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
867 g_date_set_month (GDate *d,
870 g_return_if_fail (d != NULL);
871 g_return_if_fail (g_date_valid_month (m));
873 if (d->julian && !d->dmy) g_date_update_dmy(d);
878 if (g_date_valid_dmy (d->day, d->month, d->year))
885 g_date_set_day (GDate *d,
888 g_return_if_fail (d != NULL);
889 g_return_if_fail (g_date_valid_day (day));
891 if (d->julian && !d->dmy) g_date_update_dmy(d);
896 if (g_date_valid_dmy (d->day, d->month, d->year))
903 g_date_set_year (GDate *d,
906 g_return_if_fail (d != NULL);
907 g_return_if_fail (g_date_valid_year (y));
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_dmy (GDate *d,
926 g_return_if_fail (d != NULL);
927 g_return_if_fail (g_date_valid_dmy (day, m, y));
939 g_date_set_julian (GDate *d, guint32 j)
941 g_return_if_fail (d != NULL);
942 g_return_if_fail (g_date_valid_julian (j));
951 g_date_is_first_of_month (const GDate *d)
953 g_return_val_if_fail (d != NULL, FALSE);
954 g_return_val_if_fail (g_date_valid (d), FALSE);
958 g_date_update_dmy (d);
960 g_return_val_if_fail (d->dmy, FALSE);
962 if (d->day == 1) return TRUE;
967 g_date_is_last_of_month (const GDate *d)
971 g_return_val_if_fail (d != NULL, FALSE);
972 g_return_val_if_fail (g_date_valid (d), FALSE);
976 g_date_update_dmy (d);
978 g_return_val_if_fail (d->dmy, FALSE);
980 index = g_date_is_leap_year (d->year) ? 1 : 0;
982 if (d->day == days_in_months[index][d->month]) return TRUE;
987 g_date_add_days (GDate *d, guint ndays)
989 g_return_if_fail (d != NULL);
990 g_return_if_fail (g_date_valid (d));
994 g_date_update_julian (d);
996 g_return_if_fail (d->julian);
998 d->julian_days += ndays;
1003 g_date_subtract_days (GDate *d, guint ndays)
1005 g_return_if_fail (d != NULL);
1006 g_return_if_fail (g_date_valid (d));
1010 g_date_update_julian (d);
1012 g_return_if_fail (d->julian);
1013 g_return_if_fail (d->julian_days > ndays);
1015 d->julian_days -= ndays;
1020 g_date_add_months (GDate *d,
1023 guint years, months;
1026 g_return_if_fail (d != NULL);
1027 g_return_if_fail (g_date_valid (d));
1031 g_date_update_dmy (d);
1033 g_return_if_fail (d->dmy);
1035 nmonths += d->month - 1;
1038 months = nmonths%12;
1040 d->month = months + 1;
1043 index = g_date_is_leap_year (d->year) ? 1 : 0;
1045 if (d->day > days_in_months[index][d->month])
1046 d->day = days_in_months[index][d->month];
1050 g_return_if_fail (g_date_valid (d));
1054 g_date_subtract_months (GDate *d,
1057 guint years, months;
1060 g_return_if_fail (d != NULL);
1061 g_return_if_fail (g_date_valid (d));
1065 g_date_update_dmy (d);
1067 g_return_if_fail (d->dmy);
1070 months = nmonths%12;
1072 g_return_if_fail (d->year > years);
1076 if (d->month > months) d->month -= months;
1080 d->month = 12 - months;
1084 index = g_date_is_leap_year (d->year) ? 1 : 0;
1086 if (d->day > days_in_months[index][d->month])
1087 d->day = days_in_months[index][d->month];
1091 g_return_if_fail (g_date_valid (d));
1095 g_date_add_years (GDate *d,
1098 g_return_if_fail (d != NULL);
1099 g_return_if_fail (g_date_valid (d));
1103 g_date_update_dmy (d);
1105 g_return_if_fail (d->dmy);
1109 if (d->month == 2 && d->day == 29)
1111 if (!g_date_is_leap_year (d->year))
1121 g_date_subtract_years (GDate *d,
1124 g_return_if_fail (d != NULL);
1125 g_return_if_fail (g_date_valid (d));
1129 g_date_update_dmy (d);
1131 g_return_if_fail (d->dmy);
1132 g_return_if_fail (d->year > nyears);
1136 if (d->month == 2 && d->day == 29)
1138 if (!g_date_is_leap_year (d->year))
1149 g_date_is_leap_year (GDateYear year)
1151 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1153 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1154 (year % 400) == 0 );
1158 g_date_get_days_in_month (GDateMonth month,
1163 g_return_val_if_fail (g_date_valid_year (year), 0);
1164 g_return_val_if_fail (g_date_valid_month (month), 0);
1166 index = g_date_is_leap_year (year) ? 1 : 0;
1168 return days_in_months[index][month];
1172 g_date_get_monday_weeks_in_year (GDateYear year)
1176 g_return_val_if_fail (g_date_valid_year (year), 0);
1178 g_date_clear (&d, 1);
1179 g_date_set_dmy (&d, 1, 1, year);
1180 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1181 g_date_set_dmy (&d, 31, 12, year);
1182 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1183 if (g_date_is_leap_year (year))
1185 g_date_set_dmy (&d, 2, 1, year);
1186 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1187 g_date_set_dmy (&d, 30, 12, year);
1188 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1194 g_date_get_sunday_weeks_in_year (GDateYear year)
1198 g_return_val_if_fail (g_date_valid_year (year), 0);
1200 g_date_clear (&d, 1);
1201 g_date_set_dmy (&d, 1, 1, year);
1202 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1203 g_date_set_dmy (&d, 31, 12, year);
1204 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1205 if (g_date_is_leap_year (year))
1207 g_date_set_dmy (&d, 2, 1, year);
1208 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1209 g_date_set_dmy (&d, 30, 12, year);
1210 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1216 g_date_compare (const GDate *lhs,
1219 g_return_val_if_fail (lhs != NULL, 0);
1220 g_return_val_if_fail (rhs != NULL, 0);
1221 g_return_val_if_fail (g_date_valid (lhs), 0);
1222 g_return_val_if_fail (g_date_valid (rhs), 0);
1224 /* Remember the self-comparison case! I think it works right now. */
1229 if (lhs->julian && rhs->julian)
1231 if (lhs->julian_days < rhs->julian_days) return -1;
1232 else if (lhs->julian_days > rhs->julian_days) return 1;
1235 else if (lhs->dmy && rhs->dmy)
1237 if (lhs->year < rhs->year) return -1;
1238 else if (lhs->year > rhs->year) return 1;
1241 if (lhs->month < rhs->month) return -1;
1242 else if (lhs->month > rhs->month) return 1;
1245 if (lhs->day < rhs->day) return -1;
1246 else if (lhs->day > rhs->day) return 1;
1255 if (!lhs->julian) g_date_update_julian (lhs);
1256 if (!rhs->julian) g_date_update_julian (rhs);
1257 g_return_val_if_fail (lhs->julian, 0);
1258 g_return_val_if_fail (rhs->julian, 0);
1262 return 0; /* warnings */
1267 g_date_to_struct_tm (const GDate *d,
1272 g_return_if_fail (d != NULL);
1273 g_return_if_fail (g_date_valid (d));
1274 g_return_if_fail (tm != NULL);
1278 g_date_update_dmy (d);
1280 g_return_if_fail (d->dmy);
1282 /* zero all the irrelevant fields to be sure they're valid */
1284 /* On Linux and maybe other systems, there are weird non-POSIX
1285 * fields on the end of struct tm that choke strftime if they
1286 * contain garbage. So we need to 0 the entire struct, not just the
1287 * fields we know to exist.
1290 memset (tm, 0x0, sizeof (struct tm));
1292 tm->tm_mday = d->day;
1293 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1294 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1296 day = g_date_get_weekday (d);
1297 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1299 tm->tm_wday = (int)day;
1301 tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
1302 tm->tm_isdst = -1; /* -1 means "information not available" */
1306 g_date_clamp (GDate *date,
1307 const GDate *min_date,
1308 const GDate *max_date)
1310 g_return_if_fail (date);
1311 g_return_if_fail (g_date_valid (date));
1312 if (min_date != NULL)
1313 g_return_if_fail (g_date_valid (min_date));
1314 if (max_date != NULL)
1315 g_return_if_fail (g_date_valid (max_date));
1316 if (min_date != NULL && max_date != NULL)
1317 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
1319 if (min_date && g_date_compare (date, min_date) < 0)
1322 if (max_date && g_date_compare (max_date, date) < 0)
1327 g_date_order (GDate *date1,
1330 g_return_if_fail (date1 != NULL);
1331 g_return_if_fail (date2 != NULL);
1332 g_return_if_fail (g_date_valid (date1));
1333 g_return_if_fail (g_date_valid (date2));
1335 if (g_date_compare (date1, date2) > 0)
1344 g_date_strftime (gchar *s,
1346 const gchar *format,
1350 gsize locale_format_len = 0;
1351 gchar *locale_format;
1357 GError *error = NULL;
1360 g_return_val_if_fail (d != NULL, 0);
1361 g_return_val_if_fail (g_date_valid (d), 0);
1362 g_return_val_if_fail (slen > 0, 0);
1363 g_return_val_if_fail (format != 0, 0);
1364 g_return_val_if_fail (s != 0, 0);
1366 g_date_to_struct_tm (d, &tm);
1368 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
1372 g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
1373 g_error_free (error);
1379 tmpbufsize = MAX (128, locale_format_len * 2);
1382 tmpbuf = g_malloc (tmpbufsize);
1384 /* Set the first byte to something other than '\0', to be able to
1385 * recognize whether strftime actually failed or just returned "".
1388 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
1390 if (tmplen == 0 && tmpbuf[0] != '\0')
1395 if (tmpbufsize > 65536)
1397 g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
1398 g_free (locale_format);
1407 g_free (locale_format);
1409 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
1414 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
1415 g_error_free (error);
1421 if (slen <= convlen)
1423 /* Ensure only whole characters are copied into the buffer.
1425 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1426 g_assert (end != NULL);
1427 convlen = end - convbuf;
1429 /* Return 0 because the buffer isn't large enough.
1436 memcpy (s, convbuf, convlen);