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 ; */
51 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
57 g_date_new_dmy (GDateDay day, GDateMonth m, GDateYear y)
60 g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
71 g_assert (g_date_valid (d));
77 g_date_new_julian (guint32 j)
80 g_return_val_if_fail (g_date_valid_julian (j), NULL);
89 g_assert (g_date_valid (d));
95 g_date_free (GDate *d)
97 g_return_if_fail (d != NULL);
103 g_date_valid (const GDate *d)
105 g_return_val_if_fail (d != NULL, FALSE);
107 return (d->julian || d->dmy);
110 static const guint8 days_in_months[2][13] =
111 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
112 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
113 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
116 static const guint16 days_in_year[2][14] =
117 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
118 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
119 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
123 g_date_valid_month (GDateMonth m)
125 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
129 g_date_valid_year (GDateYear y)
131 return ( y > G_DATE_BAD_YEAR );
135 g_date_valid_day (GDateDay d)
137 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
141 g_date_valid_weekday (GDateWeekday w)
143 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
147 g_date_valid_julian (guint32 j)
149 return (j > G_DATE_BAD_JULIAN);
153 g_date_valid_dmy (GDateDay d,
157 return ( (m > G_DATE_BAD_MONTH) &&
159 (d > G_DATE_BAD_DAY) &&
160 (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
161 (d <= (g_date_is_leap_year (y) ?
162 days_in_months[1][m] : days_in_months[0][m])) );
166 /* "Julian days" just means an absolute number of days, where Day 1 ==
170 g_date_update_julian (const GDate *const_d)
172 GDate *d = (GDate *) const_d;
176 g_return_if_fail (d != NULL);
177 g_return_if_fail (d->dmy);
178 g_return_if_fail (!d->julian);
179 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
181 /* What we actually do is: multiply years * 365 days in the year,
182 * add the number of years divided by 4, subtract the number of
183 * years divided by 100 and add the number of years divided by 400,
184 * which accounts for leap year stuff. Code from Steffen Beyer's
188 year = d->year - 1; /* we know d->year > 0 since it's valid */
190 d->julian_days = year * 365U;
191 d->julian_days += (year >>= 2); /* divide by 4 and add */
192 d->julian_days -= (year /= 25); /* divides original # years by 100 */
193 d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
195 index = g_date_is_leap_year (d->year) ? 1 : 0;
197 d->julian_days += days_in_year[index][d->month] + d->day;
199 g_return_if_fail (g_date_valid_julian (d->julian_days));
205 g_date_update_dmy (const GDate *const_d)
207 GDate *d = (GDate *) const_d;
212 guint32 A, B, C, D, E, M;
214 g_return_if_fail (d != NULL);
215 g_return_if_fail (d->julian);
216 g_return_if_fail (!d->dmy);
217 g_return_if_fail (g_date_valid_julian (d->julian_days));
219 /* Formula taken from the Calendar FAQ; the formula was for the
220 * Julian Period which starts on 1 January 4713 BC, so we add
221 * 1,721,425 to the number of days before doing the formula.
223 * I'm sure this can be simplified for our 1 January 1 AD period
224 * start, but I can't figure out how to unpack the formula.
227 A = d->julian_days + 1721425 + 32045;
228 B = ( 4 *(A + 36524) )/ 146097 - 1;
229 C = A - (146097 * B)/4;
230 D = ( 4 * (C + 365) ) / 1461 - 1;
231 E = C - ((1461*D) / 4);
232 M = (5 * (E - 1) + 2)/153;
234 m = M + 3 - (12*(M/10));
235 day = E - (153*M + 2)/5;
236 y = 100 * B + D - 4800 + (M/10);
238 #ifdef G_ENABLE_DEBUG
239 if (!g_date_valid_dmy (day, m, y))
241 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
242 d->julian_days, day, m, y);
254 g_date_get_weekday (const GDate *d)
256 g_return_val_if_fail (d != NULL, G_DATE_BAD_WEEKDAY);
257 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 (d != NULL, G_DATE_BAD_MONTH);
272 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
276 g_date_update_dmy (d);
278 g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
284 g_date_get_year (const GDate *d)
286 g_return_val_if_fail (d != NULL, G_DATE_BAD_YEAR);
287 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
291 g_date_update_dmy (d);
293 g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
299 g_date_get_day (const GDate *d)
301 g_return_val_if_fail (d != NULL, G_DATE_BAD_DAY);
302 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
306 g_date_update_dmy (d);
308 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
314 g_date_get_julian (const GDate *d)
316 g_return_val_if_fail (d != NULL, G_DATE_BAD_JULIAN);
317 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
321 g_date_update_julian (d);
323 g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
325 return d->julian_days;
329 g_date_get_day_of_year (const GDate *d)
333 g_return_val_if_fail (d != NULL, 0);
334 g_return_val_if_fail (g_date_valid (d), 0);
338 g_date_update_dmy (d);
340 g_return_val_if_fail (d->dmy, 0);
342 index = g_date_is_leap_year (d->year) ? 1 : 0;
344 return (days_in_year[index][d->month] + d->day);
348 g_date_get_monday_week_of_year (const GDate *d)
354 g_return_val_if_fail (d != NULL, 0);
355 g_return_val_if_fail (g_date_valid (d), 0);
359 g_date_update_dmy (d);
361 g_return_val_if_fail (d->dmy, 0);
363 g_date_clear (&first, 1);
365 g_date_set_dmy (&first, 1, 1, d->year);
367 wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
368 day = g_date_get_day_of_year (d) - 1;
370 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
374 g_date_get_sunday_week_of_year (const GDate *d)
380 g_return_val_if_fail (d != NULL, 0);
381 g_return_val_if_fail (g_date_valid (d), 0);
385 g_date_update_dmy (d);
387 g_return_val_if_fail (d->dmy, 0);
389 g_date_clear (&first, 1);
391 g_date_set_dmy (&first, 1, 1, d->year);
393 wd = g_date_get_weekday (&first);
394 if (wd == 7) wd = 0; /* make Sunday day 0 */
395 day = g_date_get_day_of_year (d) - 1;
397 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
401 g_date_days_between (const GDate *d1,
404 g_return_val_if_fail (d1 != NULL, 0);
405 g_return_val_if_fail (d2 != NULL, 0);
407 g_return_val_if_fail (g_date_valid (d1), 0);
408 g_return_val_if_fail (g_date_valid (d2), 0);
410 return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
414 g_date_clear (GDate *d, guint ndates)
416 g_return_if_fail (d != NULL);
417 g_return_if_fail (ndates != 0);
419 memset (d, 0x0, ndates*sizeof (GDate));
422 G_LOCK_DEFINE_STATIC (g_date_global);
424 /* These are for the parser, output to the user should use *
425 * g_date_strftime () - this creates more never-freed memory to annoy
426 * all those memory debugger users. :-)
429 static gchar *long_month_names[13] =
431 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
434 static gchar *short_month_names[13] =
436 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
439 /* This tells us if we need to update the parse info */
440 static gchar *current_locale = NULL;
442 /* order of these in the current locale */
443 static GDateDMY dmy_order[3] =
445 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
448 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
449 * 29 and below are counted as in the year 2000, numbers 30 and above
450 * are counted as in the year 1900.
453 static GDateYear twodigit_start_year = 1930;
455 /* It is impossible to enter a year between 1 AD and 99 AD with this
458 static gboolean using_twodigit_years = FALSE;
460 struct _GDateParseTokens {
466 typedef struct _GDateParseTokens GDateParseTokens;
470 /* HOLDS: g_date_global_lock */
472 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
474 gchar num[4][NUM_LEN+1];
478 /* We count 4, but store 3; so we can give an error
481 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
483 s = (const guchar *) str;
485 while (*s && pt->num_ints < 4)
489 while (*s && isdigit (*s) && i <= NUM_LEN)
491 num[pt->num_ints][i] = *s;
498 num[pt->num_ints][i] = '\0';
502 if (*s == '\0') break;
507 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
508 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
509 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
511 pt->month = G_DATE_BAD_MONTH;
513 if (pt->num_ints < 3)
518 casefold = g_utf8_casefold (str, -1);
519 normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
525 if (long_month_names[i] != NULL)
527 const gchar *found = strstr (normalized, long_month_names[i]);
536 if (short_month_names[i] != NULL)
538 const gchar *found = strstr (normalized, short_month_names[i]);
552 /* HOLDS: g_date_global_lock */
554 g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
556 const gchar *locale = setlocale (LC_TIME, NULL);
557 gboolean recompute_localeinfo = FALSE;
560 g_return_if_fail (locale != NULL); /* should not happen */
562 g_date_clear (&d, 1); /* clear for scratch use */
564 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
566 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
569 if (recompute_localeinfo)
572 GDateParseTokens testpt;
575 g_free (current_locale); /* still works if current_locale == NULL */
577 current_locale = g_strdup (locale);
583 g_date_set_dmy (&d, 1, i, 1);
585 g_return_if_fail (g_date_valid (&d));
587 g_date_strftime (buf, 127, "%b", &d);
589 casefold = g_utf8_casefold (buf, -1);
590 g_free (short_month_names[i]);
591 short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
594 g_date_strftime (buf, 127, "%B", &d);
595 casefold = g_utf8_casefold (buf, -1);
596 g_free (long_month_names[i]);
597 long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
603 /* Determine DMY order */
605 /* had to pick a random day - don't change this, some strftimes
606 * are broken on some days, and this one is good so far. */
607 g_date_set_dmy (&d, 4, 7, 1976);
609 g_date_strftime (buf, 127, "%x", &d);
611 g_date_fill_parse_tokens (buf, &testpt);
614 while (i < testpt.num_ints)
619 dmy_order[i] = G_DATE_MONTH;
622 dmy_order[i] = G_DATE_DAY;
625 using_twodigit_years = TRUE; /* FALL THRU */
627 dmy_order[i] = G_DATE_YEAR;
630 /* leave it unchanged */
636 #ifdef G_ENABLE_DEBUG
637 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
641 DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
644 if (using_twodigit_years)
645 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
651 switch (dmy_order[i])
654 strings[i] = "Month";
668 DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
669 DEBUG_MSG (("**Sample date in this locale: `%s'", buf));
674 g_date_fill_parse_tokens (str, pt);
678 g_date_set_parse (GDate *d,
682 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
684 g_return_if_fail (d != NULL);
689 G_LOCK (g_date_global);
691 g_date_prepare_to_parse (str, &pt);
693 DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d",
694 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
697 if (pt.num_ints == 4)
699 G_UNLOCK (g_date_global);
700 return; /* presumably a typo; bail out. */
708 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
710 while (i < pt.num_ints && j < 3)
712 switch (dmy_order[j])
716 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
719 ++j; /* skip months, but don't skip this number */
728 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
731 ++j; /* skip days, since we may have month/year */
741 if (using_twodigit_years && y < 100)
743 guint two = twodigit_start_year % 100;
744 guint century = (twodigit_start_year / 100) * 100;
762 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
769 if (using_twodigit_years && y < 100)
770 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
772 else if (pt.num_ints == 2)
774 if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
780 else if (pt.num_ints == 1)
782 if (pt.month != G_DATE_BAD_MONTH)
784 /* Month name and year? */
791 /* Try yyyymmdd and yymmdd */
793 m = (pt.n[0]/100) % 100;
797 /* FIXME move this into a separate function */
798 if (using_twodigit_years && y < 100)
800 guint two = twodigit_start_year % 100;
801 guint century = (twodigit_start_year / 100) * 100;
811 /* See if we got anything valid out of all this. */
812 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
813 if (y < 8000 && g_date_valid_dmy (day, m, y))
820 #ifdef G_ENABLE_DEBUG
822 DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
824 G_UNLOCK (g_date_global);
828 g_date_set_time (GDate *d,
834 g_return_if_fail (d != NULL);
836 #ifdef HAVE_LOCALTIME_R
837 localtime_r (&t, &tm);
840 struct tm *ptm = localtime (&t);
842 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
848 d->month = tm.tm_mon + 1;
850 d->year = tm.tm_year + 1900;
852 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
858 g_date_set_month (GDate *d,
861 g_return_if_fail (d != NULL);
862 g_return_if_fail (g_date_valid_month (m));
864 if (d->julian && !d->dmy) g_date_update_dmy(d);
869 if (g_date_valid_dmy (d->day, d->month, d->year))
876 g_date_set_day (GDate *d,
879 g_return_if_fail (d != NULL);
880 g_return_if_fail (g_date_valid_day (day));
882 if (d->julian && !d->dmy) g_date_update_dmy(d);
887 if (g_date_valid_dmy (d->day, d->month, d->year))
894 g_date_set_year (GDate *d,
897 g_return_if_fail (d != NULL);
898 g_return_if_fail (g_date_valid_year (y));
900 if (d->julian && !d->dmy) g_date_update_dmy(d);
905 if (g_date_valid_dmy (d->day, d->month, d->year))
912 g_date_set_dmy (GDate *d,
917 g_return_if_fail (d != NULL);
918 g_return_if_fail (g_date_valid_dmy (day, m, y));
930 g_date_set_julian (GDate *d, guint32 j)
932 g_return_if_fail (d != NULL);
933 g_return_if_fail (g_date_valid_julian (j));
942 g_date_is_first_of_month (const GDate *d)
944 g_return_val_if_fail (d != NULL, FALSE);
945 g_return_val_if_fail (g_date_valid (d), FALSE);
949 g_date_update_dmy (d);
951 g_return_val_if_fail (d->dmy, FALSE);
953 if (d->day == 1) return TRUE;
958 g_date_is_last_of_month (const GDate *d)
962 g_return_val_if_fail (d != NULL, FALSE);
963 g_return_val_if_fail (g_date_valid (d), FALSE);
967 g_date_update_dmy (d);
969 g_return_val_if_fail (d->dmy, FALSE);
971 index = g_date_is_leap_year (d->year) ? 1 : 0;
973 if (d->day == days_in_months[index][d->month]) return TRUE;
978 g_date_add_days (GDate *d, guint ndays)
980 g_return_if_fail (d != NULL);
981 g_return_if_fail (g_date_valid (d));
985 g_date_update_julian (d);
987 g_return_if_fail (d->julian);
989 d->julian_days += ndays;
994 g_date_subtract_days (GDate *d, guint ndays)
996 g_return_if_fail (d != NULL);
997 g_return_if_fail (g_date_valid (d));
1001 g_date_update_julian (d);
1003 g_return_if_fail (d->julian);
1004 g_return_if_fail (d->julian_days > ndays);
1006 d->julian_days -= ndays;
1011 g_date_add_months (GDate *d,
1014 guint years, months;
1017 g_return_if_fail (d != NULL);
1018 g_return_if_fail (g_date_valid (d));
1022 g_date_update_dmy (d);
1024 g_return_if_fail (d->dmy);
1026 nmonths += d->month - 1;
1029 months = nmonths%12;
1031 d->month = months + 1;
1034 index = g_date_is_leap_year (d->year) ? 1 : 0;
1036 if (d->day > days_in_months[index][d->month])
1037 d->day = days_in_months[index][d->month];
1041 g_return_if_fail (g_date_valid (d));
1045 g_date_subtract_months (GDate *d,
1048 guint years, months;
1051 g_return_if_fail (d != NULL);
1052 g_return_if_fail (g_date_valid (d));
1056 g_date_update_dmy (d);
1058 g_return_if_fail (d->dmy);
1061 months = nmonths%12;
1063 g_return_if_fail (d->year > years);
1067 if (d->month > months) d->month -= months;
1071 d->month = 12 - months;
1075 index = g_date_is_leap_year (d->year) ? 1 : 0;
1077 if (d->day > days_in_months[index][d->month])
1078 d->day = days_in_months[index][d->month];
1082 g_return_if_fail (g_date_valid (d));
1086 g_date_add_years (GDate *d,
1089 g_return_if_fail (d != NULL);
1090 g_return_if_fail (g_date_valid (d));
1094 g_date_update_dmy (d);
1096 g_return_if_fail (d->dmy);
1100 if (d->month == 2 && d->day == 29)
1102 if (!g_date_is_leap_year (d->year))
1112 g_date_subtract_years (GDate *d,
1115 g_return_if_fail (d != NULL);
1116 g_return_if_fail (g_date_valid (d));
1120 g_date_update_dmy (d);
1122 g_return_if_fail (d->dmy);
1123 g_return_if_fail (d->year > nyears);
1127 if (d->month == 2 && d->day == 29)
1129 if (!g_date_is_leap_year (d->year))
1140 g_date_is_leap_year (GDateYear year)
1142 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1144 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1145 (year % 400) == 0 );
1149 g_date_get_days_in_month (GDateMonth month,
1154 g_return_val_if_fail (g_date_valid_year (year), 0);
1155 g_return_val_if_fail (g_date_valid_month (month), 0);
1157 index = g_date_is_leap_year (year) ? 1 : 0;
1159 return days_in_months[index][month];
1163 g_date_get_monday_weeks_in_year (GDateYear year)
1167 g_return_val_if_fail (g_date_valid_year (year), 0);
1169 g_date_clear (&d, 1);
1170 g_date_set_dmy (&d, 1, 1, year);
1171 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1172 g_date_set_dmy (&d, 31, 12, year);
1173 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1174 if (g_date_is_leap_year (year))
1176 g_date_set_dmy (&d, 2, 1, year);
1177 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1178 g_date_set_dmy (&d, 30, 12, year);
1179 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1185 g_date_get_sunday_weeks_in_year (GDateYear year)
1189 g_return_val_if_fail (g_date_valid_year (year), 0);
1191 g_date_clear (&d, 1);
1192 g_date_set_dmy (&d, 1, 1, year);
1193 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1194 g_date_set_dmy (&d, 31, 12, year);
1195 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1196 if (g_date_is_leap_year (year))
1198 g_date_set_dmy (&d, 2, 1, year);
1199 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1200 g_date_set_dmy (&d, 30, 12, year);
1201 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1207 g_date_compare (const GDate *lhs,
1210 g_return_val_if_fail (lhs != NULL, 0);
1211 g_return_val_if_fail (rhs != NULL, 0);
1212 g_return_val_if_fail (g_date_valid (lhs), 0);
1213 g_return_val_if_fail (g_date_valid (rhs), 0);
1215 /* Remember the self-comparison case! I think it works right now. */
1220 if (lhs->julian && rhs->julian)
1222 if (lhs->julian_days < rhs->julian_days) return -1;
1223 else if (lhs->julian_days > rhs->julian_days) return 1;
1226 else if (lhs->dmy && rhs->dmy)
1228 if (lhs->year < rhs->year) return -1;
1229 else if (lhs->year > rhs->year) return 1;
1232 if (lhs->month < rhs->month) return -1;
1233 else if (lhs->month > rhs->month) return 1;
1236 if (lhs->day < rhs->day) return -1;
1237 else if (lhs->day > rhs->day) return 1;
1246 if (!lhs->julian) g_date_update_julian (lhs);
1247 if (!rhs->julian) g_date_update_julian (rhs);
1248 g_return_val_if_fail (lhs->julian, 0);
1249 g_return_val_if_fail (rhs->julian, 0);
1253 return 0; /* warnings */
1258 g_date_to_struct_tm (const GDate *d,
1263 g_return_if_fail (d != NULL);
1264 g_return_if_fail (g_date_valid (d));
1265 g_return_if_fail (tm != NULL);
1269 g_date_update_dmy (d);
1271 g_return_if_fail (d->dmy);
1273 /* zero all the irrelevant fields to be sure they're valid */
1275 /* On Linux and maybe other systems, there are weird non-POSIX
1276 * fields on the end of struct tm that choke strftime if they
1277 * contain garbage. So we need to 0 the entire struct, not just the
1278 * fields we know to exist.
1281 memset (tm, 0x0, sizeof (struct tm));
1283 tm->tm_mday = d->day;
1284 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1285 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1287 day = g_date_get_weekday (d);
1288 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1290 tm->tm_wday = (int)day;
1292 tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
1293 tm->tm_isdst = -1; /* -1 means "information not available" */
1297 g_date_clamp (GDate *date,
1298 const GDate *min_date,
1299 const GDate *max_date)
1301 g_return_if_fail (date);
1302 g_return_if_fail (g_date_valid (date));
1303 if (min_date != NULL)
1304 g_return_if_fail (g_date_valid (min_date));
1305 if (max_date != NULL)
1306 g_return_if_fail (g_date_valid (max_date));
1307 if (min_date != NULL && max_date != NULL)
1308 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
1310 if (min_date && g_date_compare (date, min_date) < 0)
1313 if (max_date && g_date_compare (max_date, date) < 0)
1318 g_date_order (GDate *date1,
1321 g_return_if_fail (date1 != NULL);
1322 g_return_if_fail (date2 != NULL);
1323 g_return_if_fail (g_date_valid (date1));
1324 g_return_if_fail (g_date_valid (date2));
1326 if (g_date_compare (date1, date2) == 1) {
1334 g_date_strftime (gchar *s,
1336 const gchar *format,
1340 const gchar *charset;
1342 g_return_val_if_fail (d != NULL, 0);
1343 g_return_val_if_fail (g_date_valid (d), 0);
1344 g_return_val_if_fail (slen > 0, 0);
1345 g_return_val_if_fail (format != 0, 0);
1346 g_return_val_if_fail (s != 0, 0);
1348 g_date_to_struct_tm (d, &tm);
1350 if (g_get_charset (&charset))
1352 gint retval = strftime (s, slen, format, &tm);
1355 /* If retval == 0, the contents of s are undefined. We define
1365 gchar *locale_format;
1371 GError *error = NULL;
1373 locale_format = g_convert (format, -1 , "UTF-8", charset,
1374 NULL, NULL, &error);
1377 g_warning (G_STRLOC "Error converting format to %s: %s\n",
1378 charset, error->message);
1379 g_error_free (error);
1384 tmpbufsize = MAX (128, strlen (locale_format) * 2);
1387 tmpbuf = g_malloc (tmpbufsize + 1);
1388 tmplen = strftime (tmpbuf, tmpbufsize + 1, locale_format, &tm);
1389 if (tmplen == tmpbufsize + 1)
1397 g_free (locale_format);
1401 /* If retval == 0, the contents of s are undefined. We define
1404 g_free (locale_format);
1409 convbuf = g_convert (tmpbuf, tmplen, "UTF-8", charset, 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);
1419 /* Only copy whole characters into the buffer
1421 gchar *in = convbuf;
1423 gchar *end = s + slen - 1;
1427 int len = g_utf8_skip[*(guchar *)in];
1428 if (out + len < end)
1437 memcpy (s, convbuf, out - s);