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 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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/.
32 #include "glibconfig.h"
34 #define DEBUG_MSG(x) /* */
36 /* #define DEBUG_MSG(args) g_message args ; */
52 #include "gstrfuncs.h"
53 #include "gtestutils.h"
63 * @title: Date and Time Functions
64 * @short_description: calendrical calculations and miscellaneous time stuff
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>.
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).
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.
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.
104 * GLib also features #GDateTime which represents a precise time.
110 * Number of microseconds in one second (1 million).
111 * This macro is provided for code readability.
117 * @tv_usec: microseconds
119 * Represents a precise time, with seconds and microseconds.
120 * Similar to the struct timeval returned by the gettimeofday()
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.
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
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.
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.
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,
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.
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()
165 * Instead, do the following:
171 * gtime = (GTime)ttime;
178 * @G_DATE_MONTH: a month
179 * @G_DATE_YEAR: a year
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.
188 * Integer representing a day of the month; between 1 and
189 * 31. #G_DATE_BAD_DAY represents an invalid day of the month.
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
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
208 * Enumeration representing a month; values are #G_DATE_JANUARY,
209 * #G_DATE_FEBRUARY, etc. #G_DATE_BAD_MONTH is the invalid value.
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.
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
231 * Enumeration representing a day of the week; #G_DATE_MONDAY,
232 * #G_DATE_TUESDAY, etc. #G_DATE_BAD_WEEKDAY is an invalid weekday.
238 * Represents an invalid #GDateDay.
244 * Represents an invalid Julian day number.
250 * Represents an invalid year.
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().
261 * Returns: a newly-allocated #GDate
266 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
273 * @day: day of the month
274 * @month: month of the year
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.
281 * Returns: a newly-allocated #GDate initialized with @day, @month, and @year
284 g_date_new_dmy (GDateDay day,
289 g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
291 d = g_new (GDate, 1);
300 g_assert (g_date_valid (d));
307 * @julian_day: days since January 1, Year 1
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.
313 * Returns: a newly-allocated #GDate initialized with @julian_day
316 g_date_new_julian (guint32 julian_day)
319 g_return_val_if_fail (g_date_valid_julian (julian_day), NULL);
321 d = g_new (GDate, 1);
326 d->julian_days = julian_day;
328 g_assert (g_date_valid (d));
335 * @date: a #GDate to free
337 * Frees a #GDate returned from g_date_new().
340 g_date_free (GDate *date)
342 g_return_if_fail (date != NULL);
349 * @date: a #GDate to check
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.
355 * Returns: Whether the date is valid
358 g_date_valid (const GDate *d)
360 g_return_val_if_fail (d != NULL, FALSE);
362 return (d->julian || d->dmy);
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 */
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 }
378 * g_date_valid_month:
381 * Returns %TRUE if the month value is valid. The 12 #GDateMonth
382 * enumeration values are the only valid months.
384 * Returns: %TRUE if the month is valid
387 g_date_valid_month (GDateMonth m)
389 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
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.
399 * Returns: %TRUE if the year is valid
402 g_date_valid_year (GDateYear y)
404 return ( y > G_DATE_BAD_YEAR );
411 * Returns %TRUE if the day of the month is valid (a day is valid if it's
412 * between 1 and 31 inclusive).
414 * Returns: %TRUE if the day is valid
418 g_date_valid_day (GDateDay d)
420 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
424 * g_date_valid_weekday:
427 * Returns %TRUE if the weekday is valid. The seven #GDateWeekday enumeration
428 * values are the only valid weekdays.
430 * Returns: %TRUE if the weekday is valid
433 g_date_valid_weekday (GDateWeekday w)
435 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
439 * g_date_valid_julian:
440 * @julian_date: Julian day to check
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.
445 * Returns: %TRUE if the Julian day is valid
448 g_date_valid_julian (guint32 j)
450 return (j > G_DATE_BAD_JULIAN);
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).
463 * Returns: %TRUE if the date is a valid one
466 g_date_valid_dmy (GDateDay d,
470 return ( (m > G_DATE_BAD_MONTH) &&
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])) );
479 /* "Julian days" just means an absolute number of days, where Day 1 ==
483 g_date_update_julian (const GDate *const_d)
485 GDate *d = (GDate *) const_d;
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));
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
501 year = d->year - 1; /* we know d->year > 0 since it's valid */
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 */
508 idx = g_date_is_leap_year (d->year) ? 1 : 0;
510 d->julian_days += days_in_year[idx][d->month] + d->day;
512 g_return_if_fail (g_date_valid_julian (d->julian_days));
518 g_date_update_dmy (const GDate *const_d)
520 GDate *d = (GDate *) const_d;
525 guint32 A, B, C, D, E, M;
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));
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.
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.
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;
547 m = M + 3 - (12*(M/10));
548 day = E - (153*M + 2)/5;
549 y = 100 * B + D - 4800 + (M/10);
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);
565 * g_date_get_weekday:
568 * Returns the day of the week for a #GDate. The date must be valid.
570 * Returns: day of the week as a #GDateWeekday.
573 g_date_get_weekday (const GDate *d)
575 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
578 g_date_update_julian (d);
580 g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
582 return ((d->julian_days - 1) % 7) + 1;
587 * @date: a #GDate to get the month from
589 * Returns the month of the year. The date must be valid.
591 * Returns: month of the year as a #GDateMonth
594 g_date_get_month (const GDate *d)
596 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
599 g_date_update_dmy (d);
601 g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
610 * Returns the year of a #GDate. The date must be valid.
612 * Returns: year in which the date falls
615 g_date_get_year (const GDate *d)
617 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
620 g_date_update_dmy (d);
622 g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
629 * @date: a #GDate to extract the day of the month from
631 * Returns the day of the month. The date must be valid.
633 * Returns: day of the month
636 g_date_get_day (const GDate *d)
638 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
641 g_date_update_dmy (d);
643 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
650 * @date: a #GDate to extract the Julian day from
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.
657 * Returns: Julian day
660 g_date_get_julian (const GDate *d)
662 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
665 g_date_update_julian (d);
667 g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
669 return d->julian_days;
673 * g_date_get_day_of_year:
674 * @date: a #GDate to extract day of year from
676 * Returns the day of the year, where Jan 1 is the first day of the
677 * year. The date must be valid.
679 * Returns: day of the year
682 g_date_get_day_of_year (const GDate *d)
686 g_return_val_if_fail (g_date_valid (d), 0);
689 g_date_update_dmy (d);
691 g_return_val_if_fail (d->dmy, 0);
693 idx = g_date_is_leap_year (d->year) ? 1 : 0;
695 return (days_in_year[idx][d->month] + d->day);
699 * g_date_get_monday_week_of_year:
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.
706 * Returns: week of the year
709 g_date_get_monday_week_of_year (const GDate *d)
715 g_return_val_if_fail (g_date_valid (d), 0);
718 g_date_update_dmy (d);
720 g_return_val_if_fail (d->dmy, 0);
722 g_date_clear (&first, 1);
724 g_date_set_dmy (&first, 1, 1, d->year);
726 wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
727 day = g_date_get_day_of_year (d) - 1;
729 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
733 * g_date_get_sunday_week_of_year:
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.
740 * Returns: week number
743 g_date_get_sunday_week_of_year (const GDate *d)
749 g_return_val_if_fail (g_date_valid (d), 0);
752 g_date_update_dmy (d);
754 g_return_val_if_fail (d->dmy, 0);
756 g_date_clear (&first, 1);
758 g_date_set_dmy (&first, 1, 1, d->year);
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;
764 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
768 * g_date_get_iso8601_week_of_year:
769 * @date: a valid #GDate
771 * Returns the week of the year, where weeks are interpreted according
774 * Returns: ISO 8601 week number of the year.
779 g_date_get_iso8601_week_of_year (const GDate *d)
781 guint j, d4, L, d1, w;
783 g_return_val_if_fail (g_date_valid (d), 0);
786 g_date_update_julian (d);
788 g_return_val_if_fail (d->julian, 0);
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.
794 j = d->julian_days + 1721425;
795 d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
797 d1 = ((d4 - L) % 365) + L;
804 * g_date_days_between:
805 * @date1: the first date
806 * @date2: the second date
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.
812 * Returns: the number of days between @date1 and @date2
815 g_date_days_between (const GDate *d1,
818 g_return_val_if_fail (g_date_valid (d1), 0);
819 g_return_val_if_fail (g_date_valid (d2), 0);
821 return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
826 * @date: pointer to one or more dates to clear
827 * @n_dates: number of dates to clear
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().
835 g_date_clear (GDate *d, guint ndates)
837 g_return_if_fail (d != NULL);
838 g_return_if_fail (ndates != 0);
840 memset (d, 0x0, ndates*sizeof (GDate));
843 G_LOCK_DEFINE_STATIC (g_date_global);
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. :-)
850 static gchar *long_month_names[13] =
855 static gchar *short_month_names[13] =
860 /* This tells us if we need to update the parse info */
861 static gchar *current_locale = NULL;
863 /* order of these in the current locale */
864 static GDateDMY dmy_order[3] =
866 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
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.
874 static const GDateYear twodigit_start_year = 1930;
876 /* It is impossible to enter a year between 1 AD and 99 AD with this
879 static gboolean using_twodigit_years = FALSE;
881 /* Adjustment of locale era to AD, non-zero means using locale era
883 static gint locale_era_adjust = 0;
885 struct _GDateParseTokens {
891 typedef struct _GDateParseTokens GDateParseTokens;
895 /* HOLDS: g_date_global_lock */
897 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
899 gchar num[4][NUM_LEN+1];
903 /* We count 4, but store 3; so we can give an error
906 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
908 s = (const guchar *) str;
910 while (*s && pt->num_ints < 4)
914 while (*s && g_ascii_isdigit (*s) && i < NUM_LEN)
916 num[pt->num_ints][i] = *s;
923 num[pt->num_ints][i] = '\0';
927 if (*s == '\0') break;
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;
936 pt->month = G_DATE_BAD_MONTH;
938 if (pt->num_ints < 3)
943 casefold = g_utf8_casefold (str, -1);
944 normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
950 if (long_month_names[i] != NULL)
952 const gchar *found = strstr (normalized, long_month_names[i]);
961 if (short_month_names[i] != NULL)
963 const gchar *found = strstr (normalized, short_month_names[i]);
979 /* HOLDS: g_date_global_lock */
981 g_date_prepare_to_parse (const gchar *str,
982 GDateParseTokens *pt)
984 const gchar *locale = setlocale (LC_TIME, NULL);
985 gboolean recompute_localeinfo = FALSE;
988 g_return_if_fail (locale != NULL); /* should not happen */
990 g_date_clear (&d, 1); /* clear for scratch use */
992 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
993 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
995 if (recompute_localeinfo)
998 GDateParseTokens testpt;
1001 g_free (current_locale); /* still works if current_locale == NULL */
1003 current_locale = g_strdup (locale);
1005 short_month_names[0] = "Error";
1006 long_month_names[0] = "Error";
1012 g_date_set_dmy (&d, 1, i, 1);
1014 g_return_if_fail (g_date_valid (&d));
1016 g_date_strftime (buf, 127, "%b", &d);
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);
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);
1032 /* Determine DMY order */
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);
1038 g_date_strftime (buf, 127, "%x", &d);
1040 g_date_fill_parse_tokens (buf, &testpt);
1043 while (i < testpt.num_ints)
1045 switch (testpt.n[i])
1048 dmy_order[i] = G_DATE_MONTH;
1051 dmy_order[i] = G_DATE_DAY;
1054 using_twodigit_years = TRUE; /* FALL THRU */
1056 dmy_order[i] = G_DATE_YEAR;
1059 /* assume locale era */
1060 locale_era_adjust = 1976 - testpt.n[i];
1061 dmy_order[i] = G_DATE_YEAR;
1067 #if defined(G_ENABLE_DEBUG) && 0
1068 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
1072 DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
1075 if (using_twodigit_years)
1077 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
1084 switch (dmy_order[i])
1087 strings[i] = "Month";
1090 strings[i] = "Year";
1101 DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
1102 DEBUG_MSG (("**Sample date in this locale: '%s'", buf));
1107 g_date_fill_parse_tokens (str, pt);
1112 * @date: a #GDate to fill in
1113 * @str: string to parse
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.
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
1128 g_date_set_parse (GDate *d,
1131 GDateParseTokens pt;
1132 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
1134 g_return_if_fail (d != NULL);
1137 g_date_clear (d, 1);
1139 G_LOCK (g_date_global);
1141 g_date_prepare_to_parse (str, &pt);
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));
1147 if (pt.num_ints == 4)
1149 G_UNLOCK (g_date_global);
1150 return; /* presumably a typo; bail out. */
1153 if (pt.num_ints > 1)
1158 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
1160 while (i < pt.num_ints && j < 3)
1162 switch (dmy_order[j])
1166 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
1169 ++j; /* skip months, but don't skip this number */
1178 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
1181 ++j; /* skip days, since we may have month/year */
1191 if (locale_era_adjust != 0)
1193 y += locale_era_adjust;
1195 else if (using_twodigit_years && y < 100)
1197 guint two = twodigit_start_year % 100;
1198 guint century = (twodigit_start_year / 100) * 100;
1216 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
1218 /* Try YYYY MM DD */
1223 if (using_twodigit_years && y < 100)
1224 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
1226 else if (pt.num_ints == 2)
1228 if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
1232 else if (pt.num_ints == 1)
1234 if (pt.month != G_DATE_BAD_MONTH)
1236 /* Month name and year? */
1243 /* Try yyyymmdd and yymmdd */
1245 m = (pt.n[0]/100) % 100;
1246 day = pt.n[0] % 100;
1249 /* FIXME move this into a separate function */
1250 if (using_twodigit_years && y < 100)
1252 guint two = twodigit_start_year % 100;
1253 guint century = (twodigit_start_year / 100) * 100;
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))
1272 #ifdef G_ENABLE_DEBUG
1275 DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
1278 G_UNLOCK (g_date_global);
1282 * g_date_set_time_t:
1284 * @timet: time_t value to set
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.
1290 * To set the value of a date to the current day, you could write:
1292 * g_date_set_time_t (date, time (NULL));
1298 g_date_set_time_t (GDate *date,
1303 g_return_if_fail (date != NULL);
1305 #ifdef HAVE_LOCALTIME_R
1306 localtime_r (&timet, &tm);
1309 struct tm *ptm = localtime (&timet);
1313 /* Happens at least in Microsoft's C library if you pass a
1314 * negative time_t. Use 2000-01-01 as default date.
1316 #ifndef G_DISABLE_CHECKS
1317 g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
1325 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
1329 date->julian = FALSE;
1331 date->month = tm.tm_mon + 1;
1332 date->day = tm.tm_mday;
1333 date->year = tm.tm_year + 1900;
1335 g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
1344 * @time_: #GTime value to set.
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.
1349 * Deprecated: 2.10: Use g_date_set_time_t() instead.
1352 g_date_set_time (GDate *date,
1355 g_date_set_time_t (date, (time_t) time_);
1359 * g_date_set_time_val:
1361 * @timeval: #GTimeVal value to set
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.
1367 * The time to date conversion is done using the user's current timezone.
1372 g_date_set_time_val (GDate *date,
1375 g_date_set_time_t (date, (time_t) timeval->tv_sec);
1381 * @month: month to set
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.
1387 g_date_set_month (GDate *d,
1390 g_return_if_fail (d != NULL);
1391 g_return_if_fail (g_date_valid_month (m));
1393 if (d->julian && !d->dmy) g_date_update_dmy(d);
1398 if (g_date_valid_dmy (d->day, d->month, d->year))
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.
1413 g_date_set_day (GDate *d,
1416 g_return_if_fail (d != NULL);
1417 g_return_if_fail (g_date_valid_day (day));
1419 if (d->julian && !d->dmy) g_date_update_dmy(d);
1424 if (g_date_valid_dmy (d->day, d->month, d->year))
1433 * @year: year to set
1435 * Sets the year for a #GDate. If the resulting day-month-year
1436 * triplet is invalid, the date will be invalid.
1439 g_date_set_year (GDate *d,
1442 g_return_if_fail (d != NULL);
1443 g_return_if_fail (g_date_valid_year (y));
1445 if (d->julian && !d->dmy) g_date_update_dmy(d);
1450 if (g_date_valid_dmy (d->day, d->month, d->year))
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
1469 g_date_set_dmy (GDate *d,
1474 g_return_if_fail (d != NULL);
1475 g_return_if_fail (g_date_valid_dmy (day, m, y));
1487 * g_date_set_julian:
1489 * @julian_date: Julian day number (days since January 1, Year 1)
1491 * Sets the value of a #GDate from a Julian day number.
1494 g_date_set_julian (GDate *d,
1497 g_return_if_fail (d != NULL);
1498 g_return_if_fail (g_date_valid_julian (j));
1506 * g_date_is_first_of_month:
1507 * @date: a #GDate to check
1509 * Returns %TRUE if the date is on the first of a month.
1510 * The date must be valid.
1512 * Returns: %TRUE if the date is the first of the month
1515 g_date_is_first_of_month (const GDate *d)
1517 g_return_val_if_fail (g_date_valid (d), FALSE);
1520 g_date_update_dmy (d);
1522 g_return_val_if_fail (d->dmy, FALSE);
1524 if (d->day == 1) return TRUE;
1529 * g_date_is_last_of_month:
1530 * @date: a #GDate to check
1532 * Returns %TRUE if the date is the last day of the month.
1533 * The date must be valid.
1535 * Returns: %TRUE if the date is the last day of the month
1538 g_date_is_last_of_month (const GDate *d)
1542 g_return_val_if_fail (g_date_valid (d), FALSE);
1545 g_date_update_dmy (d);
1547 g_return_val_if_fail (d->dmy, FALSE);
1549 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1551 if (d->day == days_in_months[idx][d->month]) return TRUE;
1557 * @date: a #GDate to increment
1558 * @n_days: number of days to move the date forward
1560 * Increments a date some number of days.
1561 * To move forward by weeks, add weeks*7 days.
1562 * The date must be valid.
1565 g_date_add_days (GDate *d,
1568 g_return_if_fail (g_date_valid (d));
1571 g_date_update_julian (d);
1573 g_return_if_fail (d->julian);
1575 d->julian_days += ndays;
1580 * g_date_subtract_days:
1581 * @date: a #GDate to decrement
1582 * @n_days: number of days to move
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.
1589 g_date_subtract_days (GDate *d,
1592 g_return_if_fail (g_date_valid (d));
1595 g_date_update_julian (d);
1597 g_return_if_fail (d->julian);
1598 g_return_if_fail (d->julian_days > ndays);
1600 d->julian_days -= ndays;
1605 * g_date_add_months:
1606 * @date: a #GDate to increment
1607 * @n_months: number of months to move forward
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.
1616 g_date_add_months (GDate *d,
1619 guint years, months;
1622 g_return_if_fail (g_date_valid (d));
1625 g_date_update_dmy (d);
1627 g_return_if_fail (d->dmy);
1629 nmonths += d->month - 1;
1632 months = nmonths%12;
1634 d->month = months + 1;
1637 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1639 if (d->day > days_in_months[idx][d->month])
1640 d->day = days_in_months[idx][d->month];
1644 g_return_if_fail (g_date_valid (d));
1648 * g_date_subtract_months:
1649 * @date: a #GDate to decrement
1650 * @n_months: number of months to move
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.
1658 g_date_subtract_months (GDate *d,
1661 guint years, months;
1664 g_return_if_fail (g_date_valid (d));
1667 g_date_update_dmy (d);
1669 g_return_if_fail (d->dmy);
1672 months = nmonths%12;
1674 g_return_if_fail (d->year > years);
1678 if (d->month > months) d->month -= months;
1682 d->month = 12 - months;
1686 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1688 if (d->day > days_in_months[idx][d->month])
1689 d->day = days_in_months[idx][d->month];
1693 g_return_if_fail (g_date_valid (d));
1698 * @date: a #GDate to increment
1699 * @n_years: number of years to move forward
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.
1707 g_date_add_years (GDate *d,
1710 g_return_if_fail (g_date_valid (d));
1713 g_date_update_dmy (d);
1715 g_return_if_fail (d->dmy);
1719 if (d->month == 2 && d->day == 29)
1721 if (!g_date_is_leap_year (d->year))
1729 * g_date_subtract_years:
1730 * @date: a #GDate to decrement
1731 * @n_years: number of years to move
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
1740 g_date_subtract_years (GDate *d,
1743 g_return_if_fail (g_date_valid (d));
1746 g_date_update_dmy (d);
1748 g_return_if_fail (d->dmy);
1749 g_return_if_fail (d->year > nyears);
1753 if (d->month == 2 && d->day == 29)
1755 if (!g_date_is_leap_year (d->year))
1763 * g_date_is_leap_year:
1764 * @year: year to check
1766 * Returns %TRUE if the year is a leap year.
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.
1773 * Returns: %TRUE if the year is a leap year
1776 g_date_is_leap_year (GDateYear year)
1778 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1780 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1781 (year % 400) == 0 );
1785 * g_date_get_days_in_month:
1789 * Returns the number of days in a month, taking leap
1790 * years into account.
1792 * Returns: number of days in @month during the @year
1795 g_date_get_days_in_month (GDateMonth month,
1800 g_return_val_if_fail (g_date_valid_year (year), 0);
1801 g_return_val_if_fail (g_date_valid_month (month), 0);
1803 idx = g_date_is_leap_year (year) ? 1 : 0;
1805 return days_in_months[idx][month];
1809 * g_date_get_monday_weeks_in_year:
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.)
1820 * Returns: number of Mondays in the year
1823 g_date_get_monday_weeks_in_year (GDateYear year)
1827 g_return_val_if_fail (g_date_valid_year (year), 0);
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))
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;
1845 * g_date_get_sunday_weeks_in_year:
1846 * @year: year to count weeks in
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.)
1856 * Returns: the number of weeks in @year
1859 g_date_get_sunday_weeks_in_year (GDateYear year)
1863 g_return_val_if_fail (g_date_valid_year (year), 0);
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))
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;
1882 * @lhs: first date to compare
1883 * @rhs: second date to compare
1885 * qsort()-style comparison function for dates.
1886 * Both dates must be valid.
1888 * Returns: 0 for equal, less than zero if @lhs is less than @rhs,
1889 * greater than zero if @lhs is greater than @rhs
1892 g_date_compare (const GDate *lhs,
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);
1900 /* Remember the self-comparison case! I think it works right now. */
1904 if (lhs->julian && rhs->julian)
1906 if (lhs->julian_days < rhs->julian_days) return -1;
1907 else if (lhs->julian_days > rhs->julian_days) return 1;
1910 else if (lhs->dmy && rhs->dmy)
1912 if (lhs->year < rhs->year) return -1;
1913 else if (lhs->year > rhs->year) return 1;
1916 if (lhs->month < rhs->month) return -1;
1917 else if (lhs->month > rhs->month) return 1;
1920 if (lhs->day < rhs->day) return -1;
1921 else if (lhs->day > rhs->day) return 1;
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);
1937 return 0; /* warnings */
1941 * g_date_to_struct_tm:
1942 * @date: a #GDate to set the struct tm from
1943 * @tm: struct tm to fill
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.
1949 g_date_to_struct_tm (const GDate *d,
1954 g_return_if_fail (g_date_valid (d));
1955 g_return_if_fail (tm != NULL);
1958 g_date_update_dmy (d);
1960 g_return_if_fail (d->dmy);
1962 /* zero all the irrelevant fields to be sure they're valid */
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.
1970 memset (tm, 0x0, sizeof (struct tm));
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 */
1976 day = g_date_get_weekday (d);
1977 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1979 tm->tm_wday = (int)day;
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" */
1987 * @date: a #GDate to clamp
1988 * @min_date: minimum accepted value for @date
1989 * @max_date: maximum accepted value for @date
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.
1998 g_date_clamp (GDate *date,
1999 const GDate *min_date,
2000 const GDate *max_date)
2002 g_return_if_fail (g_date_valid (date));
2004 if (min_date != NULL)
2005 g_return_if_fail (g_date_valid (min_date));
2007 if (max_date != NULL)
2008 g_return_if_fail (g_date_valid (max_date));
2010 if (min_date != NULL && max_date != NULL)
2011 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
2013 if (min_date && g_date_compare (date, min_date) < 0)
2016 if (max_date && g_date_compare (max_date, date) < 0)
2022 * @date1: the first date
2023 * @date2: the second date
2025 * Checks if @date1 is less than or equal to @date2,
2026 * and swap the values if this is not the case.
2029 g_date_order (GDate *date1,
2032 g_return_if_fail (g_date_valid (date1));
2033 g_return_if_fail (g_date_valid (date2));
2035 if (g_date_compare (date1, date2) > 0)
2045 win32_strftime_helper (const GDate *d,
2046 const gchar *format,
2047 const struct tm *tm,
2051 SYSTEMTIME systemtime;
2052 TIME_ZONE_INFORMATION tzinfo;
2058 const wchar_t digits[] = L"0123456789";
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;
2072 lcid = GetThreadLocale ();
2073 result = g_array_sized_new (FALSE, FALSE, sizeof (wchar_t), MAX (128, strlen (format) * 2));
2078 c = g_utf8_get_char (p);
2081 p = g_utf8_next_char (p);
2085 g_array_free (result, TRUE);
2090 c = g_utf8_get_char (p);
2091 if (c == 'E' || c == 'O')
2093 /* Ignore modified conversion specifiers for now. */
2094 p = g_utf8_next_char (p);
2098 g_array_free (result, TRUE);
2103 c = g_utf8_get_char (p);
2109 if (systemtime.wDayOfWeek == 0)
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);
2119 if (systemtime.wDayOfWeek == 0)
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);
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);
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);
2142 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
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);
2149 g_array_append_vals (result, L" ", 1);
2150 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
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);
2159 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
2160 g_array_append_vals (result, digits + (systemtime.wYear/1000)%10, 1);
2163 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2164 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
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);
2177 if (systemtime.wDay >= 10)
2178 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2180 g_array_append_vals (result, L" ", 1);
2181 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
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()
2193 g_array_append_vals (result, L"00", 2);
2195 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2196 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2201 g_array_append_vals (result, L"12", 2);
2203 if (systemtime.wHour == 0)
2204 g_array_append_vals (result, L"12", 2);
2207 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
2208 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
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);
2218 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
2219 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
2223 g_array_append_vals (result, L"00", 2);
2225 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2226 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2230 g_array_append_vals (result, L"\n", 1);
2233 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
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);
2242 /* This is a rather odd format. Hard to say what to do.
2243 * Let's always use the POSIX %I:%M:%S %p
2246 g_array_append_vals (result, L"12:00:00", 8);
2248 if (systemtime.wHour == 0)
2249 g_array_append_vals (result, L"12", 2);
2252 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
2253 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
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);
2263 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
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);
2273 g_array_append_vals (result, L"00:00", 5);
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);
2284 g_array_append_vals (result, L"00", 2);
2286 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2287 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2291 g_array_append_vals (result, L"\t", 1);
2295 g_array_append_vals (result, L"00:00:00", 8);
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);
2308 if (systemtime.wDayOfWeek == 0)
2309 g_array_append_vals (result, L"7", 1);
2311 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
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);
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);
2324 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
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);
2332 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
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);
2341 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
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);
2350 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2351 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
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);
2360 n = GetTimeZoneInformation (&tzinfo);
2361 if (n == TIME_ZONE_ID_UNKNOWN)
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));
2369 g_array_append_vals (result, L"%", 1);
2373 else if (c <= 0xFFFF)
2376 g_array_append_vals (result, &wc, 1);
2383 ws = g_ucs4_to_utf16 (&c, 1, NULL, &nwc, NULL);
2384 g_array_append_vals (result, ws, nwc);
2387 p = g_utf8_next_char (p);
2390 convbuf = g_utf16_to_utf8 ((wchar_t *) result->data, result->len, NULL, &convlen, NULL);
2391 g_array_free (result, TRUE);
2399 if (slen <= convlen)
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;
2406 /* Return 0 because the buffer isn't large enough. */
2412 memcpy (s, convbuf, convlen);
2423 * @s: destination buffer
2424 * @slen: buffer size
2425 * @format: format string
2426 * @date: valid #GDate
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.
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.
2442 * Returns: number of characters written to the buffer, or 0 the buffer was too small
2445 g_date_strftime (gchar *s,
2447 const gchar *format,
2452 gsize locale_format_len = 0;
2453 gchar *locale_format;
2459 GError *error = NULL;
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);
2468 g_date_to_struct_tm (d, &tm);
2471 if (!g_utf8_validate (format, -1, NULL))
2476 return win32_strftime_helper (d, format, &tm, s, slen);
2479 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
2483 g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
2484 g_error_free (error);
2490 tmpbufsize = MAX (128, locale_format_len * 2);
2493 tmpbuf = g_malloc (tmpbufsize);
2495 /* Set the first byte to something other than '\0', to be able to
2496 * recognize whether strftime actually failed or just returned "".
2499 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
2501 if (tmplen == 0 && tmpbuf[0] != '\0')
2506 if (tmpbufsize > 65536)
2508 g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
2509 g_free (locale_format);
2518 g_free (locale_format);
2520 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
2525 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
2526 g_error_free (error);
2532 if (slen <= convlen)
2534 /* 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.
2547 memcpy (s, convbuf, convlen);