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 ; */
49 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
55 g_date_new_dmy (GDateDay day, GDateMonth m, GDateYear y)
58 g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
69 g_assert (g_date_valid (d));
75 g_date_new_julian (guint32 j)
78 g_return_val_if_fail (g_date_valid_julian (j), NULL);
87 g_assert (g_date_valid (d));
93 g_date_free (GDate *d)
95 g_return_if_fail (d != NULL);
101 g_date_valid (const GDate *d)
103 g_return_val_if_fail (d != NULL, FALSE);
105 return (d->julian || d->dmy);
108 static const guint8 days_in_months[2][13] =
109 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
110 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
111 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
114 static const guint16 days_in_year[2][14] =
115 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
116 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
117 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
121 g_date_valid_month (GDateMonth m)
123 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
127 g_date_valid_year (GDateYear y)
129 return ( y > G_DATE_BAD_YEAR );
133 g_date_valid_day (GDateDay d)
135 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
139 g_date_valid_weekday (GDateWeekday w)
141 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
145 g_date_valid_julian (guint32 j)
147 return (j > G_DATE_BAD_JULIAN);
151 g_date_valid_dmy (GDateDay d,
155 return ( (m > G_DATE_BAD_MONTH) &&
157 (d > G_DATE_BAD_DAY) &&
158 (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
159 (d <= (g_date_is_leap_year (y) ?
160 days_in_months[1][m] : days_in_months[0][m])) );
164 /* "Julian days" just means an absolute number of days, where Day 1 ==
168 g_date_update_julian (const GDate *const_d)
170 GDate *d = (GDate *) const_d;
174 g_return_if_fail (d != NULL);
175 g_return_if_fail (d->dmy);
176 g_return_if_fail (!d->julian);
177 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
179 /* What we actually do is: multiply years * 365 days in the year,
180 * add the number of years divided by 4, subtract the number of
181 * years divided by 100 and add the number of years divided by 400,
182 * which accounts for leap year stuff. Code from Steffen Beyer's
186 year = d->year - 1; /* we know d->year > 0 since it's valid */
188 d->julian_days = year * 365U;
189 d->julian_days += (year >>= 2); /* divide by 4 and add */
190 d->julian_days -= (year /= 25); /* divides original # years by 100 */
191 d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
193 index = g_date_is_leap_year (d->year) ? 1 : 0;
195 d->julian_days += days_in_year[index][d->month] + d->day;
197 g_return_if_fail (g_date_valid_julian (d->julian_days));
203 g_date_update_dmy (const GDate *const_d)
205 GDate *d = (GDate *) const_d;
210 guint32 A, B, C, D, E, M;
212 g_return_if_fail (d != NULL);
213 g_return_if_fail (d->julian);
214 g_return_if_fail (!d->dmy);
215 g_return_if_fail (g_date_valid_julian (d->julian_days));
217 /* Formula taken from the Calendar FAQ; the formula was for the
218 * Julian Period which starts on 1 January 4713 BC, so we add
219 * 1,721,425 to the number of days before doing the formula.
221 * I'm sure this can be simplified for our 1 January 1 AD period
222 * start, but I can't figure out how to unpack the formula.
225 A = d->julian_days + 1721425 + 32045;
226 B = ( 4 *(A + 36524) )/ 146097 - 1;
227 C = A - (146097 * B)/4;
228 D = ( 4 * (C + 365) ) / 1461 - 1;
229 E = C - ((1461*D) / 4);
230 M = (5 * (E - 1) + 2)/153;
232 m = M + 3 - (12*(M/10));
233 day = E - (153*M + 2)/5;
234 y = 100 * B + D - 4800 + (M/10);
236 #ifdef G_ENABLE_DEBUG
237 if (!g_date_valid_dmy (day, m, y))
239 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
240 d->julian_days, day, m, y);
252 g_date_get_weekday (const GDate *d)
254 g_return_val_if_fail (d != NULL, G_DATE_BAD_WEEKDAY);
255 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
259 g_date_update_julian (d);
261 g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
263 return ((d->julian_days - 1) % 7) + 1;
267 g_date_get_month (const GDate *d)
269 g_return_val_if_fail (d != NULL, G_DATE_BAD_MONTH);
270 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
274 g_date_update_dmy (d);
276 g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
282 g_date_get_year (const GDate *d)
284 g_return_val_if_fail (d != NULL, G_DATE_BAD_YEAR);
285 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
289 g_date_update_dmy (d);
291 g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
297 g_date_get_day (const GDate *d)
299 g_return_val_if_fail (d != NULL, G_DATE_BAD_DAY);
300 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
304 g_date_update_dmy (d);
306 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
312 g_date_get_julian (const GDate *d)
314 g_return_val_if_fail (d != NULL, G_DATE_BAD_JULIAN);
315 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
319 g_date_update_julian (d);
321 g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
323 return d->julian_days;
327 g_date_get_day_of_year (const GDate *d)
331 g_return_val_if_fail (d != NULL, 0);
332 g_return_val_if_fail (g_date_valid (d), 0);
336 g_date_update_dmy (d);
338 g_return_val_if_fail (d->dmy, 0);
340 index = g_date_is_leap_year (d->year) ? 1 : 0;
342 return (days_in_year[index][d->month] + d->day);
346 g_date_get_monday_week_of_year (const GDate *d)
352 g_return_val_if_fail (d != NULL, 0);
353 g_return_val_if_fail (g_date_valid (d), 0);
357 g_date_update_dmy (d);
359 g_return_val_if_fail (d->dmy, 0);
361 g_date_clear (&first, 1);
363 g_date_set_dmy (&first, 1, 1, d->year);
365 wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
366 day = g_date_get_day_of_year (d) - 1;
368 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
372 g_date_get_sunday_week_of_year (const GDate *d)
378 g_return_val_if_fail (d != NULL, 0);
379 g_return_val_if_fail (g_date_valid (d), 0);
383 g_date_update_dmy (d);
385 g_return_val_if_fail (d->dmy, 0);
387 g_date_clear (&first, 1);
389 g_date_set_dmy (&first, 1, 1, d->year);
391 wd = g_date_get_weekday (&first);
392 if (wd == 7) wd = 0; /* make Sunday day 0 */
393 day = g_date_get_day_of_year (d) - 1;
395 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
399 * g_date_get_iso8601_week_of_year:
400 * @date: a valid #GDate
402 * Returns the week of the year, where weeks are interpreted according
405 * Returns: ISO 8601 week number of the year.
410 g_date_get_iso8601_week_of_year (const GDate *d)
412 guint j, d4, L, d1, w;
414 g_return_val_if_fail (d != NULL, 0);
415 g_return_val_if_fail (g_date_valid (d), 0);
418 g_date_update_julian (d);
419 g_return_val_if_fail (d->julian, 0);
421 /* Formula taken from the Calendar FAQ; the formula was for the
422 * Julian Period which starts on 1 January 4713 BC, so we add
423 * 1,721,425 to the number of days before doing the formula.
425 j = d->julian + 1721425;
426 d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
428 d1 = ((d4 - L) % 365) + L;
435 g_date_days_between (const GDate *d1,
438 g_return_val_if_fail (d1 != NULL, 0);
439 g_return_val_if_fail (d2 != NULL, 0);
441 g_return_val_if_fail (g_date_valid (d1), 0);
442 g_return_val_if_fail (g_date_valid (d2), 0);
444 return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
448 g_date_clear (GDate *d, guint ndates)
450 g_return_if_fail (d != NULL);
451 g_return_if_fail (ndates != 0);
453 memset (d, 0x0, ndates*sizeof (GDate));
456 G_LOCK_DEFINE_STATIC (g_date_global);
458 /* These are for the parser, output to the user should use *
459 * g_date_strftime () - this creates more never-freed memory to annoy
460 * all those memory debugger users. :-)
463 static gchar *long_month_names[13] =
465 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
468 static gchar *short_month_names[13] =
470 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
473 /* This tells us if we need to update the parse info */
474 static gchar *current_locale = NULL;
476 /* order of these in the current locale */
477 static GDateDMY dmy_order[3] =
479 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
482 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
483 * 29 and below are counted as in the year 2000, numbers 30 and above
484 * are counted as in the year 1900.
487 static GDateYear twodigit_start_year = 1930;
489 /* It is impossible to enter a year between 1 AD and 99 AD with this
492 static gboolean using_twodigit_years = FALSE;
494 /* Adjustment of locale era to AD, non-zero means using locale era
496 static gint locale_era_adjust = 0;
498 struct _GDateParseTokens {
504 typedef struct _GDateParseTokens GDateParseTokens;
508 /* HOLDS: g_date_global_lock */
510 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
512 gchar num[4][NUM_LEN+1];
516 /* We count 4, but store 3; so we can give an error
519 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
521 s = (const guchar *) str;
523 while (*s && pt->num_ints < 4)
527 while (*s && g_ascii_isdigit (*s) && i <= NUM_LEN)
529 num[pt->num_ints][i] = *s;
536 num[pt->num_ints][i] = '\0';
540 if (*s == '\0') break;
545 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
546 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
547 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
549 pt->month = G_DATE_BAD_MONTH;
551 if (pt->num_ints < 3)
556 casefold = g_utf8_casefold (str, -1);
557 normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
563 if (long_month_names[i] != NULL)
565 const gchar *found = strstr (normalized, long_month_names[i]);
574 if (short_month_names[i] != NULL)
576 const gchar *found = strstr (normalized, short_month_names[i]);
592 /* HOLDS: g_date_global_lock */
594 g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
596 const gchar *locale = setlocale (LC_TIME, NULL);
597 gboolean recompute_localeinfo = FALSE;
600 g_return_if_fail (locale != NULL); /* should not happen */
602 g_date_clear (&d, 1); /* clear for scratch use */
604 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
606 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
609 if (recompute_localeinfo)
612 GDateParseTokens testpt;
615 g_free (current_locale); /* still works if current_locale == NULL */
617 current_locale = g_strdup (locale);
623 g_date_set_dmy (&d, 1, i, 1);
625 g_return_if_fail (g_date_valid (&d));
627 g_date_strftime (buf, 127, "%b", &d);
629 casefold = g_utf8_casefold (buf, -1);
630 g_free (short_month_names[i]);
631 short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
634 g_date_strftime (buf, 127, "%B", &d);
635 casefold = g_utf8_casefold (buf, -1);
636 g_free (long_month_names[i]);
637 long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
643 /* Determine DMY order */
645 /* had to pick a random day - don't change this, some strftimes
646 * are broken on some days, and this one is good so far. */
647 g_date_set_dmy (&d, 4, 7, 1976);
649 g_date_strftime (buf, 127, "%x", &d);
651 g_date_fill_parse_tokens (buf, &testpt);
654 while (i < testpt.num_ints)
659 dmy_order[i] = G_DATE_MONTH;
662 dmy_order[i] = G_DATE_DAY;
665 using_twodigit_years = TRUE; /* FALL THRU */
667 dmy_order[i] = G_DATE_YEAR;
670 /* assume locale era */
671 locale_era_adjust = 1976 - testpt.n[i];
672 dmy_order[i] = G_DATE_YEAR;
678 #ifdef G_ENABLE_DEBUG
679 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
683 DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
686 if (using_twodigit_years)
687 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
693 switch (dmy_order[i])
696 strings[i] = "Month";
710 DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
711 DEBUG_MSG (("**Sample date in this locale: `%s'", buf));
716 g_date_fill_parse_tokens (str, pt);
720 g_date_set_parse (GDate *d,
724 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
726 g_return_if_fail (d != NULL);
731 G_LOCK (g_date_global);
733 g_date_prepare_to_parse (str, &pt);
735 DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d",
736 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
739 if (pt.num_ints == 4)
741 G_UNLOCK (g_date_global);
742 return; /* presumably a typo; bail out. */
750 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
752 while (i < pt.num_ints && j < 3)
754 switch (dmy_order[j])
758 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
761 ++j; /* skip months, but don't skip this number */
770 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
773 ++j; /* skip days, since we may have month/year */
783 if (locale_era_adjust != 0)
785 y += locale_era_adjust;
787 else if (using_twodigit_years && y < 100)
789 guint two = twodigit_start_year % 100;
790 guint century = (twodigit_start_year / 100) * 100;
808 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
815 if (using_twodigit_years && y < 100)
816 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
818 else if (pt.num_ints == 2)
820 if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
826 else if (pt.num_ints == 1)
828 if (pt.month != G_DATE_BAD_MONTH)
830 /* Month name and year? */
837 /* Try yyyymmdd and yymmdd */
839 m = (pt.n[0]/100) % 100;
843 /* FIXME move this into a separate function */
844 if (using_twodigit_years && y < 100)
846 guint two = twodigit_start_year % 100;
847 guint century = (twodigit_start_year / 100) * 100;
857 /* See if we got anything valid out of all this. */
858 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
859 if (y < 8000 && g_date_valid_dmy (day, m, y))
866 #ifdef G_ENABLE_DEBUG
868 DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
870 G_UNLOCK (g_date_global);
874 g_date_set_time (GDate *d,
880 g_return_if_fail (d != NULL);
882 #ifdef HAVE_LOCALTIME_R
883 localtime_r (&t, &tm);
886 struct tm *ptm = localtime (&t);
890 /* Happens at least in Microsoft's C library if you pass a
891 * negative time_t. Use 2000-01-01 as default date.
893 #ifndef G_DISABLE_CHECKS
894 g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
902 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
908 d->month = tm.tm_mon + 1;
910 d->year = tm.tm_year + 1900;
912 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
918 g_date_set_month (GDate *d,
921 g_return_if_fail (d != NULL);
922 g_return_if_fail (g_date_valid_month (m));
924 if (d->julian && !d->dmy) g_date_update_dmy(d);
929 if (g_date_valid_dmy (d->day, d->month, d->year))
936 g_date_set_day (GDate *d,
939 g_return_if_fail (d != NULL);
940 g_return_if_fail (g_date_valid_day (day));
942 if (d->julian && !d->dmy) g_date_update_dmy(d);
947 if (g_date_valid_dmy (d->day, d->month, d->year))
954 g_date_set_year (GDate *d,
957 g_return_if_fail (d != NULL);
958 g_return_if_fail (g_date_valid_year (y));
960 if (d->julian && !d->dmy) g_date_update_dmy(d);
965 if (g_date_valid_dmy (d->day, d->month, d->year))
972 g_date_set_dmy (GDate *d,
977 g_return_if_fail (d != NULL);
978 g_return_if_fail (g_date_valid_dmy (day, m, y));
990 g_date_set_julian (GDate *d, guint32 j)
992 g_return_if_fail (d != NULL);
993 g_return_if_fail (g_date_valid_julian (j));
1002 g_date_is_first_of_month (const GDate *d)
1004 g_return_val_if_fail (d != NULL, FALSE);
1005 g_return_val_if_fail (g_date_valid (d), FALSE);
1009 g_date_update_dmy (d);
1011 g_return_val_if_fail (d->dmy, FALSE);
1013 if (d->day == 1) return TRUE;
1018 g_date_is_last_of_month (const GDate *d)
1022 g_return_val_if_fail (d != NULL, FALSE);
1023 g_return_val_if_fail (g_date_valid (d), FALSE);
1027 g_date_update_dmy (d);
1029 g_return_val_if_fail (d->dmy, FALSE);
1031 index = g_date_is_leap_year (d->year) ? 1 : 0;
1033 if (d->day == days_in_months[index][d->month]) return TRUE;
1038 g_date_add_days (GDate *d, guint ndays)
1040 g_return_if_fail (d != NULL);
1041 g_return_if_fail (g_date_valid (d));
1045 g_date_update_julian (d);
1047 g_return_if_fail (d->julian);
1049 d->julian_days += ndays;
1054 g_date_subtract_days (GDate *d, guint ndays)
1056 g_return_if_fail (d != NULL);
1057 g_return_if_fail (g_date_valid (d));
1061 g_date_update_julian (d);
1063 g_return_if_fail (d->julian);
1064 g_return_if_fail (d->julian_days > ndays);
1066 d->julian_days -= ndays;
1071 g_date_add_months (GDate *d,
1074 guint years, months;
1077 g_return_if_fail (d != NULL);
1078 g_return_if_fail (g_date_valid (d));
1082 g_date_update_dmy (d);
1084 g_return_if_fail (d->dmy);
1086 nmonths += d->month - 1;
1089 months = nmonths%12;
1091 d->month = months + 1;
1094 index = g_date_is_leap_year (d->year) ? 1 : 0;
1096 if (d->day > days_in_months[index][d->month])
1097 d->day = days_in_months[index][d->month];
1101 g_return_if_fail (g_date_valid (d));
1105 g_date_subtract_months (GDate *d,
1108 guint years, months;
1111 g_return_if_fail (d != NULL);
1112 g_return_if_fail (g_date_valid (d));
1116 g_date_update_dmy (d);
1118 g_return_if_fail (d->dmy);
1121 months = nmonths%12;
1123 g_return_if_fail (d->year > years);
1127 if (d->month > months) d->month -= months;
1131 d->month = 12 - months;
1135 index = g_date_is_leap_year (d->year) ? 1 : 0;
1137 if (d->day > days_in_months[index][d->month])
1138 d->day = days_in_months[index][d->month];
1142 g_return_if_fail (g_date_valid (d));
1146 g_date_add_years (GDate *d,
1149 g_return_if_fail (d != NULL);
1150 g_return_if_fail (g_date_valid (d));
1154 g_date_update_dmy (d);
1156 g_return_if_fail (d->dmy);
1160 if (d->month == 2 && d->day == 29)
1162 if (!g_date_is_leap_year (d->year))
1172 g_date_subtract_years (GDate *d,
1175 g_return_if_fail (d != NULL);
1176 g_return_if_fail (g_date_valid (d));
1180 g_date_update_dmy (d);
1182 g_return_if_fail (d->dmy);
1183 g_return_if_fail (d->year > nyears);
1187 if (d->month == 2 && d->day == 29)
1189 if (!g_date_is_leap_year (d->year))
1200 g_date_is_leap_year (GDateYear year)
1202 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1204 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1205 (year % 400) == 0 );
1209 g_date_get_days_in_month (GDateMonth month,
1214 g_return_val_if_fail (g_date_valid_year (year), 0);
1215 g_return_val_if_fail (g_date_valid_month (month), 0);
1217 index = g_date_is_leap_year (year) ? 1 : 0;
1219 return days_in_months[index][month];
1223 g_date_get_monday_weeks_in_year (GDateYear year)
1227 g_return_val_if_fail (g_date_valid_year (year), 0);
1229 g_date_clear (&d, 1);
1230 g_date_set_dmy (&d, 1, 1, year);
1231 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1232 g_date_set_dmy (&d, 31, 12, year);
1233 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1234 if (g_date_is_leap_year (year))
1236 g_date_set_dmy (&d, 2, 1, year);
1237 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1238 g_date_set_dmy (&d, 30, 12, year);
1239 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1245 g_date_get_sunday_weeks_in_year (GDateYear year)
1249 g_return_val_if_fail (g_date_valid_year (year), 0);
1251 g_date_clear (&d, 1);
1252 g_date_set_dmy (&d, 1, 1, year);
1253 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1254 g_date_set_dmy (&d, 31, 12, year);
1255 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1256 if (g_date_is_leap_year (year))
1258 g_date_set_dmy (&d, 2, 1, year);
1259 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1260 g_date_set_dmy (&d, 30, 12, year);
1261 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1267 g_date_compare (const GDate *lhs,
1270 g_return_val_if_fail (lhs != NULL, 0);
1271 g_return_val_if_fail (rhs != NULL, 0);
1272 g_return_val_if_fail (g_date_valid (lhs), 0);
1273 g_return_val_if_fail (g_date_valid (rhs), 0);
1275 /* Remember the self-comparison case! I think it works right now. */
1280 if (lhs->julian && rhs->julian)
1282 if (lhs->julian_days < rhs->julian_days) return -1;
1283 else if (lhs->julian_days > rhs->julian_days) return 1;
1286 else if (lhs->dmy && rhs->dmy)
1288 if (lhs->year < rhs->year) return -1;
1289 else if (lhs->year > rhs->year) return 1;
1292 if (lhs->month < rhs->month) return -1;
1293 else if (lhs->month > rhs->month) return 1;
1296 if (lhs->day < rhs->day) return -1;
1297 else if (lhs->day > rhs->day) return 1;
1306 if (!lhs->julian) g_date_update_julian (lhs);
1307 if (!rhs->julian) g_date_update_julian (rhs);
1308 g_return_val_if_fail (lhs->julian, 0);
1309 g_return_val_if_fail (rhs->julian, 0);
1313 return 0; /* warnings */
1318 g_date_to_struct_tm (const GDate *d,
1323 g_return_if_fail (d != NULL);
1324 g_return_if_fail (g_date_valid (d));
1325 g_return_if_fail (tm != NULL);
1329 g_date_update_dmy (d);
1331 g_return_if_fail (d->dmy);
1333 /* zero all the irrelevant fields to be sure they're valid */
1335 /* On Linux and maybe other systems, there are weird non-POSIX
1336 * fields on the end of struct tm that choke strftime if they
1337 * contain garbage. So we need to 0 the entire struct, not just the
1338 * fields we know to exist.
1341 memset (tm, 0x0, sizeof (struct tm));
1343 tm->tm_mday = d->day;
1344 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1345 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1347 day = g_date_get_weekday (d);
1348 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1350 tm->tm_wday = (int)day;
1352 tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
1353 tm->tm_isdst = -1; /* -1 means "information not available" */
1357 g_date_clamp (GDate *date,
1358 const GDate *min_date,
1359 const GDate *max_date)
1361 g_return_if_fail (date);
1362 g_return_if_fail (g_date_valid (date));
1363 if (min_date != NULL)
1364 g_return_if_fail (g_date_valid (min_date));
1365 if (max_date != NULL)
1366 g_return_if_fail (g_date_valid (max_date));
1367 if (min_date != NULL && max_date != NULL)
1368 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
1370 if (min_date && g_date_compare (date, min_date) < 0)
1373 if (max_date && g_date_compare (max_date, date) < 0)
1378 g_date_order (GDate *date1,
1381 g_return_if_fail (date1 != NULL);
1382 g_return_if_fail (date2 != NULL);
1383 g_return_if_fail (g_date_valid (date1));
1384 g_return_if_fail (g_date_valid (date2));
1386 if (g_date_compare (date1, date2) > 0)
1395 g_date_strftime (gchar *s,
1397 const gchar *format,
1401 gsize locale_format_len = 0;
1402 gchar *locale_format;
1408 GError *error = NULL;
1411 g_return_val_if_fail (d != NULL, 0);
1412 g_return_val_if_fail (g_date_valid (d), 0);
1413 g_return_val_if_fail (slen > 0, 0);
1414 g_return_val_if_fail (format != 0, 0);
1415 g_return_val_if_fail (s != 0, 0);
1417 g_date_to_struct_tm (d, &tm);
1419 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
1423 g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
1424 g_error_free (error);
1430 tmpbufsize = MAX (128, locale_format_len * 2);
1433 tmpbuf = g_malloc (tmpbufsize);
1435 /* Set the first byte to something other than '\0', to be able to
1436 * recognize whether strftime actually failed or just returned "".
1439 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
1441 if (tmplen == 0 && tmpbuf[0] != '\0')
1446 if (tmpbufsize > 65536)
1448 g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
1449 g_free (locale_format);
1458 g_free (locale_format);
1460 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
1465 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
1466 g_error_free (error);
1472 if (slen <= convlen)
1474 /* Ensure only whole characters are copied into the buffer.
1476 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1477 g_assert (end != NULL);
1478 convlen = end - convbuf;
1480 /* Return 0 because the buffer isn't large enough.
1487 memcpy (s, convbuf, convlen);