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