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 ; */
54 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
60 g_date_new_dmy (GDateDay day,
65 g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
76 g_assert (g_date_valid (d));
82 g_date_new_julian (guint32 j)
85 g_return_val_if_fail (g_date_valid_julian (j), NULL);
94 g_assert (g_date_valid (d));
100 g_date_free (GDate *d)
102 g_return_if_fail (d != NULL);
108 g_date_valid (const GDate *d)
110 g_return_val_if_fail (d != NULL, FALSE);
112 return (d->julian || d->dmy);
115 static const guint8 days_in_months[2][13] =
116 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
117 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
118 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
121 static const guint16 days_in_year[2][14] =
122 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
123 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
124 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
128 g_date_valid_month (GDateMonth m)
130 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
134 g_date_valid_year (GDateYear y)
136 return ( y > G_DATE_BAD_YEAR );
140 g_date_valid_day (GDateDay d)
142 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
146 g_date_valid_weekday (GDateWeekday w)
148 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
152 g_date_valid_julian (guint32 j)
154 return (j > G_DATE_BAD_JULIAN);
158 g_date_valid_dmy (GDateDay d,
162 return ( (m > G_DATE_BAD_MONTH) &&
164 (d > G_DATE_BAD_DAY) &&
165 (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
166 (d <= (g_date_is_leap_year (y) ?
167 days_in_months[1][m] : days_in_months[0][m])) );
171 /* "Julian days" just means an absolute number of days, where Day 1 ==
175 g_date_update_julian (const GDate *const_d)
177 GDate *d = (GDate *) const_d;
181 g_return_if_fail (d != NULL);
182 g_return_if_fail (d->dmy);
183 g_return_if_fail (!d->julian);
184 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
186 /* What we actually do is: multiply years * 365 days in the year,
187 * add the number of years divided by 4, subtract the number of
188 * years divided by 100 and add the number of years divided by 400,
189 * which accounts for leap year stuff. Code from Steffen Beyer's
193 year = d->year - 1; /* we know d->year > 0 since it's valid */
195 d->julian_days = year * 365U;
196 d->julian_days += (year >>= 2); /* divide by 4 and add */
197 d->julian_days -= (year /= 25); /* divides original # years by 100 */
198 d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
200 index = g_date_is_leap_year (d->year) ? 1 : 0;
202 d->julian_days += days_in_year[index][d->month] + d->day;
204 g_return_if_fail (g_date_valid_julian (d->julian_days));
210 g_date_update_dmy (const GDate *const_d)
212 GDate *d = (GDate *) const_d;
217 guint32 A, B, C, D, E, M;
219 g_return_if_fail (d != NULL);
220 g_return_if_fail (d->julian);
221 g_return_if_fail (!d->dmy);
222 g_return_if_fail (g_date_valid_julian (d->julian_days));
224 /* Formula taken from the Calendar FAQ; the formula was for the
225 * Julian Period which starts on 1 January 4713 BC, so we add
226 * 1,721,425 to the number of days before doing the formula.
228 * I'm sure this can be simplified for our 1 January 1 AD period
229 * start, but I can't figure out how to unpack the formula.
232 A = d->julian_days + 1721425 + 32045;
233 B = ( 4 *(A + 36524) )/ 146097 - 1;
234 C = A - (146097 * B)/4;
235 D = ( 4 * (C + 365) ) / 1461 - 1;
236 E = C - ((1461*D) / 4);
237 M = (5 * (E - 1) + 2)/153;
239 m = M + 3 - (12*(M/10));
240 day = E - (153*M + 2)/5;
241 y = 100 * B + D - 4800 + (M/10);
243 #ifdef G_ENABLE_DEBUG
244 if (!g_date_valid_dmy (day, m, y))
245 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
246 d->julian_days, day, m, y);
257 g_date_get_weekday (const GDate *d)
259 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
262 g_date_update_julian (d);
264 g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
266 return ((d->julian_days - 1) % 7) + 1;
270 g_date_get_month (const GDate *d)
272 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 (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 (g_date_valid (d), G_DATE_BAD_DAY);
301 g_date_update_dmy (d);
303 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
309 g_date_get_julian (const GDate *d)
311 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
314 g_date_update_julian (d);
316 g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
318 return d->julian_days;
322 g_date_get_day_of_year (const GDate *d)
326 g_return_val_if_fail (g_date_valid (d), 0);
329 g_date_update_dmy (d);
331 g_return_val_if_fail (d->dmy, 0);
333 index = g_date_is_leap_year (d->year) ? 1 : 0;
335 return (days_in_year[index][d->month] + d->day);
339 g_date_get_monday_week_of_year (const GDate *d)
345 g_return_val_if_fail (g_date_valid (d), 0);
348 g_date_update_dmy (d);
350 g_return_val_if_fail (d->dmy, 0);
352 g_date_clear (&first, 1);
354 g_date_set_dmy (&first, 1, 1, d->year);
356 wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
357 day = g_date_get_day_of_year (d) - 1;
359 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
363 g_date_get_sunday_week_of_year (const GDate *d)
369 g_return_val_if_fail (g_date_valid (d), 0);
372 g_date_update_dmy (d);
374 g_return_val_if_fail (d->dmy, 0);
376 g_date_clear (&first, 1);
378 g_date_set_dmy (&first, 1, 1, d->year);
380 wd = g_date_get_weekday (&first);
381 if (wd == 7) wd = 0; /* make Sunday day 0 */
382 day = g_date_get_day_of_year (d) - 1;
384 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
388 * g_date_get_iso8601_week_of_year:
389 * @date: a valid #GDate
391 * Returns the week of the year, where weeks are interpreted according
394 * Returns: ISO 8601 week number of the year.
399 g_date_get_iso8601_week_of_year (const GDate *d)
401 guint j, d4, L, d1, w;
403 g_return_val_if_fail (g_date_valid (d), 0);
406 g_date_update_julian (d);
408 g_return_val_if_fail (d->julian, 0);
410 /* Formula taken from the Calendar FAQ; the formula was for the
411 * Julian Period which starts on 1 January 4713 BC, so we add
412 * 1,721,425 to the number of days before doing the formula.
414 j = d->julian_days + 1721425;
415 d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
417 d1 = ((d4 - L) % 365) + L;
424 g_date_days_between (const GDate *d1,
427 g_return_val_if_fail (g_date_valid (d1), 0);
428 g_return_val_if_fail (g_date_valid (d2), 0);
430 return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
434 g_date_clear (GDate *d, guint ndates)
436 g_return_if_fail (d != NULL);
437 g_return_if_fail (ndates != 0);
439 memset (d, 0x0, ndates*sizeof (GDate));
442 G_LOCK_DEFINE_STATIC (g_date_global);
444 /* These are for the parser, output to the user should use *
445 * g_date_strftime () - this creates more never-freed memory to annoy
446 * all those memory debugger users. :-)
449 static gchar *long_month_names[13] =
454 static gchar *short_month_names[13] =
459 /* This tells us if we need to update the parse info */
460 static gchar *current_locale = NULL;
462 /* order of these in the current locale */
463 static GDateDMY dmy_order[3] =
465 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
468 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
469 * 29 and below are counted as in the year 2000, numbers 30 and above
470 * are counted as in the year 1900.
473 static const GDateYear twodigit_start_year = 1930;
475 /* It is impossible to enter a year between 1 AD and 99 AD with this
478 static gboolean using_twodigit_years = FALSE;
480 /* Adjustment of locale era to AD, non-zero means using locale era
482 static gint locale_era_adjust = 0;
484 struct _GDateParseTokens {
490 typedef struct _GDateParseTokens GDateParseTokens;
494 /* HOLDS: g_date_global_lock */
496 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
498 gchar num[4][NUM_LEN+1];
502 /* We count 4, but store 3; so we can give an error
505 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
507 s = (const guchar *) str;
509 while (*s && pt->num_ints < 4)
513 while (*s && g_ascii_isdigit (*s) && i < NUM_LEN)
515 num[pt->num_ints][i] = *s;
522 num[pt->num_ints][i] = '\0';
526 if (*s == '\0') break;
531 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
532 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
533 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
535 pt->month = G_DATE_BAD_MONTH;
537 if (pt->num_ints < 3)
542 casefold = g_utf8_casefold (str, -1);
543 normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
549 if (long_month_names[i] != NULL)
551 const gchar *found = strstr (normalized, long_month_names[i]);
560 if (short_month_names[i] != NULL)
562 const gchar *found = strstr (normalized, short_month_names[i]);
578 /* HOLDS: g_date_global_lock */
580 g_date_prepare_to_parse (const gchar *str,
581 GDateParseTokens *pt)
583 const gchar *locale = setlocale (LC_TIME, NULL);
584 gboolean recompute_localeinfo = FALSE;
587 g_return_if_fail (locale != NULL); /* should not happen */
589 g_date_clear (&d, 1); /* clear for scratch use */
591 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
592 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
594 if (recompute_localeinfo)
597 GDateParseTokens testpt;
600 g_free (current_locale); /* still works if current_locale == NULL */
602 current_locale = g_strdup (locale);
604 short_month_names[0] = "Error";
605 long_month_names[0] = "Error";
611 g_date_set_dmy (&d, 1, i, 1);
613 g_return_if_fail (g_date_valid (&d));
615 g_date_strftime (buf, 127, "%b", &d);
617 casefold = g_utf8_casefold (buf, -1);
618 g_free (short_month_names[i]);
619 short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
622 g_date_strftime (buf, 127, "%B", &d);
623 casefold = g_utf8_casefold (buf, -1);
624 g_free (long_month_names[i]);
625 long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
631 /* Determine DMY order */
633 /* had to pick a random day - don't change this, some strftimes
634 * are broken on some days, and this one is good so far. */
635 g_date_set_dmy (&d, 4, 7, 1976);
637 g_date_strftime (buf, 127, "%x", &d);
639 g_date_fill_parse_tokens (buf, &testpt);
642 while (i < testpt.num_ints)
647 dmy_order[i] = G_DATE_MONTH;
650 dmy_order[i] = G_DATE_DAY;
653 using_twodigit_years = TRUE; /* FALL THRU */
655 dmy_order[i] = G_DATE_YEAR;
658 /* assume locale era */
659 locale_era_adjust = 1976 - testpt.n[i];
660 dmy_order[i] = G_DATE_YEAR;
666 #ifdef G_ENABLE_DEBUG
667 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
671 DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
674 if (using_twodigit_years)
675 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
681 switch (dmy_order[i])
684 strings[i] = "Month";
698 DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
699 DEBUG_MSG (("**Sample date in this locale: `%s'", buf));
704 g_date_fill_parse_tokens (str, pt);
708 g_date_set_parse (GDate *d,
712 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
714 g_return_if_fail (d != NULL);
719 G_LOCK (g_date_global);
721 g_date_prepare_to_parse (str, &pt);
723 DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d",
724 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
727 if (pt.num_ints == 4)
729 G_UNLOCK (g_date_global);
730 return; /* presumably a typo; bail out. */
738 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
740 while (i < pt.num_ints && j < 3)
742 switch (dmy_order[j])
746 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
749 ++j; /* skip months, but don't skip this number */
758 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
761 ++j; /* skip days, since we may have month/year */
771 if (locale_era_adjust != 0)
773 y += locale_era_adjust;
775 else if (using_twodigit_years && y < 100)
777 guint two = twodigit_start_year % 100;
778 guint century = (twodigit_start_year / 100) * 100;
796 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
803 if (using_twodigit_years && y < 100)
804 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
806 else if (pt.num_ints == 2)
808 if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
812 else if (pt.num_ints == 1)
814 if (pt.month != G_DATE_BAD_MONTH)
816 /* Month name and year? */
823 /* Try yyyymmdd and yymmdd */
825 m = (pt.n[0]/100) % 100;
829 /* FIXME move this into a separate function */
830 if (using_twodigit_years && y < 100)
832 guint two = twodigit_start_year % 100;
833 guint century = (twodigit_start_year / 100) * 100;
843 /* See if we got anything valid out of all this. */
844 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
845 if (y < 8000 && g_date_valid_dmy (day, m, y))
852 #ifdef G_ENABLE_DEBUG
854 DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
856 G_UNLOCK (g_date_global);
862 * @timet: <type>time_t</type> value to set
864 * Sets the value of a date to the date corresponding to a time
865 * specified as a time_t. The time to date conversion is done using
866 * the user's current timezone.
868 * To set the value of a date to the current day, you could write:
870 * g_date_set_time_t (date, time (NULL));
876 g_date_set_time_t (GDate *date,
881 g_return_if_fail (date != NULL);
883 #ifdef HAVE_LOCALTIME_R
884 localtime_r (&timet, &tm);
887 struct tm *ptm = localtime (&timet);
891 /* Happens at least in Microsoft's C library if you pass a
892 * negative time_t. Use 2000-01-01 as default date.
894 #ifndef G_DISABLE_CHECKS
895 g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
903 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
907 date->julian = FALSE;
909 date->month = tm.tm_mon + 1;
910 date->day = tm.tm_mday;
911 date->year = tm.tm_year + 1900;
913 g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
922 * @time_: #GTime value to set.
924 * Sets the value of a date from a #GTime value.
925 * The time to date conversion is done using the user's current timezone.
927 * @Deprecated:2.10: Use g_date_set_time_t() instead.
930 g_date_set_time (GDate *date,
933 g_date_set_time_t (date, (time_t) time_);
937 * g_date_set_time_val:
939 * @timeval: #GTimeVal value to set
941 * Sets the value of a date from a #GTimeVal value. Note that the
942 * @tv_usec member is ignored, because #GDate can't make use of the
943 * additional precision.
948 g_date_set_time_val (GDate *date,
951 g_date_set_time_t (date, (time_t) timeval->tv_sec);
955 g_date_set_month (GDate *d,
958 g_return_if_fail (d != NULL);
959 g_return_if_fail (g_date_valid_month (m));
961 if (d->julian && !d->dmy) g_date_update_dmy(d);
966 if (g_date_valid_dmy (d->day, d->month, d->year))
973 g_date_set_day (GDate *d,
976 g_return_if_fail (d != NULL);
977 g_return_if_fail (g_date_valid_day (day));
979 if (d->julian && !d->dmy) g_date_update_dmy(d);
984 if (g_date_valid_dmy (d->day, d->month, d->year))
991 g_date_set_year (GDate *d,
994 g_return_if_fail (d != NULL);
995 g_return_if_fail (g_date_valid_year (y));
997 if (d->julian && !d->dmy) g_date_update_dmy(d);
1002 if (g_date_valid_dmy (d->day, d->month, d->year))
1009 g_date_set_dmy (GDate *d,
1014 g_return_if_fail (d != NULL);
1015 g_return_if_fail (g_date_valid_dmy (day, m, y));
1027 g_date_set_julian (GDate *d,
1030 g_return_if_fail (d != NULL);
1031 g_return_if_fail (g_date_valid_julian (j));
1040 g_date_is_first_of_month (const GDate *d)
1042 g_return_val_if_fail (g_date_valid (d), FALSE);
1045 g_date_update_dmy (d);
1047 g_return_val_if_fail (d->dmy, FALSE);
1049 if (d->day == 1) return TRUE;
1054 g_date_is_last_of_month (const GDate *d)
1058 g_return_val_if_fail (g_date_valid (d), FALSE);
1061 g_date_update_dmy (d);
1063 g_return_val_if_fail (d->dmy, FALSE);
1065 index = g_date_is_leap_year (d->year) ? 1 : 0;
1067 if (d->day == days_in_months[index][d->month]) return TRUE;
1072 g_date_add_days (GDate *d,
1075 g_return_if_fail (g_date_valid (d));
1078 g_date_update_julian (d);
1080 g_return_if_fail (d->julian);
1082 d->julian_days += ndays;
1087 g_date_subtract_days (GDate *d,
1090 g_return_if_fail (g_date_valid (d));
1093 g_date_update_julian (d);
1095 g_return_if_fail (d->julian);
1096 g_return_if_fail (d->julian_days > ndays);
1098 d->julian_days -= ndays;
1103 g_date_add_months (GDate *d,
1106 guint years, months;
1109 g_return_if_fail (g_date_valid (d));
1112 g_date_update_dmy (d);
1114 g_return_if_fail (d->dmy);
1116 nmonths += d->month - 1;
1119 months = nmonths%12;
1121 d->month = months + 1;
1124 index = g_date_is_leap_year (d->year) ? 1 : 0;
1126 if (d->day > days_in_months[index][d->month])
1127 d->day = days_in_months[index][d->month];
1131 g_return_if_fail (g_date_valid (d));
1135 g_date_subtract_months (GDate *d,
1138 guint years, months;
1141 g_return_if_fail (g_date_valid (d));
1144 g_date_update_dmy (d);
1146 g_return_if_fail (d->dmy);
1149 months = nmonths%12;
1151 g_return_if_fail (d->year > years);
1155 if (d->month > months) d->month -= months;
1159 d->month = 12 - months;
1163 index = g_date_is_leap_year (d->year) ? 1 : 0;
1165 if (d->day > days_in_months[index][d->month])
1166 d->day = days_in_months[index][d->month];
1170 g_return_if_fail (g_date_valid (d));
1174 g_date_add_years (GDate *d,
1177 g_return_if_fail (g_date_valid (d));
1180 g_date_update_dmy (d);
1182 g_return_if_fail (d->dmy);
1186 if (d->month == 2 && d->day == 29)
1188 if (!g_date_is_leap_year (d->year))
1196 g_date_subtract_years (GDate *d,
1199 g_return_if_fail (g_date_valid (d));
1202 g_date_update_dmy (d);
1204 g_return_if_fail (d->dmy);
1205 g_return_if_fail (d->year > nyears);
1209 if (d->month == 2 && d->day == 29)
1211 if (!g_date_is_leap_year (d->year))
1219 g_date_is_leap_year (GDateYear year)
1221 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1223 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1224 (year % 400) == 0 );
1228 g_date_get_days_in_month (GDateMonth month,
1233 g_return_val_if_fail (g_date_valid_year (year), 0);
1234 g_return_val_if_fail (g_date_valid_month (month), 0);
1236 index = g_date_is_leap_year (year) ? 1 : 0;
1238 return days_in_months[index][month];
1242 g_date_get_monday_weeks_in_year (GDateYear year)
1246 g_return_val_if_fail (g_date_valid_year (year), 0);
1248 g_date_clear (&d, 1);
1249 g_date_set_dmy (&d, 1, 1, year);
1250 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1251 g_date_set_dmy (&d, 31, 12, year);
1252 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1253 if (g_date_is_leap_year (year))
1255 g_date_set_dmy (&d, 2, 1, year);
1256 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1257 g_date_set_dmy (&d, 30, 12, year);
1258 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1264 g_date_get_sunday_weeks_in_year (GDateYear year)
1268 g_return_val_if_fail (g_date_valid_year (year), 0);
1270 g_date_clear (&d, 1);
1271 g_date_set_dmy (&d, 1, 1, year);
1272 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1273 g_date_set_dmy (&d, 31, 12, year);
1274 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1275 if (g_date_is_leap_year (year))
1277 g_date_set_dmy (&d, 2, 1, year);
1278 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1279 g_date_set_dmy (&d, 30, 12, year);
1280 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1286 g_date_compare (const GDate *lhs,
1289 g_return_val_if_fail (lhs != NULL, 0);
1290 g_return_val_if_fail (rhs != NULL, 0);
1291 g_return_val_if_fail (g_date_valid (lhs), 0);
1292 g_return_val_if_fail (g_date_valid (rhs), 0);
1294 /* Remember the self-comparison case! I think it works right now. */
1298 if (lhs->julian && rhs->julian)
1300 if (lhs->julian_days < rhs->julian_days) return -1;
1301 else if (lhs->julian_days > rhs->julian_days) return 1;
1304 else if (lhs->dmy && rhs->dmy)
1306 if (lhs->year < rhs->year) return -1;
1307 else if (lhs->year > rhs->year) return 1;
1310 if (lhs->month < rhs->month) return -1;
1311 else if (lhs->month > rhs->month) return 1;
1314 if (lhs->day < rhs->day) return -1;
1315 else if (lhs->day > rhs->day) return 1;
1324 if (!lhs->julian) g_date_update_julian (lhs);
1325 if (!rhs->julian) g_date_update_julian (rhs);
1326 g_return_val_if_fail (lhs->julian, 0);
1327 g_return_val_if_fail (rhs->julian, 0);
1331 return 0; /* warnings */
1336 g_date_to_struct_tm (const GDate *d,
1341 g_return_if_fail (g_date_valid (d));
1342 g_return_if_fail (tm != NULL);
1345 g_date_update_dmy (d);
1347 g_return_if_fail (d->dmy);
1349 /* zero all the irrelevant fields to be sure they're valid */
1351 /* On Linux and maybe other systems, there are weird non-POSIX
1352 * fields on the end of struct tm that choke strftime if they
1353 * contain garbage. So we need to 0 the entire struct, not just the
1354 * fields we know to exist.
1357 memset (tm, 0x0, sizeof (struct tm));
1359 tm->tm_mday = d->day;
1360 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1361 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1363 day = g_date_get_weekday (d);
1364 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1366 tm->tm_wday = (int)day;
1368 tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
1369 tm->tm_isdst = -1; /* -1 means "information not available" */
1373 g_date_clamp (GDate *date,
1374 const GDate *min_date,
1375 const GDate *max_date)
1377 g_return_if_fail (g_date_valid (date));
1379 if (min_date != NULL)
1380 g_return_if_fail (g_date_valid (min_date));
1382 if (max_date != NULL)
1383 g_return_if_fail (g_date_valid (max_date));
1385 if (min_date != NULL && max_date != NULL)
1386 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
1388 if (min_date && g_date_compare (date, min_date) < 0)
1391 if (max_date && g_date_compare (max_date, date) < 0)
1396 g_date_order (GDate *date1,
1399 g_return_if_fail (g_date_valid (date1));
1400 g_return_if_fail (g_date_valid (date2));
1402 if (g_date_compare (date1, date2) > 0)
1412 win32_strftime_helper (const GDate *d,
1413 const gchar *format,
1414 const struct tm *tm,
1418 SYSTEMTIME systemtime;
1419 TIME_ZONE_INFORMATION tzinfo;
1425 const wchar_t digits[] = L"0123456789";
1430 systemtime.wYear = tm->tm_year + 1900;
1431 systemtime.wMonth = tm->tm_mon + 1;
1432 systemtime.wDayOfWeek = tm->tm_wday;
1433 systemtime.wDay = tm->tm_mday;
1434 systemtime.wHour = tm->tm_hour;
1435 systemtime.wMinute = tm->tm_min;
1436 systemtime.wSecond = tm->tm_sec;
1437 systemtime.wMilliseconds = 0;
1439 lcid = GetThreadLocale ();
1440 result = g_array_sized_new (FALSE, FALSE, sizeof (wchar_t), MAX (128, strlen (format) * 2));
1445 c = g_utf8_get_char (p);
1448 p = g_utf8_next_char (p);
1452 g_array_free (result, TRUE);
1457 c = g_utf8_get_char (p);
1458 if (c == 'E' || c == 'O')
1460 /* Ignore modified conversion specifiers for now. */
1461 p = g_utf8_next_char (p);
1465 g_array_free (result, TRUE);
1470 c = g_utf8_get_char (p);
1476 if (systemtime.wDayOfWeek == 0)
1479 k = systemtime.wDayOfWeek - 1;
1480 n = GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, NULL, 0);
1481 g_array_set_size (result, result->len + n);
1482 GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
1483 g_array_set_size (result, result->len - 1);
1486 if (systemtime.wDayOfWeek == 0)
1489 k = systemtime.wDayOfWeek - 1;
1490 n = GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, NULL, 0);
1491 g_array_set_size (result, result->len + n);
1492 GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
1493 g_array_set_size (result, result->len - 1);
1497 n = GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, NULL, 0);
1498 g_array_set_size (result, result->len + n);
1499 GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
1500 g_array_set_size (result, result->len - 1);
1503 n = GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, NULL, 0);
1504 g_array_set_size (result, result->len + n);
1505 GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
1506 g_array_set_size (result, result->len - 1);
1509 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1512 g_array_set_size (result, result->len + n);
1513 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1514 g_array_set_size (result, result->len - 1);
1516 g_array_append_vals (result, L" ", 1);
1517 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1520 g_array_set_size (result, result->len + n);
1521 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1522 g_array_set_size (result, result->len - 1);
1526 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
1527 g_array_append_vals (result, digits + (systemtime.wYear/1000)%10, 1);
1530 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1531 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1534 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
1535 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
1536 g_array_append_vals (result, L"/", 1);
1537 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1538 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1539 g_array_append_vals (result, L"/", 1);
1540 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
1541 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
1544 if (systemtime.wDay >= 10)
1545 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
1547 g_array_append_vals (result, L" ", 1);
1548 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
1551 /* A GDate has no time fields, so for now we can
1552 * hardcode all time conversions into zeros (or 12 for
1553 * %I). The alternative code snippets in the #else
1554 * branches are here ready to be taken into use when
1555 * needed by a g_strftime() or g_date_and_time_format()
1560 g_array_append_vals (result, L"00", 2);
1562 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1563 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1568 g_array_append_vals (result, L"12", 2);
1570 if (systemtime.wHour == 0)
1571 g_array_append_vals (result, L"12", 2);
1574 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
1575 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
1580 g_array_append_vals (result, digits + (tm->tm_yday+1)/100, 1);
1581 g_array_append_vals (result, digits + ((tm->tm_yday+1)/10)%10, 1);
1582 g_array_append_vals (result, digits + (tm->tm_yday+1)%10, 1);
1585 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
1586 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
1590 g_array_append_vals (result, L"00", 2);
1592 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1593 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1597 g_array_append_vals (result, L"\n", 1);
1600 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
1603 g_array_set_size (result, result->len + n);
1604 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
1605 g_array_set_size (result, result->len - 1);
1609 /* This is a rather odd format. Hard to say what to do.
1610 * Let's always use the POSIX %I:%M:%S %p
1613 g_array_append_vals (result, L"12:00:00", 8);
1615 if (systemtime.wHour == 0)
1616 g_array_append_vals (result, L"12", 2);
1619 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
1620 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
1622 g_array_append_vals (result, L":", 1);
1623 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1624 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1625 g_array_append_vals (result, L":", 1);
1626 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1627 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1628 g_array_append_vals (result, L" ", 1);
1630 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
1633 g_array_set_size (result, result->len + n);
1634 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
1635 g_array_set_size (result, result->len - 1);
1640 g_array_append_vals (result, L"00:00", 5);
1642 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1643 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1644 g_array_append_vals (result, L":", 1);
1645 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1646 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1651 g_array_append_vals (result, L"00", 2);
1653 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1654 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1658 g_array_append_vals (result, L"\t", 1);
1662 g_array_append_vals (result, L"00:00:00", 8);
1664 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
1665 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
1666 g_array_append_vals (result, L":", 1);
1667 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
1668 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
1669 g_array_append_vals (result, L":", 1);
1670 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
1671 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
1675 if (systemtime.wDayOfWeek == 0)
1676 g_array_append_vals (result, L"7", 1);
1678 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
1681 n = g_date_get_sunday_week_of_year (d);
1682 g_array_append_vals (result, digits + n/10, 1);
1683 g_array_append_vals (result, digits + n%10, 1);
1686 n = g_date_get_iso8601_week_of_year (d);
1687 g_array_append_vals (result, digits + n/10, 1);
1688 g_array_append_vals (result, digits + n%10, 1);
1691 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
1694 n = g_date_get_monday_week_of_year (d);
1695 g_array_append_vals (result, digits + n/10, 1);
1696 g_array_append_vals (result, digits + n%10, 1);
1699 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1702 g_array_set_size (result, result->len + n);
1703 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1704 g_array_set_size (result, result->len - 1);
1708 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
1711 g_array_set_size (result, result->len + n);
1712 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
1713 g_array_set_size (result, result->len - 1);
1717 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
1718 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
1721 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
1722 g_array_append_vals (result, digits + (systemtime.wYear/100)%10, 1);
1723 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
1724 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
1727 n = GetTimeZoneInformation (&tzinfo);
1728 if (n == TIME_ZONE_ID_UNKNOWN)
1730 else if (n == TIME_ZONE_ID_STANDARD)
1731 g_array_append_vals (result, tzinfo.StandardName, wcslen (tzinfo.StandardName));
1732 else if (n == TIME_ZONE_ID_DAYLIGHT)
1733 g_array_append_vals (result, tzinfo.DaylightName, wcslen (tzinfo.DaylightName));
1736 g_array_append_vals (result, L"%", 1);
1740 else if (c <= 0xFFFF)
1743 g_array_append_vals (result, &wc, 1);
1750 ws = g_ucs4_to_utf16 (&c, 1, NULL, &nwc, NULL);
1751 g_array_append_vals (result, ws, nwc);
1754 p = g_utf8_next_char (p);
1757 convbuf = g_utf16_to_utf8 ((wchar_t *) result->data, result->len, NULL, &convlen, NULL);
1758 g_array_free (result, TRUE);
1766 if (slen <= convlen)
1768 /* Ensure only whole characters are copied into the buffer. */
1769 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1770 g_assert (end != NULL);
1771 convlen = end - convbuf;
1773 /* Return 0 because the buffer isn't large enough. */
1779 memcpy (s, convbuf, convlen);
1789 g_date_strftime (gchar *s,
1791 const gchar *format,
1796 gsize locale_format_len = 0;
1797 gchar *locale_format;
1803 GError *error = NULL;
1807 g_return_val_if_fail (g_date_valid (d), 0);
1808 g_return_val_if_fail (slen > 0, 0);
1809 g_return_val_if_fail (format != NULL, 0);
1810 g_return_val_if_fail (s != NULL, 0);
1812 g_date_to_struct_tm (d, &tm);
1815 if (!g_utf8_validate (format, -1, NULL))
1820 return win32_strftime_helper (d, format, &tm, s, slen);
1823 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
1827 g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
1828 g_error_free (error);
1834 tmpbufsize = MAX (128, locale_format_len * 2);
1837 tmpbuf = g_malloc (tmpbufsize);
1839 /* Set the first byte to something other than '\0', to be able to
1840 * recognize whether strftime actually failed or just returned "".
1843 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
1845 if (tmplen == 0 && tmpbuf[0] != '\0')
1850 if (tmpbufsize > 65536)
1852 g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
1853 g_free (locale_format);
1862 g_free (locale_format);
1864 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
1869 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
1870 g_error_free (error);
1876 if (slen <= convlen)
1878 /* Ensure only whole characters are copied into the buffer.
1880 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1881 g_assert (end != NULL);
1882 convlen = end - convbuf;
1884 /* Return 0 because the buffer isn't large enough.
1891 memcpy (s, convbuf, convlen);
1899 #define __G_DATE_C__
1900 #include "galiasdef.c"