Partly revert "gio: Add filename type annotations"
[glib.git] / glib / gdate.c
blobbea2448df2798c137b730658bf00db3732c747b5
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, 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_valid:
347 * @date: a #GDate to check
349 * Returns %TRUE if the #GDate represents an existing day. The date must not
350 * contain garbage; it should have been initialized with g_date_clear()
351 * if it wasn't allocated by one of the g_date_new() variants.
353 * Returns: Whether the date is valid
355 gboolean
356 g_date_valid (const GDate *d)
358 g_return_val_if_fail (d != NULL, FALSE);
360 return (d->julian || d->dmy);
363 static const guint8 days_in_months[2][13] =
364 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
365 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
366 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
369 static const guint16 days_in_year[2][14] =
370 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
371 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
372 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
376 * g_date_valid_month:
377 * @month: month
379 * Returns %TRUE if the month value is valid. The 12 #GDateMonth
380 * enumeration values are the only valid months.
382 * Returns: %TRUE if the month is valid
384 gboolean
385 g_date_valid_month (GDateMonth m)
387 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
391 * g_date_valid_year:
392 * @year: year
394 * Returns %TRUE if the year is valid. Any year greater than 0 is valid,
395 * though there is a 16-bit limit to what #GDate will understand.
397 * Returns: %TRUE if the year is valid
399 gboolean
400 g_date_valid_year (GDateYear y)
402 return ( y > G_DATE_BAD_YEAR );
406 * g_date_valid_day:
407 * @day: day to check
409 * Returns %TRUE if the day of the month is valid (a day is valid if it's
410 * between 1 and 31 inclusive).
412 * Returns: %TRUE if the day is valid
415 gboolean
416 g_date_valid_day (GDateDay d)
418 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
422 * g_date_valid_weekday:
423 * @weekday: weekday
425 * Returns %TRUE if the weekday is valid. The seven #GDateWeekday enumeration
426 * values are the only valid weekdays.
428 * Returns: %TRUE if the weekday is valid
430 gboolean
431 g_date_valid_weekday (GDateWeekday w)
433 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
437 * g_date_valid_julian:
438 * @julian_date: Julian day to check
440 * Returns %TRUE if the Julian day is valid. Anything greater than zero
441 * is basically a valid Julian, though there is a 32-bit limit.
443 * Returns: %TRUE if the Julian day is valid
445 gboolean
446 g_date_valid_julian (guint32 j)
448 return (j > G_DATE_BAD_JULIAN);
452 * g_date_valid_dmy:
453 * @day: day
454 * @month: month
455 * @year: year
457 * Returns %TRUE if the day-month-year triplet forms a valid, existing day
458 * in the range of days #GDate understands (Year 1 or later, no more than
459 * a few thousand years in the future).
461 * Returns: %TRUE if the date is a valid one
463 gboolean
464 g_date_valid_dmy (GDateDay d,
465 GDateMonth m,
466 GDateYear y)
468 return ( (m > G_DATE_BAD_MONTH) &&
469 (m < 13) &&
470 (d > G_DATE_BAD_DAY) &&
471 (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
472 (d <= (g_date_is_leap_year (y) ?
473 days_in_months[1][m] : days_in_months[0][m])) );
477 /* "Julian days" just means an absolute number of days, where Day 1 ==
478 * Jan 1, Year 1
480 static void
481 g_date_update_julian (const GDate *const_d)
483 GDate *d = (GDate *) const_d;
484 GDateYear year;
485 gint idx;
487 g_return_if_fail (d != NULL);
488 g_return_if_fail (d->dmy);
489 g_return_if_fail (!d->julian);
490 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
492 /* What we actually do is: multiply years * 365 days in the year,
493 * add the number of years divided by 4, subtract the number of
494 * years divided by 100 and add the number of years divided by 400,
495 * which accounts for leap year stuff. Code from Steffen Beyer's
496 * DateCalc.
499 year = d->year - 1; /* we know d->year > 0 since it's valid */
501 d->julian_days = year * 365U;
502 d->julian_days += (year >>= 2); /* divide by 4 and add */
503 d->julian_days -= (year /= 25); /* divides original # years by 100 */
504 d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
506 idx = g_date_is_leap_year (d->year) ? 1 : 0;
508 d->julian_days += days_in_year[idx][d->month] + d->day;
510 g_return_if_fail (g_date_valid_julian (d->julian_days));
512 d->julian = TRUE;
515 static void
516 g_date_update_dmy (const GDate *const_d)
518 GDate *d = (GDate *) const_d;
519 GDateYear y;
520 GDateMonth m;
521 GDateDay day;
523 guint32 A, B, C, D, E, M;
525 g_return_if_fail (d != NULL);
526 g_return_if_fail (d->julian);
527 g_return_if_fail (!d->dmy);
528 g_return_if_fail (g_date_valid_julian (d->julian_days));
530 /* Formula taken from the Calendar FAQ; the formula was for the
531 * Julian Period which starts on 1 January 4713 BC, so we add
532 * 1,721,425 to the number of days before doing the formula.
534 * I'm sure this can be simplified for our 1 January 1 AD period
535 * start, but I can't figure out how to unpack the formula.
538 A = d->julian_days + 1721425 + 32045;
539 B = ( 4 *(A + 36524) )/ 146097 - 1;
540 C = A - (146097 * B)/4;
541 D = ( 4 * (C + 365) ) / 1461 - 1;
542 E = C - ((1461*D) / 4);
543 M = (5 * (E - 1) + 2)/153;
545 m = M + 3 - (12*(M/10));
546 day = E - (153*M + 2)/5;
547 y = 100 * B + D - 4800 + (M/10);
549 #ifdef G_ENABLE_DEBUG
550 if (!g_date_valid_dmy (day, m, y))
551 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
552 d->julian_days, day, m, y);
553 #endif
555 d->month = m;
556 d->day = day;
557 d->year = y;
559 d->dmy = TRUE;
563 * g_date_get_weekday:
564 * @date: a #GDate
566 * Returns the day of the week for a #GDate. The date must be valid.
568 * Returns: day of the week as a #GDateWeekday.
570 GDateWeekday
571 g_date_get_weekday (const GDate *d)
573 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
575 if (!d->julian)
576 g_date_update_julian (d);
578 g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
580 return ((d->julian_days - 1) % 7) + 1;
584 * g_date_get_month:
585 * @date: a #GDate to get the month from
587 * Returns the month of the year. The date must be valid.
589 * Returns: month of the year as a #GDateMonth
591 GDateMonth
592 g_date_get_month (const GDate *d)
594 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
596 if (!d->dmy)
597 g_date_update_dmy (d);
599 g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
601 return d->month;
605 * g_date_get_year:
606 * @date: a #GDate
608 * Returns the year of a #GDate. The date must be valid.
610 * Returns: year in which the date falls
612 GDateYear
613 g_date_get_year (const GDate *d)
615 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
617 if (!d->dmy)
618 g_date_update_dmy (d);
620 g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
622 return d->year;
626 * g_date_get_day:
627 * @date: a #GDate to extract the day of the month from
629 * Returns the day of the month. The date must be valid.
631 * Returns: day of the month
633 GDateDay
634 g_date_get_day (const GDate *d)
636 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
638 if (!d->dmy)
639 g_date_update_dmy (d);
641 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
643 return d->day;
647 * g_date_get_julian:
648 * @date: a #GDate to extract the Julian day from
650 * Returns the Julian day or "serial number" of the #GDate. The
651 * Julian day is simply the number of days since January 1, Year 1; i.e.,
652 * January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2,
653 * etc. The date must be valid.
655 * Returns: Julian day
657 guint32
658 g_date_get_julian (const GDate *d)
660 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
662 if (!d->julian)
663 g_date_update_julian (d);
665 g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
667 return d->julian_days;
671 * g_date_get_day_of_year:
672 * @date: a #GDate to extract day of year from
674 * Returns the day of the year, where Jan 1 is the first day of the
675 * year. The date must be valid.
677 * Returns: day of the year
679 guint
680 g_date_get_day_of_year (const GDate *d)
682 gint idx;
684 g_return_val_if_fail (g_date_valid (d), 0);
686 if (!d->dmy)
687 g_date_update_dmy (d);
689 g_return_val_if_fail (d->dmy, 0);
691 idx = g_date_is_leap_year (d->year) ? 1 : 0;
693 return (days_in_year[idx][d->month] + d->day);
697 * g_date_get_monday_week_of_year:
698 * @date: a #GDate
700 * Returns the week of the year, where weeks are understood to start on
701 * Monday. If the date is before the first Monday of the year, return 0.
702 * The date must be valid.
704 * Returns: week of the year
706 guint
707 g_date_get_monday_week_of_year (const GDate *d)
709 GDateWeekday wd;
710 guint day;
711 GDate first;
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 g_date_clear (&first, 1);
722 g_date_set_dmy (&first, 1, 1, d->year);
724 wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
725 day = g_date_get_day_of_year (d) - 1;
727 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
731 * g_date_get_sunday_week_of_year:
732 * @date: a #GDate
734 * Returns the week of the year during which this date falls, if
735 * weeks are understood to begin on Sunday. The date must be valid.
736 * Can return 0 if the day is before the first Sunday of the year.
738 * Returns: week number
740 guint
741 g_date_get_sunday_week_of_year (const GDate *d)
743 GDateWeekday wd;
744 guint day;
745 GDate first;
747 g_return_val_if_fail (g_date_valid (d), 0);
749 if (!d->dmy)
750 g_date_update_dmy (d);
752 g_return_val_if_fail (d->dmy, 0);
754 g_date_clear (&first, 1);
756 g_date_set_dmy (&first, 1, 1, d->year);
758 wd = g_date_get_weekday (&first);
759 if (wd == 7) wd = 0; /* make Sunday day 0 */
760 day = g_date_get_day_of_year (d) - 1;
762 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
766 * g_date_get_iso8601_week_of_year:
767 * @date: a valid #GDate
769 * Returns the week of the year, where weeks are interpreted according
770 * to ISO 8601.
772 * Returns: ISO 8601 week number of the year.
774 * Since: 2.6
776 guint
777 g_date_get_iso8601_week_of_year (const GDate *d)
779 guint j, d4, L, d1, w;
781 g_return_val_if_fail (g_date_valid (d), 0);
783 if (!d->julian)
784 g_date_update_julian (d);
786 g_return_val_if_fail (d->julian, 0);
788 /* Formula taken from the Calendar FAQ; the formula was for the
789 * Julian Period which starts on 1 January 4713 BC, so we add
790 * 1,721,425 to the number of days before doing the formula.
792 j = d->julian_days + 1721425;
793 d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
794 L = d4 / 1460;
795 d1 = ((d4 - L) % 365) + L;
796 w = d1 / 7 + 1;
798 return w;
802 * g_date_days_between:
803 * @date1: the first date
804 * @date2: the second date
806 * Computes the number of days between two dates.
807 * If @date2 is prior to @date1, the returned value is negative.
808 * Both dates must be valid.
810 * Returns: the number of days between @date1 and @date2
812 gint
813 g_date_days_between (const GDate *d1,
814 const GDate *d2)
816 g_return_val_if_fail (g_date_valid (d1), 0);
817 g_return_val_if_fail (g_date_valid (d2), 0);
819 return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
823 * g_date_clear:
824 * @date: pointer to one or more dates to clear
825 * @n_dates: number of dates to clear
827 * Initializes one or more #GDate structs to a sane but invalid
828 * state. The cleared dates will not represent an existing date, but will
829 * not contain garbage. Useful to init a date declared on the stack.
830 * Validity can be tested with g_date_valid().
832 void
833 g_date_clear (GDate *d, guint ndates)
835 g_return_if_fail (d != NULL);
836 g_return_if_fail (ndates != 0);
838 memset (d, 0x0, ndates*sizeof (GDate));
841 G_LOCK_DEFINE_STATIC (g_date_global);
843 /* These are for the parser, output to the user should use *
844 * g_date_strftime () - this creates more never-freed memory to annoy
845 * all those memory debugger users. :-)
848 static gchar *long_month_names[13] =
850 NULL,
853 static gchar *short_month_names[13] =
855 NULL,
858 /* This tells us if we need to update the parse info */
859 static gchar *current_locale = NULL;
861 /* order of these in the current locale */
862 static GDateDMY dmy_order[3] =
864 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
867 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
868 * 29 and below are counted as in the year 2000, numbers 30 and above
869 * are counted as in the year 1900.
872 static const GDateYear twodigit_start_year = 1930;
874 /* It is impossible to enter a year between 1 AD and 99 AD with this
875 * in effect.
877 static gboolean using_twodigit_years = FALSE;
879 /* Adjustment of locale era to AD, non-zero means using locale era
881 static gint locale_era_adjust = 0;
883 struct _GDateParseTokens {
884 gint num_ints;
885 gint n[3];
886 guint month;
889 typedef struct _GDateParseTokens GDateParseTokens;
891 #define NUM_LEN 10
893 /* HOLDS: g_date_global_lock */
894 static void
895 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
897 gchar num[4][NUM_LEN+1];
898 gint i;
899 const guchar *s;
901 /* We count 4, but store 3; so we can give an error
902 * if there are 4.
904 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
906 s = (const guchar *) str;
907 pt->num_ints = 0;
908 while (*s && pt->num_ints < 4)
911 i = 0;
912 while (*s && g_ascii_isdigit (*s) && i < NUM_LEN)
914 num[pt->num_ints][i] = *s;
915 ++s;
916 ++i;
919 if (i > 0)
921 num[pt->num_ints][i] = '\0';
922 ++(pt->num_ints);
925 if (*s == '\0') break;
927 ++s;
930 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
931 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
932 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
934 pt->month = G_DATE_BAD_MONTH;
936 if (pt->num_ints < 3)
938 gchar *casefold;
939 gchar *normalized;
941 casefold = g_utf8_casefold (str, -1);
942 normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
943 g_free (casefold);
945 i = 1;
946 while (i < 13)
948 if (long_month_names[i] != NULL)
950 const gchar *found = strstr (normalized, long_month_names[i]);
952 if (found != NULL)
954 pt->month = i;
955 break;
959 if (short_month_names[i] != NULL)
961 const gchar *found = strstr (normalized, short_month_names[i]);
963 if (found != NULL)
965 pt->month = i;
966 break;
970 ++i;
973 g_free (normalized);
977 /* HOLDS: g_date_global_lock */
978 static void
979 g_date_prepare_to_parse (const gchar *str,
980 GDateParseTokens *pt)
982 const gchar *locale = setlocale (LC_TIME, NULL);
983 gboolean recompute_localeinfo = FALSE;
984 GDate d;
986 g_return_if_fail (locale != NULL); /* should not happen */
988 g_date_clear (&d, 1); /* clear for scratch use */
990 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
991 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
993 if (recompute_localeinfo)
995 int i = 1;
996 GDateParseTokens testpt;
997 gchar buf[128];
999 g_free (current_locale); /* still works if current_locale == NULL */
1001 current_locale = g_strdup (locale);
1003 short_month_names[0] = "Error";
1004 long_month_names[0] = "Error";
1006 while (i < 13)
1008 gchar *casefold;
1010 g_date_set_dmy (&d, 1, i, 1);
1012 g_return_if_fail (g_date_valid (&d));
1014 g_date_strftime (buf, 127, "%b", &d);
1016 casefold = g_utf8_casefold (buf, -1);
1017 g_free (short_month_names[i]);
1018 short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1019 g_free (casefold);
1021 g_date_strftime (buf, 127, "%B", &d);
1022 casefold = g_utf8_casefold (buf, -1);
1023 g_free (long_month_names[i]);
1024 long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1025 g_free (casefold);
1027 ++i;
1030 /* Determine DMY order */
1032 /* had to pick a random day - don't change this, some strftimes
1033 * are broken on some days, and this one is good so far. */
1034 g_date_set_dmy (&d, 4, 7, 1976);
1036 g_date_strftime (buf, 127, "%x", &d);
1038 g_date_fill_parse_tokens (buf, &testpt);
1040 i = 0;
1041 while (i < testpt.num_ints)
1043 switch (testpt.n[i])
1045 case 7:
1046 dmy_order[i] = G_DATE_MONTH;
1047 break;
1048 case 4:
1049 dmy_order[i] = G_DATE_DAY;
1050 break;
1051 case 76:
1052 using_twodigit_years = TRUE; /* FALL THRU */
1053 case 1976:
1054 dmy_order[i] = G_DATE_YEAR;
1055 break;
1056 default:
1057 /* assume locale era */
1058 locale_era_adjust = 1976 - testpt.n[i];
1059 dmy_order[i] = G_DATE_YEAR;
1060 break;
1062 ++i;
1065 #if defined(G_ENABLE_DEBUG) && 0
1066 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
1067 i = 1;
1068 while (i < 13)
1070 DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
1071 ++i;
1073 if (using_twodigit_years)
1075 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
1078 gchar *strings[3];
1079 i = 0;
1080 while (i < 3)
1082 switch (dmy_order[i])
1084 case G_DATE_MONTH:
1085 strings[i] = "Month";
1086 break;
1087 case G_DATE_YEAR:
1088 strings[i] = "Year";
1089 break;
1090 case G_DATE_DAY:
1091 strings[i] = "Day";
1092 break;
1093 default:
1094 strings[i] = NULL;
1095 break;
1097 ++i;
1099 DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
1100 DEBUG_MSG (("**Sample date in this locale: '%s'", buf));
1102 #endif
1105 g_date_fill_parse_tokens (str, pt);
1109 * g_date_set_parse:
1110 * @date: a #GDate to fill in
1111 * @str: string to parse
1113 * Parses a user-inputted string @str, and try to figure out what date it
1114 * represents, taking the [current locale][setlocale] into account. If the
1115 * string is successfully parsed, the date will be valid after the call.
1116 * Otherwise, it will be invalid. You should check using g_date_valid()
1117 * to see whether the parsing succeeded.
1119 * This function is not appropriate for file formats and the like; it
1120 * isn't very precise, and its exact behavior varies with the locale.
1121 * It's intended to be a heuristic routine that guesses what the user
1122 * means by a given string (and it does work pretty well in that
1123 * capacity).
1125 void
1126 g_date_set_parse (GDate *d,
1127 const gchar *str)
1129 GDateParseTokens pt;
1130 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
1132 g_return_if_fail (d != NULL);
1134 /* set invalid */
1135 g_date_clear (d, 1);
1137 G_LOCK (g_date_global);
1139 g_date_prepare_to_parse (str, &pt);
1141 DEBUG_MSG (("Found %d ints, '%d' '%d' '%d' and written out month %d",
1142 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
1145 if (pt.num_ints == 4)
1147 G_UNLOCK (g_date_global);
1148 return; /* presumably a typo; bail out. */
1151 if (pt.num_ints > 1)
1153 int i = 0;
1154 int j = 0;
1156 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
1158 while (i < pt.num_ints && j < 3)
1160 switch (dmy_order[j])
1162 case G_DATE_MONTH:
1164 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
1166 m = pt.month;
1167 ++j; /* skip months, but don't skip this number */
1168 continue;
1170 else
1171 m = pt.n[i];
1173 break;
1174 case G_DATE_DAY:
1176 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
1178 day = 1;
1179 ++j; /* skip days, since we may have month/year */
1180 continue;
1182 day = pt.n[i];
1184 break;
1185 case G_DATE_YEAR:
1187 y = pt.n[i];
1189 if (locale_era_adjust != 0)
1191 y += locale_era_adjust;
1193 else if (using_twodigit_years && y < 100)
1195 guint two = twodigit_start_year % 100;
1196 guint century = (twodigit_start_year / 100) * 100;
1198 if (y < two)
1199 century += 100;
1201 y += century;
1204 break;
1205 default:
1206 break;
1209 ++i;
1210 ++j;
1214 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
1216 /* Try YYYY MM DD */
1217 y = pt.n[0];
1218 m = pt.n[1];
1219 day = pt.n[2];
1221 if (using_twodigit_years && y < 100)
1222 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
1224 else if (pt.num_ints == 2)
1226 if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
1227 m = pt.month;
1230 else if (pt.num_ints == 1)
1232 if (pt.month != G_DATE_BAD_MONTH)
1234 /* Month name and year? */
1235 m = pt.month;
1236 day = 1;
1237 y = pt.n[0];
1239 else
1241 /* Try yyyymmdd and yymmdd */
1243 m = (pt.n[0]/100) % 100;
1244 day = pt.n[0] % 100;
1245 y = pt.n[0]/10000;
1247 /* FIXME move this into a separate function */
1248 if (using_twodigit_years && y < 100)
1250 guint two = twodigit_start_year % 100;
1251 guint century = (twodigit_start_year / 100) * 100;
1253 if (y < two)
1254 century += 100;
1256 y += century;
1261 /* See if we got anything valid out of all this. */
1262 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
1263 if (y < 8000 && g_date_valid_dmy (day, m, y))
1265 d->month = m;
1266 d->day = day;
1267 d->year = y;
1268 d->dmy = TRUE;
1270 #ifdef G_ENABLE_DEBUG
1271 else
1273 DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
1275 #endif
1276 G_UNLOCK (g_date_global);
1280 * g_date_set_time_t:
1281 * @date: a #GDate
1282 * @timet: time_t value to set
1284 * Sets the value of a date to the date corresponding to a time
1285 * specified as a time_t. The time to date conversion is done using
1286 * the user's current timezone.
1288 * To set the value of a date to the current day, you could write:
1289 * |[<!-- language="C" -->
1290 * g_date_set_time_t (date, time (NULL));
1291 * ]|
1293 * Since: 2.10
1295 void
1296 g_date_set_time_t (GDate *date,
1297 time_t timet)
1299 struct tm tm;
1301 g_return_if_fail (date != NULL);
1303 #ifdef HAVE_LOCALTIME_R
1304 localtime_r (&timet, &tm);
1305 #else
1307 struct tm *ptm = localtime (&timet);
1309 if (ptm == NULL)
1311 /* Happens at least in Microsoft's C library if you pass a
1312 * negative time_t. Use 2000-01-01 as default date.
1314 #ifndef G_DISABLE_CHECKS
1315 g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
1316 #endif
1318 tm.tm_mon = 0;
1319 tm.tm_mday = 1;
1320 tm.tm_year = 100;
1322 else
1323 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
1325 #endif
1327 date->julian = FALSE;
1329 date->month = tm.tm_mon + 1;
1330 date->day = tm.tm_mday;
1331 date->year = tm.tm_year + 1900;
1333 g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
1335 date->dmy = TRUE;
1340 * g_date_set_time:
1341 * @date: a #GDate.
1342 * @time_: #GTime value to set.
1344 * Sets the value of a date from a #GTime value.
1345 * The time to date conversion is done using the user's current timezone.
1347 * Deprecated: 2.10: Use g_date_set_time_t() instead.
1349 void
1350 g_date_set_time (GDate *date,
1351 GTime time_)
1353 g_date_set_time_t (date, (time_t) time_);
1357 * g_date_set_time_val:
1358 * @date: a #GDate
1359 * @timeval: #GTimeVal value to set
1361 * Sets the value of a date from a #GTimeVal value. Note that the
1362 * @tv_usec member is ignored, because #GDate can't make use of the
1363 * additional precision.
1365 * The time to date conversion is done using the user's current timezone.
1367 * Since: 2.10
1369 void
1370 g_date_set_time_val (GDate *date,
1371 GTimeVal *timeval)
1373 g_date_set_time_t (date, (time_t) timeval->tv_sec);
1377 * g_date_set_month:
1378 * @date: a #GDate
1379 * @month: month to set
1381 * Sets the month of the year for a #GDate. If the resulting
1382 * day-month-year triplet is invalid, the date will be invalid.
1384 void
1385 g_date_set_month (GDate *d,
1386 GDateMonth m)
1388 g_return_if_fail (d != NULL);
1389 g_return_if_fail (g_date_valid_month (m));
1391 if (d->julian && !d->dmy) g_date_update_dmy(d);
1392 d->julian = FALSE;
1394 d->month = m;
1396 if (g_date_valid_dmy (d->day, d->month, d->year))
1397 d->dmy = TRUE;
1398 else
1399 d->dmy = FALSE;
1403 * g_date_set_day:
1404 * @date: a #GDate
1405 * @day: day to set
1407 * Sets the day of the month for a #GDate. If the resulting
1408 * day-month-year triplet is invalid, the date will be invalid.
1410 void
1411 g_date_set_day (GDate *d,
1412 GDateDay day)
1414 g_return_if_fail (d != NULL);
1415 g_return_if_fail (g_date_valid_day (day));
1417 if (d->julian && !d->dmy) g_date_update_dmy(d);
1418 d->julian = FALSE;
1420 d->day = day;
1422 if (g_date_valid_dmy (d->day, d->month, d->year))
1423 d->dmy = TRUE;
1424 else
1425 d->dmy = FALSE;
1429 * g_date_set_year:
1430 * @date: a #GDate
1431 * @year: year to set
1433 * Sets the year for a #GDate. If the resulting day-month-year
1434 * triplet is invalid, the date will be invalid.
1436 void
1437 g_date_set_year (GDate *d,
1438 GDateYear y)
1440 g_return_if_fail (d != NULL);
1441 g_return_if_fail (g_date_valid_year (y));
1443 if (d->julian && !d->dmy) g_date_update_dmy(d);
1444 d->julian = FALSE;
1446 d->year = y;
1448 if (g_date_valid_dmy (d->day, d->month, d->year))
1449 d->dmy = TRUE;
1450 else
1451 d->dmy = FALSE;
1455 * g_date_set_dmy:
1456 * @date: a #GDate
1457 * @day: day
1458 * @month: month
1459 * @y: year
1461 * Sets the value of a #GDate from a day, month, and year.
1462 * The day-month-year triplet must be valid; if you aren't
1463 * sure it is, call g_date_valid_dmy() to check before you
1464 * set it.
1466 void
1467 g_date_set_dmy (GDate *d,
1468 GDateDay day,
1469 GDateMonth m,
1470 GDateYear y)
1472 g_return_if_fail (d != NULL);
1473 g_return_if_fail (g_date_valid_dmy (day, m, y));
1475 d->julian = FALSE;
1477 d->month = m;
1478 d->day = day;
1479 d->year = y;
1481 d->dmy = TRUE;
1485 * g_date_set_julian:
1486 * @date: a #GDate
1487 * @julian_date: Julian day number (days since January 1, Year 1)
1489 * Sets the value of a #GDate from a Julian day number.
1491 void
1492 g_date_set_julian (GDate *d,
1493 guint32 j)
1495 g_return_if_fail (d != NULL);
1496 g_return_if_fail (g_date_valid_julian (j));
1498 d->julian_days = j;
1499 d->julian = TRUE;
1500 d->dmy = FALSE;
1504 * g_date_is_first_of_month:
1505 * @date: a #GDate to check
1507 * Returns %TRUE if the date is on the first of a month.
1508 * The date must be valid.
1510 * Returns: %TRUE if the date is the first of the month
1512 gboolean
1513 g_date_is_first_of_month (const GDate *d)
1515 g_return_val_if_fail (g_date_valid (d), FALSE);
1517 if (!d->dmy)
1518 g_date_update_dmy (d);
1520 g_return_val_if_fail (d->dmy, FALSE);
1522 if (d->day == 1) return TRUE;
1523 else return FALSE;
1527 * g_date_is_last_of_month:
1528 * @date: a #GDate to check
1530 * Returns %TRUE if the date is the last day of the month.
1531 * The date must be valid.
1533 * Returns: %TRUE if the date is the last day of the month
1535 gboolean
1536 g_date_is_last_of_month (const GDate *d)
1538 gint idx;
1540 g_return_val_if_fail (g_date_valid (d), FALSE);
1542 if (!d->dmy)
1543 g_date_update_dmy (d);
1545 g_return_val_if_fail (d->dmy, FALSE);
1547 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1549 if (d->day == days_in_months[idx][d->month]) return TRUE;
1550 else return FALSE;
1554 * g_date_add_days:
1555 * @date: a #GDate to increment
1556 * @n_days: number of days to move the date forward
1558 * Increments a date some number of days.
1559 * To move forward by weeks, add weeks*7 days.
1560 * The date must be valid.
1562 void
1563 g_date_add_days (GDate *d,
1564 guint ndays)
1566 g_return_if_fail (g_date_valid (d));
1568 if (!d->julian)
1569 g_date_update_julian (d);
1571 g_return_if_fail (d->julian);
1573 d->julian_days += ndays;
1574 d->dmy = FALSE;
1578 * g_date_subtract_days:
1579 * @date: a #GDate to decrement
1580 * @n_days: number of days to move
1582 * Moves a date some number of days into the past.
1583 * To move by weeks, just move by weeks*7 days.
1584 * The date must be valid.
1586 void
1587 g_date_subtract_days (GDate *d,
1588 guint ndays)
1590 g_return_if_fail (g_date_valid (d));
1592 if (!d->julian)
1593 g_date_update_julian (d);
1595 g_return_if_fail (d->julian);
1596 g_return_if_fail (d->julian_days > ndays);
1598 d->julian_days -= ndays;
1599 d->dmy = FALSE;
1603 * g_date_add_months:
1604 * @date: a #GDate to increment
1605 * @n_months: number of months to move forward
1607 * Increments a date by some number of months.
1608 * If the day of the month is greater than 28,
1609 * this routine may change the day of the month
1610 * (because the destination month may not have
1611 * the current day in it). The date must be valid.
1613 void
1614 g_date_add_months (GDate *d,
1615 guint nmonths)
1617 guint years, months;
1618 gint idx;
1620 g_return_if_fail (g_date_valid (d));
1622 if (!d->dmy)
1623 g_date_update_dmy (d);
1625 g_return_if_fail (d->dmy);
1627 nmonths += d->month - 1;
1629 years = nmonths/12;
1630 months = nmonths%12;
1632 d->month = months + 1;
1633 d->year += years;
1635 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1637 if (d->day > days_in_months[idx][d->month])
1638 d->day = days_in_months[idx][d->month];
1640 d->julian = FALSE;
1642 g_return_if_fail (g_date_valid (d));
1646 * g_date_subtract_months:
1647 * @date: a #GDate to decrement
1648 * @n_months: number of months to move
1650 * Moves a date some number of months into the past.
1651 * If the current day of the month doesn't exist in
1652 * the destination month, the day of the month
1653 * may change. The date must be valid.
1655 void
1656 g_date_subtract_months (GDate *d,
1657 guint nmonths)
1659 guint years, months;
1660 gint idx;
1662 g_return_if_fail (g_date_valid (d));
1664 if (!d->dmy)
1665 g_date_update_dmy (d);
1667 g_return_if_fail (d->dmy);
1669 years = nmonths/12;
1670 months = nmonths%12;
1672 g_return_if_fail (d->year > years);
1674 d->year -= years;
1676 if (d->month > months) d->month -= months;
1677 else
1679 months -= d->month;
1680 d->month = 12 - months;
1681 d->year -= 1;
1684 idx = g_date_is_leap_year (d->year) ? 1 : 0;
1686 if (d->day > days_in_months[idx][d->month])
1687 d->day = days_in_months[idx][d->month];
1689 d->julian = FALSE;
1691 g_return_if_fail (g_date_valid (d));
1695 * g_date_add_years:
1696 * @date: a #GDate to increment
1697 * @n_years: number of years to move forward
1699 * Increments a date by some number of years.
1700 * If the date is February 29, and the destination
1701 * year is not a leap year, the date will be changed
1702 * to February 28. The date must be valid.
1704 void
1705 g_date_add_years (GDate *d,
1706 guint nyears)
1708 g_return_if_fail (g_date_valid (d));
1710 if (!d->dmy)
1711 g_date_update_dmy (d);
1713 g_return_if_fail (d->dmy);
1715 d->year += nyears;
1717 if (d->month == 2 && d->day == 29)
1719 if (!g_date_is_leap_year (d->year))
1720 d->day = 28;
1723 d->julian = FALSE;
1727 * g_date_subtract_years:
1728 * @date: a #GDate to decrement
1729 * @n_years: number of years to move
1731 * Moves a date some number of years into the past.
1732 * If the current day doesn't exist in the destination
1733 * year (i.e. it's February 29 and you move to a non-leap-year)
1734 * then the day is changed to February 29. The date
1735 * must be valid.
1737 void
1738 g_date_subtract_years (GDate *d,
1739 guint nyears)
1741 g_return_if_fail (g_date_valid (d));
1743 if (!d->dmy)
1744 g_date_update_dmy (d);
1746 g_return_if_fail (d->dmy);
1747 g_return_if_fail (d->year > nyears);
1749 d->year -= nyears;
1751 if (d->month == 2 && d->day == 29)
1753 if (!g_date_is_leap_year (d->year))
1754 d->day = 28;
1757 d->julian = FALSE;
1761 * g_date_is_leap_year:
1762 * @year: year to check
1764 * Returns %TRUE if the year is a leap year.
1766 * For the purposes of this function, leap year is every year
1767 * divisible by 4 unless that year is divisible by 100. If it
1768 * is divisible by 100 it would be a leap year only if that year
1769 * is also divisible by 400.
1771 * Returns: %TRUE if the year is a leap year
1773 gboolean
1774 g_date_is_leap_year (GDateYear year)
1776 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1778 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1779 (year % 400) == 0 );
1783 * g_date_get_days_in_month:
1784 * @month: month
1785 * @year: year
1787 * Returns the number of days in a month, taking leap
1788 * years into account.
1790 * Returns: number of days in @month during the @year
1792 guint8
1793 g_date_get_days_in_month (GDateMonth month,
1794 GDateYear year)
1796 gint idx;
1798 g_return_val_if_fail (g_date_valid_year (year), 0);
1799 g_return_val_if_fail (g_date_valid_month (month), 0);
1801 idx = g_date_is_leap_year (year) ? 1 : 0;
1803 return days_in_months[idx][month];
1807 * g_date_get_monday_weeks_in_year:
1808 * @year: a year
1810 * Returns the number of weeks in the year, where weeks
1811 * are taken to start on Monday. Will be 52 or 53. The
1812 * date must be valid. (Years always have 52 7-day periods,
1813 * plus 1 or 2 extra days depending on whether it's a leap
1814 * year. This function is basically telling you how many
1815 * Mondays are in the year, i.e. there are 53 Mondays if
1816 * one of the extra days happens to be a Monday.)
1818 * Returns: number of Mondays in the year
1820 guint8
1821 g_date_get_monday_weeks_in_year (GDateYear year)
1823 GDate d;
1825 g_return_val_if_fail (g_date_valid_year (year), 0);
1827 g_date_clear (&d, 1);
1828 g_date_set_dmy (&d, 1, 1, year);
1829 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1830 g_date_set_dmy (&d, 31, 12, year);
1831 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1832 if (g_date_is_leap_year (year))
1834 g_date_set_dmy (&d, 2, 1, year);
1835 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1836 g_date_set_dmy (&d, 30, 12, year);
1837 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1839 return 52;
1843 * g_date_get_sunday_weeks_in_year:
1844 * @year: year to count weeks in
1846 * Returns the number of weeks in the year, where weeks
1847 * are taken to start on Sunday. Will be 52 or 53. The
1848 * date must be valid. (Years always have 52 7-day periods,
1849 * plus 1 or 2 extra days depending on whether it's a leap
1850 * year. This function is basically telling you how many
1851 * Sundays are in the year, i.e. there are 53 Sundays if
1852 * one of the extra days happens to be a Sunday.)
1854 * Returns: the number of weeks in @year
1856 guint8
1857 g_date_get_sunday_weeks_in_year (GDateYear year)
1859 GDate d;
1861 g_return_val_if_fail (g_date_valid_year (year), 0);
1863 g_date_clear (&d, 1);
1864 g_date_set_dmy (&d, 1, 1, year);
1865 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1866 g_date_set_dmy (&d, 31, 12, year);
1867 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1868 if (g_date_is_leap_year (year))
1870 g_date_set_dmy (&d, 2, 1, year);
1871 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1872 g_date_set_dmy (&d, 30, 12, year);
1873 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1875 return 52;
1879 * g_date_compare:
1880 * @lhs: first date to compare
1881 * @rhs: second date to compare
1883 * qsort()-style comparison function for dates.
1884 * Both dates must be valid.
1886 * Returns: 0 for equal, less than zero if @lhs is less than @rhs,
1887 * greater than zero if @lhs is greater than @rhs
1889 gint
1890 g_date_compare (const GDate *lhs,
1891 const GDate *rhs)
1893 g_return_val_if_fail (lhs != NULL, 0);
1894 g_return_val_if_fail (rhs != NULL, 0);
1895 g_return_val_if_fail (g_date_valid (lhs), 0);
1896 g_return_val_if_fail (g_date_valid (rhs), 0);
1898 /* Remember the self-comparison case! I think it works right now. */
1900 while (TRUE)
1902 if (lhs->julian && rhs->julian)
1904 if (lhs->julian_days < rhs->julian_days) return -1;
1905 else if (lhs->julian_days > rhs->julian_days) return 1;
1906 else return 0;
1908 else if (lhs->dmy && rhs->dmy)
1910 if (lhs->year < rhs->year) return -1;
1911 else if (lhs->year > rhs->year) return 1;
1912 else
1914 if (lhs->month < rhs->month) return -1;
1915 else if (lhs->month > rhs->month) return 1;
1916 else
1918 if (lhs->day < rhs->day) return -1;
1919 else if (lhs->day > rhs->day) return 1;
1920 else return 0;
1926 else
1928 if (!lhs->julian) g_date_update_julian (lhs);
1929 if (!rhs->julian) g_date_update_julian (rhs);
1930 g_return_val_if_fail (lhs->julian, 0);
1931 g_return_val_if_fail (rhs->julian, 0);
1935 return 0; /* warnings */
1939 * g_date_to_struct_tm:
1940 * @date: a #GDate to set the struct tm from
1941 * @tm: (not nullable): struct tm to fill
1943 * Fills in the date-related bits of a struct tm using the @date value.
1944 * Initializes the non-date parts with something sane but meaningless.
1946 void
1947 g_date_to_struct_tm (const GDate *d,
1948 struct tm *tm)
1950 GDateWeekday day;
1952 g_return_if_fail (g_date_valid (d));
1953 g_return_if_fail (tm != NULL);
1955 if (!d->dmy)
1956 g_date_update_dmy (d);
1958 g_return_if_fail (d->dmy);
1960 /* zero all the irrelevant fields to be sure they're valid */
1962 /* On Linux and maybe other systems, there are weird non-POSIX
1963 * fields on the end of struct tm that choke strftime if they
1964 * contain garbage. So we need to 0 the entire struct, not just the
1965 * fields we know to exist.
1968 memset (tm, 0x0, sizeof (struct tm));
1970 tm->tm_mday = d->day;
1971 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1972 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1974 day = g_date_get_weekday (d);
1975 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1977 tm->tm_wday = (int)day;
1979 tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
1980 tm->tm_isdst = -1; /* -1 means "information not available" */
1984 * g_date_clamp:
1985 * @date: a #GDate to clamp
1986 * @min_date: minimum accepted value for @date
1987 * @max_date: maximum accepted value for @date
1989 * If @date is prior to @min_date, sets @date equal to @min_date.
1990 * If @date falls after @max_date, sets @date equal to @max_date.
1991 * Otherwise, @date is unchanged.
1992 * Either of @min_date and @max_date may be %NULL.
1993 * All non-%NULL dates must be valid.
1995 void
1996 g_date_clamp (GDate *date,
1997 const GDate *min_date,
1998 const GDate *max_date)
2000 g_return_if_fail (g_date_valid (date));
2002 if (min_date != NULL)
2003 g_return_if_fail (g_date_valid (min_date));
2005 if (max_date != NULL)
2006 g_return_if_fail (g_date_valid (max_date));
2008 if (min_date != NULL && max_date != NULL)
2009 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
2011 if (min_date && g_date_compare (date, min_date) < 0)
2012 *date = *min_date;
2014 if (max_date && g_date_compare (max_date, date) < 0)
2015 *date = *max_date;
2019 * g_date_order:
2020 * @date1: the first date
2021 * @date2: the second date
2023 * Checks if @date1 is less than or equal to @date2,
2024 * and swap the values if this is not the case.
2026 void
2027 g_date_order (GDate *date1,
2028 GDate *date2)
2030 g_return_if_fail (g_date_valid (date1));
2031 g_return_if_fail (g_date_valid (date2));
2033 if (g_date_compare (date1, date2) > 0)
2035 GDate tmp = *date1;
2036 *date1 = *date2;
2037 *date2 = tmp;
2041 #ifdef G_OS_WIN32
2042 static gsize
2043 win32_strftime_helper (const GDate *d,
2044 const gchar *format,
2045 const struct tm *tm,
2046 gchar *s,
2047 gsize slen)
2049 SYSTEMTIME systemtime;
2050 TIME_ZONE_INFORMATION tzinfo;
2051 LCID lcid;
2052 int n, k;
2053 GArray *result;
2054 const gchar *p;
2055 gunichar c;
2056 const wchar_t digits[] = L"0123456789";
2057 gchar *convbuf;
2058 glong convlen = 0;
2059 gsize retval;
2061 systemtime.wYear = tm->tm_year + 1900;
2062 systemtime.wMonth = tm->tm_mon + 1;
2063 systemtime.wDayOfWeek = tm->tm_wday;
2064 systemtime.wDay = tm->tm_mday;
2065 systemtime.wHour = tm->tm_hour;
2066 systemtime.wMinute = tm->tm_min;
2067 systemtime.wSecond = tm->tm_sec;
2068 systemtime.wMilliseconds = 0;
2070 lcid = GetThreadLocale ();
2071 result = g_array_sized_new (FALSE, FALSE, sizeof (wchar_t), MAX (128, strlen (format) * 2));
2073 p = format;
2074 while (*p)
2076 c = g_utf8_get_char (p);
2077 if (c == '%')
2079 p = g_utf8_next_char (p);
2080 if (!*p)
2082 s[0] = '\0';
2083 g_array_free (result, TRUE);
2085 return 0;
2088 c = g_utf8_get_char (p);
2089 if (c == 'E' || c == 'O')
2091 /* Ignore modified conversion specifiers for now. */
2092 p = g_utf8_next_char (p);
2093 if (!*p)
2095 s[0] = '\0';
2096 g_array_free (result, TRUE);
2098 return 0;
2101 c = g_utf8_get_char (p);
2104 switch (c)
2106 case 'a':
2107 if (systemtime.wDayOfWeek == 0)
2108 k = 6;
2109 else
2110 k = systemtime.wDayOfWeek - 1;
2111 n = GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, NULL, 0);
2112 g_array_set_size (result, result->len + n);
2113 GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
2114 g_array_set_size (result, result->len - 1);
2115 break;
2116 case 'A':
2117 if (systemtime.wDayOfWeek == 0)
2118 k = 6;
2119 else
2120 k = systemtime.wDayOfWeek - 1;
2121 n = GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, NULL, 0);
2122 g_array_set_size (result, result->len + n);
2123 GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
2124 g_array_set_size (result, result->len - 1);
2125 break;
2126 case 'b':
2127 case 'h':
2128 n = GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, NULL, 0);
2129 g_array_set_size (result, result->len + n);
2130 GetLocaleInfoW (lcid, LOCALE_SABBREVMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
2131 g_array_set_size (result, result->len - 1);
2132 break;
2133 case 'B':
2134 n = GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, NULL, 0);
2135 g_array_set_size (result, result->len + n);
2136 GetLocaleInfoW (lcid, LOCALE_SMONTHNAME1+systemtime.wMonth-1, ((wchar_t *) result->data) + result->len - n, n);
2137 g_array_set_size (result, result->len - 1);
2138 break;
2139 case 'c':
2140 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2141 if (n > 0)
2143 g_array_set_size (result, result->len + n);
2144 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2145 g_array_set_size (result, result->len - 1);
2147 g_array_append_vals (result, L" ", 1);
2148 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2149 if (n > 0)
2151 g_array_set_size (result, result->len + n);
2152 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2153 g_array_set_size (result, result->len - 1);
2155 break;
2156 case 'C':
2157 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
2158 g_array_append_vals (result, digits + (systemtime.wYear/1000)%10, 1);
2159 break;
2160 case 'd':
2161 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2162 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2163 break;
2164 case 'D':
2165 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
2166 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
2167 g_array_append_vals (result, L"/", 1);
2168 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2169 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2170 g_array_append_vals (result, L"/", 1);
2171 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2172 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2173 break;
2174 case 'e':
2175 if (systemtime.wDay >= 10)
2176 g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2177 else
2178 g_array_append_vals (result, L" ", 1);
2179 g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2180 break;
2182 /* A GDate has no time fields, so for now we can
2183 * hardcode all time conversions into zeros (or 12 for
2184 * %I). The alternative code snippets in the #else
2185 * branches are here ready to be taken into use when
2186 * needed by a g_strftime() or g_date_and_time_format()
2187 * or whatever.
2189 case 'H':
2190 #if 1
2191 g_array_append_vals (result, L"00", 2);
2192 #else
2193 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2194 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2195 #endif
2196 break;
2197 case 'I':
2198 #if 1
2199 g_array_append_vals (result, L"12", 2);
2200 #else
2201 if (systemtime.wHour == 0)
2202 g_array_append_vals (result, L"12", 2);
2203 else
2205 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
2206 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
2208 #endif
2209 break;
2210 case 'j':
2211 g_array_append_vals (result, digits + (tm->tm_yday+1)/100, 1);
2212 g_array_append_vals (result, digits + ((tm->tm_yday+1)/10)%10, 1);
2213 g_array_append_vals (result, digits + (tm->tm_yday+1)%10, 1);
2214 break;
2215 case 'm':
2216 g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
2217 g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
2218 break;
2219 case 'M':
2220 #if 1
2221 g_array_append_vals (result, L"00", 2);
2222 #else
2223 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2224 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2225 #endif
2226 break;
2227 case 'n':
2228 g_array_append_vals (result, L"\n", 1);
2229 break;
2230 case 'p':
2231 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
2232 if (n > 0)
2234 g_array_set_size (result, result->len + n);
2235 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
2236 g_array_set_size (result, result->len - 1);
2238 break;
2239 case 'r':
2240 /* This is a rather odd format. Hard to say what to do.
2241 * Let's always use the POSIX %I:%M:%S %p
2243 #if 1
2244 g_array_append_vals (result, L"12:00:00", 8);
2245 #else
2246 if (systemtime.wHour == 0)
2247 g_array_append_vals (result, L"12", 2);
2248 else
2250 g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
2251 g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
2253 g_array_append_vals (result, L":", 1);
2254 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2255 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2256 g_array_append_vals (result, L":", 1);
2257 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2258 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2259 g_array_append_vals (result, L" ", 1);
2260 #endif
2261 n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
2262 if (n > 0)
2264 g_array_set_size (result, result->len + n);
2265 GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
2266 g_array_set_size (result, result->len - 1);
2268 break;
2269 case 'R':
2270 #if 1
2271 g_array_append_vals (result, L"00:00", 5);
2272 #else
2273 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2274 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2275 g_array_append_vals (result, L":", 1);
2276 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2277 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2278 #endif
2279 break;
2280 case 'S':
2281 #if 1
2282 g_array_append_vals (result, L"00", 2);
2283 #else
2284 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2285 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2286 #endif
2287 break;
2288 case 't':
2289 g_array_append_vals (result, L"\t", 1);
2290 break;
2291 case 'T':
2292 #if 1
2293 g_array_append_vals (result, L"00:00:00", 8);
2294 #else
2295 g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2296 g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2297 g_array_append_vals (result, L":", 1);
2298 g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2299 g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2300 g_array_append_vals (result, L":", 1);
2301 g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2302 g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2303 #endif
2304 break;
2305 case 'u':
2306 if (systemtime.wDayOfWeek == 0)
2307 g_array_append_vals (result, L"7", 1);
2308 else
2309 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
2310 break;
2311 case 'U':
2312 n = g_date_get_sunday_week_of_year (d);
2313 g_array_append_vals (result, digits + n/10, 1);
2314 g_array_append_vals (result, digits + n%10, 1);
2315 break;
2316 case 'V':
2317 n = g_date_get_iso8601_week_of_year (d);
2318 g_array_append_vals (result, digits + n/10, 1);
2319 g_array_append_vals (result, digits + n%10, 1);
2320 break;
2321 case 'w':
2322 g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
2323 break;
2324 case 'W':
2325 n = g_date_get_monday_week_of_year (d);
2326 g_array_append_vals (result, digits + n/10, 1);
2327 g_array_append_vals (result, digits + n%10, 1);
2328 break;
2329 case 'x':
2330 n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2331 if (n > 0)
2333 g_array_set_size (result, result->len + n);
2334 GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2335 g_array_set_size (result, result->len - 1);
2337 break;
2338 case 'X':
2339 n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2340 if (n > 0)
2342 g_array_set_size (result, result->len + n);
2343 GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2344 g_array_set_size (result, result->len - 1);
2346 break;
2347 case 'y':
2348 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2349 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2350 break;
2351 case 'Y':
2352 g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
2353 g_array_append_vals (result, digits + (systemtime.wYear/100)%10, 1);
2354 g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2355 g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2356 break;
2357 case 'Z':
2358 n = GetTimeZoneInformation (&tzinfo);
2359 if (n == TIME_ZONE_ID_UNKNOWN)
2361 else if (n == TIME_ZONE_ID_STANDARD)
2362 g_array_append_vals (result, tzinfo.StandardName, wcslen (tzinfo.StandardName));
2363 else if (n == TIME_ZONE_ID_DAYLIGHT)
2364 g_array_append_vals (result, tzinfo.DaylightName, wcslen (tzinfo.DaylightName));
2365 break;
2366 case '%':
2367 g_array_append_vals (result, L"%", 1);
2368 break;
2371 else if (c <= 0xFFFF)
2373 wchar_t wc = c;
2374 g_array_append_vals (result, &wc, 1);
2376 else
2378 glong nwc;
2379 wchar_t *ws;
2381 ws = g_ucs4_to_utf16 (&c, 1, NULL, &nwc, NULL);
2382 g_array_append_vals (result, ws, nwc);
2383 g_free (ws);
2385 p = g_utf8_next_char (p);
2388 convbuf = g_utf16_to_utf8 ((wchar_t *) result->data, result->len, NULL, &convlen, NULL);
2389 g_array_free (result, TRUE);
2391 if (!convbuf)
2393 s[0] = '\0';
2394 return 0;
2397 if (slen <= convlen)
2399 /* Ensure only whole characters are copied into the buffer. */
2400 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
2401 g_assert (end != NULL);
2402 convlen = end - convbuf;
2404 /* Return 0 because the buffer isn't large enough. */
2405 retval = 0;
2407 else
2408 retval = convlen;
2410 memcpy (s, convbuf, convlen);
2411 s[convlen] = '\0';
2412 g_free (convbuf);
2414 return retval;
2417 #endif
2420 * g_date_strftime:
2421 * @s: destination buffer
2422 * @slen: buffer size
2423 * @format: format string
2424 * @date: valid #GDate
2426 * Generates a printed representation of the date, in a
2427 * [locale][setlocale]-specific way.
2428 * Works just like the platform's C library strftime() function,
2429 * but only accepts date-related formats; time-related formats
2430 * give undefined results. Date must be valid. Unlike strftime()
2431 * (which uses the locale encoding), works on a UTF-8 format
2432 * string and stores a UTF-8 result.
2434 * This function does not provide any conversion specifiers in
2435 * addition to those implemented by the platform's C library.
2436 * For example, don't expect that using g_date_strftime() would
2437 * make the \%F provided by the C99 strftime() work on Windows
2438 * where the C library only complies to C89.
2440 * Returns: number of characters written to the buffer, or 0 the buffer was too small
2442 #pragma GCC diagnostic push
2443 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
2445 gsize
2446 g_date_strftime (gchar *s,
2447 gsize slen,
2448 const gchar *format,
2449 const GDate *d)
2451 struct tm tm;
2452 #ifndef G_OS_WIN32
2453 gsize locale_format_len = 0;
2454 gchar *locale_format;
2455 gsize tmplen;
2456 gchar *tmpbuf;
2457 gsize tmpbufsize;
2458 gsize convlen = 0;
2459 gchar *convbuf;
2460 GError *error = NULL;
2461 gsize retval;
2462 #endif
2464 g_return_val_if_fail (g_date_valid (d), 0);
2465 g_return_val_if_fail (slen > 0, 0);
2466 g_return_val_if_fail (format != NULL, 0);
2467 g_return_val_if_fail (s != NULL, 0);
2469 g_date_to_struct_tm (d, &tm);
2471 #ifdef G_OS_WIN32
2472 if (!g_utf8_validate (format, -1, NULL))
2474 s[0] = '\0';
2475 return 0;
2477 return win32_strftime_helper (d, format, &tm, s, slen);
2478 #else
2480 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
2482 if (error)
2484 g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
2485 g_error_free (error);
2487 s[0] = '\0';
2488 return 0;
2491 tmpbufsize = MAX (128, locale_format_len * 2);
2492 while (TRUE)
2494 tmpbuf = g_malloc (tmpbufsize);
2496 /* Set the first byte to something other than '\0', to be able to
2497 * recognize whether strftime actually failed or just returned "".
2499 tmpbuf[0] = '\1';
2500 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
2502 if (tmplen == 0 && tmpbuf[0] != '\0')
2504 g_free (tmpbuf);
2505 tmpbufsize *= 2;
2507 if (tmpbufsize > 65536)
2509 g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
2510 g_free (locale_format);
2512 s[0] = '\0';
2513 return 0;
2516 else
2517 break;
2519 g_free (locale_format);
2521 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
2522 g_free (tmpbuf);
2524 if (error)
2526 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
2527 g_error_free (error);
2529 s[0] = '\0';
2530 return 0;
2533 if (slen <= convlen)
2535 /* Ensure only whole characters are copied into the buffer.
2537 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
2538 g_assert (end != NULL);
2539 convlen = end - convbuf;
2541 /* Return 0 because the buffer isn't large enough.
2543 retval = 0;
2545 else
2546 retval = convlen;
2548 memcpy (s, convbuf, convlen);
2549 s[convlen] = '\0';
2550 g_free (convbuf);
2552 return retval;
2553 #endif
2556 #pragma GCC diagnostic pop