Increase the timeout for some GLib tests
[glib.git] / glib / gdate.c
blob8ff224b60ea088ac059f39994d209313f55c43ed
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.1 of the License, or (at your option) any later version.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 /*
26 * MT safe
29 #include "config.h"
30 #include "glibconfig.h"
32 #define DEBUG_MSG(x) /* */
33 #ifdef G_ENABLE_DEBUG
34 /* #define DEBUG_MSG(args) g_message args ; */
35 #endif
37 #include <time.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <locale.h>
42 #ifdef G_OS_WIN32
43 #include <windows.h>
44 #endif
46 #include "gdate.h"
48 #include "gconvert.h"
49 #include "gmem.h"
50 #include "gstrfuncs.h"
51 #include "gtestutils.h"
52 #include "gthread.h"
53 #include "gunicode.h"
55 #ifdef G_OS_WIN32
56 #include "garray.h"
57 #endif
59 /**
60 * SECTION:date
61 * @title: Date and Time Functions
62 * @short_description: calendrical calculations and miscellaneous time stuff
64 * The #GDate data structure represents a day between January 1, Year 1,
65 * and sometime a few thousand years in the future (right now it will go
66 * to the year 65535 or so, but g_date_set_parse() only parses up to the
67 * year 8000 or so - just count on "a few thousand"). #GDate is meant to
68 * represent everyday dates, not astronomical dates or historical dates
69 * or ISO timestamps or the like. It extrapolates the current Gregorian
70 * calendar forward and backward in time; there is no attempt to change
71 * the calendar to match time periods or locations. #GDate does not store
72 * time information; it represents a day.
74 * The #GDate implementation has several nice features; it is only a
75 * 64-bit struct, so storing large numbers of dates is very efficient. It
76 * can keep both a Julian and day-month-year representation of the date,
77 * since some calculations are much easier with one representation or the
78 * other. A Julian representation is simply a count of days since some
79 * fixed day in the past; for #GDate the fixed day is January 1, 1 AD.
80 * ("Julian" dates in the #GDate API aren't really Julian dates in the
81 * technical sense; technically, Julian dates count from the start of the
82 * Julian period, Jan 1, 4713 BC).
84 * #GDate is simple to use. First you need a "blank" date; you can get a
85 * dynamically allocated date from g_date_new(), or you can declare an
86 * automatic variable or array and initialize it to a sane state by
87 * calling g_date_clear(). A cleared date is sane; it's safe to call
88 * g_date_set_dmy() and the other mutator functions to initialize the
89 * value of a cleared date. However, a cleared date is initially
90 * invalid, meaning that it doesn't represent a day that exists.
91 * It is undefined to call any of the date calculation routines on an
92 * invalid date. If you obtain a date from a user or other
93 * unpredictable source, you should check its validity with the
94 * g_date_valid() predicate. g_date_valid() is also used to check for
95 * errors with g_date_set_parse() and other functions that can
96 * fail. Dates can be invalidated by calling g_date_clear() again.
98 * It is very important to use the API to access the #GDate
99 * struct. Often only the day-month-year or only the Julian
100 * representation is valid. Sometimes neither is valid. Use the API.
102 * GLib also features #GDateTime which represents a precise time.
106 * G_USEC_PER_SEC:
108 * Number of microseconds in one second (1 million).
109 * This macro is provided for code readability.
113 * GTimeVal:
114 * @tv_sec: seconds
115 * @tv_usec: microseconds
117 * Represents a precise time, with seconds and microseconds.
118 * Similar to the struct timeval returned by the gettimeofday()
119 * UNIX system call.
121 * GLib is attempting to unify around the use of 64bit integers to
122 * represent microsecond-precision time. As such, this type will be
123 * removed from a future version of GLib.
127 * GDate:
128 * @julian_days: the Julian representation of the date
129 * @julian: this bit is set if @julian_days is valid
130 * @dmy: this is set if @day, @month and @year are valid
131 * @day: the day of the day-month-year representation of the date,
132 * as a number between 1 and 31
133 * @month: the day of the day-month-year representation of the date,
134 * as a number between 1 and 12
135 * @year: the day of the day-month-year representation of the date
137 * Represents a day between January 1, Year 1 and a few thousand years in
138 * the future. None of its members should be accessed directly.
140 * If the #GDate-struct is obtained from g_date_new(), it will be safe
141 * to mutate but invalid and thus not safe for calendrical computations.
143 * If it's declared on the stack, it will contain garbage so must be
144 * initialized with g_date_clear(). g_date_clear() makes the date invalid
145 * but sane. An invalid date doesn't represent a day, it's "empty." A date
146 * becomes valid after you set it to a Julian day or you set a day, month,
147 * and year.
151 * GTime:
153 * Simply a replacement for time_t. It has been deprecated
154 * since it is not equivalent to time_t on 64-bit platforms
155 * with a 64-bit time_t. Unrelated to #GTimer.
157 * Note that #GTime is defined to always be a 32-bit integer,
158 * unlike time_t which may be 64-bit on some systems. Therefore,
159 * #GTime will overflow in the year 2038, and you cannot use the
160 * address of a #GTime variable as argument to the UNIX time()
161 * function.
163 * Instead, do the following:
164 * |[<!-- language="C" -->
165 * time_t ttime;
166 * GTime gtime;
168 * time (&ttime);
169 * gtime = (GTime)ttime;
170 * ]|
174 * GDateDMY:
175 * @G_DATE_DAY: a day
176 * @G_DATE_MONTH: a month
177 * @G_DATE_YEAR: a year
179 * This enumeration isn't used in the API, but may be useful if you need
180 * to mark a number as a day, month, or year.
184 * GDateDay:
186 * Integer representing a day of the month; between 1 and 31.
187 * #G_DATE_BAD_DAY represents an invalid day of the month.
191 * GDateMonth:
192 * @G_DATE_BAD_MONTH: invalid value
193 * @G_DATE_JANUARY: January
194 * @G_DATE_FEBRUARY: February
195 * @G_DATE_MARCH: March
196 * @G_DATE_APRIL: April
197 * @G_DATE_MAY: May
198 * @G_DATE_JUNE: June
199 * @G_DATE_JULY: July
200 * @G_DATE_AUGUST: August
201 * @G_DATE_SEPTEMBER: September
202 * @G_DATE_OCTOBER: October
203 * @G_DATE_NOVEMBER: November
204 * @G_DATE_DECEMBER: December
206 * Enumeration representing a month; values are #G_DATE_JANUARY,
207 * #G_DATE_FEBRUARY, etc. #G_DATE_BAD_MONTH is the invalid value.
211 * GDateYear:
213 * Integer representing a year; #G_DATE_BAD_YEAR is the invalid
214 * value. The year must be 1 or higher; negative (BC) years are not
215 * allowed. The year is represented with four digits.
219 * GDateWeekday:
220 * @G_DATE_BAD_WEEKDAY: invalid value
221 * @G_DATE_MONDAY: Monday
222 * @G_DATE_TUESDAY: Tuesday
223 * @G_DATE_WEDNESDAY: Wednesday
224 * @G_DATE_THURSDAY: Thursday
225 * @G_DATE_FRIDAY: Friday
226 * @G_DATE_SATURDAY: Saturday
227 * @G_DATE_SUNDAY: Sunday
229 * Enumeration representing a day of the week; #G_DATE_MONDAY,
230 * #G_DATE_TUESDAY, etc. #G_DATE_BAD_WEEKDAY is an invalid weekday.
234 * G_DATE_BAD_DAY:
236 * Represents an invalid #GDateDay.
240 * G_DATE_BAD_JULIAN:
242 * Represents an invalid Julian day number.
246 * G_DATE_BAD_YEAR:
248 * Represents an invalid year.
252 * g_date_new:
254 * Allocates a #GDate and initializes
255 * it to a sane state. The new date will
256 * be cleared (as if you'd called g_date_clear()) but invalid (it won't
257 * represent an existing day). Free the return value with g_date_free().
259 * Returns: a newly-allocated #GDate
261 GDate*
262 g_date_new (void)
264 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
266 return d;
270 * g_date_new_dmy:
271 * @day: day of the month
272 * @month: month of the year
273 * @year: year
275 * Like g_date_new(), but also sets the value of the date. Assuming the
276 * day-month-year triplet you pass in represents an existing day, the
277 * returned date will be valid.
279 * Returns: a newly-allocated #GDate initialized with @day, @month, and @year
281 GDate*
282 g_date_new_dmy (GDateDay day,
283 GDateMonth m,
284 GDateYear y)
286 GDate *d;
287 g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
289 d = g_new (GDate, 1);
291 d->julian = FALSE;
292 d->dmy = TRUE;
294 d->month = m;
295 d->day = day;
296 d->year = y;
298 g_assert (g_date_valid (d));
300 return d;
304 * g_date_new_julian:
305 * @julian_day: days since January 1, Year 1
307 * Like g_date_new(), but also sets the value of the date. Assuming the
308 * Julian day number you pass in is valid (greater than 0, less than an
309 * unreasonably large number), the returned date will be valid.
311 * Returns: a newly-allocated #GDate initialized with @julian_day
313 GDate*
314 g_date_new_julian (guint32 julian_day)
316 GDate *d;
317 g_return_val_if_fail (g_date_valid_julian (julian_day), NULL);
319 d = g_new (GDate, 1);
321 d->julian = TRUE;
322 d->dmy = FALSE;
324 d->julian_days = julian_day;
326 g_assert (g_date_valid (d));
328 return d;
332 * g_date_free:
333 * @date: a #GDate to free
335 * Frees a #GDate returned from g_date_new().
337 void
338 g_date_free (GDate *date)
340 g_return_if_fail (date != NULL);
342 g_free (date);
346 * g_date_copy:
347 * @date: a #GDate to copy
349 * Copies a GDate to a newly-allocated GDate. If the input was invalid
350 * (as determined by g_date_valid()), the invalid state will be copied
351 * as is into the new object.
353 * Returns: (transfer full): a newly-allocated #GDate initialized from @date
355 * Since: 2.56
357 GDate *
358 g_date_copy (const GDate *date)
360 GDate *res;
361 g_return_val_if_fail (date != NULL, NULL);
363 if (g_date_valid (date))
364 res = g_date_new_julian (g_date_get_julian (date));
365 else
367 res = g_date_new ();
368 *res = *date;
371 return res;
375 * g_date_valid:
376 * @date: a #GDate to check
378 * Returns %TRUE if the #GDate represents an existing day. The date must not
379 * contain garbage; it should have been initialized with g_date_clear()
380 * if it wasn't allocated by one of the g_date_new() variants.
382 * Returns: Whether the date is valid
384 gboolean
385 g_date_valid (const GDate *d)
387 g_return_val_if_fail (d != NULL, FALSE);
389 return (d->julian || d->dmy);
392 static const guint8 days_in_months[2][13] =
393 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
394 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
395 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
398 static const guint16 days_in_year[2][14] =
399 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
400 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
401 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
405 * g_date_valid_month:
406 * @month: month
408 * Returns %TRUE if the month value is valid. The 12 #GDateMonth
409 * enumeration values are the only valid months.
411 * Returns: %TRUE if the month is valid
413 gboolean
414 g_date_valid_month (GDateMonth m)
416 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
420 * g_date_valid_year:
421 * @year: year
423 * Returns %TRUE if the year is valid. Any year greater than 0 is valid,
424 * though there is a 16-bit limit to what #GDate will understand.
426 * Returns: %TRUE if the year is valid
428 gboolean
429 g_date_valid_year (GDateYear y)
431 return ( y > G_DATE_BAD_YEAR );
435 * g_date_valid_day:
436 * @day: day to check
438 * Returns %TRUE if the day of the month is valid (a day is valid if it's
439 * between 1 and 31 inclusive).
441 * Returns: %TRUE if the day is valid
444 gboolean
445 g_date_valid_day (GDateDay d)
447 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
451 * g_date_valid_weekday:
452 * @weekday: weekday
454 * Returns %TRUE if the weekday is valid. The seven #GDateWeekday enumeration
455 * values are the only valid weekdays.
457 * Returns: %TRUE if the weekday is valid
459 gboolean
460 g_date_valid_weekday (GDateWeekday w)
462 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
466 * g_date_valid_julian:
467 * @julian_date: Julian day to check
469 * Returns %TRUE if the Julian day is valid. Anything greater than zero
470 * is basically a valid Julian, though there is a 32-bit limit.
472 * Returns: %TRUE if the Julian day is valid
474 gboolean
475 g_date_valid_julian (guint32 j)
477 return (j > G_DATE_BAD_JULIAN);
481 * g_date_valid_dmy:
482 * @day: day
483 * @month: month
484 * @year: year
486 * Returns %TRUE if the day-month-year triplet forms a valid, existing day
487 * in the range of days #GDate understands (Year 1 or later, no more than
488 * a few thousand years in the future).
490 * Returns: %TRUE if the date is a valid one
492 gboolean
493 g_date_valid_dmy (GDateDay d,
494 GDateMonth m,
495 GDateYear y)
497 return ( (m > G_DATE_BAD_MONTH) &&
498 (m < 13) &&
499 (d > G_DATE_BAD_DAY) &&
500 (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
501 (d <= (g_date_is_leap_year (y) ?
502 days_in_months[1][m] : days_in_months[0][m])) );
506 /* "Julian days" just means an absolute number of days, where Day 1 ==
507 * Jan 1, Year 1
509 static void
510 g_date_update_julian (const GDate *const_d)
512 GDate *d = (GDate *) const_d;
513 GDateYear year;
514 gint idx;
516 g_return_if_fail (d != NULL);
517 g_return_if_fail (d->dmy);
518 g_return_if_fail (!d->julian);
519 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
521 /* What we actually do is: multiply years * 365 days in the year,
522 * add the number of years divided by 4, subtract the number of
523 * years divided by 100 and add the number of years divided by 400,
524 * which accounts for leap year stuff. Code from Steffen Beyer's
525 * DateCalc.
528 year = d->year - 1; /* we know d->year > 0 since it's valid */
530 d->julian_days = year * 365U;
531 d->julian_days += (year >>= 2); /* divide by 4 and add */
532 d->julian_days -= (year /= 25); /* divides original # years by 100 */
533 d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
535 idx = g_date_is_leap_year (d->year) ? 1 : 0;
537 d->julian_days += days_in_year[idx][d->month] + d->day;
539 g_return_if_fail (g_date_valid_julian (d->julian_days));
541 d->julian = TRUE;
544 static void
545 g_date_update_dmy (const GDate *const_d)
547 GDate *d = (GDate *) const_d;
548 GDateYear y;
549 GDateMonth m;
550 GDateDay day;
552 guint32 A, B, C, D, E, M;
554 g_return_if_fail (d != NULL);
555 g_return_if_fail (d->julian);
556 g_return_if_fail (!d->dmy);
557 g_return_if_fail (g_date_valid_julian (d->julian_days));
559 /* Formula taken from the Calendar FAQ; the formula was for the
560 * Julian Period which starts on 1 January 4713 BC, so we add
561 * 1,721,425 to the number of days before doing the formula.
563 * I'm sure this can be simplified for our 1 January 1 AD period
564 * start, but I can't figure out how to unpack the formula.
567 A = d->julian_days + 1721425 + 32045;
568 B = ( 4 *(A + 36524) )/ 146097 - 1;
569 C = A - (146097 * B)/4;
570 D = ( 4 * (C + 365) ) / 1461 - 1;
571 E = C - ((1461*D) / 4);
572 M = (5 * (E - 1) + 2)/153;
574 m = M + 3 - (12*(M/10));
575 day = E - (153*M + 2)/5;
576 y = 100 * B + D - 4800 + (M/10);
578 #ifdef G_ENABLE_DEBUG
579 if (!g_date_valid_dmy (day, m, y))
580 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
581 d->julian_days, day, m, y);
582 #endif
584 d->month = m;
585 d->day = day;
586 d->year = y;
588 d->dmy = TRUE;
592 * g_date_get_weekday:
593 * @date: a #GDate
595 * Returns the day of the week for a #GDate. The date must be valid.
597 * Returns: day of the week as a #GDateWeekday.
599 GDateWeekday
600 g_date_get_weekday (const GDate *d)
602 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
604 if (!d->julian)
605 g_date_update_julian (d);
607 g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
609 return ((d->julian_days - 1) % 7) + 1;
613 * g_date_get_month:
614 * @date: a #GDate to get the month from
616 * Returns the month of the year. The date must be valid.
618 * Returns: month of the year as a #GDateMonth
620 GDateMonth
621 g_date_get_month (const GDate *d)
623 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
625 if (!d->dmy)
626 g_date_update_dmy (d);
628 g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
630 return d->month;
634 * g_date_get_year:
635 * @date: a #GDate
637 * Returns the year of a #GDate. The date must be valid.
639 * Returns: year in which the date falls
641 GDateYear
642 g_date_get_year (const GDate *d)
644 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
646 if (!d->dmy)
647 g_date_update_dmy (d);
649 g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
651 return d->year;
655 * g_date_get_day:
656 * @date: a #GDate to extract the day of the month from
658 * Returns the day of the month. The date must be valid.
660 * Returns: day of the month
662 GDateDay
663 g_date_get_day (const GDate *d)
665 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
667 if (!d->dmy)
668 g_date_update_dmy (d);
670 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
672 return d->day;
676 * g_date_get_julian:
677 * @date: a #GDate to extract the Julian day from
679 * Returns the Julian day or "serial number" of the #GDate. The
680 * Julian day is simply the number of days since January 1, Year 1; i.e.,
681 * January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2,
682 * etc. The date must be valid.
684 * Returns: Julian day
686 guint32
687 g_date_get_julian (const GDate *d)
689 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
691 if (!d->julian)
692 g_date_update_julian (d);
694 g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
696 return d->julian_days;
700 * g_date_get_day_of_year:
701 * @date: a #GDate to extract day of year from
703 * Returns the day of the year, where Jan 1 is the first day of the
704 * year. The date must be valid.
706 * Returns: day of the year
708 guint
709 g_date_get_day_of_year (const GDate *d)
711 gint idx;
713 g_return_val_if_fail (g_date_valid (d), 0);
715 if (!d->dmy)
716 g_date_update_dmy (d);
718 g_return_val_if_fail (d->dmy, 0);
720 idx = g_date_is_leap_year (d->year) ? 1 : 0;
722 return (days_in_year[idx][d->month] + d->day);
726 * g_date_get_monday_week_of_year:
727 * @date: a #GDate
729 * Returns the week of the year, where weeks are understood to start on
730 * Monday. If the date is before the first Monday of the year, return 0.
731 * The date must be valid.
733 * Returns: week of the year
735 guint
736 g_date_get_monday_week_of_year (const GDate *d)
738 GDateWeekday wd;
739 guint day;
740 GDate first;
742 g_return_val_if_fail (g_date_valid (d), 0);
744 if (!d->dmy)
745 g_date_update_dmy (d);
747 g_return_val_if_fail (d->dmy, 0);
749 g_date_clear (&first, 1);
751 g_date_set_dmy (&first, 1, 1, d->year);
753 wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
754 day = g_date_get_day_of_year (d) - 1;
756 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
760 * g_date_get_sunday_week_of_year:
761 * @date: a #GDate
763 * Returns the week of the year during which this date falls, if
764 * weeks are understood to begin on Sunday. The date must be valid.
765 * Can return 0 if the day is before the first Sunday of the year.
767 * Returns: week number
769 guint
770 g_date_get_sunday_week_of_year (const GDate *d)
772 GDateWeekday wd;
773 guint day;
774 GDate first;
776 g_return_val_if_fail (g_date_valid (d), 0);
778 if (!d->dmy)
779 g_date_update_dmy (d);
781 g_return_val_if_fail (d->dmy, 0);
783 g_date_clear (&first, 1);
785 g_date_set_dmy (&first, 1, 1, d->year);
787 wd = g_date_get_weekday (&first);
788 if (wd == 7) wd = 0; /* make Sunday day 0 */
789 day = g_date_get_day_of_year (d) - 1;
791 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
795 * g_date_get_iso8601_week_of_year:
796 * @date: a valid #GDate
798 * Returns the week of the year, where weeks are interpreted according
799 * to ISO 8601.
801 * Returns: ISO 8601 week number of the year.
803 * Since: 2.6
805 guint
806 g_date_get_iso8601_week_of_year (const GDate *d)
808 guint j, d4, L, d1, w;
810 g_return_val_if_fail (g_date_valid (d), 0);
812 if (!d->julian)
813 g_date_update_julian (d);
815 g_return_val_if_fail (d->julian, 0);
817 /* Formula taken from the Calendar FAQ; the formula was for the
818 * Julian Period which starts on 1 January 4713 BC, so we add
819 * 1,721,425 to the number of days before doing the formula.
821 j = d->julian_days + 1721425;
822 d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
823 L = d4 / 1460;
824 d1 = ((d4 - L) % 365) + L;
825 w = d1 / 7 + 1;
827 return w;
831 * g_date_days_between:
832 * @date1: the first date
833 * @date2: the second date
835 * Computes the number of days between two dates.
836 * If @date2 is prior to @date1, the returned value is negative.
837 * Both dates must be valid.
839 * Returns: the number of days between @date1 and @date2
841 gint
842 g_date_days_between (const GDate *d1,
843 const GDate *d2)
845 g_return_val_if_fail (g_date_valid (d1), 0);
846 g_return_val_if_fail (g_date_valid (d2), 0);
848 return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
852 * g_date_clear:
853 * @date: pointer to one or more dates to clear
854 * @n_dates: number of dates to clear
856 * Initializes one or more #GDate structs to a sane but invalid
857 * state. The cleared dates will not represent an existing date, but will
858 * not contain garbage. Useful to init a date declared on the stack.
859 * Validity can be tested with g_date_valid().
861 void
862 g_date_clear (GDate *d, guint ndates)
864 g_return_if_fail (d != NULL);
865 g_return_if_fail (ndates != 0);
867 memset (d, 0x0, ndates*sizeof (GDate));
870 G_LOCK_DEFINE_STATIC (g_date_global);
872 /* These are for the parser, output to the user should use *
873 * g_date_strftime () - this creates more never-freed memory to annoy
874 * all those memory debugger users. :-)
877 static gchar *long_month_names[13] =
879 NULL,
882 static gchar *long_month_names_alternative[13] =
884 NULL,
887 static gchar *short_month_names[13] =
889 NULL,
892 static gchar *short_month_names_alternative[13] =
894 NULL,
897 /* This tells us if we need to update the parse info */
898 static gchar *current_locale = NULL;
900 /* order of these in the current locale */
901 static GDateDMY dmy_order[3] =
903 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
906 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
907 * 29 and below are counted as in the year 2000, numbers 30 and above
908 * are counted as in the year 1900.
911 static const GDateYear twodigit_start_year = 1930;
913 /* It is impossible to enter a year between 1 AD and 99 AD with this
914 * in effect.
916 static gboolean using_twodigit_years = FALSE;
918 /* Adjustment of locale era to AD, non-zero means using locale era
920 static gint locale_era_adjust = 0;
922 struct _GDateParseTokens {
923 gint num_ints;
924 gint n[3];
925 guint month;
928 typedef struct _GDateParseTokens GDateParseTokens;
930 #define NUM_LEN 10
932 /* HOLDS: g_date_global_lock */
933 static void
934 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
936 gchar num[4][NUM_LEN+1];
937 gint i;
938 const guchar *s;
940 /* We count 4, but store 3; so we can give an error
941 * if there are 4.
943 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
945 s = (const guchar *) str;
946 pt->num_ints = 0;
947 while (*s && pt->num_ints < 4)
950 i = 0;
951 while (*s && g_ascii_isdigit (*s) && i < NUM_LEN)
953 num[pt->num_ints][i] = *s;
954 ++s;
955 ++i;
958 if (i > 0)
960 num[pt->num_ints][i] = '\0';
961 ++(pt->num_ints);
964 if (*s == '\0') break;
966 ++s;
969 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
970 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
971 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
973 pt->month = G_DATE_BAD_MONTH;
975 if (pt->num_ints < 3)
977 gchar *casefold;
978 gchar *normalized;
980 casefold = g_utf8_casefold (str, -1);
981 normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
982 g_free (casefold);
984 i = 1;
985 while (i < 13)
987 /* Here month names will be in a genitive case.
988 * Examples of how January may look in some languages:
989 * Catalan: "de gener", Croatian: "siječnja", Polish: "stycznia",
990 * Upper Sorbian: "januara".
992 if (long_month_names[i] != NULL)
994 const gchar *found = strstr (normalized, long_month_names[i]);
996 if (found != NULL)
998 pt->month = i;
999 break;
1003 /* Here month names will be in a nominative case.
1004 * Examples of how January may look in some languages:
1005 * Catalan: "gener", Croatian: "Siječanj", Polish: "styczeń",
1006 * Upper Sorbian: "Januar".
1008 if (long_month_names_alternative[i] != NULL)
1010 const gchar *found = strstr (normalized, long_month_names_alternative[i]);
1012 if (found != NULL)
1014 pt->month = i;
1015 break;
1019 /* Differences between abbreviated nominative and abbreviated
1020 * genitive month names are visible in very few languages but
1021 * let's handle them.
1023 if (short_month_names[i] != NULL)
1025 const gchar *found = strstr (normalized, short_month_names[i]);
1027 if (found != NULL)
1029 pt->month = i;
1030 break;
1034 if (short_month_names_alternative[i] != NULL)
1036 const gchar *found = strstr (normalized, short_month_names_alternative[i]);
1038 if (found != NULL)
1040 pt->month = i;
1041 break;
1045 ++i;
1048 g_free (normalized);
1052 /* HOLDS: g_date_global_lock */
1053 static void
1054 g_date_prepare_to_parse (const gchar *str,
1055 GDateParseTokens *pt)
1057 const gchar *locale = setlocale (LC_TIME, NULL);
1058 gboolean recompute_localeinfo = FALSE;
1059 GDate d;
1061 g_return_if_fail (locale != NULL); /* should not happen */
1063 g_date_clear (&d, 1); /* clear for scratch use */
1065 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
1066 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
1068 if (recompute_localeinfo)
1070 int i = 1;
1071 GDateParseTokens testpt;
1072 gchar buf[128];
1074 g_free (current_locale); /* still works if current_locale == NULL */
1076 current_locale = g_strdup (locale);
1078 short_month_names[0] = "Error";
1079 long_month_names[0] = "Error";
1081 while (i < 13)
1083 gchar *casefold;
1085 g_date_set_dmy (&d, 1, i, 1);
1087 g_return_if_fail (g_date_valid (&d));
1089 g_date_strftime (buf, 127, "%b", &d);
1091 casefold = g_utf8_casefold (buf, -1);
1092 g_free (short_month_names[i]);
1093 short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1094 g_free (casefold);
1096 g_date_strftime (buf, 127, "%B", &d);
1097 casefold = g_utf8_casefold (buf, -1);
1098 g_free (long_month_names[i]);
1099 long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1100 g_free (casefold);
1102 g_date_strftime (buf, 127, "%Ob", &d);
1103 casefold = g_utf8_casefold (buf, -1);
1104 g_free (short_month_names_alternative[i]);
1105 short_month_names_alternative[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1106 g_free (casefold);
1108 g_date_strftime (buf, 127, "%OB", &d);
1109 casefold = g_utf8_casefold (buf, -1);
1110 g_free (long_month_names_alternative[i]);
1111 long_month_names_alternative[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1112 g_free (casefold);
1114 ++i;
1117 /* Determine DMY order */
1119 /* had to pick a random day - don't change this, some strftimes
1120 * are broken on some days, and this one is good so far. */
1121 g_date_set_dmy (&d, 4, 7, 1976);
1123 g_date_strftime (buf, 127, "%x", &d);
1125 g_date_fill_parse_tokens (buf, &testpt);
1127 i = 0;
1128 while (i < testpt.num_ints)
1130 switch (testpt.n[i])
1132 case 7:
1133 dmy_order[i] = G_DATE_MONTH;
1134 break;
1135 case 4:
1136 dmy_order[i] = G_DATE_DAY;
1137 break;
1138 case 76:
1139 using_twodigit_years = TRUE; /* FALL THRU */
1140 case 1976:
1141 dmy_order[i] = G_DATE_YEAR;
1142 break;
1143 default:
1144 /* assume locale era */
1145 locale_era_adjust = 1976 - testpt.n[i];
1146 dmy_order[i] = G_DATE_YEAR;
1147 break;
1149 ++i;
1152 #if defined(G_ENABLE_DEBUG) && 0
1153 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
1154 i = 1;
1155 while (i < 13)
1157 DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
1158 ++i;
1160 DEBUG_MSG (("Alternative month names:"));
1161 i = 1;
1162 while (i < 13)
1164 DEBUG_MSG ((" %s %s", long_month_names_alternative[i], short_month_names_alternative[i]));
1165 ++i;
1167 if (using_twodigit_years)
1169 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
1172 gchar *strings[3];
1173 i = 0;
1174 while (i < 3)
1176 switch (dmy_order[i])
1178 case G_DATE_MONTH:
1179 strings[i] = "Month";
1180 break;
1181 case G_DATE_YEAR:
1182 strings[i] = "Year";
1183 break;
1184 case G_DATE_DAY:
1185 strings[i] = "Day";
1186 break;
1187 default:
1188 strings[i] = NULL;
1189 break;
1191 ++i;
1193 DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
1194 DEBUG_MSG (("**Sample date in this locale: '%s'", buf));
1196 #endif
1199 g_date_fill_parse_tokens (str, pt);
1203 * g_date_set_parse:
1204 * @date: a #GDate to fill in
1205 * @str: string to parse
1207 * Parses a user-inputted string @str, and try to figure out what date it
1208 * represents, taking the [current locale][setlocale] into account. If the
1209 * string is successfully parsed, the date will be valid after the call.
1210 * Otherwise, it will be invalid. You should check using g_date_valid()
1211 * to see whether the parsing succeeded.
1213 * This function is not appropriate for file formats and the like; it
1214 * isn't very precise, and its exact behavior varies with the locale.
1215 * It's intended to be a heuristic routine that guesses what the user
1216 * means by a given string (and it does work pretty well in that
1217 * capacity).
1219 void
1220 g_date_set_parse (GDate *d,
1221 const gchar *str)
1223 GDateParseTokens pt;
1224 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
1226 g_return_if_fail (d != NULL);
1228 /* set invalid */
1229 g_date_clear (d, 1);
1231 G_LOCK (g_date_global);
1233 g_date_prepare_to_parse (str, &pt);
1235 DEBUG_MSG (("Found %d ints, '%d' '%d' '%d' and written out month %d",
1236 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
1239 if (pt.num_ints == 4)
1241 G_UNLOCK (g_date_global);
1242 return; /* presumably a typo; bail out. */
1245 if (pt.num_ints > 1)
1247 int i = 0;
1248 int j = 0;
1250 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
1252 while (i < pt.num_ints && j < 3)
1254 switch (dmy_order[j])
1256 case G_DATE_MONTH:
1258 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
1260 m = pt.month;
1261 ++j; /* skip months, but don't skip this number */
1262 continue;
1264 else
1265 m = pt.n[i];
1267 break;
1268 case G_DATE_DAY:
1270 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
1272 day = 1;
1273 ++j; /* skip days, since we may have month/year */
1274 continue;
1276 day = pt.n[i];
1278 break;
1279 case G_DATE_YEAR:
1281 y = pt.n[i];
1283 if (locale_era_adjust != 0)
1285 y += locale_era_adjust;
1287 else if (using_twodigit_years && y < 100)
1289 guint two = twodigit_start_year % 100;
1290 guint century = (twodigit_start_year / 100) * 100;
1292 if (y < two)
1293 century += 100;
1295 y += century;
1298 break;
1299 default:
1300 break;
1303 ++i;
1304 ++j;
1308 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
1310 /* Try YYYY MM DD */
1311 y = pt.n[0];
1312 m = pt.n[1];
1313 day = pt.n[2];
1315 if (using_twodigit_years && y < 100)
1316 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
1318 else if (pt.num_ints == 2)
1320 if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
1321 m = pt.month;
1324 else if (pt.num_ints == 1)
1326 if (pt.month != G_DATE_BAD_MONTH)
1328 /* Month name and year? */
1329 m = pt.month;
1330 day = 1;
1331 y = pt.n[0];
1333 else
1335 /* Try yyyymmdd and yymmdd */
1337 m = (pt.n[0]/100) % 100;
1338 day = pt.n[0] % 100;
1339 y = pt.n[0]/10000;
1341 /* FIXME move this into a separate function */
1342 if (using_twodigit_years && y < 100)
1344 guint two = twodigit_start_year % 100;
1345 guint century = (twodigit_start_year / 100) * 100;
1347 if (y < two)
1348 century += 100;
1350 y += century;
1355 /* See if we got anything valid out of all this. */
1356 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
1357 if (y < 8000 && g_date_valid_dmy (day, m, y))
1359 d->month = m;
1360 d->day = day;
1361 d->year = y;
1362 d->dmy = TRUE;
1364 #ifdef G_ENABLE_DEBUG
1365 else
1367 DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
1369 #endif
1370 G_UNLOCK (g_date_global);
1374 * g_date_set_time_t:
1375 * @date: a #GDate
1376 * @timet: time_t value to set
1378 * Sets the value of a date to the date corresponding to a time
1379 * specified as a time_t. The time to date conversion is done using
1380 * the user's current timezone.
1382 * To set the value of a date to the current day, you could write:
1383 * |[<!-- language="C" -->
1384 * g_date_set_time_t (date, time (NULL));
1385 * ]|
1387 * Since: 2.10
1389 void
1390 g_date_set_time_t (GDate *date,
1391 time_t timet)
1393 struct tm tm;
1395 g_return_if_fail (date != NULL);
1397 #ifdef HAVE_LOCALTIME_R
1398 localtime_r (&timet, &tm);
1399 #else
1401 struct tm *ptm = localtime (&timet);
1403 if (ptm == NULL)
1405 /* Happens at least in Microsoft's C library if you pass a
1406 * negative time_t. Use 2000-01-01 as default date.
1408 #ifndef G_DISABLE_CHECKS
1409 g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
1410 #endif
1412 tm.tm_mon = 0;
1413 tm.tm_mday = 1;
1414 tm.tm_year = 100;
1416 else
1417 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
1419 #endif
1421 date->julian = FALSE;
1423 date->month = tm.tm_mon + 1;
1424 date->day = tm.tm_mday;
1425 date->year = tm.tm_year + 1900;
1427 g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
1429 date->dmy = TRUE;
1434 * g_date_set_time:
1435 * @date: a #GDate.
1436 * @time_: #GTime value to set.
1438 * Sets the value of a date from a #GTime value.
1439 * The time to date conversion is done using the user's current timezone.
1441 * Deprecated: 2.10: Use g_date_set_time_t() instead.
1443 void
1444 g_date_set_time (GDate *date,
1445 GTime time_)
1447 g_date_set_time_t (date, (time_t) time_);
1451 * g_date_set_time_val:
1452 * @date: a #GDate
1453 * @timeval: #GTimeVal value to set
1455 * Sets the value of a date from a #GTimeVal value. Note that the
1456 * @tv_usec member is ignored, because #GDate can't make use of the
1457 * additional precision.
1459 * The time to date conversion is done using the user's current timezone.
1461 * Since: 2.10
1463 void
1464 g_date_set_time_val (GDate *date,
1465 GTimeVal *timeval)
1467 g_date_set_time_t (date, (time_t) timeval->tv_sec);
1471 * g_date_set_month:
1472 * @date: a #GDate
1473 * @month: month to set
1475 * Sets the month of the year for a #GDate. If the resulting
1476 * day-month-year triplet is invalid, the date will be invalid.
1478 void
1479 g_date_set_month (GDate *d,
1480 GDateMonth m)
1482 g_return_if_fail (d != NULL);
1483 g_return_if_fail (g_date_valid_month (m));
1485 if (d->julian && !d->dmy) g_date_update_dmy(d);
1486 d->julian = FALSE;
1488 d->month = m;
1490 if (g_date_valid_dmy (d->day, d->month, d->year))
1491 d->dmy = TRUE;
1492 else
1493 d->dmy = FALSE;
1497 * g_date_set_day:
1498 * @date: a #GDate
1499 * @day: day to set
1501 * Sets the day of the month for a #GDate. If the resulting
1502 * day-month-year triplet is invalid, the date will be invalid.
1504 void
1505 g_date_set_day (GDate *d,
1506 GDateDay day)
1508 g_return_if_fail (d != NULL);
1509 g_return_if_fail (g_date_valid_day (day));
1511 if (d->julian && !d->dmy) g_date_update_dmy(d);
1512 d->julian = FALSE;
1514 d->day = day;
1516 if (g_date_valid_dmy (d->day, d->month, d->year))
1517 d->dmy = TRUE;
1518 else
1519 d->dmy = FALSE;
1523 * g_date_set_year:
1524 * @date: a #GDate
1525 * @year: year to set
1527 * Sets the year for a #GDate. If the resulting day-month-year
1528 * triplet is invalid, the date will be invalid.
1530 void
1531 g_date_set_year (GDate *d,
1532 GDateYear y)
1534 g_return_if_fail (d != NULL);
1535 g_return_if_fail (g_date_valid_year (y));
1537 if (d->julian && !d->dmy) g_date_update_dmy(d);
1538 d->julian = FALSE;
1540 d->year = y;
1542 if (g_date_valid_dmy (d->day, d->month, d->year))
1543 d->dmy = TRUE;
1544 else
1545 d->dmy = FALSE;
1549 * g_date_set_dmy:
1550 * @date: a #GDate
1551 * @day: day
1552 * @month: month
1553 * @y: year
1555 * Sets the value of a #GDate from a day, month, and year.
1556 * The day-month-year triplet must be valid; if you aren't
1557 * sure it is, call g_date_valid_dmy() to check before you
1558 * set it.
1560 void
1561 g_date_set_dmy (GDate *d,
1562 GDateDay day,
1563 GDateMonth m,
1564 GDateYear y)
1566 g_return_if_fail (d != NULL);
1567 g_return_if_fail (g_date_valid_dmy (day, m, y));
1569 d->julian = FALSE;
1571 d->month = m;
1572 d->day = day;
1573 d->year = y;
1575 d->dmy = TRUE;
1579 * g_date_set_julian:
1580 * @date: a #GDate
1581 * @julian_date: Julian day number (days since January 1, Year 1)
1583 * Sets the value of a #GDate from a Julian day number.
1585 void
1586 g_date_set_julian (GDate *d,
1587 guint32 j)
1589 g_return_if_fail (d != NULL);
1590 g_return_if_fail (g_date_valid_julian (j));
1592 d->julian_days = j;
1593 d->julian = TRUE;
1594 d->dmy = FALSE;
1598 * g_date_is_first_of_month:
1599 * @date: a #GDate to check
1601 * Returns %TRUE if the date is on the first of a month.
1602 * The date must be valid.
1604 * Returns: %TRUE if the date is the first of the month
1606 gboolean
1607 g_date_is_first_of_month (const GDate *d)
1609 g_return_val_if_fail (g_date_valid (d), FALSE);
1611 if (!d->dmy)
1612 g_date_update_dmy (d);
1614 g_return_val_if_fail (d->dmy, FALSE);
1616 if (d->day == 1) return TRUE;
1617 else return FALSE;
1621 * g_date_is_last_of_month:
1622 * @date: a #GDate to check
1624 * Returns %TRUE if the date is the last day of the month.
1625 * The date must be valid.
1627 * Returns: %TRUE if the date is the last day of the month
1629 gboolean
1630 g_date_is_last_of_month (const GDate *d)
1632 gint idx;
1634 g_return_val_if_fail (g_date_valid (d), FALSE);
1636 if (!d->dmy)
1637 g_date_update_dmy (d);
1639 g_return_val_if_fail (d->dmy, FALSE);
1641 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1643 if (d->day == days_in_months[idx][d->month]) return TRUE;
1644 else return FALSE;
1648 * g_date_add_days:
1649 * @date: a #GDate to increment
1650 * @n_days: number of days to move the date forward
1652 * Increments a date some number of days.
1653 * To move forward by weeks, add weeks*7 days.
1654 * The date must be valid.
1656 void
1657 g_date_add_days (GDate *d,
1658 guint ndays)
1660 g_return_if_fail (g_date_valid (d));
1662 if (!d->julian)
1663 g_date_update_julian (d);
1665 g_return_if_fail (d->julian);
1667 d->julian_days += ndays;
1668 d->dmy = FALSE;
1672 * g_date_subtract_days:
1673 * @date: a #GDate to decrement
1674 * @n_days: number of days to move
1676 * Moves a date some number of days into the past.
1677 * To move by weeks, just move by weeks*7 days.
1678 * The date must be valid.
1680 void
1681 g_date_subtract_days (GDate *d,
1682 guint ndays)
1684 g_return_if_fail (g_date_valid (d));
1686 if (!d->julian)
1687 g_date_update_julian (d);
1689 g_return_if_fail (d->julian);
1690 g_return_if_fail (d->julian_days > ndays);
1692 d->julian_days -= ndays;
1693 d->dmy = FALSE;
1697 * g_date_add_months:
1698 * @date: a #GDate to increment
1699 * @n_months: number of months to move forward
1701 * Increments a date by some number of months.
1702 * If the day of the month is greater than 28,
1703 * this routine may change the day of the month
1704 * (because the destination month may not have
1705 * the current day in it). The date must be valid.
1707 void
1708 g_date_add_months (GDate *d,
1709 guint nmonths)
1711 guint years, months;
1712 gint idx;
1714 g_return_if_fail (g_date_valid (d));
1716 if (!d->dmy)
1717 g_date_update_dmy (d);
1719 g_return_if_fail (d->dmy);
1721 nmonths += d->month - 1;
1723 years = nmonths/12;
1724 months = nmonths%12;
1726 d->month = months + 1;
1727 d->year += years;
1729 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1731 if (d->day > days_in_months[idx][d->month])
1732 d->day = days_in_months[idx][d->month];
1734 d->julian = FALSE;
1736 g_return_if_fail (g_date_valid (d));
1740 * g_date_subtract_months:
1741 * @date: a #GDate to decrement
1742 * @n_months: number of months to move
1744 * Moves a date some number of months into the past.
1745 * If the current day of the month doesn't exist in
1746 * the destination month, the day of the month
1747 * may change. The date must be valid.
1749 void
1750 g_date_subtract_months (GDate *d,
1751 guint nmonths)
1753 guint years, months;
1754 gint idx;
1756 g_return_if_fail (g_date_valid (d));
1758 if (!d->dmy)
1759 g_date_update_dmy (d);
1761 g_return_if_fail (d->dmy);
1763 years = nmonths/12;
1764 months = nmonths%12;
1766 g_return_if_fail (d->year > years);
1768 d->year -= years;
1770 if (d->month > months) d->month -= months;
1771 else
1773 months -= d->month;
1774 d->month = 12 - months;
1775 d->year -= 1;
1778 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1780 if (d->day > days_in_months[idx][d->month])
1781 d->day = days_in_months[idx][d->month];
1783 d->julian = FALSE;
1785 g_return_if_fail (g_date_valid (d));
1789 * g_date_add_years:
1790 * @date: a #GDate to increment
1791 * @n_years: number of years to move forward
1793 * Increments a date by some number of years.
1794 * If the date is February 29, and the destination
1795 * year is not a leap year, the date will be changed
1796 * to February 28. The date must be valid.
1798 void
1799 g_date_add_years (GDate *d,
1800 guint nyears)
1802 g_return_if_fail (g_date_valid (d));
1804 if (!d->dmy)
1805 g_date_update_dmy (d);
1807 g_return_if_fail (d->dmy);
1809 d->year += nyears;
1811 if (d->month == 2 && d->day == 29)
1813 if (!g_date_is_leap_year (d->year))
1814 d->day = 28;
1817 d->julian = FALSE;
1821 * g_date_subtract_years:
1822 * @date: a #GDate to decrement
1823 * @n_years: number of years to move
1825 * Moves a date some number of years into the past.
1826 * If the current day doesn't exist in the destination
1827 * year (i.e. it's February 29 and you move to a non-leap-year)
1828 * then the day is changed to February 29. The date
1829 * must be valid.
1831 void
1832 g_date_subtract_years (GDate *d,
1833 guint nyears)
1835 g_return_if_fail (g_date_valid (d));
1837 if (!d->dmy)
1838 g_date_update_dmy (d);
1840 g_return_if_fail (d->dmy);
1841 g_return_if_fail (d->year > nyears);
1843 d->year -= nyears;
1845 if (d->month == 2 && d->day == 29)
1847 if (!g_date_is_leap_year (d->year))
1848 d->day = 28;
1851 d->julian = FALSE;
1855 * g_date_is_leap_year:
1856 * @year: year to check
1858 * Returns %TRUE if the year is a leap year.
1860 * For the purposes of this function, leap year is every year
1861 * divisible by 4 unless that year is divisible by 100. If it
1862 * is divisible by 100 it would be a leap year only if that year
1863 * is also divisible by 400.
1865 * Returns: %TRUE if the year is a leap year
1867 gboolean
1868 g_date_is_leap_year (GDateYear year)
1870 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1872 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1873 (year % 400) == 0 );
1877 * g_date_get_days_in_month:
1878 * @month: month
1879 * @year: year
1881 * Returns the number of days in a month, taking leap
1882 * years into account.
1884 * Returns: number of days in @month during the @year
1886 guint8
1887 g_date_get_days_in_month (GDateMonth month,
1888 GDateYear year)
1890 gint idx;
1892 g_return_val_if_fail (g_date_valid_year (year), 0);
1893 g_return_val_if_fail (g_date_valid_month (month), 0);
1895 idx = g_date_is_leap_year (year) ? 1 : 0;
1897 return days_in_months[idx][month];
1901 * g_date_get_monday_weeks_in_year:
1902 * @year: a year
1904 * Returns the number of weeks in the year, where weeks
1905 * are taken to start on Monday. Will be 52 or 53. The
1906 * date must be valid. (Years always have 52 7-day periods,
1907 * plus 1 or 2 extra days depending on whether it's a leap
1908 * year. This function is basically telling you how many
1909 * Mondays are in the year, i.e. there are 53 Mondays if
1910 * one of the extra days happens to be a Monday.)
1912 * Returns: number of Mondays in the year
1914 guint8
1915 g_date_get_monday_weeks_in_year (GDateYear year)
1917 GDate d;
1919 g_return_val_if_fail (g_date_valid_year (year), 0);
1921 g_date_clear (&d, 1);
1922 g_date_set_dmy (&d, 1, 1, year);
1923 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1924 g_date_set_dmy (&d, 31, 12, year);
1925 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1926 if (g_date_is_leap_year (year))
1928 g_date_set_dmy (&d, 2, 1, year);
1929 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1930 g_date_set_dmy (&d, 30, 12, year);
1931 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1933 return 52;
1937 * g_date_get_sunday_weeks_in_year:
1938 * @year: year to count weeks in
1940 * Returns the number of weeks in the year, where weeks
1941 * are taken to start on Sunday. Will be 52 or 53. The
1942 * date must be valid. (Years always have 52 7-day periods,
1943 * plus 1 or 2 extra days depending on whether it's a leap
1944 * year. This function is basically telling you how many
1945 * Sundays are in the year, i.e. there are 53 Sundays if
1946 * one of the extra days happens to be a Sunday.)
1948 * Returns: the number of weeks in @year
1950 guint8
1951 g_date_get_sunday_weeks_in_year (GDateYear year)
1953 GDate d;
1955 g_return_val_if_fail (g_date_valid_year (year), 0);
1957 g_date_clear (&d, 1);
1958 g_date_set_dmy (&d, 1, 1, year);
1959 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1960 g_date_set_dmy (&d, 31, 12, year);
1961 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1962 if (g_date_is_leap_year (year))
1964 g_date_set_dmy (&d, 2, 1, year);
1965 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1966 g_date_set_dmy (&d, 30, 12, year);
1967 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1969 return 52;
1973 * g_date_compare:
1974 * @lhs: first date to compare
1975 * @rhs: second date to compare
1977 * qsort()-style comparison function for dates.
1978 * Both dates must be valid.
1980 * Returns: 0 for equal, less than zero if @lhs is less than @rhs,
1981 * greater than zero if @lhs is greater than @rhs
1983 gint
1984 g_date_compare (const GDate *lhs,
1985 const GDate *rhs)
1987 g_return_val_if_fail (lhs != NULL, 0);
1988 g_return_val_if_fail (rhs != NULL, 0);
1989 g_return_val_if_fail (g_date_valid (lhs), 0);
1990 g_return_val_if_fail (g_date_valid (rhs), 0);
1992 /* Remember the self-comparison case! I think it works right now. */
1994 while (TRUE)
1996 if (lhs->julian && rhs->julian)
1998 if (lhs->julian_days < rhs->julian_days) return -1;
1999 else if (lhs->julian_days > rhs->julian_days) return 1;
2000 else return 0;
2002 else if (lhs->dmy && rhs->dmy)
2004 if (lhs->year < rhs->year) return -1;
2005 else if (lhs->year > rhs->year) return 1;
2006 else
2008 if (lhs->month < rhs->month) return -1;
2009 else if (lhs->month > rhs->month) return 1;
2010 else
2012 if (lhs->day < rhs->day) return -1;
2013 else if (lhs->day > rhs->day) return 1;
2014 else return 0;
2020 else
2022 if (!lhs->julian) g_date_update_julian (lhs);
2023 if (!rhs->julian) g_date_update_julian (rhs);
2024 g_return_val_if_fail (lhs->julian, 0);
2025 g_return_val_if_fail (rhs->julian, 0);
2029 return 0; /* warnings */
2033 * g_date_to_struct_tm:
2034 * @date: a #GDate to set the struct tm from
2035 * @tm: (not nullable): struct tm to fill
2037 * Fills in the date-related bits of a struct tm using the @date value.
2038 * Initializes the non-date parts with something sane but meaningless.
2040 void
2041 g_date_to_struct_tm (const GDate *d,
2042 struct tm *tm)
2044 GDateWeekday day;
2046 g_return_if_fail (g_date_valid (d));
2047 g_return_if_fail (tm != NULL);
2049 if (!d->dmy)
2050 g_date_update_dmy (d);
2052 g_return_if_fail (d->dmy);
2054 /* zero all the irrelevant fields to be sure they're valid */
2056 /* On Linux and maybe other systems, there are weird non-POSIX
2057 * fields on the end of struct tm that choke strftime if they
2058 * contain garbage. So we need to 0 the entire struct, not just the
2059 * fields we know to exist.
2062 memset (tm, 0x0, sizeof (struct tm));
2064 tm->tm_mday = d->day;
2065 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
2066 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
2068 day = g_date_get_weekday (d);
2069 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
2071 tm->tm_wday = (int)day;
2073 tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
2074 tm->tm_isdst = -1; /* -1 means "information not available" */
2078 * g_date_clamp:
2079 * @date: a #GDate to clamp
2080 * @min_date: minimum accepted value for @date
2081 * @max_date: maximum accepted value for @date
2083 * If @date is prior to @min_date, sets @date equal to @min_date.
2084 * If @date falls after @max_date, sets @date equal to @max_date.
2085 * Otherwise, @date is unchanged.
2086 * Either of @min_date and @max_date may be %NULL.
2087 * All non-%NULL dates must be valid.
2089 void
2090 g_date_clamp (GDate *date,
2091 const GDate *min_date,
2092 const GDate *max_date)
2094 g_return_if_fail (g_date_valid (date));
2096 if (min_date != NULL)
2097 g_return_if_fail (g_date_valid (min_date));
2099 if (max_date != NULL)
2100 g_return_if_fail (g_date_valid (max_date));
2102 if (min_date != NULL && max_date != NULL)
2103 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
2105 if (min_date && g_date_compare (date, min_date) < 0)
2106 *date = *min_date;
2108 if (max_date && g_date_compare (max_date, date) < 0)
2109 *date = *max_date;
2113 * g_date_order:
2114 * @date1: the first date
2115 * @date2: the second date
2117 * Checks if @date1 is less than or equal to @date2,
2118 * and swap the values if this is not the case.
2120 void
2121 g_date_order (GDate *date1,
2122 GDate *date2)
2124 g_return_if_fail (g_date_valid (date1));
2125 g_return_if_fail (g_date_valid (date2));
2127 if (g_date_compare (date1, date2) > 0)
2129 GDate tmp = *date1;
2130 *date1 = *date2;
2131 *date2 = tmp;
2135 #ifdef G_OS_WIN32
2136 static void
2137 append_month_name (GArray *result,
2138 LCID lcid,
2139 SYSTEMTIME *systemtime,
2140 gboolean abbreviated,
2141 gboolean alternative)
2143 int n;
2144 WORD base;
2145 LPCWSTR lpFormat;
2147 if (alternative)
2149 base = abbreviated ? LOCALE_SABBREVMONTHNAME1 : LOCALE_SMONTHNAME1;
2150 n = GetLocaleInfoW (lcid, base + systemtime->wMonth - 1, NULL, 0);
2151 g_array_set_size (result, result->len + n);
2152 GetLocaleInfoW (lcid, base + systemtime->wMonth - 1,
2153 ((wchar_t *) result->data) + result->len - n, n);
2154 g_array_set_size (result, result->len - 1);
2156 else
2158 /* According to MSDN, this is the correct method to obtain
2159 * the form of the month name used when formatting a full
2160 * date; it must be a genitive case in some languages.
2162 lpFormat = abbreviated ? L"ddMMM" : L"ddMMMM";
2163 n = GetDateFormatW (lcid, 0, systemtime, lpFormat, NULL, 0);
2164 g_array_set_size (result, result->len + n);
2165 GetDateFormatW (lcid, 0, systemtime, lpFormat,
2166 ((wchar_t *) result->data) + result->len - n, n);
2167 /* We have obtained a day number as two digits and the month name.
2168 * Now let's get rid of those two digits: overwrite them with the
2169 * month name.
2171 memmove (((wchar_t *) result->data) + result->len - n,
2172 ((wchar_t *) result->data) + result->len - n + 2,
2173 (n - 2) * sizeof (wchar_t));
2174 g_array_set_size (result, result->len - 3);
2178 static gsize
2179 win32_strftime_helper (const GDate *d,
2180 const gchar *format,
2181 const struct tm *tm,
2182 gchar *s,
2183 gsize slen)
2185 SYSTEMTIME systemtime;
2186 TIME_ZONE_INFORMATION tzinfo;
2187 LCID lcid;
2188 int n, k;
2189 GArray *result;
2190 const gchar *p;
2191 gunichar c, modifier;
2192 const wchar_t digits[] = L"0123456789";
2193 gchar *convbuf;
2194 glong convlen = 0;
2195 gsize retval;
2197 systemtime.wYear = tm->tm_year + 1900;
2198 systemtime.wMonth = tm->tm_mon + 1;
2199 systemtime.wDayOfWeek = tm->tm_wday;
2200 systemtime.wDay = tm->tm_mday;
2201 systemtime.wHour = tm->tm_hour;
2202 systemtime.wMinute = tm->tm_min;
2203 systemtime.wSecond = tm->tm_sec;
2204 systemtime.wMilliseconds = 0;
2206 lcid = GetThreadLocale ();
2207 result = g_array_sized_new (FALSE, FALSE, sizeof (wchar_t), MAX (128, strlen (format) * 2));
2209 p = format;
2210 while (*p)
2212 c = g_utf8_get_char (p);
2213 if (c == '%')
2215 p = g_utf8_next_char (p);
2216 if (!*p)
2218 s[0] = '\0';
2219 g_array_free (result, TRUE);
2221 return 0;
2224 modifier = '\0';
2225 c = g_utf8_get_char (p);
2226 if (c == 'E' || c == 'O')
2228 /* "%OB", "%Ob", and "%Oh" are supported, ignore other modified
2229 * conversion specifiers for now.
2231 modifier = c;
2232 p = g_utf8_next_char (p);
2233 if (!*p)
2235 s[0] = '\0';
2236 g_array_free (result, TRUE);
2238 return 0;
2241 c = g_utf8_get_char (p);
2244 switch (c)
2246 case 'a':
2247 if (systemtime.wDayOfWeek == 0)
2248 k = 6;
2249 else
2250 k = systemtime.wDayOfWeek - 1;
2251 n = GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, NULL, 0);
2252 g_array_set_size (result, result->len + n);
2253 GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
2254 g_array_set_size (result, result->len - 1);
2255 break;
2256 case 'A':
2257 if (systemtime.wDayOfWeek == 0)
2258 k = 6;
2259 else
2260 k = systemtime.wDayOfWeek - 1;
2261 n = GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, NULL, 0);
2262 g_array_set_size (result, result->len + n);
2263 GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
2264 g_array_set_size (result, result->len - 1);
2265 break;
2266 case 'b':
2267 case 'h':
2268 append_month_name (result, lcid, &systemtime, TRUE,
2269 modifier == 'O');
2270 break;
2271 case 'B':
2272 append_month_name (result, lcid, &systemtime, FALSE,
2273 modifier == 'O');
2274 break;
2275 case 'c':
2276 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2277 if (n > 0)
2279 g_array_set_size (result, result->len + n);
2280 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2281 g_array_set_size (result, result->len - 1);
2283 g_array_append_vals (result, L" ", 1);
2284 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2285 if (n > 0)
2287 g_array_set_size (result, result->len + n);
2288 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2289 g_array_set_size (result, result->len - 1);
2291 break;
2292 case 'C':
2293 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
2294 g_array_append_vals (result, digits + (systemtime.wYear/1000)%10, 1);
2295 break;
2296 case 'd':
2297 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2298 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2299 break;
2300 case 'D':
2301 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
2302 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
2303 g_array_append_vals (result, L"/", 1);
2304 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2305 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2306 g_array_append_vals (result, L"/", 1);
2307 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2308 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2309 break;
2310 case 'e':
2311 if (systemtime.wDay >= 10)
2312 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2313 else
2314 g_array_append_vals (result, L" ", 1);
2315 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2316 break;
2318 /* A GDate has no time fields, so for now we can
2319 * hardcode all time conversions into zeros (or 12 for
2320 * %I). The alternative code snippets in the #else
2321 * branches are here ready to be taken into use when
2322 * needed by a g_strftime() or g_date_and_time_format()
2323 * or whatever.
2325 case 'H':
2326 #if 1
2327 g_array_append_vals (result, L"00", 2);
2328 #else
2329 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2330 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2331 #endif
2332 break;
2333 case 'I':
2334 #if 1
2335 g_array_append_vals (result, L"12", 2);
2336 #else
2337 if (systemtime.wHour == 0)
2338 g_array_append_vals (result, L"12", 2);
2339 else
2341 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
2342 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
2344 #endif
2345 break;
2346 case 'j':
2347 g_array_append_vals (result, digits + (tm->tm_yday+1)/100, 1);
2348 g_array_append_vals (result, digits + ((tm->tm_yday+1)/10)%10, 1);
2349 g_array_append_vals (result, digits + (tm->tm_yday+1)%10, 1);
2350 break;
2351 case 'm':
2352 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
2353 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
2354 break;
2355 case 'M':
2356 #if 1
2357 g_array_append_vals (result, L"00", 2);
2358 #else
2359 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2360 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2361 #endif
2362 break;
2363 case 'n':
2364 g_array_append_vals (result, L"\n", 1);
2365 break;
2366 case 'p':
2367 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
2368 if (n > 0)
2370 g_array_set_size (result, result->len + n);
2371 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
2372 g_array_set_size (result, result->len - 1);
2374 break;
2375 case 'r':
2376 /* This is a rather odd format. Hard to say what to do.
2377 * Let's always use the POSIX %I:%M:%S %p
2379 #if 1
2380 g_array_append_vals (result, L"12:00:00", 8);
2381 #else
2382 if (systemtime.wHour == 0)
2383 g_array_append_vals (result, L"12", 2);
2384 else
2386 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
2387 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
2389 g_array_append_vals (result, L":", 1);
2390 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2391 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2392 g_array_append_vals (result, L":", 1);
2393 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2394 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2395 g_array_append_vals (result, L" ", 1);
2396 #endif
2397 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
2398 if (n > 0)
2400 g_array_set_size (result, result->len + n);
2401 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
2402 g_array_set_size (result, result->len - 1);
2404 break;
2405 case 'R':
2406 #if 1
2407 g_array_append_vals (result, L"00:00", 5);
2408 #else
2409 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2410 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2411 g_array_append_vals (result, L":", 1);
2412 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2413 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2414 #endif
2415 break;
2416 case 'S':
2417 #if 1
2418 g_array_append_vals (result, L"00", 2);
2419 #else
2420 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2421 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2422 #endif
2423 break;
2424 case 't':
2425 g_array_append_vals (result, L"\t", 1);
2426 break;
2427 case 'T':
2428 #if 1
2429 g_array_append_vals (result, L"00:00:00", 8);
2430 #else
2431 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2432 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2433 g_array_append_vals (result, L":", 1);
2434 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2435 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2436 g_array_append_vals (result, L":", 1);
2437 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2438 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2439 #endif
2440 break;
2441 case 'u':
2442 if (systemtime.wDayOfWeek == 0)
2443 g_array_append_vals (result, L"7", 1);
2444 else
2445 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
2446 break;
2447 case 'U':
2448 n = g_date_get_sunday_week_of_year (d);
2449 g_array_append_vals (result, digits + n/10, 1);
2450 g_array_append_vals (result, digits + n%10, 1);
2451 break;
2452 case 'V':
2453 n = g_date_get_iso8601_week_of_year (d);
2454 g_array_append_vals (result, digits + n/10, 1);
2455 g_array_append_vals (result, digits + n%10, 1);
2456 break;
2457 case 'w':
2458 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
2459 break;
2460 case 'W':
2461 n = g_date_get_monday_week_of_year (d);
2462 g_array_append_vals (result, digits + n/10, 1);
2463 g_array_append_vals (result, digits + n%10, 1);
2464 break;
2465 case 'x':
2466 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2467 if (n > 0)
2469 g_array_set_size (result, result->len + n);
2470 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2471 g_array_set_size (result, result->len - 1);
2473 break;
2474 case 'X':
2475 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2476 if (n > 0)
2478 g_array_set_size (result, result->len + n);
2479 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2480 g_array_set_size (result, result->len - 1);
2482 break;
2483 case 'y':
2484 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2485 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2486 break;
2487 case 'Y':
2488 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
2489 g_array_append_vals (result, digits + (systemtime.wYear/100)%10, 1);
2490 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2491 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2492 break;
2493 case 'Z':
2494 n = GetTimeZoneInformation (&tzinfo);
2495 if (n == TIME_ZONE_ID_UNKNOWN)
2497 else if (n == TIME_ZONE_ID_STANDARD)
2498 g_array_append_vals (result, tzinfo.StandardName, wcslen (tzinfo.StandardName));
2499 else if (n == TIME_ZONE_ID_DAYLIGHT)
2500 g_array_append_vals (result, tzinfo.DaylightName, wcslen (tzinfo.DaylightName));
2501 break;
2502 case '%':
2503 g_array_append_vals (result, L"%", 1);
2504 break;
2507 else if (c <= 0xFFFF)
2509 wchar_t wc = c;
2510 g_array_append_vals (result, &wc, 1);
2512 else
2514 glong nwc;
2515 wchar_t *ws;
2517 ws = g_ucs4_to_utf16 (&c, 1, NULL, &nwc, NULL);
2518 g_array_append_vals (result, ws, nwc);
2519 g_free (ws);
2521 p = g_utf8_next_char (p);
2524 convbuf = g_utf16_to_utf8 ((wchar_t *) result->data, result->len, NULL, &convlen, NULL);
2525 g_array_free (result, TRUE);
2527 if (!convbuf)
2529 s[0] = '\0';
2530 return 0;
2533 if (slen <= convlen)
2535 /* Ensure only whole characters are copied into the buffer. */
2536 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
2537 g_assert (end != NULL);
2538 convlen = end - convbuf;
2540 /* Return 0 because the buffer isn't large enough. */
2541 retval = 0;
2543 else
2544 retval = convlen;
2546 memcpy (s, convbuf, convlen);
2547 s[convlen] = '\0';
2548 g_free (convbuf);
2550 return retval;
2553 #endif
2556 * g_date_strftime:
2557 * @s: destination buffer
2558 * @slen: buffer size
2559 * @format: format string
2560 * @date: valid #GDate
2562 * Generates a printed representation of the date, in a
2563 * [locale][setlocale]-specific way.
2564 * Works just like the platform's C library strftime() function,
2565 * but only accepts date-related formats; time-related formats
2566 * give undefined results. Date must be valid. Unlike strftime()
2567 * (which uses the locale encoding), works on a UTF-8 format
2568 * string and stores a UTF-8 result.
2570 * This function does not provide any conversion specifiers in
2571 * addition to those implemented by the platform's C library.
2572 * For example, don't expect that using g_date_strftime() would
2573 * make the \%F provided by the C99 strftime() work on Windows
2574 * where the C library only complies to C89.
2576 * Returns: number of characters written to the buffer, or 0 the buffer was too small
2578 #pragma GCC diagnostic push
2579 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
2581 gsize
2582 g_date_strftime (gchar *s,
2583 gsize slen,
2584 const gchar *format,
2585 const GDate *d)
2587 struct tm tm;
2588 #ifndef G_OS_WIN32
2589 gsize locale_format_len = 0;
2590 gchar *locale_format;
2591 gsize tmplen;
2592 gchar *tmpbuf;
2593 gsize tmpbufsize;
2594 gsize convlen = 0;
2595 gchar *convbuf;
2596 GError *error = NULL;
2597 gsize retval;
2598 #endif
2600 g_return_val_if_fail (g_date_valid (d), 0);
2601 g_return_val_if_fail (slen > 0, 0);
2602 g_return_val_if_fail (format != NULL, 0);
2603 g_return_val_if_fail (s != NULL, 0);
2605 g_date_to_struct_tm (d, &tm);
2607 #ifdef G_OS_WIN32
2608 if (!g_utf8_validate (format, -1, NULL))
2610 s[0] = '\0';
2611 return 0;
2613 return win32_strftime_helper (d, format, &tm, s, slen);
2614 #else
2616 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
2618 if (error)
2620 g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
2621 g_error_free (error);
2623 s[0] = '\0';
2624 return 0;
2627 tmpbufsize = MAX (128, locale_format_len * 2);
2628 while (TRUE)
2630 tmpbuf = g_malloc (tmpbufsize);
2632 /* Set the first byte to something other than '\0', to be able to
2633 * recognize whether strftime actually failed or just returned "".
2635 tmpbuf[0] = '\1';
2636 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
2638 if (tmplen == 0 && tmpbuf[0] != '\0')
2640 g_free (tmpbuf);
2641 tmpbufsize *= 2;
2643 if (tmpbufsize > 65536)
2645 g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
2646 g_free (locale_format);
2648 s[0] = '\0';
2649 return 0;
2652 else
2653 break;
2655 g_free (locale_format);
2657 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
2658 g_free (tmpbuf);
2660 if (error)
2662 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
2663 g_error_free (error);
2665 s[0] = '\0';
2666 return 0;
2669 if (slen <= convlen)
2671 /* Ensure only whole characters are copied into the buffer.
2673 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
2674 g_assert (end != NULL);
2675 convlen = end - convbuf;
2677 /* Return 0 because the buffer isn't large enough.
2679 retval = 0;
2681 else
2682 retval = convlen;
2684 memcpy (s, convbuf, convlen);
2685 s[convlen] = '\0';
2686 g_free (convbuf);
2688 return retval;
2689 #endif
2692 #pragma GCC diagnostic pop