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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
39 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
45 g_date_new_dmy (GDateDay day, GDateMonth m, GDateYear y)
48 g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
59 g_assert (g_date_valid (d));
65 g_date_new_julian (guint32 j)
68 g_return_val_if_fail (g_date_valid_julian (j), NULL);
77 g_assert (g_date_valid (d));
83 g_date_free (GDate *d)
85 g_return_if_fail (d != NULL);
91 g_date_valid (GDate *d)
93 g_return_val_if_fail (d != NULL, FALSE);
95 return (d->julian || d->dmy);
98 static const guint8 days_in_months[2][13] =
99 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
100 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
101 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
104 static const guint16 days_in_year[2][14] =
105 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
106 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
107 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
111 g_date_valid_month (GDateMonth m)
113 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
117 g_date_valid_year (GDateYear y)
119 return ( y > G_DATE_BAD_YEAR );
123 g_date_valid_day (GDateDay d)
125 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
129 g_date_valid_weekday (GDateWeekday w)
131 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
135 g_date_valid_julian (guint32 j)
137 return (j > G_DATE_BAD_JULIAN);
141 g_date_valid_dmy (GDateDay d,
145 return ( (m > G_DATE_BAD_MONTH) &&
147 (d > G_DATE_BAD_DAY) &&
148 (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
149 (d <= (g_date_is_leap_year (y) ?
150 days_in_months[1][m] : days_in_months[0][m])) );
154 /* "Julian days" just means an absolute number of days, where Day 1 ==
158 g_date_update_julian (GDate *d)
163 g_return_if_fail (d != NULL);
164 g_return_if_fail (d->dmy);
165 g_return_if_fail (!d->julian);
166 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
168 /* What we actually do is: multiply years * 365 days in the year,
169 * add the number of years divided by 4, subtract the number of
170 * years divided by 100 and add the number of years divided by 400,
171 * which accounts for leap year stuff. Code from Steffen Beyer's
175 year = d->year - 1; /* we know d->year > 0 since it's valid */
177 d->julian_days = year * 365U;
178 d->julian_days += (year >>= 2); /* divide by 4 and add */
179 d->julian_days -= (year /= 25); /* divides original # years by 100 */
180 d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
182 index = g_date_is_leap_year (d->year) ? 1 : 0;
184 d->julian_days += days_in_year[index][d->month] + d->day;
186 g_return_if_fail (g_date_valid_julian (d->julian_days));
192 g_date_update_dmy (GDate *d)
198 guint32 A, B, C, D, E, M;
200 g_return_if_fail (d != NULL);
201 g_return_if_fail (d->julian);
202 g_return_if_fail (!d->dmy);
203 g_return_if_fail (g_date_valid_julian (d->julian_days));
205 /* Formula taken from the Calendar FAQ; the formula was for the
206 * Julian Period which starts on 1 January 4713 BC, so we add
207 * 1,721,425 to the number of days before doing the formula.
209 * I'm sure this can be simplified for our 1 January 1 AD period
210 * start, but I can't figure out how to unpack the formula.
213 A = d->julian_days + 1721425 + 32045;
214 B = ( 4 *(A + 36524) )/ 146097 - 1;
215 C = A - (146097 * B)/4;
216 D = ( 4 * (C + 365) ) / 1461 - 1;
217 E = C - ((1461*D) / 4);
218 M = (5 * (E - 1) + 2)/153;
220 m = M + 3 - (12*(M/10));
221 day = E - (153*M + 2)/5;
222 y = 100 * B + D - 4800 + (M/10);
224 #ifdef G_ENABLE_DEBUG
225 if (!g_date_valid_dmy (day, m, y))
227 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
228 d->julian_days, day, m, y);
240 g_date_weekday (GDate *d)
242 g_return_val_if_fail (d != NULL, G_DATE_BAD_WEEKDAY);
243 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
247 g_date_update_julian (d);
249 g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
251 return ((d->julian_days - 1) % 7) + 1;
255 g_date_month (GDate *d)
257 g_return_val_if_fail (d != NULL, G_DATE_BAD_MONTH);
258 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
262 g_date_update_dmy (d);
264 g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
270 g_date_year (GDate *d)
272 g_return_val_if_fail (d != NULL, G_DATE_BAD_YEAR);
273 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
277 g_date_update_dmy (d);
279 g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
285 g_date_day (GDate *d)
287 g_return_val_if_fail (d != NULL, G_DATE_BAD_DAY);
288 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
292 g_date_update_dmy (d);
294 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
300 g_date_julian (GDate *d)
302 g_return_val_if_fail (d != NULL, G_DATE_BAD_JULIAN);
303 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
307 g_date_update_julian (d);
309 g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
311 return d->julian_days;
315 g_date_day_of_year (GDate *d)
319 g_return_val_if_fail (d != NULL, 0);
320 g_return_val_if_fail (g_date_valid (d), 0);
324 g_date_update_dmy (d);
326 g_return_val_if_fail (d->dmy, 0);
328 index = g_date_is_leap_year (d->year) ? 1 : 0;
330 return (days_in_year[index][d->month] + d->day);
334 g_date_monday_week_of_year (GDate *d)
340 g_return_val_if_fail (d != NULL, 0);
341 g_return_val_if_fail (g_date_valid (d), 0);
345 g_date_update_dmy (d);
347 g_return_val_if_fail (d->dmy, 0);
349 g_date_clear (&first, 1);
351 g_date_set_dmy (&first, 1, 1, d->year);
353 wd = g_date_weekday (&first) - 1; /* make Monday day 0 */
354 day = g_date_day_of_year (d) - 1;
356 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
360 g_date_sunday_week_of_year (GDate *d)
366 g_return_val_if_fail (d != NULL, 0);
367 g_return_val_if_fail (g_date_valid (d), 0);
371 g_date_update_dmy (d);
373 g_return_val_if_fail (d->dmy, 0);
375 g_date_clear (&first, 1);
377 g_date_set_dmy (&first, 1, 1, d->year);
379 wd = g_date_weekday (&first);
380 if (wd == 7) wd = 0; /* make Sunday day 0 */
381 day = g_date_day_of_year (d) - 1;
383 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
387 g_date_clear (GDate *d, guint ndates)
389 g_return_if_fail (d != NULL);
390 g_return_if_fail (ndates != 0);
392 memset (d, 0x0, ndates*sizeof (GDate));
395 G_LOCK_DEFINE_STATIC (g_date_global);
397 /* These are for the parser, output to the user should use *
398 * g_date_strftime () - this creates more never-freed memory to annoy
399 * all those memory debugger users. :-)
402 static gchar *long_month_names[13] =
404 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
407 static gchar *short_month_names[13] =
409 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
412 /* This tells us if we need to update the parse info */
413 static gchar *current_locale = NULL;
415 /* order of these in the current locale */
416 static GDateDMY dmy_order[3] =
418 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
421 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
422 * 29 and below are counted as in the year 2000, numbers 30 and above
423 * are counted as in the year 1900.
426 static GDateYear twodigit_start_year = 1930;
428 /* It is impossible to enter a year between 1 AD and 99 AD with this
431 static gboolean using_twodigit_years = FALSE;
433 struct _GDateParseTokens {
439 typedef struct _GDateParseTokens GDateParseTokens;
443 /* HOLDS: g_date_global_lock */
445 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
447 gchar num[4][NUM_LEN+1];
451 /* We count 4, but store 3; so we can give an error
454 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
458 while (*s && pt->num_ints < 4)
462 while (*s && isdigit (*s) && i <= NUM_LEN)
464 num[pt->num_ints][i] = *s;
471 num[pt->num_ints][i] = '\0';
475 if (*s == '\0') break;
480 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
481 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
482 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
484 pt->month = G_DATE_BAD_MONTH;
486 if (pt->num_ints < 3)
491 strncpy (lcstr, str, 127);
496 if (long_month_names[i] != NULL)
498 const gchar *found = strstr (lcstr, long_month_names[i]);
507 if (short_month_names[i] != NULL)
509 const gchar *found = strstr (lcstr, short_month_names[i]);
523 /* HOLDS: g_date_global_lock */
525 g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
527 const gchar *locale = setlocale (LC_TIME, NULL);
528 gboolean recompute_localeinfo = FALSE;
531 g_return_if_fail (locale != NULL); /* should not happen */
533 g_date_clear (&d, 1); /* clear for scratch use */
535 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
537 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
540 if (recompute_localeinfo)
543 GDateParseTokens testpt;
546 g_free (current_locale); /* still works if current_locale == NULL */
548 current_locale = g_strdup (locale);
552 g_date_set_dmy (&d, 1, i, 1);
554 g_return_if_fail (g_date_valid (&d));
556 g_date_strftime (buf, 127, "%b", &d);
557 g_free (short_month_names[i]);
559 short_month_names[i] = g_strdup (buf);
563 g_date_strftime (buf, 127, "%B", &d);
564 g_free (long_month_names[i]);
566 long_month_names[i] = g_strdup (buf);
571 /* Determine DMY order */
573 /* had to pick a random day - don't change this, some strftimes
574 * are broken on some days, and this one is good so far. */
575 g_date_set_dmy (&d, 4, 7, 1976);
577 g_date_strftime (buf, 127, "%x", &d);
579 g_date_fill_parse_tokens (buf, &testpt);
582 while (i < testpt.num_ints)
587 dmy_order[i] = G_DATE_MONTH;
590 dmy_order[i] = G_DATE_DAY;
593 using_twodigit_years = TRUE; /* FALL THRU */
595 dmy_order[i] = G_DATE_YEAR;
598 /* leave it unchanged */
604 #ifdef G_ENABLE_DEBUG
605 g_message ("**GDate prepared a new set of locale-specific parse rules.");
609 g_message (" %s %s", long_month_names[i], short_month_names[i]);
612 if (using_twodigit_years)
614 g_message ("**Using twodigit years with cutoff year: %u", twodigit_start_year);
621 switch (dmy_order[i])
624 strings[i] = "Month";
638 g_message ("**Order: %s, %s, %s", strings[0], strings[1], strings[2]);
639 g_message ("**Sample date in this locale: `%s'", buf);
644 g_date_fill_parse_tokens (str, pt);
648 g_date_set_parse (GDate *d,
652 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
654 g_return_if_fail (d != NULL);
659 G_LOCK (g_date_global);
661 g_date_prepare_to_parse (str, &pt);
663 #ifdef G_ENABLE_DEBUG
664 g_message ("Found %d ints, `%d' `%d' `%d' and written out month %d",
665 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month);
669 if (pt.num_ints == 4)
671 G_UNLOCK (g_date_global);
672 return; /* presumably a typo; bail out. */
680 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
682 while (i < pt.num_ints && j < 3)
684 switch (dmy_order[j])
688 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
691 ++j; /* skip months, but don't skip this number */
700 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
703 ++j; /* skip days, since we may have month/year */
713 if (using_twodigit_years && y < 100)
715 guint two = twodigit_start_year % 100;
716 guint century = (twodigit_start_year / 100) * 100;
734 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
741 if (using_twodigit_years && y < 100)
742 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
745 else if (pt.num_ints == 1)
747 if (pt.month != G_DATE_BAD_MONTH)
749 /* Month name and year? */
756 /* Try yyyymmdd and yymmdd */
758 m = (pt.n[0]/100) % 100;
762 /* FIXME move this into a separate function */
763 if (using_twodigit_years && y < 100)
765 guint two = twodigit_start_year % 100;
766 guint century = (twodigit_start_year / 100) * 100;
776 /* See if we got anything valid out of all this. */
777 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
778 if (y < 8000 && g_date_valid_dmy (day, m, y))
785 #ifdef G_ENABLE_DEBUG
787 g_message ("Rejected DMY %u %u %u", day, m, y);
789 G_UNLOCK (g_date_global);
793 g_date_set_time (GDate *d,
799 g_return_if_fail (d != NULL);
801 #ifdef HAVE_LOCALTIME_R
802 localtime_r (&t, &tm);
805 struct tm *ptm = localtime (&t);
807 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
813 d->month = tm.tm_mon + 1;
815 d->year = tm.tm_year + 1900;
817 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
823 g_date_set_month (GDate *d,
826 g_return_if_fail (d != NULL);
827 g_return_if_fail (g_date_valid_month (m));
829 if (d->julian && !d->dmy) g_date_update_dmy(d);
834 if (g_date_valid_dmy (d->day, d->month, d->year))
841 g_date_set_day (GDate *d,
844 g_return_if_fail (d != NULL);
845 g_return_if_fail (g_date_valid_day (day));
847 if (d->julian && !d->dmy) g_date_update_dmy(d);
852 if (g_date_valid_dmy (d->day, d->month, d->year))
859 g_date_set_year (GDate *d,
862 g_return_if_fail (d != NULL);
863 g_return_if_fail (g_date_valid_year (y));
865 if (d->julian && !d->dmy) g_date_update_dmy(d);
870 if (g_date_valid_dmy (d->day, d->month, d->year))
877 g_date_set_dmy (GDate *d,
882 g_return_if_fail (d != NULL);
883 g_return_if_fail (g_date_valid_dmy (day, m, y));
895 g_date_set_julian (GDate *d, guint32 j)
897 g_return_if_fail (d != NULL);
898 g_return_if_fail (g_date_valid_julian (j));
907 g_date_is_first_of_month (GDate *d)
909 g_return_val_if_fail (d != NULL, FALSE);
910 g_return_val_if_fail (g_date_valid (d), FALSE);
914 g_date_update_dmy (d);
916 g_return_val_if_fail (d->dmy, FALSE);
918 if (d->day == 1) return TRUE;
923 g_date_is_last_of_month (GDate *d)
927 g_return_val_if_fail (d != NULL, FALSE);
928 g_return_val_if_fail (g_date_valid (d), FALSE);
932 g_date_update_dmy (d);
934 g_return_val_if_fail (d->dmy, FALSE);
936 index = g_date_is_leap_year (d->year) ? 1 : 0;
938 if (d->day == days_in_months[index][d->month]) return TRUE;
943 g_date_add_days (GDate *d, guint ndays)
945 g_return_if_fail (d != NULL);
946 g_return_if_fail (g_date_valid (d));
950 g_date_update_julian (d);
952 g_return_if_fail (d->julian);
954 d->julian_days += ndays;
959 g_date_subtract_days (GDate *d, guint ndays)
961 g_return_if_fail (d != NULL);
962 g_return_if_fail (g_date_valid (d));
966 g_date_update_julian (d);
968 g_return_if_fail (d->julian);
969 g_return_if_fail (d->julian_days > ndays);
971 d->julian_days -= ndays;
976 g_date_add_months (GDate *d,
982 g_return_if_fail (d != NULL);
983 g_return_if_fail (g_date_valid (d));
987 g_date_update_dmy (d);
989 g_return_if_fail (d->dmy);
991 nmonths += d->month - 1;
996 d->month = months + 1;
999 index = g_date_is_leap_year (d->year) ? 1 : 0;
1001 if (d->day > days_in_months[index][d->month])
1002 d->day = days_in_months[index][d->month];
1006 g_return_if_fail (g_date_valid (d));
1010 g_date_subtract_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);
1026 months = nmonths%12;
1028 g_return_if_fail (d->year > years);
1032 if (d->month > months) d->month -= months;
1036 d->month = 12 - months;
1040 index = g_date_is_leap_year (d->year) ? 1 : 0;
1042 if (d->day > days_in_months[index][d->month])
1043 d->day = days_in_months[index][d->month];
1047 g_return_if_fail (g_date_valid (d));
1051 g_date_add_years (GDate *d,
1054 g_return_if_fail (d != NULL);
1055 g_return_if_fail (g_date_valid (d));
1059 g_date_update_dmy (d);
1061 g_return_if_fail (d->dmy);
1065 if (d->month == 2 && d->day == 29)
1067 if (!g_date_is_leap_year (d->year))
1077 g_date_subtract_years (GDate *d,
1080 g_return_if_fail (d != NULL);
1081 g_return_if_fail (g_date_valid (d));
1085 g_date_update_dmy (d);
1087 g_return_if_fail (d->dmy);
1088 g_return_if_fail (d->year > nyears);
1092 if (d->month == 2 && d->day == 29)
1094 if (!g_date_is_leap_year (d->year))
1105 g_date_is_leap_year (GDateYear year)
1107 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1109 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1110 (year % 400) == 0 );
1114 g_date_days_in_month (GDateMonth month,
1119 g_return_val_if_fail (g_date_valid_year (year), 0);
1120 g_return_val_if_fail (g_date_valid_month (month), 0);
1122 index = g_date_is_leap_year (year) ? 1 : 0;
1124 return days_in_months[index][month];
1128 g_date_monday_weeks_in_year (GDateYear year)
1132 g_return_val_if_fail (g_date_valid_year (year), 0);
1134 g_date_clear (&d, 1);
1135 g_date_set_dmy (&d, 1, 1, year);
1136 if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
1137 g_date_set_dmy (&d, 31, 12, year);
1138 if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
1139 if (g_date_is_leap_year (year))
1141 g_date_set_dmy (&d, 2, 1, year);
1142 if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
1143 g_date_set_dmy (&d, 30, 12, year);
1144 if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
1150 g_date_sunday_weeks_in_year (GDateYear year)
1154 g_return_val_if_fail (g_date_valid_year (year), 0);
1156 g_date_clear (&d, 1);
1157 g_date_set_dmy (&d, 1, 1, year);
1158 if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
1159 g_date_set_dmy (&d, 31, 12, year);
1160 if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
1161 if (g_date_is_leap_year (year))
1163 g_date_set_dmy (&d, 2, 1, year);
1164 if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
1165 g_date_set_dmy (&d, 30, 12, year);
1166 if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
1172 g_date_compare (GDate *lhs,
1175 g_return_val_if_fail (lhs != NULL, 0);
1176 g_return_val_if_fail (rhs != NULL, 0);
1177 g_return_val_if_fail (g_date_valid (lhs), 0);
1178 g_return_val_if_fail (g_date_valid (rhs), 0);
1180 /* Remember the self-comparison case! I think it works right now. */
1185 if (lhs->julian && rhs->julian)
1187 if (lhs->julian_days < rhs->julian_days) return -1;
1188 else if (lhs->julian_days > rhs->julian_days) return 1;
1191 else if (lhs->dmy && rhs->dmy)
1193 if (lhs->year < rhs->year) return -1;
1194 else if (lhs->year > rhs->year) return 1;
1197 if (lhs->month < rhs->month) return -1;
1198 else if (lhs->month > rhs->month) return 1;
1201 if (lhs->day < rhs->day) return -1;
1202 else if (lhs->day > rhs->day) return 1;
1211 if (!lhs->julian) g_date_update_julian (lhs);
1212 if (!rhs->julian) g_date_update_julian (rhs);
1213 g_return_val_if_fail (lhs->julian, 0);
1214 g_return_val_if_fail (rhs->julian, 0);
1218 return 0; /* warnings */
1223 g_date_to_struct_tm (GDate *d,
1228 g_return_if_fail (d != NULL);
1229 g_return_if_fail (g_date_valid (d));
1230 g_return_if_fail (tm != NULL);
1234 g_date_update_dmy (d);
1236 g_return_if_fail (d->dmy);
1238 /* zero all the irrelevant fields to be sure they're valid */
1240 /* On Linux and maybe other systems, there are weird non-POSIX
1241 * fields on the end of struct tm that choke strftime if they
1242 * contain garbage. So we need to 0 the entire struct, not just the
1243 * fields we know to exist.
1246 memset (tm, 0x0, sizeof (struct tm));
1248 tm->tm_mday = d->day;
1249 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1250 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1252 day = g_date_weekday (d);
1253 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1255 tm->tm_wday = (int)day;
1257 tm->tm_yday = g_date_day_of_year (d) - 1; /* 0 to 365 */
1258 tm->tm_isdst = -1; /* -1 means "information not available" */
1262 g_date_strftime (gchar *s,
1264 const gchar *format,
1270 g_return_val_if_fail (d != NULL, 0);
1271 g_return_val_if_fail (g_date_valid (d), 0);
1272 g_return_val_if_fail (slen > 0, 0);
1273 g_return_val_if_fail (format != 0, 0);
1274 g_return_val_if_fail (s != 0, 0);
1276 g_date_to_struct_tm (d, &tm);
1278 retval = strftime (s, slen, format, &tm);
1281 /* If retval == 0, the contents of s are undefined. We define