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