Commented out debugging output.
[platform/upstream/glib.git] / glib / gdate.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 /* 
28  * MT safe
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include "glib.h"
36
37 #include <time.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <stdlib.h>
41 #include <locale.h>
42
43 GDate*
44 g_date_new ()
45 {
46   GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
47   
48   return d;
49 }
50
51 GDate*
52 g_date_new_dmy (GDateDay day, GDateMonth m, GDateYear y)
53 {
54   GDate *d;
55   g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
56   
57   d = g_new (GDate, 1);
58   
59   d->julian = FALSE;
60   d->dmy    = TRUE;
61   
62   d->month = m;
63   d->day   = day;
64   d->year  = y;
65   
66   g_assert (g_date_valid (d));
67   
68   return d;
69 }
70
71 GDate*
72 g_date_new_julian (guint32 j)
73 {
74   GDate *d;
75   g_return_val_if_fail (g_date_valid_julian (j), NULL);
76   
77   d = g_new (GDate, 1);
78   
79   d->julian = TRUE;
80   d->dmy    = FALSE;
81   
82   d->julian_days = j;
83   
84   g_assert (g_date_valid (d));
85   
86   return d;
87 }
88
89 void
90 g_date_free (GDate *d)
91 {
92   g_return_if_fail (d != NULL);
93   
94   g_free (d);
95 }
96
97 gboolean     
98 g_date_valid (GDate       *d)
99 {
100   g_return_val_if_fail (d != NULL, FALSE);
101   
102   return (d->julian || d->dmy);
103 }
104
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 */
109 };
110
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 }
115 };
116
117 gboolean     
118 g_date_valid_month (GDateMonth   m)
119
120   return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
121 }
122
123 gboolean     
124 g_date_valid_year (GDateYear    y)
125 {
126   return ( y > G_DATE_BAD_YEAR );
127 }
128
129 gboolean     
130 g_date_valid_day (GDateDay     d)
131 {
132   return ( (d > G_DATE_BAD_DAY) && (d < 32) );
133 }
134
135 gboolean     
136 g_date_valid_weekday (GDateWeekday w)
137 {
138   return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
139 }
140
141 gboolean     
142 g_date_valid_julian (guint32      j)
143 {
144   return (j > G_DATE_BAD_JULIAN);
145 }
146
147 gboolean     
148 g_date_valid_dmy (GDateDay     d, 
149                   GDateMonth   m, 
150                   GDateYear    y)
151 {
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])) );
158 }
159
160
161 /* "Julian days" just means an absolute number of days, where Day 1 ==
162  *   Jan 1, Year 1
163  */
164 static void
165 g_date_update_julian (GDate *d)
166 {
167   GDateYear year;
168   gint index;
169   
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));
174   
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. 
180    */
181   
182   year = d->year - 1; /* we know d->year > 0 since it's valid */
183   
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 */
188   
189   index = g_date_is_leap_year (d->year) ? 1 : 0;
190   
191   d->julian_days += days_in_year[index][d->month] + d->day;
192   
193   g_return_if_fail (g_date_valid_julian (d->julian_days));
194   
195   d->julian = TRUE;
196 }
197
198 static void 
199 g_date_update_dmy (GDate *d)
200 {
201   GDateYear y;
202   GDateMonth m;
203   GDateDay day;
204   
205   guint32 A, B, C, D, E, M;
206   
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));
211   
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.
215    *
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.  
218    */
219   
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;
226   
227   m = M + 3 - (12*(M/10));
228   day = E - (153*M + 2)/5;
229   y = 100 * B + D - 4800 + (M/10);
230   
231 #ifdef G_ENABLE_DEBUG
232   if (!g_date_valid_dmy (day, m, y)) 
233     {
234       g_warning ("\nOOPS julian: %u  computed dmy: %u %u %u\n", 
235                  d->julian_days, day, m, y);
236     }
237 #endif
238   
239   d->month = m;
240   d->day   = day;
241   d->year  = y;
242   
243   d->dmy = TRUE;
244 }
245
246 GDateWeekday 
247 g_date_weekday (GDate *d)
248 {
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);
251   
252   if (!d->julian) 
253     {
254       g_date_update_julian (d);
255     }
256   g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
257   
258   return ((d->julian_days - 1) % 7) + 1;
259 }
260
261 GDateMonth   
262 g_date_month (GDate *d)
263 {
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);
266   
267   if (!d->dmy) 
268     {
269       g_date_update_dmy (d);
270     }
271   g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
272   
273   return d->month;
274 }
275
276 GDateYear    
277 g_date_year (GDate *d)
278 {
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);
281   
282   if (!d->dmy) 
283     {
284       g_date_update_dmy (d);
285     }
286   g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);  
287   
288   return d->year;
289 }
290
291 GDateDay     
292 g_date_day (GDate *d)
293 {
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);
296   
297   if (!d->dmy) 
298     {
299       g_date_update_dmy (d);
300     }
301   g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);  
302   
303   return d->day;
304 }
305
306 guint32      
307 g_date_julian (GDate *d)
308 {
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);
311   
312   if (!d->julian) 
313     {
314       g_date_update_julian (d);
315     }
316   g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);  
317   
318   return d->julian_days;
319 }
320
321 guint        
322 g_date_day_of_year (GDate *d)
323 {
324   gint index;
325   
326   g_return_val_if_fail (d != NULL, 0);
327   g_return_val_if_fail (g_date_valid (d), 0);
328   
329   if (!d->dmy) 
330     {
331       g_date_update_dmy (d);
332     }
333   g_return_val_if_fail (d->dmy, 0);  
334   
335   index = g_date_is_leap_year (d->year) ? 1 : 0;
336   
337   return (days_in_year[index][d->month] + d->day);
338 }
339
340 guint        
341 g_date_monday_week_of_year (GDate *d)
342 {
343   GDateWeekday wd;
344   guint day;
345   GDate first;
346   
347   g_return_val_if_fail (d != NULL, 0);
348   g_return_val_if_fail (g_date_valid (d), 0);
349   
350   if (!d->dmy) 
351     {
352       g_date_update_dmy (d);
353     }
354   g_return_val_if_fail (d->dmy, 0);  
355   
356   g_date_clear (&first, 1);
357   
358   g_date_set_dmy (&first, 1, 1, d->year);
359   
360   wd = g_date_weekday (&first) - 1; /* make Monday day 0 */
361   day = g_date_day_of_year (d) - 1;
362   
363   return ((day + wd)/7U + (wd == 0 ? 1 : 0));
364 }
365
366 guint        
367 g_date_sunday_week_of_year (GDate *d)
368 {
369   GDateWeekday wd;
370   guint day;
371   GDate first;
372   
373   g_return_val_if_fail (d != NULL, 0);
374   g_return_val_if_fail (g_date_valid (d), 0);
375   
376   if (!d->dmy) 
377     {
378       g_date_update_dmy (d);
379     }
380   g_return_val_if_fail (d->dmy, 0);  
381   
382   g_date_clear (&first, 1);
383   
384   g_date_set_dmy (&first, 1, 1, d->year);
385   
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;
389   
390   return ((day + wd)/7U + (wd == 0 ? 1 : 0));
391 }
392
393 void         
394 g_date_clear (GDate       *d, guint ndates)
395 {
396   g_return_if_fail (d != NULL);
397   g_return_if_fail (ndates != 0);
398   
399   memset (d, 0x0, ndates*sizeof (GDate)); 
400 }
401
402 G_LOCK_DEFINE_STATIC (g_date_global);
403
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. :-) 
407  */
408
409 static gchar *long_month_names[13] = 
410
411   "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 
412 };
413
414 static gchar *short_month_names[13] = 
415 {
416   "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 
417 };
418
419 /* This tells us if we need to update the parse info */
420 static gchar *current_locale = NULL;
421
422 /* order of these in the current locale */
423 static GDateDMY dmy_order[3] = 
424 {
425    G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
426 };
427
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.  
431  */
432
433 static GDateYear twodigit_start_year = 1930;
434
435 /* It is impossible to enter a year between 1 AD and 99 AD with this
436  * in effect.  
437  */
438 static gboolean using_twodigit_years = FALSE;
439
440 struct _GDateParseTokens {
441   gint num_ints;
442   gint n[3];
443   guint month;
444 };
445
446 typedef struct _GDateParseTokens GDateParseTokens;
447
448 #define NUM_LEN 10
449
450 /* HOLDS: g_date_global_lock */
451 static void
452 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
453 {
454   gchar num[4][NUM_LEN+1];
455   gint i;
456   const gchar *s;
457   
458   /* We count 4, but store 3; so we can give an error
459    * if there are 4.
460    */
461   num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
462   
463   s = str;
464   pt->num_ints = 0;
465   while (*s && pt->num_ints < 4) 
466     {
467       
468       i = 0;
469       while (*s && isdigit (*s) && i <= NUM_LEN)
470         {
471           num[pt->num_ints][i] = *s;
472           ++s; 
473           ++i;
474         }
475       
476       if (i > 0) 
477         {
478           num[pt->num_ints][i] = '\0';
479           ++(pt->num_ints);
480         }
481       
482       if (*s == '\0') break;
483       
484       ++s;
485     }
486   
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;
490   
491   pt->month = G_DATE_BAD_MONTH;
492   
493   if (pt->num_ints < 3)
494     {
495       gchar lcstr[128];
496       int i = 1;
497       
498       strncpy (lcstr, str, 127);
499       g_strdown (lcstr);
500       
501       while (i < 13)
502         {
503           if (long_month_names[i] != NULL) 
504             {
505               const gchar *found = strstr (lcstr, long_month_names[i]);
506               
507               if (found != NULL)
508                 {
509                   pt->month = i;
510                   return;
511                 }
512             }
513           
514           if (short_month_names[i] != NULL) 
515             {
516               const gchar *found = strstr (lcstr, short_month_names[i]);
517               
518               if (found != NULL)
519                 {
520                   pt->month = i;
521                   return;
522                 }
523             }
524
525           ++i;
526         }      
527     }
528 }
529
530 /* HOLDS: g_date_global_lock */
531 static void
532 g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
533 {
534   const gchar *locale = setlocale (LC_TIME, NULL);
535   gboolean recompute_localeinfo = FALSE;
536   GDate d;
537   
538   g_return_if_fail (locale != NULL); /* should not happen */
539   
540   g_date_clear (&d, 1);              /* clear for scratch use */
541   
542   if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) ) 
543     {
544       recompute_localeinfo = TRUE;  /* Uh, there used to be a reason for the temporary */
545     }
546   
547   if (recompute_localeinfo)
548     {
549       int i = 1;
550       GDateParseTokens testpt;
551       gchar buf[128];
552       
553       g_free (current_locale); /* still works if current_locale == NULL */
554       
555       current_locale = g_strdup (locale);
556       
557       while (i < 13) 
558         {
559           g_date_set_dmy (&d, 1, i, 1);
560           
561           g_return_if_fail (g_date_valid (&d));
562           
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);
567           
568           
569           
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);
574           
575           ++i;
576         }
577       
578       /* Determine DMY order */
579       
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);
583       
584       g_date_strftime (buf, 127, "%x", &d);
585       
586       g_date_fill_parse_tokens (buf, &testpt);
587       
588       i = 0;
589       while (i < testpt.num_ints)
590         {
591           switch (testpt.n[i])
592             {
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;
607             }
608           ++i;
609         }
610       
611 #ifdef G_ENABLE_DEBUG
612 #  if 0
613       g_message ("**GDate prepared a new set of locale-specific parse rules.");
614 #  endif
615       i = 1;
616       while (i < 13) 
617         {
618 #  if 0
619           g_message ("  %s   %s", long_month_names[i], short_month_names[i]);
620 #  endif
621           ++i;
622         }
623       if (using_twodigit_years)
624         {
625 #  if 0
626           g_message ("**Using twodigit years with cutoff year: %u", twodigit_start_year);
627 #  endif
628         }
629       { 
630         gchar *strings[3];
631         i = 0;
632         while (i < 3)
633           {
634             switch (dmy_order[i])
635               {
636               case G_DATE_MONTH:
637                 strings[i] = "Month";
638                 break;
639               case G_DATE_YEAR:
640                 strings[i] = "Year";
641                 break;
642               case G_DATE_DAY:
643                 strings[i] = "Day";
644                 break;
645               default:
646                 strings[i] = NULL;
647                 break;
648               }
649             ++i;
650           }
651 #  if 0
652         g_message ("**Order: %s, %s, %s", strings[0], strings[1], strings[2]);
653         g_message ("**Sample date in this locale: `%s'", buf);
654 #  endif
655       }
656 #endif
657     }
658   
659   g_date_fill_parse_tokens (str, pt);
660 }
661
662 void         
663 g_date_set_parse (GDate       *d, 
664                   const gchar *str)
665 {
666   GDateParseTokens pt;
667   guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
668   
669   g_return_if_fail (d != NULL);
670   
671   /* set invalid */
672   g_date_clear (d, 1);
673   
674   G_LOCK (g_date_global);
675
676   g_date_prepare_to_parse (str, &pt);
677   
678 #ifdef G_ENABLE_DEBUG
679 #  if 0
680   g_message ("Found %d ints, `%d' `%d' `%d' and written out month %d", 
681              pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month);
682 #  endif
683 #endif
684   
685   
686   if (pt.num_ints == 4) 
687     {
688       G_UNLOCK (g_date_global);
689       return; /* presumably a typo; bail out. */
690     }
691   
692   if (pt.num_ints > 1)
693     {
694       int i = 0;
695       int j = 0;
696       
697       g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
698       
699       while (i < pt.num_ints && j < 3) 
700         {
701           switch (dmy_order[j])
702             {
703             case G_DATE_MONTH:
704             {
705               if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
706                 {
707                   m = pt.month;
708                   ++j;      /* skip months, but don't skip this number */
709                   continue;
710                 }
711               else 
712                 m = pt.n[i];
713             }
714             break;
715             case G_DATE_DAY:
716             {
717               if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
718                 {
719                   day = 1;
720                   ++j;      /* skip days, since we may have month/year */
721                   continue;
722                 }
723               day = pt.n[i];
724             }
725             break;
726             case G_DATE_YEAR:
727             {
728               y  = pt.n[i];
729               
730               if (using_twodigit_years && y < 100)
731                 {
732                   guint two     =  twodigit_start_year % 100;
733                   guint century = (twodigit_start_year / 100) * 100;
734                   
735                   if (y < two)
736                     century += 100;
737                   
738                   y += century;
739                 }
740             }
741             break;
742             default:
743               break;
744             }
745           
746           ++i;
747           ++j;
748         }
749       
750       
751       if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
752         {
753           /* Try YYYY MM DD */
754           y   = pt.n[0];
755           m   = pt.n[1];
756           day = pt.n[2];
757           
758           if (using_twodigit_years && y < 100) 
759             y = G_DATE_BAD_YEAR; /* avoids ambiguity */
760         }
761     }
762   else if (pt.num_ints == 1) 
763     {
764       if (pt.month != G_DATE_BAD_MONTH)
765         {
766           /* Month name and year? */
767           m    = pt.month;
768           day  = 1;
769           y = pt.n[0];
770         }
771       else
772         {
773           /* Try yyyymmdd and yymmdd */
774           
775           m   = (pt.n[0]/100) % 100;
776           day = pt.n[0] % 100;
777           y   = pt.n[0]/10000;
778           
779           /* FIXME move this into a separate function */
780           if (using_twodigit_years && y < 100)
781             {
782               guint two     =  twodigit_start_year % 100;
783               guint century = (twodigit_start_year / 100) * 100;
784               
785               if (y < two)
786                 century += 100;
787               
788               y += century;
789             }
790         }
791     }
792   
793   /* See if we got anything valid out of all this. */
794   /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
795   if (y < 8000 && g_date_valid_dmy (day, m, y)) 
796     {
797       d->month = m;
798       d->day   = day;
799       d->year  = y;
800       d->dmy   = TRUE;
801     }
802 #ifdef G_ENABLE_DEBUG
803   else 
804     g_message ("Rejected DMY %u %u %u", day, m, y);
805 #endif
806   G_UNLOCK (g_date_global);
807 }
808
809 void         
810 g_date_set_time (GDate *d,
811                  GTime  time)
812 {
813   time_t t = time;
814   struct tm tm;
815   
816   g_return_if_fail (d != NULL);
817   
818 #ifdef HAVE_LOCALTIME_R
819   localtime_r (&t, &tm);
820 #else
821   {
822     struct tm *ptm = localtime (&t);
823     g_assert (ptm);
824     memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
825   }
826 #endif
827   
828   d->julian = FALSE;
829   
830   d->month = tm.tm_mon + 1;
831   d->day   = tm.tm_mday;
832   d->year  = tm.tm_year + 1900;
833   
834   g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
835   
836   d->dmy    = TRUE;
837 }
838
839 void         
840 g_date_set_month (GDate     *d, 
841                   GDateMonth m)
842 {
843   g_return_if_fail (d != NULL);
844   g_return_if_fail (g_date_valid_month (m));
845
846   if (d->julian && !d->dmy) g_date_update_dmy(d);
847   d->julian = FALSE;
848   
849   d->month = m;
850   
851   if (g_date_valid_dmy (d->day, d->month, d->year))
852     d->dmy = TRUE;
853   else 
854     d->dmy = FALSE;
855 }
856
857 void         
858 g_date_set_day (GDate     *d, 
859                 GDateDay day)
860 {
861   g_return_if_fail (d != NULL);
862   g_return_if_fail (g_date_valid_day (day));
863   
864   if (d->julian && !d->dmy) g_date_update_dmy(d);
865   d->julian = FALSE;
866   
867   d->day = day;
868   
869   if (g_date_valid_dmy (d->day, d->month, d->year))
870     d->dmy = TRUE;
871   else 
872     d->dmy = FALSE;
873 }
874
875 void         
876 g_date_set_year (GDate     *d, 
877                  GDateYear  y)
878 {
879   g_return_if_fail (d != NULL);
880   g_return_if_fail (g_date_valid_year (y));
881   
882   if (d->julian && !d->dmy) g_date_update_dmy(d);
883   d->julian = FALSE;
884   
885   d->year = y;
886   
887   if (g_date_valid_dmy (d->day, d->month, d->year))
888     d->dmy = TRUE;
889   else 
890     d->dmy = FALSE;
891 }
892
893 void         
894 g_date_set_dmy (GDate     *d, 
895                 GDateDay   day, 
896                 GDateMonth m, 
897                 GDateYear  y)
898 {
899   g_return_if_fail (d != NULL);
900   g_return_if_fail (g_date_valid_dmy (day, m, y));
901   
902   d->julian = FALSE;
903   
904   d->month = m;
905   d->day   = day;
906   d->year  = y;
907   
908   d->dmy = TRUE;
909 }
910
911 void         
912 g_date_set_julian (GDate *d, guint32 j)
913 {
914   g_return_if_fail (d != NULL);
915   g_return_if_fail (g_date_valid_julian (j));
916   
917   d->julian_days = j;
918   d->julian = TRUE;
919   d->dmy = FALSE;
920 }
921
922
923 gboolean     
924 g_date_is_first_of_month (GDate *d)
925 {
926   g_return_val_if_fail (d != NULL, FALSE);
927   g_return_val_if_fail (g_date_valid (d), FALSE);
928   
929   if (!d->dmy) 
930     {
931       g_date_update_dmy (d);
932     }
933   g_return_val_if_fail (d->dmy, FALSE);  
934   
935   if (d->day == 1) return TRUE;
936   else return FALSE;
937 }
938
939 gboolean     
940 g_date_is_last_of_month (GDate *d)
941 {
942   gint index;
943   
944   g_return_val_if_fail (d != NULL, FALSE);
945   g_return_val_if_fail (g_date_valid (d), FALSE);
946   
947   if (!d->dmy) 
948     {
949       g_date_update_dmy (d);
950     }
951   g_return_val_if_fail (d->dmy, FALSE);  
952   
953   index = g_date_is_leap_year (d->year) ? 1 : 0;
954   
955   if (d->day == days_in_months[index][d->month]) return TRUE;
956   else return FALSE;
957 }
958
959 void         
960 g_date_add_days (GDate *d, guint ndays)
961 {
962   g_return_if_fail (d != NULL);
963   g_return_if_fail (g_date_valid (d));
964   
965   if (!d->julian)
966     {
967       g_date_update_julian (d);
968     }
969   g_return_if_fail (d->julian);
970   
971   d->julian_days += ndays;
972   d->dmy = FALSE;
973 }
974
975 void         
976 g_date_subtract_days (GDate *d, guint ndays)
977 {
978   g_return_if_fail (d != NULL);
979   g_return_if_fail (g_date_valid (d));
980   
981   if (!d->julian)
982     {
983       g_date_update_julian (d);
984     }
985   g_return_if_fail (d->julian);
986   g_return_if_fail (d->julian_days > ndays);
987   
988   d->julian_days -= ndays;
989   d->dmy = FALSE;
990 }
991
992 void         
993 g_date_add_months (GDate       *d, 
994                    guint        nmonths)
995 {
996   guint years, months;
997   gint index;
998   
999   g_return_if_fail (d != NULL);
1000   g_return_if_fail (g_date_valid (d));
1001   
1002   if (!d->dmy) 
1003     {
1004       g_date_update_dmy (d);
1005     }
1006   g_return_if_fail (d->dmy);  
1007   
1008   nmonths += d->month - 1;
1009   
1010   years  = nmonths/12;
1011   months = nmonths%12;
1012   
1013   d->month = months + 1;
1014   d->year  += years;
1015   
1016   index = g_date_is_leap_year (d->year) ? 1 : 0;
1017   
1018   if (d->day > days_in_months[index][d->month])
1019     d->day = days_in_months[index][d->month];
1020   
1021   d->julian = FALSE;
1022   
1023   g_return_if_fail (g_date_valid (d));
1024 }
1025
1026 void         
1027 g_date_subtract_months (GDate       *d, 
1028                         guint        nmonths)
1029 {
1030   guint years, months;
1031   gint index;
1032   
1033   g_return_if_fail (d != NULL);
1034   g_return_if_fail (g_date_valid (d));
1035   
1036   if (!d->dmy) 
1037     {
1038       g_date_update_dmy (d);
1039     }
1040   g_return_if_fail (d->dmy);  
1041   
1042   years  = nmonths/12;
1043   months = nmonths%12;
1044   
1045   g_return_if_fail (d->year > years);
1046   
1047   d->year  -= years;
1048   
1049   if (d->month > months) d->month -= months;
1050   else 
1051     {
1052       months -= d->month;
1053       d->month = 12 - months;
1054       d->year -= 1;
1055     }
1056   
1057   index = g_date_is_leap_year (d->year) ? 1 : 0;
1058   
1059   if (d->day > days_in_months[index][d->month])
1060     d->day = days_in_months[index][d->month];
1061   
1062   d->julian = FALSE;
1063   
1064   g_return_if_fail (g_date_valid (d));
1065 }
1066
1067 void         
1068 g_date_add_years (GDate       *d, 
1069                   guint        nyears)
1070 {
1071   g_return_if_fail (d != NULL);
1072   g_return_if_fail (g_date_valid (d));
1073   
1074   if (!d->dmy) 
1075     {
1076       g_date_update_dmy (d);
1077     }
1078   g_return_if_fail (d->dmy);  
1079   
1080   d->year += nyears;
1081   
1082   if (d->month == 2 && d->day == 29)
1083     {
1084       if (!g_date_is_leap_year (d->year))
1085         {
1086           d->day = 28;
1087         }
1088     }
1089   
1090   d->julian = FALSE;
1091 }
1092
1093 void         
1094 g_date_subtract_years (GDate       *d, 
1095                        guint        nyears)
1096 {
1097   g_return_if_fail (d != NULL);
1098   g_return_if_fail (g_date_valid (d));
1099   
1100   if (!d->dmy) 
1101     {
1102       g_date_update_dmy (d);
1103     }
1104   g_return_if_fail (d->dmy);  
1105   g_return_if_fail (d->year > nyears);
1106   
1107   d->year -= nyears;
1108   
1109   if (d->month == 2 && d->day == 29)
1110     {
1111       if (!g_date_is_leap_year (d->year))
1112         {
1113           d->day = 28;
1114         }
1115     }
1116   
1117   d->julian = FALSE;
1118 }
1119
1120
1121 gboolean     
1122 g_date_is_leap_year (GDateYear  year)
1123 {
1124   g_return_val_if_fail (g_date_valid_year (year), FALSE);
1125   
1126   return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1127            (year % 400) == 0 );
1128 }
1129
1130 guint8         
1131 g_date_days_in_month (GDateMonth month, 
1132                       GDateYear  year)
1133 {
1134   gint index;
1135   
1136   g_return_val_if_fail (g_date_valid_year (year), 0);
1137   g_return_val_if_fail (g_date_valid_month (month), 0);
1138   
1139   index = g_date_is_leap_year (year) ? 1 : 0;
1140   
1141   return days_in_months[index][month];
1142 }
1143
1144 guint8       
1145 g_date_monday_weeks_in_year (GDateYear  year)
1146 {
1147   GDate d;
1148   
1149   g_return_val_if_fail (g_date_valid_year (year), 0);
1150   
1151   g_date_clear (&d, 1);
1152   g_date_set_dmy (&d, 1, 1, year);
1153   if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
1154   g_date_set_dmy (&d, 31, 12, year);
1155   if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
1156   if (g_date_is_leap_year (year)) 
1157     {
1158       g_date_set_dmy (&d, 2, 1, year);
1159       if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
1160       g_date_set_dmy (&d, 30, 12, year);
1161       if (g_date_weekday (&d) == G_DATE_MONDAY) return 53;
1162     }
1163   return 52;
1164 }
1165
1166 guint8       
1167 g_date_sunday_weeks_in_year (GDateYear  year)
1168 {
1169   GDate d;
1170   
1171   g_return_val_if_fail (g_date_valid_year (year), 0);
1172   
1173   g_date_clear (&d, 1);
1174   g_date_set_dmy (&d, 1, 1, year);
1175   if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
1176   g_date_set_dmy (&d, 31, 12, year);
1177   if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
1178   if (g_date_is_leap_year (year)) 
1179     {
1180       g_date_set_dmy (&d, 2, 1, year);
1181       if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
1182       g_date_set_dmy (&d, 30, 12, year);
1183       if (g_date_weekday (&d) == G_DATE_SUNDAY) return 53;
1184     }
1185   return 52;
1186 }
1187
1188 gint         
1189 g_date_compare (GDate     *lhs, 
1190                 GDate     *rhs)
1191 {
1192   g_return_val_if_fail (lhs != NULL, 0);
1193   g_return_val_if_fail (rhs != NULL, 0);
1194   g_return_val_if_fail (g_date_valid (lhs), 0);
1195   g_return_val_if_fail (g_date_valid (rhs), 0);
1196   
1197   /* Remember the self-comparison case! I think it works right now. */
1198   
1199   while (TRUE)
1200     {
1201       
1202       if (lhs->julian && rhs->julian) 
1203         {
1204           if (lhs->julian_days < rhs->julian_days) return -1;
1205           else if (lhs->julian_days > rhs->julian_days) return 1;
1206           else                                          return 0;
1207         }
1208       else if (lhs->dmy && rhs->dmy) 
1209         {
1210           if (lhs->year < rhs->year)               return -1;
1211           else if (lhs->year > rhs->year)               return 1;
1212           else 
1213             {
1214               if (lhs->month < rhs->month)         return -1;
1215               else if (lhs->month > rhs->month)         return 1;
1216               else 
1217                 {
1218                   if (lhs->day < rhs->day)              return -1;
1219                   else if (lhs->day > rhs->day)              return 1;
1220                   else                                       return 0;
1221                 }
1222               
1223             }
1224           
1225         }
1226       else
1227         {
1228           if (!lhs->julian) g_date_update_julian (lhs);
1229           if (!rhs->julian) g_date_update_julian (rhs);
1230           g_return_val_if_fail (lhs->julian, 0);
1231           g_return_val_if_fail (rhs->julian, 0);
1232         }
1233       
1234     }
1235   return 0; /* warnings */
1236 }
1237
1238
1239 void        
1240 g_date_to_struct_tm (GDate      *d, 
1241                      struct tm   *tm)
1242 {
1243   GDateWeekday day;
1244      
1245   g_return_if_fail (d != NULL);
1246   g_return_if_fail (g_date_valid (d));
1247   g_return_if_fail (tm != NULL);
1248   
1249   if (!d->dmy) 
1250     {
1251       g_date_update_dmy (d);
1252     }
1253   g_return_if_fail (d->dmy);
1254   
1255   /* zero all the irrelevant fields to be sure they're valid */
1256   
1257   /* On Linux and maybe other systems, there are weird non-POSIX
1258    * fields on the end of struct tm that choke strftime if they
1259    * contain garbage.  So we need to 0 the entire struct, not just the
1260    * fields we know to exist. 
1261    */
1262   
1263   memset (tm, 0x0, sizeof (struct tm));
1264   
1265   tm->tm_mday = d->day;
1266   tm->tm_mon  = d->month - 1; /* 0-11 goes in tm */
1267   tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1268   
1269   day = g_date_weekday (d);
1270   if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1271   
1272   tm->tm_wday = (int)day;
1273   
1274   tm->tm_yday = g_date_day_of_year (d) - 1; /* 0 to 365 */
1275   tm->tm_isdst = -1; /* -1 means "information not available" */
1276 }
1277
1278 gsize     
1279 g_date_strftime (gchar       *s, 
1280                  gsize        slen, 
1281                  const gchar *format, 
1282                  GDate       *d)
1283 {
1284   struct tm tm;
1285   gsize retval;
1286   
1287   g_return_val_if_fail (d != NULL, 0);
1288   g_return_val_if_fail (g_date_valid (d), 0);
1289   g_return_val_if_fail (slen > 0, 0); 
1290   g_return_val_if_fail (format != 0, 0);
1291   g_return_val_if_fail (s != 0, 0);
1292   
1293   g_date_to_struct_tm (d, &tm);
1294   
1295   retval = strftime (s, slen, format, &tm);
1296   if (retval == 0)
1297     {
1298       /* If retval == 0, the contents of s are undefined.  We define
1299        *  them. 
1300        */
1301       s[0] = '\0';
1302     }
1303   return retval;
1304 }