Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / glib / gdate.c
blobb278a5a4ec234feddde47413c2f2631157e74c02
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.
21 * Modified by the GLib Team and others 1997-1999. 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/.
27 /*
28 * MT safe
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include "glib.h"
37 #include <time.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <stdlib.h>
41 #include <locale.h>
43 GDate*
44 g_date_new ()
46 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
48 return d;
51 GDate*
52 g_date_new_dmy (GDateDay day, GDateMonth m, GDateYear y)
54 GDate *d;
55 g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
57 d = g_new (GDate, 1);
59 d->julian = FALSE;
60 d->dmy = TRUE;
62 d->month = m;
63 d->day = day;
64 d->year = y;
66 g_assert (g_date_valid (d));
68 return d;
71 GDate*
72 g_date_new_julian (guint32 j)
74 GDate *d;
75 g_return_val_if_fail (g_date_valid_julian (j), NULL);
77 d = g_new (GDate, 1);
79 d->julian = TRUE;
80 d->dmy = FALSE;
82 d->julian_days = j;
84 g_assert (g_date_valid (d));
86 return d;
89 void
90 g_date_free (GDate *d)
92 g_return_if_fail (d != NULL);
94 g_free (d);
97 gboolean
98 g_date_valid (GDate *d)
100 g_return_val_if_fail (d != NULL, FALSE);
102 return (d->julian || d->dmy);
105 static const guint8 days_in_months[2][13] =
106 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
107 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
108 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
111 static const guint16 days_in_year[2][14] =
112 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
113 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
114 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
117 gboolean
118 g_date_valid_month (GDateMonth m)
120 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
123 gboolean
124 g_date_valid_year (GDateYear y)
126 return ( y > G_DATE_BAD_YEAR );
129 gboolean
130 g_date_valid_day (GDateDay d)
132 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
135 gboolean
136 g_date_valid_weekday (GDateWeekday w)
138 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
141 gboolean
142 g_date_valid_julian (guint32 j)
144 return (j > G_DATE_BAD_JULIAN);
147 gboolean
148 g_date_valid_dmy (GDateDay d,
149 GDateMonth m,
150 GDateYear y)
152 return ( (m > G_DATE_BAD_MONTH) &&
153 (m < 13) &&
154 (d > G_DATE_BAD_DAY) &&
155 (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
156 (d <= (g_date_is_leap_year (y) ?
157 days_in_months[1][m] : days_in_months[0][m])) );
161 /* "Julian days" just means an absolute number of days, where Day 1 ==
162 * Jan 1, Year 1
164 static void
165 g_date_update_julian (GDate *d)
167 GDateYear year;
168 gint index;
170 g_return_if_fail (d != NULL);
171 g_return_if_fail (d->dmy);
172 g_return_if_fail (!d->julian);
173 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
175 /* What we actually do is: multiply years * 365 days in the year,
176 * add the number of years divided by 4, subtract the number of
177 * years divided by 100 and add the number of years divided by 400,
178 * which accounts for leap year stuff. Code from Steffen Beyer's
179 * DateCalc.
182 year = d->year - 1; /* we know d->year > 0 since it's valid */
184 d->julian_days = year * 365U;
185 d->julian_days += (year >>= 2); /* divide by 4 and add */
186 d->julian_days -= (year /= 25); /* divides original # years by 100 */
187 d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
189 index = g_date_is_leap_year (d->year) ? 1 : 0;
191 d->julian_days += days_in_year[index][d->month] + d->day;
193 g_return_if_fail (g_date_valid_julian (d->julian_days));
195 d->julian = TRUE;
198 static void
199 g_date_update_dmy (GDate *d)
201 GDateYear y;
202 GDateMonth m;
203 GDateDay day;
205 guint32 A, B, C, D, E, M;
207 g_return_if_fail (d != NULL);
208 g_return_if_fail (d->julian);
209 g_return_if_fail (!d->dmy);
210 g_return_if_fail (g_date_valid_julian (d->julian_days));
212 /* Formula taken from the Calendar FAQ; the formula was for the
213 * Julian Period which starts on 1 January 4713 BC, so we add
214 * 1,721,425 to the number of days before doing the formula.
216 * I'm sure this can be simplified for our 1 January 1 AD period
217 * start, but I can't figure out how to unpack the formula.
220 A = d->julian_days + 1721425 + 32045;
221 B = ( 4 *(A + 36524) )/ 146097 - 1;
222 C = A - (146097 * B)/4;
223 D = ( 4 * (C + 365) ) / 1461 - 1;
224 E = C - ((1461*D) / 4);
225 M = (5 * (E - 1) + 2)/153;
227 m = M + 3 - (12*(M/10));
228 day = E - (153*M + 2)/5;
229 y = 100 * B + D - 4800 + (M/10);
231 #ifdef G_ENABLE_DEBUG
232 if (!g_date_valid_dmy (day, m, y))
234 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
235 d->julian_days, day, m, y);
237 #endif
239 d->month = m;
240 d->day = day;
241 d->year = y;
243 d->dmy = TRUE;
246 GDateWeekday
247 g_date_weekday (GDate *d)
249 g_return_val_if_fail (d != NULL, G_DATE_BAD_WEEKDAY);
250 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
252 if (!d->julian)
254 g_date_update_julian (d);
256 g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
258 return ((d->julian_days - 1) % 7) + 1;
261 GDateMonth
262 g_date_month (GDate *d)
264 g_return_val_if_fail (d != NULL, G_DATE_BAD_MONTH);
265 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
267 if (!d->dmy)
269 g_date_update_dmy (d);
271 g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
273 return d->month;
276 GDateYear
277 g_date_year (GDate *d)
279 g_return_val_if_fail (d != NULL, G_DATE_BAD_YEAR);
280 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
282 if (!d->dmy)
284 g_date_update_dmy (d);
286 g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
288 return d->year;
291 GDateDay
292 g_date_day (GDate *d)
294 g_return_val_if_fail (d != NULL, G_DATE_BAD_DAY);
295 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
297 if (!d->dmy)
299 g_date_update_dmy (d);
301 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
303 return d->day;
306 guint32
307 g_date_julian (GDate *d)
309 g_return_val_if_fail (d != NULL, G_DATE_BAD_JULIAN);
310 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
312 if (!d->julian)
314 g_date_update_julian (d);
316 g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
318 return d->julian_days;
321 guint
322 g_date_day_of_year (GDate *d)
324 gint index;
326 g_return_val_if_fail (d != NULL, 0);
327 g_return_val_if_fail (g_date_valid (d), 0);
329 if (!d->dmy)
331 g_date_update_dmy (d);
333 g_return_val_if_fail (d->dmy, 0);
335 index = g_date_is_leap_year (d->year) ? 1 : 0;
337 return (days_in_year[index][d->month] + d->day);
340 guint
341 g_date_monday_week_of_year (GDate *d)
343 GDateWeekday wd;
344 guint day;
345 GDate first;
347 g_return_val_if_fail (d != NULL, 0);
348 g_return_val_if_fail (g_date_valid (d), 0);
350 if (!d->dmy)
352 g_date_update_dmy (d);
354 g_return_val_if_fail (d->dmy, 0);
356 g_date_clear (&first, 1);
358 g_date_set_dmy (&first, 1, 1, d->year);
360 wd = g_date_weekday (&first) - 1; /* make Monday day 0 */
361 day = g_date_day_of_year (d) - 1;
363 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
366 guint
367 g_date_sunday_week_of_year (GDate *d)
369 GDateWeekday wd;
370 guint day;
371 GDate first;
373 g_return_val_if_fail (d != NULL, 0);
374 g_return_val_if_fail (g_date_valid (d), 0);
376 if (!d->dmy)
378 g_date_update_dmy (d);
380 g_return_val_if_fail (d->dmy, 0);
382 g_date_clear (&first, 1);
384 g_date_set_dmy (&first, 1, 1, d->year);
386 wd = g_date_weekday (&first);
387 if (wd == 7) wd = 0; /* make Sunday day 0 */
388 day = g_date_day_of_year (d) - 1;
390 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
393 void
394 g_date_clear (GDate *d, guint ndates)
396 g_return_if_fail (d != NULL);
397 g_return_if_fail (ndates != 0);
399 memset (d, 0x0, ndates*sizeof (GDate));
402 G_LOCK_DEFINE_STATIC (g_date_global);
404 /* These are for the parser, output to the user should use *
405 * g_date_strftime () - this creates more never-freed memory to annoy
406 * all those memory debugger users. :-)
409 static gchar *long_month_names[13] =
411 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
414 static gchar *short_month_names[13] =
416 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
419 /* This tells us if we need to update the parse info */
420 static gchar *current_locale = NULL;
422 /* order of these in the current locale */
423 static GDateDMY dmy_order[3] =
425 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
428 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
429 * 29 and below are counted as in the year 2000, numbers 30 and above
430 * are counted as in the year 1900.
433 static GDateYear twodigit_start_year = 1930;
435 /* It is impossible to enter a year between 1 AD and 99 AD with this
436 * in effect.
438 static gboolean using_twodigit_years = FALSE;
440 struct _GDateParseTokens {
441 gint num_ints;
442 gint n[3];
443 guint month;
446 typedef struct _GDateParseTokens GDateParseTokens;
448 #define NUM_LEN 10
450 /* HOLDS: g_date_global_lock */
451 static void
452 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
454 gchar num[4][NUM_LEN+1];
455 gint i;
456 const guchar *s;
458 /* We count 4, but store 3; so we can give an error
459 * if there are 4.
461 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
463 s = str;
464 pt->num_ints = 0;
465 while (*s && pt->num_ints < 4)
468 i = 0;
469 while (*s && isdigit (*s) && i <= NUM_LEN)
471 num[pt->num_ints][i] = *s;
472 ++s;
473 ++i;
476 if (i > 0)
478 num[pt->num_ints][i] = '\0';
479 ++(pt->num_ints);
482 if (*s == '\0') break;
484 ++s;
487 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
488 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
489 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
491 pt->month = G_DATE_BAD_MONTH;
493 if (pt->num_ints < 3)
495 gchar lcstr[128];
496 int i = 1;
498 strncpy (lcstr, str, 127);
499 g_strdown (lcstr);
501 while (i < 13)
503 if (long_month_names[i] != NULL)
505 const gchar *found = strstr (lcstr, long_month_names[i]);
507 if (found != NULL)
509 pt->month = i;
510 return;
514 if (short_month_names[i] != NULL)
516 const gchar *found = strstr (lcstr, short_month_names[i]);
518 if (found != NULL)
520 pt->month = i;
521 return;
525 ++i;
530 /* HOLDS: g_date_global_lock */
531 static void
532 g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
534 const gchar *locale = setlocale (LC_TIME, NULL);
535 gboolean recompute_localeinfo = FALSE;
536 GDate d;
538 g_return_if_fail (locale != NULL); /* should not happen */
540 g_date_clear (&d, 1); /* clear for scratch use */
542 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
544 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
547 if (recompute_localeinfo)
549 int i = 1;
550 GDateParseTokens testpt;
551 gchar buf[128];
553 g_free (current_locale); /* still works if current_locale == NULL */
555 current_locale = g_strdup (locale);
557 while (i < 13)
559 g_date_set_dmy (&d, 1, i, 1);
561 g_return_if_fail (g_date_valid (&d));
563 g_date_strftime (buf, 127, "%b", &d);
564 g_free (short_month_names[i]);
565 g_strdown (buf);
566 short_month_names[i] = g_strdup (buf);
570 g_date_strftime (buf, 127, "%B", &d);
571 g_free (long_month_names[i]);
572 g_strdown (buf);
573 long_month_names[i] = g_strdup (buf);
575 ++i;
578 /* Determine DMY order */
580 /* had to pick a random day - don't change this, some strftimes
581 * are broken on some days, and this one is good so far. */
582 g_date_set_dmy (&d, 4, 7, 1976);
584 g_date_strftime (buf, 127, "%x", &d);
586 g_date_fill_parse_tokens (buf, &testpt);
588 i = 0;
589 while (i < testpt.num_ints)
591 switch (testpt.n[i])
593 case 7:
594 dmy_order[i] = G_DATE_MONTH;
595 break;
596 case 4:
597 dmy_order[i] = G_DATE_DAY;
598 break;
599 case 76:
600 using_twodigit_years = TRUE; /* FALL THRU */
601 case 1976:
602 dmy_order[i] = G_DATE_YEAR;
603 break;
604 default:
605 /* leave it unchanged */
606 break;
608 ++i;
611 #ifdef G_ENABLE_DEBUG
612 g_message ("**GDate prepared a new set of locale-specific parse rules.");
613 i = 1;
614 while (i < 13)
616 g_message (" %s %s", long_month_names[i], short_month_names[i]);
617 ++i;
619 if (using_twodigit_years)
621 g_message ("**Using twodigit years with cutoff year: %u", twodigit_start_year);
624 gchar *strings[3];
625 i = 0;
626 while (i < 3)
628 switch (dmy_order[i])
630 case G_DATE_MONTH:
631 strings[i] = "Month";
632 break;
633 case G_DATE_YEAR:
634 strings[i] = "Year";
635 break;
636 case G_DATE_DAY:
637 strings[i] = "Day";
638 break;
639 default:
640 strings[i] = NULL;
641 break;
643 ++i;
645 g_message ("**Order: %s, %s, %s", strings[0], strings[1], strings[2]);
646 g_message ("**Sample date in this locale: `%s'", buf);
648 #endif
651 g_date_fill_parse_tokens (str, pt);
654 void
655 g_date_set_parse (GDate *d,
656 const gchar *str)
658 GDateParseTokens pt;
659 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
661 g_return_if_fail (d != NULL);
663 /* set invalid */
664 g_date_clear (d, 1);
666 G_LOCK (g_date_global);
668 g_date_prepare_to_parse (str, &pt);
670 #ifdef G_ENABLE_DEBUG
671 g_message ("Found %d ints, `%d' `%d' `%d' and written out month %d",
672 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month);
673 #endif
676 if (pt.num_ints == 4)
678 G_UNLOCK (g_date_global);
679 return; /* presumably a typo; bail out. */
682 if (pt.num_ints > 1)
684 int i = 0;
685 int j = 0;
687 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
689 while (i < pt.num_ints && j < 3)
691 switch (dmy_order[j])
693 case G_DATE_MONTH:
695 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
697 m = pt.month;
698 ++j; /* skip months, but don't skip this number */
699 continue;
701 else
702 m = pt.n[i];
704 break;
705 case G_DATE_DAY:
707 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
709 day = 1;
710 ++j; /* skip days, since we may have month/year */
711 continue;
713 day = pt.n[i];
715 break;
716 case G_DATE_YEAR:
718 y = pt.n[i];
720 if (using_twodigit_years && y < 100)
722 guint two = twodigit_start_year % 100;
723 guint century = (twodigit_start_year / 100) * 100;
725 if (y < two)
726 century += 100;
728 y += century;
731 break;
732 default:
733 break;
736 ++i;
737 ++j;
741 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
743 /* Try YYYY MM DD */
744 y = pt.n[0];
745 m = pt.n[1];
746 day = pt.n[2];
748 if (using_twodigit_years && y < 100)
749 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
752 else if (pt.num_ints == 1)
754 if (pt.month != G_DATE_BAD_MONTH)
756 /* Month name and year? */
757 m = pt.month;
758 day = 1;
759 y = pt.n[0];
761 else
763 /* Try yyyymmdd and yymmdd */
765 m = (pt.n[0]/100) % 100;
766 day = pt.n[0] % 100;
767 y = pt.n[0]/10000;
769 /* FIXME move this into a separate function */
770 if (using_twodigit_years && y < 100)
772 guint two = twodigit_start_year % 100;
773 guint century = (twodigit_start_year / 100) * 100;
775 if (y < two)
776 century += 100;
778 y += century;
783 /* See if we got anything valid out of all this. */
784 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
785 if (y < 8000 && g_date_valid_dmy (day, m, y))
787 d->month = m;
788 d->day = day;
789 d->year = y;
790 d->dmy = TRUE;
792 #ifdef G_ENABLE_DEBUG
793 else
794 g_message ("Rejected DMY %u %u %u", day, m, y);
795 #endif
796 G_UNLOCK (g_date_global);
799 void
800 g_date_set_time (GDate *d,
801 GTime time)
803 time_t t = time;
804 struct tm tm;
806 g_return_if_fail (d != NULL);
808 #ifdef HAVE_LOCALTIME_R
809 localtime_r (&t, &tm);
810 #else
812 struct tm *ptm = localtime (&t);
813 g_assert (ptm);
814 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
816 #endif
818 d->julian = FALSE;
820 d->month = tm.tm_mon + 1;
821 d->day = tm.tm_mday;
822 d->year = tm.tm_year + 1900;
824 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
826 d->dmy = TRUE;
829 void
830 g_date_set_month (GDate *d,
831 GDateMonth m)
833 g_return_if_fail (d != NULL);
834 g_return_if_fail (g_date_valid_month (m));
836 if (d->julian && !d->dmy) g_date_update_dmy(d);
837 d->julian = FALSE;
839 d->month = m;
841 if (g_date_valid_dmy (d->day, d->month, d->year))
842 d->dmy = TRUE;
843 else
844 d->dmy = FALSE;
847 void
848 g_date_set_day (GDate *d,
849 GDateDay day)
851 g_return_if_fail (d != NULL);
852 g_return_if_fail (g_date_valid_day (day));
854 if (d->julian && !d->dmy) g_date_update_dmy(d);
855 d->julian = FALSE;
857 d->day = day;
859 if (g_date_valid_dmy (d->day, d->month, d->year))
860 d->dmy = TRUE;
861 else
862 d->dmy = FALSE;
865 void
866 g_date_set_year (GDate *d,
867 GDateYear y)
869 g_return_if_fail (d != NULL);
870 g_return_if_fail (g_date_valid_year (y));
872 if (d->julian && !d->dmy) g_date_update_dmy(d);
873 d->julian = FALSE;
875 d->year = y;
877 if (g_date_valid_dmy (d->day, d->month, d->year))
878 d->dmy = TRUE;
879 else
880 d->dmy = FALSE;
883 void
884 g_date_set_dmy (GDate *d,
885 GDateDay day,
886 GDateMonth m,
887 GDateYear y)
889 g_return_if_fail (d != NULL);
890 g_return_if_fail (g_date_valid_dmy (day, m, y));
892 d->julian = FALSE;
894 d->month = m;
895 d->day = day;
896 d->year = y;
898 d->dmy = TRUE;
901 void
902 g_date_set_julian (GDate *d, guint32 j)
904 g_return_if_fail (d != NULL);
905 g_return_if_fail (g_date_valid_julian (j));
907 d->julian_days = j;
908 d->julian = TRUE;
909 d->dmy = FALSE;
913 gboolean
914 g_date_is_first_of_month (GDate *d)
916 g_return_val_if_fail (d != NULL, FALSE);
917 g_return_val_if_fail (g_date_valid (d), FALSE);
919 if (!d->dmy)
921 g_date_update_dmy (d);
923 g_return_val_if_fail (d->dmy, FALSE);
925 if (d->day == 1) return TRUE;
926 else return FALSE;
929 gboolean
930 g_date_is_last_of_month (GDate *d)
932 gint index;
934 g_return_val_if_fail (d != NULL, FALSE);
935 g_return_val_if_fail (g_date_valid (d), FALSE);
937 if (!d->dmy)
939 g_date_update_dmy (d);
941 g_return_val_if_fail (d->dmy, FALSE);
943 index = g_date_is_leap_year (d->year) ? 1 : 0;
945 if (d->day == days_in_months[index][d->month]) return TRUE;
946 else return FALSE;
949 void
950 g_date_add_days (GDate *d, guint ndays)
952 g_return_if_fail (d != NULL);
953 g_return_if_fail (g_date_valid (d));
955 if (!d->julian)
957 g_date_update_julian (d);
959 g_return_if_fail (d->julian);
961 d->julian_days += ndays;
962 d->dmy = FALSE;
965 void
966 g_date_subtract_days (GDate *d, guint ndays)
968 g_return_if_fail (d != NULL);
969 g_return_if_fail (g_date_valid (d));
971 if (!d->julian)
973 g_date_update_julian (d);
975 g_return_if_fail (d->julian);
976 g_return_if_fail (d->julian_days > ndays);
978 d->julian_days -= ndays;
979 d->dmy = FALSE;
982 void
983 g_date_add_months (GDate *d,
984 guint nmonths)
986 guint years, months;
987 gint index;
989 g_return_if_fail (d != NULL);
990 g_return_if_fail (g_date_valid (d));
992 if (!d->dmy)
994 g_date_update_dmy (d);
996 g_return_if_fail (d->dmy);
998 nmonths += d->month - 1;
1000 years = nmonths/12;
1001 months = nmonths%12;
1003 d->month = months + 1;
1004 d->year += years;
1006 index = g_date_is_leap_year (d->year) ? 1 : 0;
1008 if (d->day > days_in_months[index][d->month])
1009 d->day = days_in_months[index][d->month];
1011 d->julian = FALSE;
1013 g_return_if_fail (g_date_valid (d));
1016 void
1017 g_date_subtract_months (GDate *d,
1018 guint nmonths)
1020 guint years, months;
1021 gint index;
1023 g_return_if_fail (d != NULL);
1024 g_return_if_fail (g_date_valid (d));
1026 if (!d->dmy)
1028 g_date_update_dmy (d);
1030 g_return_if_fail (d->dmy);
1032 years = nmonths/12;
1033 months = nmonths%12;
1035 g_return_if_fail (d->year > years);
1037 d->year -= years;
1039 if (d->month > months) d->month -= months;
1040 else
1042 months -= d->month;
1043 d->month = 12 - months;
1044 d->year -= 1;
1047 index = g_date_is_leap_year (d->year) ? 1 : 0;
1049 if (d->day > days_in_months[index][d->month])
1050 d->day = days_in_months[index][d->month];
1052 d->julian = FALSE;
1054 g_return_if_fail (g_date_valid (d));
1057 void
1058 g_date_add_years (GDate *d,
1059 guint nyears)
1061 g_return_if_fail (d != NULL);
1062 g_return_if_fail (g_date_valid (d));
1064 if (!d->dmy)
1066 g_date_update_dmy (d);
1068 g_return_if_fail (d->dmy);
1070 d->year += nyears;
1072 if (d->month == 2 && d->day == 29)
1074 if (!g_date_is_leap_year (d->year))
1076 d->day = 28;
1080 d->julian = FALSE;
1083 void
1084 g_date_subtract_years (GDate *d,
1085 guint nyears)
1087 g_return_if_fail (d != NULL);
1088 g_return_if_fail (g_date_valid (d));
1090 if (!d->dmy)
1092 g_date_update_dmy (d);
1094 g_return_if_fail (d->dmy);
1095 g_return_if_fail (d->year > nyears);
1097 d->year -= nyears;
1099 if (d->month == 2 && d->day == 29)
1101 if (!g_date_is_leap_year (d->year))
1103 d->day = 28;
1107 d->julian = FALSE;
1111 gboolean
1112 g_date_is_leap_year (GDateYear year)
1114 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1116 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1117 (year % 400) == 0 );
1120 guint8
1121 g_date_days_in_month (GDateMonth month,
1122 GDateYear year)
1124 gint index;
1126 g_return_val_if_fail (g_date_valid_year (year), 0);
1127 g_return_val_if_fail (g_date_valid_month (month), 0);
1129 index = g_date_is_leap_year (year) ? 1 : 0;
1131 return days_in_months[index][month];
1134 guint8
1135 g_date_monday_weeks_in_year (GDateYear year)
1137 GDate d;
1139 g_return_val_if_fail (g_date_valid_year (year), 0);
1141 g_date_clear (&d, 1);
1142 g_date_set_dmy (&d, 1, 1, year);
1143 if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
1144 g_date_set_dmy (&d, 31, 12, year);
1145 if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
1146 if (g_date_is_leap_year (year))
1148 g_date_set_dmy (&d, 2, 1, year);
1149 if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
1150 g_date_set_dmy (&d, 30, 12, year);
1151 if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
1153 return 52;
1156 guint8
1157 g_date_sunday_weeks_in_year (GDateYear year)
1159 GDate d;
1161 g_return_val_if_fail (g_date_valid_year (year), 0);
1163 g_date_clear (&d, 1);
1164 g_date_set_dmy (&d, 1, 1, year);
1165 if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
1166 g_date_set_dmy (&d, 31, 12, year);
1167 if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
1168 if (g_date_is_leap_year (year))
1170 g_date_set_dmy (&d, 2, 1, year);
1171 if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
1172 g_date_set_dmy (&d, 30, 12, year);
1173 if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
1175 return 52;
1178 gint
1179 g_date_compare (GDate *lhs,
1180 GDate *rhs)
1182 g_return_val_if_fail (lhs != NULL, 0);
1183 g_return_val_if_fail (rhs != NULL, 0);
1184 g_return_val_if_fail (g_date_valid (lhs), 0);
1185 g_return_val_if_fail (g_date_valid (rhs), 0);
1187 /* Remember the self-comparison case! I think it works right now. */
1189 while (TRUE)
1192 if (lhs->julian && rhs->julian)
1194 if (lhs->julian_days < rhs->julian_days) return -1;
1195 else if (lhs->julian_days > rhs->julian_days) return 1;
1196 else return 0;
1198 else if (lhs->dmy && rhs->dmy)
1200 if (lhs->year < rhs->year) return -1;
1201 else if (lhs->year > rhs->year) return 1;
1202 else
1204 if (lhs->month < rhs->month) return -1;
1205 else if (lhs->month > rhs->month) return 1;
1206 else
1208 if (lhs->day < rhs->day) return -1;
1209 else if (lhs->day > rhs->day) return 1;
1210 else return 0;
1216 else
1218 if (!lhs->julian) g_date_update_julian (lhs);
1219 if (!rhs->julian) g_date_update_julian (rhs);
1220 g_return_val_if_fail (lhs->julian, 0);
1221 g_return_val_if_fail (rhs->julian, 0);
1225 return 0; /* warnings */
1229 void
1230 g_date_to_struct_tm (GDate *d,
1231 struct tm *tm)
1233 GDateWeekday day;
1235 g_return_if_fail (d != NULL);
1236 g_return_if_fail (g_date_valid (d));
1237 g_return_if_fail (tm != NULL);
1239 if (!d->dmy)
1241 g_date_update_dmy (d);
1243 g_return_if_fail (d->dmy);
1245 /* zero all the irrelevant fields to be sure they're valid */
1247 /* On Linux and maybe other systems, there are weird non-POSIX
1248 * fields on the end of struct tm that choke strftime if they
1249 * contain garbage. So we need to 0 the entire struct, not just the
1250 * fields we know to exist.
1253 memset (tm, 0x0, sizeof (struct tm));
1255 tm->tm_mday = d->day;
1256 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1257 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1259 day = g_date_weekday (d);
1260 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1262 tm->tm_wday = (int)day;
1264 tm->tm_yday = g_date_day_of_year (d) - 1; /* 0 to 365 */
1265 tm->tm_isdst = -1; /* -1 means "information not available" */
1268 gsize
1269 g_date_strftime (gchar *s,
1270 gsize slen,
1271 const gchar *format,
1272 GDate *d)
1274 struct tm tm;
1275 gsize retval;
1277 g_return_val_if_fail (d != NULL, 0);
1278 g_return_val_if_fail (g_date_valid (d), 0);
1279 g_return_val_if_fail (slen > 0, 0);
1280 g_return_val_if_fail (format != 0, 0);
1281 g_return_val_if_fail (s != 0, 0);
1283 g_date_to_struct_tm (d, &tm);
1285 retval = strftime (s, slen, format, &tm);
1286 if (retval == 0)
1288 /* If retval == 0, the contents of s are undefined. We define
1289 * them.
1291 s[0] = '\0';
1293 return retval;