Docs: don't use the type tag
[platform/upstream/glib.git] / glib / gdate.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 /* 
28  * MT safe
29  */
30
31 #include "config.h"
32 #include "glibconfig.h"
33
34 #define DEBUG_MSG(x)    /* */
35 #ifdef G_ENABLE_DEBUG
36 /* #define DEBUG_MSG(args)      g_message args ; */
37 #endif
38
39 #include <time.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <locale.h>
43
44 #ifdef G_OS_WIN32
45 #include <windows.h>
46 #endif
47
48 #include "gdate.h"
49
50 #include "gconvert.h"
51 #include "gmem.h"
52 #include "gstrfuncs.h"
53 #include "gtestutils.h"
54 #include "gthread.h"
55 #include "gunicode.h"
56
57 #ifdef G_OS_WIN32
58 #include "garray.h"
59 #endif
60
61 /**
62  * SECTION:date
63  * @title: Date and Time Functions
64  * @short_description: calendrical calculations and miscellaneous time stuff
65  *
66  * The #GDate data structure represents a day between January 1, Year 1,
67  * and sometime a few thousand years in the future (right now it will go
68  * to the year 65535 or so, but g_date_set_parse() only parses up to the
69  * year 8000 or so - just count on "a few thousand"). #GDate is meant to
70  * represent everyday dates, not astronomical dates or historical dates
71  * or ISO timestamps or the like. It extrapolates the current Gregorian
72  * calendar forward and backward in time; there is no attempt to change
73  * the calendar to match time periods or locations. #GDate does not store
74  * time information; it represents a <emphasis>day</emphasis>.
75  *
76  * The #GDate implementation has several nice features; it is only a
77  * 64-bit struct, so storing large numbers of dates is very efficient. It
78  * can keep both a Julian and day-month-year representation of the date,
79  * since some calculations are much easier with one representation or the
80  * other. A Julian representation is simply a count of days since some
81  * fixed day in the past; for #GDate the fixed day is January 1, 1 AD.
82  * ("Julian" dates in the #GDate API aren't really Julian dates in the
83  * technical sense; technically, Julian dates count from the start of the
84  * Julian period, Jan 1, 4713 BC).
85  *
86  * #GDate is simple to use. First you need a "blank" date; you can get a
87  * dynamically allocated date from g_date_new(), or you can declare an
88  * automatic variable or array and initialize it to a sane state by
89  * calling g_date_clear(). A cleared date is sane; it's safe to call
90  * g_date_set_dmy() and the other mutator functions to initialize the
91  * value of a cleared date. However, a cleared date is initially
92  * <emphasis>invalid</emphasis>, meaning that it doesn't represent a day
93  * that exists. It is undefined to call any of the date calculation
94  * routines on an invalid date. If you obtain a date from a user or other
95  * unpredictable source, you should check its validity with the
96  * g_date_valid() predicate. g_date_valid() is also used to check for
97  * errors with g_date_set_parse() and other functions that can
98  * fail. Dates can be invalidated by calling g_date_clear() again.
99  *
100  * <emphasis>It is very important to use the API to access the #GDate
101  * struct.</emphasis> Often only the day-month-year or only the Julian
102  * representation is valid. Sometimes neither is valid. Use the API.
103  *
104  * GLib also features #GDateTime which represents a precise time.
105  */
106
107 /**
108  * G_USEC_PER_SEC:
109  *
110  * Number of microseconds in one second (1 million).
111  * This macro is provided for code readability.
112  */
113
114 /**
115  * GTimeVal:
116  * @tv_sec: seconds
117  * @tv_usec: microseconds
118  *
119  * Represents a precise time, with seconds and microseconds.
120  * Similar to the struct timeval returned by the gettimeofday()
121  * UNIX system call.
122  *
123  * GLib is attempting to unify around the use of 64bit integers to
124  * represent microsecond-precision time. As such, this type will be
125  * removed from a future version of GLib.
126  */
127
128 /**
129  * GDate:
130  * @julian_days: the Julian representation of the date
131  * @julian: this bit is set if @julian_days is valid
132  * @dmy: this is set if @day, @month and @year are valid
133  * @day: the day of the day-month-year representation of the date,
134  *     as a number between 1 and 31
135  * @month: the day of the day-month-year representation of the date,
136  *     as a number between 1 and 12
137  * @year: the day of the day-month-year representation of the date
138  *
139  * Represents a day between January 1, Year 1 and a few thousand years in
140  * the future. None of its members should be accessed directly.
141  *
142  * If the #GDate-struct is obtained from g_date_new(), it will be safe
143  * to mutate but invalid and thus not safe for calendrical computations.
144  *
145  * If it's declared on the stack, it will contain garbage so must be
146  * initialized with g_date_clear(). g_date_clear() makes the date invalid
147  * but sane. An invalid date doesn't represent a day, it's "empty." A date
148  * becomes valid after you set it to a Julian day or you set a day, month,
149  * and year.
150  */
151
152 /**
153  * GTime:
154  *
155  * Simply a replacement for time_t. It has been deprecated
156  * since it is <emphasis>not</emphasis> equivalent to time_t
157  * on 64-bit platforms with a 64-bit time_t. Unrelated to #GTimer.
158  *
159  * Note that #GTime is defined to always be a 32bit integer,
160  * unlike time_t which may be 64bit on some systems. Therefore,
161  * #GTime will overflow in the year 2038, and you cannot use the
162  * address of a #GTime variable as argument to the UNIX time()
163  * function.
164  *
165  * Instead, do the following:
166  * |[
167  * time_t ttime;
168  * GTime gtime;
169  *
170  * time (&amp;ttime);
171  * gtime = (GTime)ttime;
172  * ]|
173  */
174
175 /**
176  * GDateDMY:
177  * @G_DATE_DAY: a day
178  * @G_DATE_MONTH: a month
179  * @G_DATE_YEAR: a year
180  *
181  * This enumeration isn't used in the API, but may be useful if you need
182  * to mark a number as a day, month, or year.
183  */
184
185 /**
186  * GDateDay:
187  *
188  * Integer representing a day of the month; between 1 and
189  * 31. #G_DATE_BAD_DAY represents an invalid day of the month.
190  */
191
192 /**
193  * GDateMonth:
194  * @G_DATE_BAD_MONTH: invalid value
195  * @G_DATE_JANUARY: January
196  * @G_DATE_FEBRUARY: February
197  * @G_DATE_MARCH: March
198  * @G_DATE_APRIL: April
199  * @G_DATE_MAY: May
200  * @G_DATE_JUNE: June
201  * @G_DATE_JULY: July
202  * @G_DATE_AUGUST: August
203  * @G_DATE_SEPTEMBER: September
204  * @G_DATE_OCTOBER: October
205  * @G_DATE_NOVEMBER: November
206  * @G_DATE_DECEMBER: December
207  *
208  * Enumeration representing a month; values are #G_DATE_JANUARY,
209  * #G_DATE_FEBRUARY, etc. #G_DATE_BAD_MONTH is the invalid value.
210  */
211
212 /**
213  * GDateYear:
214  *
215  * Integer representing a year; #G_DATE_BAD_YEAR is the invalid
216  * value. The year must be 1 or higher; negative (BC) years are not
217  * allowed. The year is represented with four digits.
218  */
219
220 /**
221  * GDateWeekday:
222  * @G_DATE_BAD_WEEKDAY: invalid value
223  * @G_DATE_MONDAY: Monday
224  * @G_DATE_TUESDAY: Tuesday
225  * @G_DATE_WEDNESDAY: Wednesday
226  * @G_DATE_THURSDAY: Thursday
227  * @G_DATE_FRIDAY: Friday
228  * @G_DATE_SATURDAY: Saturday
229  * @G_DATE_SUNDAY: Sunday
230  *
231  * Enumeration representing a day of the week; #G_DATE_MONDAY,
232  * #G_DATE_TUESDAY, etc. #G_DATE_BAD_WEEKDAY is an invalid weekday.
233  */
234
235 /**
236  * G_DATE_BAD_DAY:
237  *
238  * Represents an invalid #GDateDay.
239  */
240
241 /**
242  * G_DATE_BAD_JULIAN:
243  *
244  * Represents an invalid Julian day number.
245  */
246
247 /**
248  * G_DATE_BAD_YEAR:
249  *
250  * Represents an invalid year.
251  */
252
253 /**
254  * g_date_new:
255  *
256  * Allocates a #GDate and initializes
257  * it to a sane state. The new date will
258  * be cleared (as if you'd called g_date_clear()) but invalid (it won't
259  * represent an existing day). Free the return value with g_date_free().
260  *
261  * Returns: a newly-allocated #GDate
262  */
263 GDate*
264 g_date_new (void)
265 {
266   GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
267   
268   return d;
269 }
270
271 /**
272  * g_date_new_dmy:
273  * @day: day of the month
274  * @month: month of the year
275  * @year: year
276  *
277  * Like g_date_new(), but also sets the value of the date. Assuming the
278  * day-month-year triplet you pass in represents an existing day, the
279  * returned date will be valid.
280  *
281  * Returns: a newly-allocated #GDate initialized with @day, @month, and @year
282  */
283 GDate*
284 g_date_new_dmy (GDateDay   day, 
285                 GDateMonth m, 
286                 GDateYear  y)
287 {
288   GDate *d;
289   g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
290   
291   d = g_new (GDate, 1);
292   
293   d->julian = FALSE;
294   d->dmy    = TRUE;
295   
296   d->month = m;
297   d->day   = day;
298   d->year  = y;
299   
300   g_assert (g_date_valid (d));
301   
302   return d;
303 }
304
305 /**
306  * g_date_new_julian:
307  * @julian_day: days since January 1, Year 1
308  *
309  * Like g_date_new(), but also sets the value of the date. Assuming the
310  * Julian day number you pass in is valid (greater than 0, less than an
311  * unreasonably large number), the returned date will be valid.
312  *
313  * Returns: a newly-allocated #GDate initialized with @julian_day
314  */
315 GDate*
316 g_date_new_julian (guint32 julian_day)
317 {
318   GDate *d;
319   g_return_val_if_fail (g_date_valid_julian (julian_day), NULL);
320   
321   d = g_new (GDate, 1);
322   
323   d->julian = TRUE;
324   d->dmy    = FALSE;
325   
326   d->julian_days = julian_day;
327   
328   g_assert (g_date_valid (d));
329   
330   return d;
331 }
332
333 /**
334  * g_date_free:
335  * @date: a #GDate to free
336  *
337  * Frees a #GDate returned from g_date_new().
338  */
339 void
340 g_date_free (GDate *date)
341 {
342   g_return_if_fail (date != NULL);
343   
344   g_free (date);
345 }
346
347 /**
348  * g_date_valid:
349  * @date: a #GDate to check
350  *
351  * Returns %TRUE if the #GDate represents an existing day. The date must not
352  * contain garbage; it should have been initialized with g_date_clear()
353  * if it wasn't allocated by one of the g_date_new() variants.
354  *
355  * Returns: Whether the date is valid
356  */
357 gboolean     
358 g_date_valid (const GDate *d)
359 {
360   g_return_val_if_fail (d != NULL, FALSE);
361   
362   return (d->julian || d->dmy);
363 }
364
365 static const guint8 days_in_months[2][13] = 
366 {  /* error, jan feb mar apr may jun jul aug sep oct nov dec */
367   {  0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, 
368   {  0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
369 };
370
371 static const guint16 days_in_year[2][14] = 
372 {  /* 0, jan feb mar apr may  jun  jul  aug  sep  oct  nov  dec */
373   {  0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, 
374   {  0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
375 };
376
377 /**
378  * g_date_valid_month:
379  * @month: month
380  *
381  * Returns %TRUE if the month value is valid. The 12 #GDateMonth
382  * enumeration values are the only valid months.
383  *
384  * Returns: %TRUE if the month is valid
385  */
386 gboolean     
387 g_date_valid_month (GDateMonth m)
388
389   return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
390 }
391
392 /**
393  * g_date_valid_year:
394  * @year: year
395  *
396  * Returns %TRUE if the year is valid. Any year greater than 0 is valid,
397  * though there is a 16-bit limit to what #GDate will understand.
398  *
399  * Returns: %TRUE if the year is valid
400  */
401 gboolean     
402 g_date_valid_year (GDateYear y)
403 {
404   return ( y > G_DATE_BAD_YEAR );
405 }
406
407 /**
408  * g_date_valid_day:
409  * @day: day to check
410  *
411  * Returns %TRUE if the day of the month is valid (a day is valid if it's
412  * between 1 and 31 inclusive).
413  *
414  * Returns: %TRUE if the day is valid
415  */
416
417 gboolean     
418 g_date_valid_day (GDateDay d)
419 {
420   return ( (d > G_DATE_BAD_DAY) && (d < 32) );
421 }
422
423 /**
424  * g_date_valid_weekday:
425  * @weekday: weekday
426  *
427  * Returns %TRUE if the weekday is valid. The seven #GDateWeekday enumeration
428  * values are the only valid weekdays.
429  *
430  * Returns: %TRUE if the weekday is valid
431  */
432 gboolean     
433 g_date_valid_weekday (GDateWeekday w)
434 {
435   return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
436 }
437
438 /**
439  * g_date_valid_julian:
440  * @julian_date: Julian day to check
441  *
442  * Returns %TRUE if the Julian day is valid. Anything greater than zero
443  * is basically a valid Julian, though there is a 32-bit limit.
444  *
445  * Returns: %TRUE if the Julian day is valid
446  */
447 gboolean     
448 g_date_valid_julian (guint32 j)
449 {
450   return (j > G_DATE_BAD_JULIAN);
451 }
452
453 /**
454  * g_date_valid_dmy:
455  * @day: day
456  * @month: month
457  * @year: year
458  *
459  * Returns %TRUE if the day-month-year triplet forms a valid, existing day
460  * in the range of days #GDate understands (Year 1 or later, no more than
461  * a few thousand years in the future).
462  *
463  * Returns: %TRUE if the date is a valid one
464  */
465 gboolean     
466 g_date_valid_dmy (GDateDay   d, 
467                   GDateMonth m, 
468                   GDateYear  y)
469 {
470   return ( (m > G_DATE_BAD_MONTH) &&
471            (m < 13)               && 
472            (d > G_DATE_BAD_DAY)   && 
473            (y > G_DATE_BAD_YEAR)  &&   /* must check before using g_date_is_leap_year */
474            (d <=  (g_date_is_leap_year (y) ? 
475                    days_in_months[1][m] : days_in_months[0][m])) );
476 }
477
478
479 /* "Julian days" just means an absolute number of days, where Day 1 ==
480  *   Jan 1, Year 1
481  */
482 static void
483 g_date_update_julian (const GDate *const_d)
484 {
485   GDate *d = (GDate *) const_d;
486   GDateYear year;
487   gint idx;
488   
489   g_return_if_fail (d != NULL);
490   g_return_if_fail (d->dmy);
491   g_return_if_fail (!d->julian);
492   g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
493   
494   /* What we actually do is: multiply years * 365 days in the year,
495    * add the number of years divided by 4, subtract the number of
496    * years divided by 100 and add the number of years divided by 400,
497    * which accounts for leap year stuff. Code from Steffen Beyer's
498    * DateCalc. 
499    */
500   
501   year = d->year - 1; /* we know d->year > 0 since it's valid */
502   
503   d->julian_days = year * 365U;
504   d->julian_days += (year >>= 2); /* divide by 4 and add */
505   d->julian_days -= (year /= 25); /* divides original # years by 100 */
506   d->julian_days += year >> 2;    /* divides by 4, which divides original by 400 */
507   
508   idx = g_date_is_leap_year (d->year) ? 1 : 0;
509   
510   d->julian_days += days_in_year[idx][d->month] + d->day;
511   
512   g_return_if_fail (g_date_valid_julian (d->julian_days));
513   
514   d->julian = TRUE;
515 }
516
517 static void 
518 g_date_update_dmy (const GDate *const_d)
519 {
520   GDate *d = (GDate *) const_d;
521   GDateYear y;
522   GDateMonth m;
523   GDateDay day;
524   
525   guint32 A, B, C, D, E, M;
526   
527   g_return_if_fail (d != NULL);
528   g_return_if_fail (d->julian);
529   g_return_if_fail (!d->dmy);
530   g_return_if_fail (g_date_valid_julian (d->julian_days));
531   
532   /* Formula taken from the Calendar FAQ; the formula was for the
533    *  Julian Period which starts on 1 January 4713 BC, so we add
534    *  1,721,425 to the number of days before doing the formula.
535    *
536    * I'm sure this can be simplified for our 1 January 1 AD period
537    * start, but I can't figure out how to unpack the formula.  
538    */
539   
540   A = d->julian_days + 1721425 + 32045;
541   B = ( 4 *(A + 36524) )/ 146097 - 1;
542   C = A - (146097 * B)/4;
543   D = ( 4 * (C + 365) ) / 1461 - 1;
544   E = C - ((1461*D) / 4);
545   M = (5 * (E - 1) + 2)/153;
546   
547   m = M + 3 - (12*(M/10));
548   day = E - (153*M + 2)/5;
549   y = 100 * B + D - 4800 + (M/10);
550   
551 #ifdef G_ENABLE_DEBUG
552   if (!g_date_valid_dmy (day, m, y)) 
553     g_warning ("\nOOPS julian: %u  computed dmy: %u %u %u\n", 
554                d->julian_days, day, m, y);
555 #endif
556   
557   d->month = m;
558   d->day   = day;
559   d->year  = y;
560   
561   d->dmy = TRUE;
562 }
563
564 /**
565  * g_date_get_weekday:
566  * @date: a #GDate
567  *
568  * Returns the day of the week for a #GDate. The date must be valid.
569  *
570  * Returns: day of the week as a #GDateWeekday.
571  */
572 GDateWeekday 
573 g_date_get_weekday (const GDate *d)
574 {
575   g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
576   
577   if (!d->julian) 
578     g_date_update_julian (d);
579
580   g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
581   
582   return ((d->julian_days - 1) % 7) + 1;
583 }
584
585 /**
586  * g_date_get_month:
587  * @date: a #GDate to get the month from
588  *
589  * Returns the month of the year. The date must be valid.
590  *
591  * Returns: month of the year as a #GDateMonth
592  */
593 GDateMonth   
594 g_date_get_month (const GDate *d)
595 {
596   g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
597   
598   if (!d->dmy) 
599     g_date_update_dmy (d);
600
601   g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
602   
603   return d->month;
604 }
605
606 /**
607  * g_date_get_year:
608  * @date: a #GDate
609  *
610  * Returns the year of a #GDate. The date must be valid.
611  *
612  * Returns: year in which the date falls
613  */
614 GDateYear    
615 g_date_get_year (const GDate *d)
616 {
617   g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
618   
619   if (!d->dmy) 
620     g_date_update_dmy (d);
621
622   g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);  
623   
624   return d->year;
625 }
626
627 /**
628  * g_date_get_day:
629  * @date: a #GDate to extract the day of the month from
630  *
631  * Returns the day of the month. The date must be valid.
632  *
633  * Returns: day of the month
634  */
635 GDateDay     
636 g_date_get_day (const GDate *d)
637 {
638   g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
639   
640   if (!d->dmy) 
641     g_date_update_dmy (d);
642
643   g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);  
644   
645   return d->day;
646 }
647
648 /**
649  * g_date_get_julian:
650  * @date: a #GDate to extract the Julian day from
651  *
652  * Returns the Julian day or "serial number" of the #GDate. The
653  * Julian day is simply the number of days since January 1, Year 1; i.e.,
654  * January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2,
655  * etc. The date must be valid.
656  *
657  * Returns: Julian day
658  */
659 guint32      
660 g_date_get_julian (const GDate *d)
661 {
662   g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
663   
664   if (!d->julian) 
665     g_date_update_julian (d);
666
667   g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);  
668   
669   return d->julian_days;
670 }
671
672 /**
673  * g_date_get_day_of_year:
674  * @date: a #GDate to extract day of year from
675  *
676  * Returns the day of the year, where Jan 1 is the first day of the
677  * year. The date must be valid.
678  *
679  * Returns: day of the year
680  */
681 guint        
682 g_date_get_day_of_year (const GDate *d)
683 {
684   gint idx;
685   
686   g_return_val_if_fail (g_date_valid (d), 0);
687   
688   if (!d->dmy) 
689     g_date_update_dmy (d);
690
691   g_return_val_if_fail (d->dmy, 0);  
692   
693   idx = g_date_is_leap_year (d->year) ? 1 : 0;
694   
695   return (days_in_year[idx][d->month] + d->day);
696 }
697
698 /**
699  * g_date_get_monday_week_of_year:
700  * @date: a #GDate
701  *
702  * Returns the week of the year, where weeks are understood to start on
703  * Monday. If the date is before the first Monday of the year, return
704  * 0. The date must be valid.
705  *
706  * Returns: week of the year
707  */
708 guint        
709 g_date_get_monday_week_of_year (const GDate *d)
710 {
711   GDateWeekday wd;
712   guint day;
713   GDate first;
714   
715   g_return_val_if_fail (g_date_valid (d), 0);
716   
717   if (!d->dmy) 
718     g_date_update_dmy (d);
719
720   g_return_val_if_fail (d->dmy, 0);  
721   
722   g_date_clear (&first, 1);
723   
724   g_date_set_dmy (&first, 1, 1, d->year);
725   
726   wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
727   day = g_date_get_day_of_year (d) - 1;
728   
729   return ((day + wd)/7U + (wd == 0 ? 1 : 0));
730 }
731
732 /**
733  * g_date_get_sunday_week_of_year:
734  * @date: a #GDate
735  *
736  * Returns the week of the year during which this date falls, if weeks
737  * are understood to being on Sunday. The date must be valid. Can return
738  * 0 if the day is before the first Sunday of the year.
739  *
740  * Returns: week number
741  */
742 guint        
743 g_date_get_sunday_week_of_year (const GDate *d)
744 {
745   GDateWeekday wd;
746   guint day;
747   GDate first;
748   
749   g_return_val_if_fail (g_date_valid (d), 0);
750   
751   if (!d->dmy) 
752     g_date_update_dmy (d);
753
754   g_return_val_if_fail (d->dmy, 0);  
755   
756   g_date_clear (&first, 1);
757   
758   g_date_set_dmy (&first, 1, 1, d->year);
759   
760   wd = g_date_get_weekday (&first);
761   if (wd == 7) wd = 0; /* make Sunday day 0 */
762   day = g_date_get_day_of_year (d) - 1;
763   
764   return ((day + wd)/7U + (wd == 0 ? 1 : 0));
765 }
766
767 /**
768  * g_date_get_iso8601_week_of_year:
769  * @date: a valid #GDate
770  *
771  * Returns the week of the year, where weeks are interpreted according
772  * to ISO 8601. 
773  * 
774  * Returns: ISO 8601 week number of the year.
775  *
776  * Since: 2.6
777  **/
778 guint
779 g_date_get_iso8601_week_of_year (const GDate *d)
780 {
781   guint j, d4, L, d1, w;
782
783   g_return_val_if_fail (g_date_valid (d), 0);
784   
785   if (!d->julian)
786     g_date_update_julian (d);
787
788   g_return_val_if_fail (d->julian, 0);
789
790   /* Formula taken from the Calendar FAQ; the formula was for the
791    * Julian Period which starts on 1 January 4713 BC, so we add
792    * 1,721,425 to the number of days before doing the formula. 
793    */
794   j  = d->julian_days + 1721425;
795   d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
796   L  = d4 / 1460;
797   d1 = ((d4 - L) % 365) + L;
798   w  = d1 / 7 + 1;
799
800   return w;
801 }
802
803 /**
804  * g_date_days_between:
805  * @date1: the first date
806  * @date2: the second date
807  *
808  * Computes the number of days between two dates.
809  * If @date2 is prior to @date1, the returned value is negative.
810  * Both dates must be valid.
811  *
812  * Returns: the number of days between @date1 and @date2
813  */
814 gint
815 g_date_days_between (const GDate *d1,
816                      const GDate *d2)
817 {
818   g_return_val_if_fail (g_date_valid (d1), 0);
819   g_return_val_if_fail (g_date_valid (d2), 0);
820
821   return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
822 }
823
824 /**
825  * g_date_clear:
826  * @date: pointer to one or more dates to clear
827  * @n_dates: number of dates to clear
828  *
829  * Initializes one or more #GDate structs to a sane but invalid
830  * state. The cleared dates will not represent an existing date, but will
831  * not contain garbage. Useful to init a date declared on the stack.
832  * Validity can be tested with g_date_valid().
833  */
834 void         
835 g_date_clear (GDate *d, guint ndates)
836 {
837   g_return_if_fail (d != NULL);
838   g_return_if_fail (ndates != 0);
839   
840   memset (d, 0x0, ndates*sizeof (GDate)); 
841 }
842
843 G_LOCK_DEFINE_STATIC (g_date_global);
844
845 /* These are for the parser, output to the user should use *
846  * g_date_strftime () - this creates more never-freed memory to annoy
847  * all those memory debugger users. :-) 
848  */
849
850 static gchar *long_month_names[13] = 
851
852   NULL,
853 };
854
855 static gchar *short_month_names[13] = 
856 {
857   NULL, 
858 };
859
860 /* This tells us if we need to update the parse info */
861 static gchar *current_locale = NULL;
862
863 /* order of these in the current locale */
864 static GDateDMY dmy_order[3] = 
865 {
866    G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
867 };
868
869 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
870  * 29 and below are counted as in the year 2000, numbers 30 and above
871  * are counted as in the year 1900.  
872  */
873
874 static const GDateYear twodigit_start_year = 1930;
875
876 /* It is impossible to enter a year between 1 AD and 99 AD with this
877  * in effect.  
878  */
879 static gboolean using_twodigit_years = FALSE;
880
881 /* Adjustment of locale era to AD, non-zero means using locale era
882  */
883 static gint locale_era_adjust = 0;
884
885 struct _GDateParseTokens {
886   gint num_ints;
887   gint n[3];
888   guint month;
889 };
890
891 typedef struct _GDateParseTokens GDateParseTokens;
892
893 #define NUM_LEN 10
894
895 /* HOLDS: g_date_global_lock */
896 static void
897 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
898 {
899   gchar num[4][NUM_LEN+1];
900   gint i;
901   const guchar *s;
902   
903   /* We count 4, but store 3; so we can give an error
904    * if there are 4.
905    */
906   num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
907   
908   s = (const guchar *) str;
909   pt->num_ints = 0;
910   while (*s && pt->num_ints < 4) 
911     {
912       
913       i = 0;
914       while (*s && g_ascii_isdigit (*s) && i < NUM_LEN)
915         {
916           num[pt->num_ints][i] = *s;
917           ++s; 
918           ++i;
919         }
920       
921       if (i > 0) 
922         {
923           num[pt->num_ints][i] = '\0';
924           ++(pt->num_ints);
925         }
926       
927       if (*s == '\0') break;
928       
929       ++s;
930     }
931   
932   pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
933   pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
934   pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
935   
936   pt->month = G_DATE_BAD_MONTH;
937   
938   if (pt->num_ints < 3)
939     {
940       gchar *casefold;
941       gchar *normalized;
942       
943       casefold = g_utf8_casefold (str, -1);
944       normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
945       g_free (casefold);
946
947       i = 1;
948       while (i < 13)
949         {
950           if (long_month_names[i] != NULL) 
951             {
952               const gchar *found = strstr (normalized, long_month_names[i]);
953               
954               if (found != NULL)
955                 {
956                   pt->month = i;
957                   break;
958                 }
959             }
960           
961           if (short_month_names[i] != NULL) 
962             {
963               const gchar *found = strstr (normalized, short_month_names[i]);
964               
965               if (found != NULL)
966                 {
967                   pt->month = i;
968                   break;
969                 }
970             }
971
972           ++i;
973         }
974
975       g_free (normalized);
976     }
977 }
978
979 /* HOLDS: g_date_global_lock */
980 static void
981 g_date_prepare_to_parse (const gchar      *str, 
982                          GDateParseTokens *pt)
983 {
984   const gchar *locale = setlocale (LC_TIME, NULL);
985   gboolean recompute_localeinfo = FALSE;
986   GDate d;
987   
988   g_return_if_fail (locale != NULL); /* should not happen */
989   
990   g_date_clear (&d, 1);              /* clear for scratch use */
991   
992   if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) ) 
993     recompute_localeinfo = TRUE;  /* Uh, there used to be a reason for the temporary */
994   
995   if (recompute_localeinfo)
996     {
997       int i = 1;
998       GDateParseTokens testpt;
999       gchar buf[128];
1000       
1001       g_free (current_locale); /* still works if current_locale == NULL */
1002       
1003       current_locale = g_strdup (locale);
1004       
1005       short_month_names[0] = "Error";
1006       long_month_names[0] = "Error";
1007
1008       while (i < 13) 
1009         {
1010           gchar *casefold;
1011           
1012           g_date_set_dmy (&d, 1, i, 1);
1013           
1014           g_return_if_fail (g_date_valid (&d));
1015           
1016           g_date_strftime (buf, 127, "%b", &d);
1017
1018           casefold = g_utf8_casefold (buf, -1);
1019           g_free (short_month_names[i]);
1020           short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1021           g_free (casefold);
1022           
1023           g_date_strftime (buf, 127, "%B", &d);
1024           casefold = g_utf8_casefold (buf, -1);
1025           g_free (long_month_names[i]);
1026           long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1027           g_free (casefold);
1028           
1029           ++i;
1030         }
1031       
1032       /* Determine DMY order */
1033       
1034       /* had to pick a random day - don't change this, some strftimes
1035        * are broken on some days, and this one is good so far. */
1036       g_date_set_dmy (&d, 4, 7, 1976);
1037       
1038       g_date_strftime (buf, 127, "%x", &d);
1039       
1040       g_date_fill_parse_tokens (buf, &testpt);
1041       
1042       i = 0;
1043       while (i < testpt.num_ints)
1044         {
1045           switch (testpt.n[i])
1046             {
1047             case 7:
1048               dmy_order[i] = G_DATE_MONTH;
1049               break;
1050             case 4:
1051               dmy_order[i] = G_DATE_DAY;
1052               break;
1053             case 76:
1054               using_twodigit_years = TRUE; /* FALL THRU */
1055             case 1976:
1056               dmy_order[i] = G_DATE_YEAR;
1057               break;
1058             default:
1059               /* assume locale era */
1060               locale_era_adjust = 1976 - testpt.n[i];
1061               dmy_order[i] = G_DATE_YEAR;
1062               break;
1063             }
1064           ++i;
1065         }
1066       
1067 #if defined(G_ENABLE_DEBUG) && 0
1068       DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
1069       i = 1;
1070       while (i < 13) 
1071         {
1072           DEBUG_MSG (("  %s   %s", long_month_names[i], short_month_names[i]));
1073           ++i;
1074         }
1075       if (using_twodigit_years)
1076         {
1077           DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
1078         }
1079       { 
1080         gchar *strings[3];
1081         i = 0;
1082         while (i < 3)
1083           {
1084             switch (dmy_order[i])
1085               {
1086               case G_DATE_MONTH:
1087                 strings[i] = "Month";
1088                 break;
1089               case G_DATE_YEAR:
1090                 strings[i] = "Year";
1091                 break;
1092               case G_DATE_DAY:
1093                 strings[i] = "Day";
1094                 break;
1095               default:
1096                 strings[i] = NULL;
1097                 break;
1098               }
1099             ++i;
1100           }
1101         DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
1102         DEBUG_MSG (("**Sample date in this locale: '%s'", buf));
1103       }
1104 #endif
1105     }
1106   
1107   g_date_fill_parse_tokens (str, pt);
1108 }
1109
1110 /**
1111  * g_date_set_parse:
1112  * @date: a #GDate to fill in
1113  * @str: string to parse
1114  *
1115  * Parses a user-inputted string @str, and try to figure out what date it
1116  * represents, taking the <link linkend="setlocale">current locale</link>
1117  * into account. If the string is successfully parsed, the date will be
1118  * valid after the call. Otherwise, it will be invalid. You should check
1119  * using g_date_valid() to see whether the parsing succeeded.
1120  *
1121  * This function is not appropriate for file formats and the like; it
1122  * isn't very precise, and its exact behavior varies with the locale.
1123  * It's intended to be a heuristic routine that guesses what the user
1124  * means by a given string (and it does work pretty well in that
1125  * capacity).
1126  */
1127 void         
1128 g_date_set_parse (GDate       *d, 
1129                   const gchar *str)
1130 {
1131   GDateParseTokens pt;
1132   guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
1133   
1134   g_return_if_fail (d != NULL);
1135   
1136   /* set invalid */
1137   g_date_clear (d, 1);
1138   
1139   G_LOCK (g_date_global);
1140
1141   g_date_prepare_to_parse (str, &pt);
1142   
1143   DEBUG_MSG (("Found %d ints, '%d' '%d' '%d' and written out month %d",
1144               pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
1145   
1146   
1147   if (pt.num_ints == 4) 
1148     {
1149       G_UNLOCK (g_date_global);
1150       return; /* presumably a typo; bail out. */
1151     }
1152   
1153   if (pt.num_ints > 1)
1154     {
1155       int i = 0;
1156       int j = 0;
1157       
1158       g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
1159       
1160       while (i < pt.num_ints && j < 3) 
1161         {
1162           switch (dmy_order[j])
1163             {
1164             case G_DATE_MONTH:
1165             {
1166               if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
1167                 {
1168                   m = pt.month;
1169                   ++j;      /* skip months, but don't skip this number */
1170                   continue;
1171                 }
1172               else 
1173                 m = pt.n[i];
1174             }
1175             break;
1176             case G_DATE_DAY:
1177             {
1178               if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
1179                 {
1180                   day = 1;
1181                   ++j;      /* skip days, since we may have month/year */
1182                   continue;
1183                 }
1184               day = pt.n[i];
1185             }
1186             break;
1187             case G_DATE_YEAR:
1188             {
1189               y  = pt.n[i];
1190               
1191               if (locale_era_adjust != 0)
1192                 {
1193                   y += locale_era_adjust;
1194                 }
1195               else if (using_twodigit_years && y < 100)
1196                 {
1197                   guint two     =  twodigit_start_year % 100;
1198                   guint century = (twodigit_start_year / 100) * 100;
1199                   
1200                   if (y < two)
1201                     century += 100;
1202                   
1203                   y += century;
1204                 }
1205             }
1206             break;
1207             default:
1208               break;
1209             }
1210           
1211           ++i;
1212           ++j;
1213         }
1214       
1215       
1216       if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
1217         {
1218           /* Try YYYY MM DD */
1219           y   = pt.n[0];
1220           m   = pt.n[1];
1221           day = pt.n[2];
1222           
1223           if (using_twodigit_years && y < 100) 
1224             y = G_DATE_BAD_YEAR; /* avoids ambiguity */
1225         }
1226       else if (pt.num_ints == 2)
1227         {
1228           if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
1229             m = pt.month;
1230         }
1231     }
1232   else if (pt.num_ints == 1) 
1233     {
1234       if (pt.month != G_DATE_BAD_MONTH)
1235         {
1236           /* Month name and year? */
1237           m    = pt.month;
1238           day  = 1;
1239           y = pt.n[0];
1240         }
1241       else
1242         {
1243           /* Try yyyymmdd and yymmdd */
1244           
1245           m   = (pt.n[0]/100) % 100;
1246           day = pt.n[0] % 100;
1247           y   = pt.n[0]/10000;
1248           
1249           /* FIXME move this into a separate function */
1250           if (using_twodigit_years && y < 100)
1251             {
1252               guint two     =  twodigit_start_year % 100;
1253               guint century = (twodigit_start_year / 100) * 100;
1254               
1255               if (y < two)
1256                 century += 100;
1257               
1258               y += century;
1259             }
1260         }
1261     }
1262   
1263   /* See if we got anything valid out of all this. */
1264   /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
1265   if (y < 8000 && g_date_valid_dmy (day, m, y)) 
1266     {
1267       d->month = m;
1268       d->day   = day;
1269       d->year  = y;
1270       d->dmy   = TRUE;
1271     }
1272 #ifdef G_ENABLE_DEBUG
1273   else 
1274     {
1275       DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
1276     }
1277 #endif
1278   G_UNLOCK (g_date_global);
1279 }
1280
1281 /**
1282  * g_date_set_time_t:
1283  * @date: a #GDate 
1284  * @timet: time_t value to set
1285  *
1286  * Sets the value of a date to the date corresponding to a time 
1287  * specified as a time_t. The time to date conversion is done using 
1288  * the user's current timezone.
1289  *
1290  * To set the value of a date to the current day, you could write:
1291  * |[
1292  *  g_date_set_time_t (date, time (NULL)); 
1293  * ]|
1294  *
1295  * Since: 2.10
1296  */
1297 void         
1298 g_date_set_time_t (GDate *date,
1299                    time_t timet)
1300 {
1301   struct tm tm;
1302   
1303   g_return_if_fail (date != NULL);
1304   
1305 #ifdef HAVE_LOCALTIME_R
1306   localtime_r (&timet, &tm);
1307 #else
1308   {
1309     struct tm *ptm = localtime (&timet);
1310
1311     if (ptm == NULL)
1312       {
1313         /* Happens at least in Microsoft's C library if you pass a
1314          * negative time_t. Use 2000-01-01 as default date.
1315          */
1316 #ifndef G_DISABLE_CHECKS
1317         g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
1318 #endif
1319
1320         tm.tm_mon = 0;
1321         tm.tm_mday = 1;
1322         tm.tm_year = 100;
1323       }
1324     else
1325       memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
1326   }
1327 #endif
1328   
1329   date->julian = FALSE;
1330   
1331   date->month = tm.tm_mon + 1;
1332   date->day   = tm.tm_mday;
1333   date->year  = tm.tm_year + 1900;
1334   
1335   g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
1336   
1337   date->dmy    = TRUE;
1338 }
1339
1340
1341 /**
1342  * g_date_set_time:
1343  * @date: a #GDate.
1344  * @time_: #GTime value to set.
1345  *
1346  * Sets the value of a date from a #GTime value.
1347  * The time to date conversion is done using the user's current timezone.
1348  *
1349  * Deprecated: 2.10: Use g_date_set_time_t() instead.
1350  */
1351 void
1352 g_date_set_time (GDate *date,
1353                  GTime  time_)
1354 {
1355   g_date_set_time_t (date, (time_t) time_);
1356 }
1357
1358 /**
1359  * g_date_set_time_val:
1360  * @date: a #GDate 
1361  * @timeval: #GTimeVal value to set
1362  *
1363  * Sets the value of a date from a #GTimeVal value.  Note that the
1364  * @tv_usec member is ignored, because #GDate can't make use of the
1365  * additional precision.
1366  *
1367  * The time to date conversion is done using the user's current timezone.
1368  *
1369  * Since: 2.10
1370  */
1371 void
1372 g_date_set_time_val (GDate    *date,
1373                      GTimeVal *timeval)
1374 {
1375   g_date_set_time_t (date, (time_t) timeval->tv_sec);
1376 }
1377
1378 /**
1379  * g_date_set_month:
1380  * @date: a #GDate
1381  * @month: month to set
1382  *
1383  * Sets the month of the year for a #GDate.  If the resulting
1384  * day-month-year triplet is invalid, the date will be invalid.
1385  */
1386 void         
1387 g_date_set_month (GDate     *d, 
1388                   GDateMonth m)
1389 {
1390   g_return_if_fail (d != NULL);
1391   g_return_if_fail (g_date_valid_month (m));
1392
1393   if (d->julian && !d->dmy) g_date_update_dmy(d);
1394   d->julian = FALSE;
1395   
1396   d->month = m;
1397   
1398   if (g_date_valid_dmy (d->day, d->month, d->year))
1399     d->dmy = TRUE;
1400   else 
1401     d->dmy = FALSE;
1402 }
1403
1404 /**
1405  * g_date_set_day:
1406  * @date: a #GDate
1407  * @day: day to set
1408  *
1409  * Sets the day of the month for a #GDate. If the resulting
1410  * day-month-year triplet is invalid, the date will be invalid.
1411  */
1412 void         
1413 g_date_set_day (GDate    *d, 
1414                 GDateDay  day)
1415 {
1416   g_return_if_fail (d != NULL);
1417   g_return_if_fail (g_date_valid_day (day));
1418   
1419   if (d->julian && !d->dmy) g_date_update_dmy(d);
1420   d->julian = FALSE;
1421   
1422   d->day = day;
1423   
1424   if (g_date_valid_dmy (d->day, d->month, d->year))
1425     d->dmy = TRUE;
1426   else 
1427     d->dmy = FALSE;
1428 }
1429
1430 /**
1431  * g_date_set_year:
1432  * @date: a #GDate
1433  * @year: year to set
1434  *
1435  * Sets the year for a #GDate. If the resulting day-month-year
1436  * triplet is invalid, the date will be invalid.
1437  */
1438 void         
1439 g_date_set_year (GDate     *d, 
1440                  GDateYear  y)
1441 {
1442   g_return_if_fail (d != NULL);
1443   g_return_if_fail (g_date_valid_year (y));
1444   
1445   if (d->julian && !d->dmy) g_date_update_dmy(d);
1446   d->julian = FALSE;
1447   
1448   d->year = y;
1449   
1450   if (g_date_valid_dmy (d->day, d->month, d->year))
1451     d->dmy = TRUE;
1452   else 
1453     d->dmy = FALSE;
1454 }
1455
1456 /**
1457  * g_date_set_dmy:
1458  * @date: a #GDate
1459  * @day: day
1460  * @month: month
1461  * @y: year
1462  *
1463  * Sets the value of a #GDate from a day, month, and year.
1464  * The day-month-year triplet must be valid; if you aren't
1465  * sure it is, call g_date_valid_dmy() to check before you
1466  * set it.
1467  */
1468 void         
1469 g_date_set_dmy (GDate      *d, 
1470                 GDateDay    day, 
1471                 GDateMonth  m, 
1472                 GDateYear   y)
1473 {
1474   g_return_if_fail (d != NULL);
1475   g_return_if_fail (g_date_valid_dmy (day, m, y));
1476   
1477   d->julian = FALSE;
1478   
1479   d->month = m;
1480   d->day   = day;
1481   d->year  = y;
1482   
1483   d->dmy = TRUE;
1484 }
1485
1486 /**
1487  * g_date_set_julian:
1488  * @date: a #GDate
1489  * @julian_date: Julian day number (days since January 1, Year 1)
1490  *
1491  * Sets the value of a #GDate from a Julian day number.
1492  */
1493 void         
1494 g_date_set_julian (GDate   *d, 
1495                    guint32  j)
1496 {
1497   g_return_if_fail (d != NULL);
1498   g_return_if_fail (g_date_valid_julian (j));
1499   
1500   d->julian_days = j;
1501   d->julian = TRUE;
1502   d->dmy = FALSE;
1503 }
1504
1505 /**
1506  * g_date_is_first_of_month:
1507  * @date: a #GDate to check
1508  *
1509  * Returns %TRUE if the date is on the first of a month.
1510  * The date must be valid.
1511  *
1512  * Returns: %TRUE if the date is the first of the month
1513  */
1514 gboolean     
1515 g_date_is_first_of_month (const GDate *d)
1516 {
1517   g_return_val_if_fail (g_date_valid (d), FALSE);
1518   
1519   if (!d->dmy) 
1520     g_date_update_dmy (d);
1521
1522   g_return_val_if_fail (d->dmy, FALSE);  
1523   
1524   if (d->day == 1) return TRUE;
1525   else return FALSE;
1526 }
1527
1528 /**
1529  * g_date_is_last_of_month:
1530  * @date: a #GDate to check
1531  *
1532  * Returns %TRUE if the date is the last day of the month.
1533  * The date must be valid.
1534  *
1535  * Returns: %TRUE if the date is the last day of the month
1536  */
1537 gboolean     
1538 g_date_is_last_of_month (const GDate *d)
1539 {
1540   gint idx;
1541   
1542   g_return_val_if_fail (g_date_valid (d), FALSE);
1543   
1544   if (!d->dmy) 
1545     g_date_update_dmy (d);
1546
1547   g_return_val_if_fail (d->dmy, FALSE);  
1548   
1549   idx = g_date_is_leap_year (d->year) ? 1 : 0;
1550   
1551   if (d->day == days_in_months[idx][d->month]) return TRUE;
1552   else return FALSE;
1553 }
1554
1555 /**
1556  * g_date_add_days:
1557  * @date: a #GDate to increment
1558  * @n_days: number of days to move the date forward
1559  *
1560  * Increments a date some number of days.
1561  * To move forward by weeks, add weeks*7 days.
1562  * The date must be valid.
1563  */
1564 void         
1565 g_date_add_days (GDate *d, 
1566                  guint  ndays)
1567 {
1568   g_return_if_fail (g_date_valid (d));
1569   
1570   if (!d->julian)
1571     g_date_update_julian (d);
1572
1573   g_return_if_fail (d->julian);
1574   
1575   d->julian_days += ndays;
1576   d->dmy = FALSE;
1577 }
1578
1579 /**
1580  * g_date_subtract_days:
1581  * @date: a #GDate to decrement
1582  * @n_days: number of days to move
1583  *
1584  * Moves a date some number of days into the past.
1585  * To move by weeks, just move by weeks*7 days.
1586  * The date must be valid.
1587  */
1588 void         
1589 g_date_subtract_days (GDate *d, 
1590                       guint  ndays)
1591 {
1592   g_return_if_fail (g_date_valid (d));
1593   
1594   if (!d->julian)
1595     g_date_update_julian (d);
1596
1597   g_return_if_fail (d->julian);
1598   g_return_if_fail (d->julian_days > ndays);
1599   
1600   d->julian_days -= ndays;
1601   d->dmy = FALSE;
1602 }
1603
1604 /**
1605  * g_date_add_months:
1606  * @date: a #GDate to increment
1607  * @n_months: number of months to move forward
1608  *
1609  * Increments a date by some number of months.
1610  * If the day of the month is greater than 28,
1611  * this routine may change the day of the month
1612  * (because the destination month may not have
1613  * the current day in it). The date must be valid.
1614  */
1615 void         
1616 g_date_add_months (GDate *d, 
1617                    guint  nmonths)
1618 {
1619   guint years, months;
1620   gint idx;
1621   
1622   g_return_if_fail (g_date_valid (d));
1623   
1624   if (!d->dmy) 
1625     g_date_update_dmy (d);
1626
1627   g_return_if_fail (d->dmy);  
1628   
1629   nmonths += d->month - 1;
1630   
1631   years  = nmonths/12;
1632   months = nmonths%12;
1633   
1634   d->month = months + 1;
1635   d->year  += years;
1636   
1637   idx = g_date_is_leap_year (d->year) ? 1 : 0;
1638   
1639   if (d->day > days_in_months[idx][d->month])
1640     d->day = days_in_months[idx][d->month];
1641   
1642   d->julian = FALSE;
1643   
1644   g_return_if_fail (g_date_valid (d));
1645 }
1646
1647 /**
1648  * g_date_subtract_months:
1649  * @date: a #GDate to decrement
1650  * @n_months: number of months to move
1651  *
1652  * Moves a date some number of months into the past.
1653  * If the current day of the month doesn't exist in
1654  * the destination month, the day of the month
1655  * may change. The date must be valid.
1656  */
1657 void         
1658 g_date_subtract_months (GDate *d, 
1659                         guint  nmonths)
1660 {
1661   guint years, months;
1662   gint idx;
1663   
1664   g_return_if_fail (g_date_valid (d));
1665   
1666   if (!d->dmy) 
1667     g_date_update_dmy (d);
1668
1669   g_return_if_fail (d->dmy);  
1670   
1671   years  = nmonths/12;
1672   months = nmonths%12;
1673   
1674   g_return_if_fail (d->year > years);
1675   
1676   d->year  -= years;
1677   
1678   if (d->month > months) d->month -= months;
1679   else 
1680     {
1681       months -= d->month;
1682       d->month = 12 - months;
1683       d->year -= 1;
1684     }
1685   
1686   idx = g_date_is_leap_year (d->year) ? 1 : 0;
1687   
1688   if (d->day > days_in_months[idx][d->month])
1689     d->day = days_in_months[idx][d->month];
1690   
1691   d->julian = FALSE;
1692   
1693   g_return_if_fail (g_date_valid (d));
1694 }
1695
1696 /**
1697  * g_date_add_years:
1698  * @date: a #GDate to increment
1699  * @n_years: number of years to move forward
1700  *
1701  * Increments a date by some number of years.
1702  * If the date is February 29, and the destination
1703  * year is not a leap year, the date will be changed
1704  * to February 28. The date must be valid.
1705  */
1706 void         
1707 g_date_add_years (GDate *d, 
1708                   guint  nyears)
1709 {
1710   g_return_if_fail (g_date_valid (d));
1711   
1712   if (!d->dmy) 
1713     g_date_update_dmy (d);
1714
1715   g_return_if_fail (d->dmy);  
1716   
1717   d->year += nyears;
1718   
1719   if (d->month == 2 && d->day == 29)
1720     {
1721       if (!g_date_is_leap_year (d->year))
1722         d->day = 28;
1723     }
1724   
1725   d->julian = FALSE;
1726 }
1727
1728 /**
1729  * g_date_subtract_years:
1730  * @date: a #GDate to decrement
1731  * @n_years: number of years to move
1732  *
1733  * Moves a date some number of years into the past.
1734  * If the current day doesn't exist in the destination
1735  * year (i.e. it's February 29 and you move to a non-leap-year)
1736  * then the day is changed to February 29. The date
1737  * must be valid.
1738  */
1739 void         
1740 g_date_subtract_years (GDate *d, 
1741                        guint  nyears)
1742 {
1743   g_return_if_fail (g_date_valid (d));
1744   
1745   if (!d->dmy) 
1746     g_date_update_dmy (d);
1747
1748   g_return_if_fail (d->dmy);  
1749   g_return_if_fail (d->year > nyears);
1750   
1751   d->year -= nyears;
1752   
1753   if (d->month == 2 && d->day == 29)
1754     {
1755       if (!g_date_is_leap_year (d->year))
1756         d->day = 28;
1757     }
1758   
1759   d->julian = FALSE;
1760 }
1761
1762 /**
1763  * g_date_is_leap_year:
1764  * @year: year to check
1765  *
1766  * Returns %TRUE if the year is a leap year.
1767  *
1768  * For the purposes of this function, leap year is every year
1769  * divisible by 4 unless that year is divisible by 100. If it
1770  * is divisible by 100 it would be a leap year only if that year
1771  * is also divisible by 400.
1772  *
1773  * Returns: %TRUE if the year is a leap year
1774  */
1775 gboolean     
1776 g_date_is_leap_year (GDateYear year)
1777 {
1778   g_return_val_if_fail (g_date_valid_year (year), FALSE);
1779   
1780   return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1781            (year % 400) == 0 );
1782 }
1783
1784 /**
1785  * g_date_get_days_in_month:
1786  * @month: month
1787  * @year: year
1788  *
1789  * Returns the number of days in a month, taking leap
1790  * years into account.
1791  *
1792  * Returns: number of days in @month during the @year
1793  */
1794 guint8         
1795 g_date_get_days_in_month (GDateMonth month, 
1796                           GDateYear  year)
1797 {
1798   gint idx;
1799   
1800   g_return_val_if_fail (g_date_valid_year (year), 0);
1801   g_return_val_if_fail (g_date_valid_month (month), 0);
1802   
1803   idx = g_date_is_leap_year (year) ? 1 : 0;
1804   
1805   return days_in_months[idx][month];
1806 }
1807
1808 /**
1809  * g_date_get_monday_weeks_in_year:
1810  * @year: a year
1811  *
1812  * Returns the number of weeks in the year, where weeks
1813  * are taken to start on Monday. Will be 52 or 53. The
1814  * date must be valid. (Years always have 52 7-day periods,
1815  * plus 1 or 2 extra days depending on whether it's a leap
1816  * year. This function is basically telling you how many
1817  * Mondays are in the year, i.e. there are 53 Mondays if
1818  * one of the extra days happens to be a Monday.)
1819  *
1820  * Returns: number of Mondays in the year
1821  */
1822 guint8       
1823 g_date_get_monday_weeks_in_year (GDateYear year)
1824 {
1825   GDate d;
1826   
1827   g_return_val_if_fail (g_date_valid_year (year), 0);
1828   
1829   g_date_clear (&d, 1);
1830   g_date_set_dmy (&d, 1, 1, year);
1831   if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1832   g_date_set_dmy (&d, 31, 12, year);
1833   if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1834   if (g_date_is_leap_year (year)) 
1835     {
1836       g_date_set_dmy (&d, 2, 1, year);
1837       if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1838       g_date_set_dmy (&d, 30, 12, year);
1839       if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1840     }
1841   return 52;
1842 }
1843
1844 /**
1845  * g_date_get_sunday_weeks_in_year:
1846  * @year: year to count weeks in
1847  *
1848  * Returns the number of weeks in the year, where weeks
1849  * are taken to start on Sunday. Will be 52 or 53. The
1850  * date must be valid. (Years always have 52 7-day periods,
1851  * plus 1 or 2 extra days depending on whether it's a leap
1852  * year. This function is basically telling you how many
1853  * Sundays are in the year, i.e. there are 53 Sundays if
1854  * one of the extra days happens to be a Sunday.)
1855  *
1856  * Returns: the number of weeks in @year
1857  */
1858 guint8       
1859 g_date_get_sunday_weeks_in_year (GDateYear year)
1860 {
1861   GDate d;
1862   
1863   g_return_val_if_fail (g_date_valid_year (year), 0);
1864   
1865   g_date_clear (&d, 1);
1866   g_date_set_dmy (&d, 1, 1, year);
1867   if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1868   g_date_set_dmy (&d, 31, 12, year);
1869   if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1870   if (g_date_is_leap_year (year)) 
1871     {
1872       g_date_set_dmy (&d, 2, 1, year);
1873       if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1874       g_date_set_dmy (&d, 30, 12, year);
1875       if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1876     }
1877   return 52;
1878 }
1879
1880 /**
1881  * g_date_compare:
1882  * @lhs: first date to compare
1883  * @rhs: second date to compare
1884  *
1885  * qsort()-style comparison function for dates.
1886  * Both dates must be valid.
1887  *
1888  * Returns: 0 for equal, less than zero if @lhs is less than @rhs,
1889  *     greater than zero if @lhs is greater than @rhs
1890  */
1891 gint         
1892 g_date_compare (const GDate *lhs, 
1893                 const GDate *rhs)
1894 {
1895   g_return_val_if_fail (lhs != NULL, 0);
1896   g_return_val_if_fail (rhs != NULL, 0);
1897   g_return_val_if_fail (g_date_valid (lhs), 0);
1898   g_return_val_if_fail (g_date_valid (rhs), 0);
1899   
1900   /* Remember the self-comparison case! I think it works right now. */
1901   
1902   while (TRUE)
1903     {
1904       if (lhs->julian && rhs->julian) 
1905         {
1906           if (lhs->julian_days < rhs->julian_days) return -1;
1907           else if (lhs->julian_days > rhs->julian_days) return 1;
1908           else                                          return 0;
1909         }
1910       else if (lhs->dmy && rhs->dmy) 
1911         {
1912           if (lhs->year < rhs->year)               return -1;
1913           else if (lhs->year > rhs->year)               return 1;
1914           else 
1915             {
1916               if (lhs->month < rhs->month)         return -1;
1917               else if (lhs->month > rhs->month)         return 1;
1918               else 
1919                 {
1920                   if (lhs->day < rhs->day)              return -1;
1921                   else if (lhs->day > rhs->day)              return 1;
1922                   else                                       return 0;
1923                 }
1924               
1925             }
1926           
1927         }
1928       else
1929         {
1930           if (!lhs->julian) g_date_update_julian (lhs);
1931           if (!rhs->julian) g_date_update_julian (rhs);
1932           g_return_val_if_fail (lhs->julian, 0);
1933           g_return_val_if_fail (rhs->julian, 0);
1934         }
1935       
1936     }
1937   return 0; /* warnings */
1938 }
1939
1940 /**
1941  * g_date_to_struct_tm:
1942  * @date: a #GDate to set the struct tm from
1943  * @tm: struct tm to fill
1944  *
1945  * Fills in the date-related bits of a struct tm using the @date value.
1946  * Initializes the non-date parts with something sane but meaningless.
1947  */
1948 void        
1949 g_date_to_struct_tm (const GDate *d, 
1950                      struct tm   *tm)
1951 {
1952   GDateWeekday day;
1953      
1954   g_return_if_fail (g_date_valid (d));
1955   g_return_if_fail (tm != NULL);
1956   
1957   if (!d->dmy) 
1958     g_date_update_dmy (d);
1959
1960   g_return_if_fail (d->dmy);
1961   
1962   /* zero all the irrelevant fields to be sure they're valid */
1963   
1964   /* On Linux and maybe other systems, there are weird non-POSIX
1965    * fields on the end of struct tm that choke strftime if they
1966    * contain garbage.  So we need to 0 the entire struct, not just the
1967    * fields we know to exist. 
1968    */
1969   
1970   memset (tm, 0x0, sizeof (struct tm));
1971   
1972   tm->tm_mday = d->day;
1973   tm->tm_mon  = d->month - 1; /* 0-11 goes in tm */
1974   tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1975   
1976   day = g_date_get_weekday (d);
1977   if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1978   
1979   tm->tm_wday = (int)day;
1980   
1981   tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
1982   tm->tm_isdst = -1; /* -1 means "information not available" */
1983 }
1984
1985 /**
1986  * g_date_clamp:
1987  * @date: a #GDate to clamp
1988  * @min_date: minimum accepted value for @date
1989  * @max_date: maximum accepted value for @date
1990  *
1991  * If @date is prior to @min_date, sets @date equal to @min_date.
1992  * If @date falls after @max_date, sets @date equal to @max_date.
1993  * Otherwise, @date is unchanged.
1994  * Either of @min_date and @max_date may be %NULL.
1995  * All non-%NULL dates must be valid.
1996  */
1997 void
1998 g_date_clamp (GDate       *date,
1999               const GDate *min_date,
2000               const GDate *max_date)
2001 {
2002   g_return_if_fail (g_date_valid (date));
2003
2004   if (min_date != NULL)
2005     g_return_if_fail (g_date_valid (min_date));
2006
2007   if (max_date != NULL)
2008     g_return_if_fail (g_date_valid (max_date));
2009
2010   if (min_date != NULL && max_date != NULL)
2011     g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
2012
2013   if (min_date && g_date_compare (date, min_date) < 0)
2014     *date = *min_date;
2015
2016   if (max_date && g_date_compare (max_date, date) < 0)
2017     *date = *max_date;
2018 }
2019
2020 /**
2021  * g_date_order:
2022  * @date1: the first date
2023  * @date2: the second date
2024  *
2025  * Checks if @date1 is less than or equal to @date2,
2026  * and swap the values if this is not the case.
2027  */
2028 void
2029 g_date_order (GDate *date1,
2030               GDate *date2)
2031 {
2032   g_return_if_fail (g_date_valid (date1));
2033   g_return_if_fail (g_date_valid (date2));
2034
2035   if (g_date_compare (date1, date2) > 0)
2036     {
2037       GDate tmp = *date1;
2038       *date1 = *date2;
2039       *date2 = tmp;
2040     }
2041 }
2042
2043 #ifdef G_OS_WIN32
2044 static gsize
2045 win32_strftime_helper (const GDate     *d,
2046                        const gchar     *format,
2047                        const struct tm *tm,
2048                        gchar           *s,
2049                        gsize            slen)
2050 {
2051   SYSTEMTIME systemtime;
2052   TIME_ZONE_INFORMATION tzinfo;
2053   LCID lcid;
2054   int n, k;
2055   GArray *result;
2056   const gchar *p;
2057   gunichar c;
2058   const wchar_t digits[] = L"0123456789";
2059   gchar *convbuf;
2060   glong convlen = 0;
2061   gsize retval;
2062
2063   systemtime.wYear = tm->tm_year + 1900;
2064   systemtime.wMonth = tm->tm_mon + 1;
2065   systemtime.wDayOfWeek = tm->tm_wday;
2066   systemtime.wDay = tm->tm_mday;
2067   systemtime.wHour = tm->tm_hour;
2068   systemtime.wMinute = tm->tm_min;
2069   systemtime.wSecond = tm->tm_sec;
2070   systemtime.wMilliseconds = 0;
2071   
2072   lcid = GetThreadLocale ();
2073   result = g_array_sized_new (FALSE, FALSE, sizeof (wchar_t), MAX (128, strlen (format) * 2));
2074
2075   p = format;
2076   while (*p)
2077     {
2078       c = g_utf8_get_char (p);
2079       if (c == '%')
2080         {
2081           p = g_utf8_next_char (p);
2082           if (!*p)
2083             {
2084               s[0] = '\0';
2085               g_array_free (result, TRUE);
2086
2087               return 0;
2088             }
2089           
2090           c = g_utf8_get_char (p);
2091           if (c == 'E' || c == 'O')
2092             {
2093               /* Ignore modified conversion specifiers for now. */
2094               p = g_utf8_next_char (p);
2095               if (!*p)
2096                 {
2097                   s[0] = '\0';
2098                   g_array_free (result, TRUE);
2099                   
2100                   return 0;
2101                 }
2102
2103               c = g_utf8_get_char (p);
2104             }
2105
2106           switch (c)
2107             {
2108             case 'a':
2109               if (systemtime.wDayOfWeek == 0)
2110                 k = 6;
2111               else
2112                 k = systemtime.wDayOfWeek - 1;
2113               n = GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, NULL, 0);
2114               g_array_set_size (result, result->len + n);
2115               GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
2116               g_array_set_size (result, result->len - 1);
2117               break;
2118             case 'A':
2119               if (systemtime.wDayOfWeek == 0)
2120                 k = 6;
2121               else
2122                 k = systemtime.wDayOfWeek - 1;
2123               n = GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, NULL, 0);
2124               g_array_set_size (result, result->len + n);
2125               GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
2126               g_array_set_size (result, result->len - 1);
2127               break;
2128             case 'b':
2129             case 'h':
2130               n = GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, NULL, 0);
2131               g_array_set_size (result, result->len + n);
2132               GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
2133               g_array_set_size (result, result->len - 1);
2134               break;
2135             case 'B':
2136               n = GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, NULL, 0);
2137               g_array_set_size (result, result->len + n);
2138               GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
2139               g_array_set_size (result, result->len - 1);
2140               break;
2141             case 'c':
2142               n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2143               if (n > 0)
2144                 {
2145                   g_array_set_size (result, result->len + n);
2146                   GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2147                   g_array_set_size (result, result->len - 1);
2148                 }
2149               g_array_append_vals (result, L" ", 1);
2150               n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2151               if (n > 0)
2152                 {
2153                   g_array_set_size (result, result->len + n);
2154                   GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2155                   g_array_set_size (result, result->len - 1);
2156                 }
2157               break;
2158             case 'C':
2159               g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
2160               g_array_append_vals (result, digits + (systemtime.wYear/1000)%10, 1);
2161               break;
2162             case 'd':
2163               g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2164               g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2165               break;
2166             case 'D':
2167               g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
2168               g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
2169               g_array_append_vals (result, L"/", 1);
2170               g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2171               g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2172               g_array_append_vals (result, L"/", 1);
2173               g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2174               g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2175               break;
2176             case 'e':
2177               if (systemtime.wDay >= 10)
2178                 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2179               else
2180                 g_array_append_vals (result, L" ", 1);
2181               g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2182               break;
2183
2184               /* A GDate has no time fields, so for now we can
2185                * hardcode all time conversions into zeros (or 12 for
2186                * %I). The alternative code snippets in the #else
2187                * branches are here ready to be taken into use when
2188                * needed by a g_strftime() or g_date_and_time_format()
2189                * or whatever.
2190                */
2191             case 'H':
2192 #if 1
2193               g_array_append_vals (result, L"00", 2);
2194 #else
2195               g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2196               g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2197 #endif
2198               break;
2199             case 'I':
2200 #if 1
2201               g_array_append_vals (result, L"12", 2);
2202 #else
2203               if (systemtime.wHour == 0)
2204                 g_array_append_vals (result, L"12", 2);
2205               else
2206                 {
2207                   g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
2208                   g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
2209                 }
2210 #endif
2211               break;
2212             case  'j':
2213               g_array_append_vals (result, digits + (tm->tm_yday+1)/100, 1);
2214               g_array_append_vals (result, digits + ((tm->tm_yday+1)/10)%10, 1);
2215               g_array_append_vals (result, digits + (tm->tm_yday+1)%10, 1);
2216               break;
2217             case 'm':
2218               g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
2219               g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
2220               break;
2221             case 'M':
2222 #if 1
2223               g_array_append_vals (result, L"00", 2);
2224 #else
2225               g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2226               g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2227 #endif
2228               break;
2229             case 'n':
2230               g_array_append_vals (result, L"\n", 1);
2231               break;
2232             case 'p':
2233               n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
2234               if (n > 0)
2235                 {
2236                   g_array_set_size (result, result->len + n);
2237                   GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
2238                   g_array_set_size (result, result->len - 1);
2239                 }
2240               break;
2241             case 'r':
2242               /* This is a rather odd format. Hard to say what to do.
2243                * Let's always use the POSIX %I:%M:%S %p
2244                */
2245 #if 1
2246               g_array_append_vals (result, L"12:00:00", 8);
2247 #else
2248               if (systemtime.wHour == 0)
2249                 g_array_append_vals (result, L"12", 2);
2250               else
2251                 {
2252                   g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
2253                   g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
2254                 }
2255               g_array_append_vals (result, L":", 1);
2256               g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2257               g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2258               g_array_append_vals (result, L":", 1);
2259               g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2260               g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2261               g_array_append_vals (result, L" ", 1);
2262 #endif
2263               n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
2264               if (n > 0)
2265                 {
2266                   g_array_set_size (result, result->len + n);
2267                   GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
2268                   g_array_set_size (result, result->len - 1);
2269                 }
2270               break;
2271             case 'R':
2272 #if 1
2273               g_array_append_vals (result, L"00:00", 5);
2274 #else
2275               g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2276               g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2277               g_array_append_vals (result, L":", 1);
2278               g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2279               g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2280 #endif
2281               break;
2282             case 'S':
2283 #if 1
2284               g_array_append_vals (result, L"00", 2);
2285 #else
2286               g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2287               g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2288 #endif
2289               break;
2290             case 't':
2291               g_array_append_vals (result, L"\t", 1);
2292               break;
2293             case 'T':
2294 #if 1
2295               g_array_append_vals (result, L"00:00:00", 8);
2296 #else
2297               g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2298               g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2299               g_array_append_vals (result, L":", 1);
2300               g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2301               g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2302               g_array_append_vals (result, L":", 1);
2303               g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2304               g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2305 #endif
2306               break;
2307             case 'u':
2308               if (systemtime.wDayOfWeek == 0)
2309                 g_array_append_vals (result, L"7", 1);
2310               else
2311                 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
2312               break;
2313             case 'U':
2314               n = g_date_get_sunday_week_of_year (d);
2315               g_array_append_vals (result, digits + n/10, 1);
2316               g_array_append_vals (result, digits + n%10, 1);
2317               break;
2318             case 'V':
2319               n = g_date_get_iso8601_week_of_year (d);
2320               g_array_append_vals (result, digits + n/10, 1);
2321               g_array_append_vals (result, digits + n%10, 1);
2322               break;
2323             case 'w':
2324               g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
2325               break;
2326             case 'W':
2327               n = g_date_get_monday_week_of_year (d);
2328               g_array_append_vals (result, digits + n/10, 1);
2329               g_array_append_vals (result, digits + n%10, 1);
2330               break;
2331             case 'x':
2332               n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2333               if (n > 0)
2334                 {
2335                   g_array_set_size (result, result->len + n);
2336                   GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2337                   g_array_set_size (result, result->len - 1);
2338                 }
2339               break;
2340             case 'X':
2341               n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2342               if (n > 0)
2343                 {
2344                   g_array_set_size (result, result->len + n);
2345                   GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2346                   g_array_set_size (result, result->len - 1);
2347                 }
2348               break;
2349             case 'y':
2350               g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2351               g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2352               break;
2353             case 'Y':
2354               g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
2355               g_array_append_vals (result, digits + (systemtime.wYear/100)%10, 1);
2356               g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2357               g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2358               break;
2359             case 'Z':
2360               n = GetTimeZoneInformation (&tzinfo);
2361               if (n == TIME_ZONE_ID_UNKNOWN)
2362                 ;
2363               else if (n == TIME_ZONE_ID_STANDARD)
2364                 g_array_append_vals (result, tzinfo.StandardName, wcslen (tzinfo.StandardName));
2365               else if (n == TIME_ZONE_ID_DAYLIGHT)
2366                 g_array_append_vals (result, tzinfo.DaylightName, wcslen (tzinfo.DaylightName));
2367               break;
2368             case '%':
2369               g_array_append_vals (result, L"%", 1);
2370               break;
2371             }      
2372         } 
2373       else if (c <= 0xFFFF)
2374         {
2375           wchar_t wc = c;
2376           g_array_append_vals (result, &wc, 1);
2377         }
2378       else
2379         {
2380           glong nwc;
2381           wchar_t *ws;
2382
2383           ws = g_ucs4_to_utf16 (&c, 1, NULL, &nwc, NULL);
2384           g_array_append_vals (result, ws, nwc);
2385           g_free (ws);
2386         }
2387       p = g_utf8_next_char (p);
2388     }
2389   
2390   convbuf = g_utf16_to_utf8 ((wchar_t *) result->data, result->len, NULL, &convlen, NULL);
2391   g_array_free (result, TRUE);
2392
2393   if (!convbuf)
2394     {
2395       s[0] = '\0';
2396       return 0;
2397     }
2398   
2399   if (slen <= convlen)
2400     {
2401       /* Ensure only whole characters are copied into the buffer. */
2402       gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
2403       g_assert (end != NULL);
2404       convlen = end - convbuf;
2405
2406       /* Return 0 because the buffer isn't large enough. */
2407       retval = 0;
2408     }
2409   else
2410     retval = convlen;
2411
2412   memcpy (s, convbuf, convlen);
2413   s[convlen] = '\0';
2414   g_free (convbuf);
2415
2416   return retval;
2417 }
2418
2419 #endif
2420
2421 /**
2422  * g_date_strftime:
2423  * @s: destination buffer
2424  * @slen: buffer size
2425  * @format: format string
2426  * @date: valid #GDate
2427  *
2428  * Generates a printed representation of the date, in a
2429  * <link linkend="setlocale">locale</link>-specific way.
2430  * Works just like the platform's C library strftime() function,
2431  * but only accepts date-related formats; time-related formats
2432  * give undefined results. Date must be valid. Unlike strftime()
2433  * (which uses the locale encoding), works on a UTF-8 format
2434  * string and stores a UTF-8 result.
2435  *
2436  * This function does not provide any conversion specifiers in
2437  * addition to those implemented by the platform's C library.
2438  * For example, don't expect that using g_date_strftime() would
2439  * make the \%F provided by the C99 strftime() work on Windows
2440  * where the C library only complies to C89.
2441  *
2442  * Returns: number of characters written to the buffer, or 0 the buffer was too small
2443  */
2444 gsize     
2445 g_date_strftime (gchar       *s, 
2446                  gsize        slen, 
2447                  const gchar *format, 
2448                  const GDate *d)
2449 {
2450   struct tm tm;
2451 #ifndef G_OS_WIN32
2452   gsize locale_format_len = 0;
2453   gchar *locale_format;
2454   gsize tmplen;
2455   gchar *tmpbuf;
2456   gsize tmpbufsize;
2457   gsize convlen = 0;
2458   gchar *convbuf;
2459   GError *error = NULL;
2460   gsize retval;
2461 #endif
2462
2463   g_return_val_if_fail (g_date_valid (d), 0);
2464   g_return_val_if_fail (slen > 0, 0); 
2465   g_return_val_if_fail (format != NULL, 0);
2466   g_return_val_if_fail (s != NULL, 0);
2467
2468   g_date_to_struct_tm (d, &tm);
2469
2470 #ifdef G_OS_WIN32
2471   if (!g_utf8_validate (format, -1, NULL))
2472     {
2473       s[0] = '\0';
2474       return 0;
2475     }
2476   return win32_strftime_helper (d, format, &tm, s, slen);
2477 #else
2478
2479   locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
2480
2481   if (error)
2482     {
2483       g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
2484       g_error_free (error);
2485
2486       s[0] = '\0';
2487       return 0;
2488     }
2489
2490   tmpbufsize = MAX (128, locale_format_len * 2);
2491   while (TRUE)
2492     {
2493       tmpbuf = g_malloc (tmpbufsize);
2494
2495       /* Set the first byte to something other than '\0', to be able to
2496        * recognize whether strftime actually failed or just returned "".
2497        */
2498       tmpbuf[0] = '\1';
2499       tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
2500
2501       if (tmplen == 0 && tmpbuf[0] != '\0')
2502         {
2503           g_free (tmpbuf);
2504           tmpbufsize *= 2;
2505
2506           if (tmpbufsize > 65536)
2507             {
2508               g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
2509               g_free (locale_format);
2510
2511               s[0] = '\0';
2512               return 0;
2513             }
2514         }
2515       else
2516         break;
2517     }
2518   g_free (locale_format);
2519
2520   convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
2521   g_free (tmpbuf);
2522
2523   if (error)
2524     {
2525       g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
2526       g_error_free (error);
2527
2528       s[0] = '\0';
2529       return 0;
2530     }
2531
2532   if (slen <= convlen)
2533     {
2534       /* Ensure only whole characters are copied into the buffer.
2535        */
2536       gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
2537       g_assert (end != NULL);
2538       convlen = end - convbuf;
2539
2540       /* Return 0 because the buffer isn't large enough.
2541        */
2542       retval = 0;
2543     }
2544   else
2545     retval = convlen;
2546
2547   memcpy (s, convbuf, convlen);
2548   s[convlen] = '\0';
2549   g_free (convbuf);
2550
2551   return retval;
2552 #endif
2553 }