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/.
31 #define DEBUG_MSG(x) /* */
33 /* #define DEBUG_MSG(args) g_message args ; */
50 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
56 g_date_new_dmy (GDateDay day, GDateMonth m, GDateYear y)
59 g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
70 g_assert (g_date_valid (d));
76 g_date_new_julian (guint32 j)
79 g_return_val_if_fail (g_date_valid_julian (j), NULL);
88 g_assert (g_date_valid (d));
94 g_date_free (GDate *d)
96 g_return_if_fail (d != NULL);
102 g_date_valid (const GDate *d)
104 g_return_val_if_fail (d != NULL, FALSE);
106 return (d->julian || d->dmy);
109 static const guint8 days_in_months[2][13] =
110 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
111 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
112 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
115 static const guint16 days_in_year[2][14] =
116 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
117 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
118 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
122 g_date_valid_month (GDateMonth m)
124 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
128 g_date_valid_year (GDateYear y)
130 return ( y > G_DATE_BAD_YEAR );
134 g_date_valid_day (GDateDay d)
136 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
140 g_date_valid_weekday (GDateWeekday w)
142 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
146 g_date_valid_julian (guint32 j)
148 return (j > G_DATE_BAD_JULIAN);
152 g_date_valid_dmy (GDateDay d,
156 return ( (m > G_DATE_BAD_MONTH) &&
158 (d > G_DATE_BAD_DAY) &&
159 (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
160 (d <= (g_date_is_leap_year (y) ?
161 days_in_months[1][m] : days_in_months[0][m])) );
165 /* "Julian days" just means an absolute number of days, where Day 1 ==
169 g_date_update_julian (const GDate *const_d)
171 GDate *d = (GDate *) const_d;
175 g_return_if_fail (d != NULL);
176 g_return_if_fail (d->dmy);
177 g_return_if_fail (!d->julian);
178 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
180 /* What we actually do is: multiply years * 365 days in the year,
181 * add the number of years divided by 4, subtract the number of
182 * years divided by 100 and add the number of years divided by 400,
183 * which accounts for leap year stuff. Code from Steffen Beyer's
187 year = d->year - 1; /* we know d->year > 0 since it's valid */
189 d->julian_days = year * 365U;
190 d->julian_days += (year >>= 2); /* divide by 4 and add */
191 d->julian_days -= (year /= 25); /* divides original # years by 100 */
192 d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
194 index = g_date_is_leap_year (d->year) ? 1 : 0;
196 d->julian_days += days_in_year[index][d->month] + d->day;
198 g_return_if_fail (g_date_valid_julian (d->julian_days));
204 g_date_update_dmy (const GDate *const_d)
206 GDate *d = (GDate *) const_d;
211 guint32 A, B, C, D, E, M;
213 g_return_if_fail (d != NULL);
214 g_return_if_fail (d->julian);
215 g_return_if_fail (!d->dmy);
216 g_return_if_fail (g_date_valid_julian (d->julian_days));
218 /* Formula taken from the Calendar FAQ; the formula was for the
219 * Julian Period which starts on 1 January 4713 BC, so we add
220 * 1,721,425 to the number of days before doing the formula.
222 * I'm sure this can be simplified for our 1 January 1 AD period
223 * start, but I can't figure out how to unpack the formula.
226 A = d->julian_days + 1721425 + 32045;
227 B = ( 4 *(A + 36524) )/ 146097 - 1;
228 C = A - (146097 * B)/4;
229 D = ( 4 * (C + 365) ) / 1461 - 1;
230 E = C - ((1461*D) / 4);
231 M = (5 * (E - 1) + 2)/153;
233 m = M + 3 - (12*(M/10));
234 day = E - (153*M + 2)/5;
235 y = 100 * B + D - 4800 + (M/10);
237 #ifdef G_ENABLE_DEBUG
238 if (!g_date_valid_dmy (day, m, y))
240 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
241 d->julian_days, day, m, y);
253 g_date_get_weekday (const GDate *d)
255 g_return_val_if_fail (d != NULL, G_DATE_BAD_WEEKDAY);
256 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
260 g_date_update_julian (d);
262 g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
264 return ((d->julian_days - 1) % 7) + 1;
268 g_date_get_month (const GDate *d)
270 g_return_val_if_fail (d != NULL, G_DATE_BAD_MONTH);
271 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
275 g_date_update_dmy (d);
277 g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
283 g_date_get_year (const GDate *d)
285 g_return_val_if_fail (d != NULL, G_DATE_BAD_YEAR);
286 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
290 g_date_update_dmy (d);
292 g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
298 g_date_get_day (const GDate *d)
300 g_return_val_if_fail (d != NULL, G_DATE_BAD_DAY);
301 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
305 g_date_update_dmy (d);
307 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
313 g_date_get_julian (const GDate *d)
315 g_return_val_if_fail (d != NULL, G_DATE_BAD_JULIAN);
316 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
320 g_date_update_julian (d);
322 g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
324 return d->julian_days;
328 g_date_get_day_of_year (const GDate *d)
332 g_return_val_if_fail (d != NULL, 0);
333 g_return_val_if_fail (g_date_valid (d), 0);
337 g_date_update_dmy (d);
339 g_return_val_if_fail (d->dmy, 0);
341 index = g_date_is_leap_year (d->year) ? 1 : 0;
343 return (days_in_year[index][d->month] + d->day);
347 g_date_get_monday_week_of_year (const GDate *d)
353 g_return_val_if_fail (d != NULL, 0);
354 g_return_val_if_fail (g_date_valid (d), 0);
358 g_date_update_dmy (d);
360 g_return_val_if_fail (d->dmy, 0);
362 g_date_clear (&first, 1);
364 g_date_set_dmy (&first, 1, 1, d->year);
366 wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
367 day = g_date_get_day_of_year (d) - 1;
369 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
373 g_date_get_sunday_week_of_year (const GDate *d)
379 g_return_val_if_fail (d != NULL, 0);
380 g_return_val_if_fail (g_date_valid (d), 0);
384 g_date_update_dmy (d);
386 g_return_val_if_fail (d->dmy, 0);
388 g_date_clear (&first, 1);
390 g_date_set_dmy (&first, 1, 1, d->year);
392 wd = g_date_get_weekday (&first);
393 if (wd == 7) wd = 0; /* make Sunday day 0 */
394 day = g_date_get_day_of_year (d) - 1;
396 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
400 g_date_days_between (const GDate *d1,
403 g_return_val_if_fail (d1 != NULL, 0);
404 g_return_val_if_fail (d2 != NULL, 0);
406 g_return_val_if_fail (g_date_valid (d1), 0);
407 g_return_val_if_fail (g_date_valid (d2), 0);
409 return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
413 g_date_clear (GDate *d, guint ndates)
415 g_return_if_fail (d != NULL);
416 g_return_if_fail (ndates != 0);
418 memset (d, 0x0, ndates*sizeof (GDate));
421 G_LOCK_DEFINE_STATIC (g_date_global);
423 /* These are for the parser, output to the user should use *
424 * g_date_strftime () - this creates more never-freed memory to annoy
425 * all those memory debugger users. :-)
428 static gchar *long_month_names[13] =
430 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
433 static gchar *short_month_names[13] =
435 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
438 /* This tells us if we need to update the parse info */
439 static gchar *current_locale = NULL;
441 /* order of these in the current locale */
442 static GDateDMY dmy_order[3] =
444 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
447 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
448 * 29 and below are counted as in the year 2000, numbers 30 and above
449 * are counted as in the year 1900.
452 static GDateYear twodigit_start_year = 1930;
454 /* It is impossible to enter a year between 1 AD and 99 AD with this
457 static gboolean using_twodigit_years = FALSE;
459 struct _GDateParseTokens {
465 typedef struct _GDateParseTokens GDateParseTokens;
469 /* HOLDS: g_date_global_lock */
471 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
473 gchar num[4][NUM_LEN+1];
477 /* We count 4, but store 3; so we can give an error
480 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
482 s = (const guchar *) str;
484 while (*s && pt->num_ints < 4)
488 while (*s && g_ascii_isdigit (*s) && i <= NUM_LEN)
490 num[pt->num_ints][i] = *s;
497 num[pt->num_ints][i] = '\0';
501 if (*s == '\0') break;
506 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
507 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
508 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
510 pt->month = G_DATE_BAD_MONTH;
512 if (pt->num_ints < 3)
517 casefold = g_utf8_casefold (str, -1);
518 normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
524 if (long_month_names[i] != NULL)
526 const gchar *found = strstr (normalized, long_month_names[i]);
535 if (short_month_names[i] != NULL)
537 const gchar *found = strstr (normalized, short_month_names[i]);
551 /* HOLDS: g_date_global_lock */
553 g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
555 const gchar *locale = setlocale (LC_TIME, NULL);
556 gboolean recompute_localeinfo = FALSE;
559 g_return_if_fail (locale != NULL); /* should not happen */
561 g_date_clear (&d, 1); /* clear for scratch use */
563 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
565 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
568 if (recompute_localeinfo)
571 GDateParseTokens testpt;
574 g_free (current_locale); /* still works if current_locale == NULL */
576 current_locale = g_strdup (locale);
582 g_date_set_dmy (&d, 1, i, 1);
584 g_return_if_fail (g_date_valid (&d));
586 g_date_strftime (buf, 127, "%b", &d);
588 casefold = g_utf8_casefold (buf, -1);
589 g_free (short_month_names[i]);
590 short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
593 g_date_strftime (buf, 127, "%B", &d);
594 casefold = g_utf8_casefold (buf, -1);
595 g_free (long_month_names[i]);
596 long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
602 /* Determine DMY order */
604 /* had to pick a random day - don't change this, some strftimes
605 * are broken on some days, and this one is good so far. */
606 g_date_set_dmy (&d, 4, 7, 1976);
608 g_date_strftime (buf, 127, "%x", &d);
610 g_date_fill_parse_tokens (buf, &testpt);
613 while (i < testpt.num_ints)
618 dmy_order[i] = G_DATE_MONTH;
621 dmy_order[i] = G_DATE_DAY;
624 using_twodigit_years = TRUE; /* FALL THRU */
626 dmy_order[i] = G_DATE_YEAR;
629 /* leave it unchanged */
635 #ifdef G_ENABLE_DEBUG
636 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
640 DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
643 if (using_twodigit_years)
644 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
650 switch (dmy_order[i])
653 strings[i] = "Month";
667 DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
668 DEBUG_MSG (("**Sample date in this locale: `%s'", buf));
673 g_date_fill_parse_tokens (str, pt);
677 g_date_set_parse (GDate *d,
681 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
683 g_return_if_fail (d != NULL);
688 G_LOCK (g_date_global);
690 g_date_prepare_to_parse (str, &pt);
692 DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d",
693 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
696 if (pt.num_ints == 4)
698 G_UNLOCK (g_date_global);
699 return; /* presumably a typo; bail out. */
707 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
709 while (i < pt.num_ints && j < 3)
711 switch (dmy_order[j])
715 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
718 ++j; /* skip months, but don't skip this number */
727 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
730 ++j; /* skip days, since we may have month/year */
740 if (using_twodigit_years && y < 100)
742 guint two = twodigit_start_year % 100;
743 guint century = (twodigit_start_year / 100) * 100;
761 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
768 if (using_twodigit_years && y < 100)
769 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
771 else if (pt.num_ints == 2)
773 if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
779 else if (pt.num_ints == 1)
781 if (pt.month != G_DATE_BAD_MONTH)
783 /* Month name and year? */
790 /* Try yyyymmdd and yymmdd */
792 m = (pt.n[0]/100) % 100;
796 /* FIXME move this into a separate function */
797 if (using_twodigit_years && y < 100)
799 guint two = twodigit_start_year % 100;
800 guint century = (twodigit_start_year / 100) * 100;
810 /* See if we got anything valid out of all this. */
811 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
812 if (y < 8000 && g_date_valid_dmy (day, m, y))
819 #ifdef G_ENABLE_DEBUG
821 DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
823 G_UNLOCK (g_date_global);
827 g_date_set_time (GDate *d,
833 g_return_if_fail (d != NULL);
835 #ifdef HAVE_LOCALTIME_R
836 localtime_r (&t, &tm);
839 struct tm *ptm = localtime (&t);
841 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
847 d->month = tm.tm_mon + 1;
849 d->year = tm.tm_year + 1900;
851 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
857 g_date_set_month (GDate *d,
860 g_return_if_fail (d != NULL);
861 g_return_if_fail (g_date_valid_month (m));
863 if (d->julian && !d->dmy) g_date_update_dmy(d);
868 if (g_date_valid_dmy (d->day, d->month, d->year))
875 g_date_set_day (GDate *d,
878 g_return_if_fail (d != NULL);
879 g_return_if_fail (g_date_valid_day (day));
881 if (d->julian && !d->dmy) g_date_update_dmy(d);
886 if (g_date_valid_dmy (d->day, d->month, d->year))
893 g_date_set_year (GDate *d,
896 g_return_if_fail (d != NULL);
897 g_return_if_fail (g_date_valid_year (y));
899 if (d->julian && !d->dmy) g_date_update_dmy(d);
904 if (g_date_valid_dmy (d->day, d->month, d->year))
911 g_date_set_dmy (GDate *d,
916 g_return_if_fail (d != NULL);
917 g_return_if_fail (g_date_valid_dmy (day, m, y));
929 g_date_set_julian (GDate *d, guint32 j)
931 g_return_if_fail (d != NULL);
932 g_return_if_fail (g_date_valid_julian (j));
941 g_date_is_first_of_month (const GDate *d)
943 g_return_val_if_fail (d != NULL, FALSE);
944 g_return_val_if_fail (g_date_valid (d), FALSE);
948 g_date_update_dmy (d);
950 g_return_val_if_fail (d->dmy, FALSE);
952 if (d->day == 1) return TRUE;
957 g_date_is_last_of_month (const GDate *d)
961 g_return_val_if_fail (d != NULL, FALSE);
962 g_return_val_if_fail (g_date_valid (d), FALSE);
966 g_date_update_dmy (d);
968 g_return_val_if_fail (d->dmy, FALSE);
970 index = g_date_is_leap_year (d->year) ? 1 : 0;
972 if (d->day == days_in_months[index][d->month]) return TRUE;
977 g_date_add_days (GDate *d, guint ndays)
979 g_return_if_fail (d != NULL);
980 g_return_if_fail (g_date_valid (d));
984 g_date_update_julian (d);
986 g_return_if_fail (d->julian);
988 d->julian_days += ndays;
993 g_date_subtract_days (GDate *d, guint ndays)
995 g_return_if_fail (d != NULL);
996 g_return_if_fail (g_date_valid (d));
1000 g_date_update_julian (d);
1002 g_return_if_fail (d->julian);
1003 g_return_if_fail (d->julian_days > ndays);
1005 d->julian_days -= ndays;
1010 g_date_add_months (GDate *d,
1013 guint years, months;
1016 g_return_if_fail (d != NULL);
1017 g_return_if_fail (g_date_valid (d));
1021 g_date_update_dmy (d);
1023 g_return_if_fail (d->dmy);
1025 nmonths += d->month - 1;
1028 months = nmonths%12;
1030 d->month = months + 1;
1033 index = g_date_is_leap_year (d->year) ? 1 : 0;
1035 if (d->day > days_in_months[index][d->month])
1036 d->day = days_in_months[index][d->month];
1040 g_return_if_fail (g_date_valid (d));
1044 g_date_subtract_months (GDate *d,
1047 guint years, months;
1050 g_return_if_fail (d != NULL);
1051 g_return_if_fail (g_date_valid (d));
1055 g_date_update_dmy (d);
1057 g_return_if_fail (d->dmy);
1060 months = nmonths%12;
1062 g_return_if_fail (d->year > years);
1066 if (d->month > months) d->month -= months;
1070 d->month = 12 - months;
1074 index = g_date_is_leap_year (d->year) ? 1 : 0;
1076 if (d->day > days_in_months[index][d->month])
1077 d->day = days_in_months[index][d->month];
1081 g_return_if_fail (g_date_valid (d));
1085 g_date_add_years (GDate *d,
1088 g_return_if_fail (d != NULL);
1089 g_return_if_fail (g_date_valid (d));
1093 g_date_update_dmy (d);
1095 g_return_if_fail (d->dmy);
1099 if (d->month == 2 && d->day == 29)
1101 if (!g_date_is_leap_year (d->year))
1111 g_date_subtract_years (GDate *d,
1114 g_return_if_fail (d != NULL);
1115 g_return_if_fail (g_date_valid (d));
1119 g_date_update_dmy (d);
1121 g_return_if_fail (d->dmy);
1122 g_return_if_fail (d->year > nyears);
1126 if (d->month == 2 && d->day == 29)
1128 if (!g_date_is_leap_year (d->year))
1139 g_date_is_leap_year (GDateYear year)
1141 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1143 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1144 (year % 400) == 0 );
1148 g_date_get_days_in_month (GDateMonth month,
1153 g_return_val_if_fail (g_date_valid_year (year), 0);
1154 g_return_val_if_fail (g_date_valid_month (month), 0);
1156 index = g_date_is_leap_year (year) ? 1 : 0;
1158 return days_in_months[index][month];
1162 g_date_get_monday_weeks_in_year (GDateYear year)
1166 g_return_val_if_fail (g_date_valid_year (year), 0);
1168 g_date_clear (&d, 1);
1169 g_date_set_dmy (&d, 1, 1, year);
1170 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1171 g_date_set_dmy (&d, 31, 12, year);
1172 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1173 if (g_date_is_leap_year (year))
1175 g_date_set_dmy (&d, 2, 1, year);
1176 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1177 g_date_set_dmy (&d, 30, 12, year);
1178 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1184 g_date_get_sunday_weeks_in_year (GDateYear year)
1188 g_return_val_if_fail (g_date_valid_year (year), 0);
1190 g_date_clear (&d, 1);
1191 g_date_set_dmy (&d, 1, 1, year);
1192 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1193 g_date_set_dmy (&d, 31, 12, year);
1194 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1195 if (g_date_is_leap_year (year))
1197 g_date_set_dmy (&d, 2, 1, year);
1198 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1199 g_date_set_dmy (&d, 30, 12, year);
1200 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1206 g_date_compare (const GDate *lhs,
1209 g_return_val_if_fail (lhs != NULL, 0);
1210 g_return_val_if_fail (rhs != NULL, 0);
1211 g_return_val_if_fail (g_date_valid (lhs), 0);
1212 g_return_val_if_fail (g_date_valid (rhs), 0);
1214 /* Remember the self-comparison case! I think it works right now. */
1219 if (lhs->julian && rhs->julian)
1221 if (lhs->julian_days < rhs->julian_days) return -1;
1222 else if (lhs->julian_days > rhs->julian_days) return 1;
1225 else if (lhs->dmy && rhs->dmy)
1227 if (lhs->year < rhs->year) return -1;
1228 else if (lhs->year > rhs->year) return 1;
1231 if (lhs->month < rhs->month) return -1;
1232 else if (lhs->month > rhs->month) return 1;
1235 if (lhs->day < rhs->day) return -1;
1236 else if (lhs->day > rhs->day) return 1;
1245 if (!lhs->julian) g_date_update_julian (lhs);
1246 if (!rhs->julian) g_date_update_julian (rhs);
1247 g_return_val_if_fail (lhs->julian, 0);
1248 g_return_val_if_fail (rhs->julian, 0);
1252 return 0; /* warnings */
1257 g_date_to_struct_tm (const GDate *d,
1262 g_return_if_fail (d != NULL);
1263 g_return_if_fail (g_date_valid (d));
1264 g_return_if_fail (tm != NULL);
1268 g_date_update_dmy (d);
1270 g_return_if_fail (d->dmy);
1272 /* zero all the irrelevant fields to be sure they're valid */
1274 /* On Linux and maybe other systems, there are weird non-POSIX
1275 * fields on the end of struct tm that choke strftime if they
1276 * contain garbage. So we need to 0 the entire struct, not just the
1277 * fields we know to exist.
1280 memset (tm, 0x0, sizeof (struct tm));
1282 tm->tm_mday = d->day;
1283 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1284 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1286 day = g_date_get_weekday (d);
1287 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1289 tm->tm_wday = (int)day;
1291 tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
1292 tm->tm_isdst = -1; /* -1 means "information not available" */
1296 g_date_clamp (GDate *date,
1297 const GDate *min_date,
1298 const GDate *max_date)
1300 g_return_if_fail (date);
1301 g_return_if_fail (g_date_valid (date));
1302 if (min_date != NULL)
1303 g_return_if_fail (g_date_valid (min_date));
1304 if (max_date != NULL)
1305 g_return_if_fail (g_date_valid (max_date));
1306 if (min_date != NULL && max_date != NULL)
1307 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
1309 if (min_date && g_date_compare (date, min_date) < 0)
1312 if (max_date && g_date_compare (max_date, date) < 0)
1317 g_date_order (GDate *date1,
1320 g_return_if_fail (date1 != NULL);
1321 g_return_if_fail (date2 != NULL);
1322 g_return_if_fail (g_date_valid (date1));
1323 g_return_if_fail (g_date_valid (date2));
1325 if (g_date_compare (date1, date2) > 0)
1334 g_date_strftime (gchar *s,
1336 const gchar *format,
1340 gsize locale_format_len = 0;
1341 gchar *locale_format;
1347 GError *error = NULL;
1350 g_return_val_if_fail (d != NULL, 0);
1351 g_return_val_if_fail (g_date_valid (d), 0);
1352 g_return_val_if_fail (slen > 0, 0);
1353 g_return_val_if_fail (format != 0, 0);
1354 g_return_val_if_fail (s != 0, 0);
1356 g_date_to_struct_tm (d, &tm);
1358 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
1362 g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
1363 g_error_free (error);
1369 tmpbufsize = MAX (128, locale_format_len * 2);
1372 tmpbuf = g_malloc (tmpbufsize);
1374 /* Set the first byte to something other than '\0', to be able to
1375 * recognize whether strftime actually failed or just returned "".
1378 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
1380 if (tmplen == 0 && tmpbuf[0] != '\0')
1385 if (tmpbufsize > 65536)
1387 g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
1388 g_free (locale_format);
1397 g_free (locale_format);
1399 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
1404 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
1405 g_error_free (error);
1411 if (slen <= convlen)
1413 /* Ensure only whole characters are copied into the buffer.
1415 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1416 g_assert (end != NULL);
1417 convlen = end - convbuf;
1419 /* Return 0 because the buffer isn't large enough.
1426 memcpy (s, convbuf, convlen);