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