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