1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
30 #include "glibconfig.h"
32 #define DEBUG_MSG(x) /* */
34 /* #define DEBUG_MSG(args) g_message args ; */
50 #include "gstrfuncs.h"
51 #include "gtestutils.h"
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.
108 * Number of microseconds in one second (1 million).
109 * This macro is provided for code readability.
115 * @tv_usec: microseconds
117 * Represents a precise time, with seconds and microseconds.
118 * Similar to the struct timeval returned by the gettimeofday()
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.
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,
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()
163 * Instead, do the following:
164 * |[<!-- language="C" -->
169 * gtime = (GTime)ttime;
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.
186 * Integer representing a day of the month; between 1 and 31.
187 * #G_DATE_BAD_DAY represents an invalid day of the month.
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
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.
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.
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.
236 * Represents an invalid #GDateDay.
242 * Represents an invalid Julian day number.
248 * Represents an invalid year.
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
264 GDate
*d
= g_new0 (GDate
, 1); /* happily, 0 is the invalid flag for everything. */
271 * @day: day of the month
272 * @month: month of the 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
282 g_date_new_dmy (GDateDay day
,
287 g_return_val_if_fail (g_date_valid_dmy (day
, m
, y
), NULL
);
289 d
= g_new (GDate
, 1);
298 g_assert (g_date_valid (d
));
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
314 g_date_new_julian (guint32 julian_day
)
317 g_return_val_if_fail (g_date_valid_julian (julian_day
), NULL
);
319 d
= g_new (GDate
, 1);
324 d
->julian_days
= julian_day
;
326 g_assert (g_date_valid (d
));
333 * @date: a #GDate to free
335 * Frees a #GDate returned from g_date_new().
338 g_date_free (GDate
*date
)
340 g_return_if_fail (date
!= NULL
);
347 * @date: a #GDate to copy
349 * Copies a GDate to a newly-allocated GDate. If the input was invalid
350 * (as determined by g_date_valid()), the invalid state will be copied
351 * as is into the new object.
353 * Returns: (transfer full): a newly-allocated #GDate initialized from @date
358 g_date_copy (const GDate
*date
)
361 g_return_val_if_fail (date
!= NULL
, NULL
);
363 if (g_date_valid (date
))
364 res
= g_date_new_julian (g_date_get_julian (date
));
376 * @date: a #GDate to check
378 * Returns %TRUE if the #GDate represents an existing day. The date must not
379 * contain garbage; it should have been initialized with g_date_clear()
380 * if it wasn't allocated by one of the g_date_new() variants.
382 * Returns: Whether the date is valid
385 g_date_valid (const GDate
*d
)
387 g_return_val_if_fail (d
!= NULL
, FALSE
);
389 return (d
->julian
|| d
->dmy
);
392 static const guint8 days_in_months
[2][13] =
393 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
394 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
395 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
398 static const guint16 days_in_year
[2][14] =
399 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
400 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
401 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
405 * g_date_valid_month:
408 * Returns %TRUE if the month value is valid. The 12 #GDateMonth
409 * enumeration values are the only valid months.
411 * Returns: %TRUE if the month is valid
414 g_date_valid_month (GDateMonth m
)
416 return ( (m
> G_DATE_BAD_MONTH
) && (m
< 13) );
423 * Returns %TRUE if the year is valid. Any year greater than 0 is valid,
424 * though there is a 16-bit limit to what #GDate will understand.
426 * Returns: %TRUE if the year is valid
429 g_date_valid_year (GDateYear y
)
431 return ( y
> G_DATE_BAD_YEAR
);
438 * Returns %TRUE if the day of the month is valid (a day is valid if it's
439 * between 1 and 31 inclusive).
441 * Returns: %TRUE if the day is valid
445 g_date_valid_day (GDateDay d
)
447 return ( (d
> G_DATE_BAD_DAY
) && (d
< 32) );
451 * g_date_valid_weekday:
454 * Returns %TRUE if the weekday is valid. The seven #GDateWeekday enumeration
455 * values are the only valid weekdays.
457 * Returns: %TRUE if the weekday is valid
460 g_date_valid_weekday (GDateWeekday w
)
462 return ( (w
> G_DATE_BAD_WEEKDAY
) && (w
< 8) );
466 * g_date_valid_julian:
467 * @julian_date: Julian day to check
469 * Returns %TRUE if the Julian day is valid. Anything greater than zero
470 * is basically a valid Julian, though there is a 32-bit limit.
472 * Returns: %TRUE if the Julian day is valid
475 g_date_valid_julian (guint32 j
)
477 return (j
> G_DATE_BAD_JULIAN
);
486 * Returns %TRUE if the day-month-year triplet forms a valid, existing day
487 * in the range of days #GDate understands (Year 1 or later, no more than
488 * a few thousand years in the future).
490 * Returns: %TRUE if the date is a valid one
493 g_date_valid_dmy (GDateDay d
,
497 return ( (m
> G_DATE_BAD_MONTH
) &&
499 (d
> G_DATE_BAD_DAY
) &&
500 (y
> G_DATE_BAD_YEAR
) && /* must check before using g_date_is_leap_year */
501 (d
<= (g_date_is_leap_year (y
) ?
502 days_in_months
[1][m
] : days_in_months
[0][m
])) );
506 /* "Julian days" just means an absolute number of days, where Day 1 ==
510 g_date_update_julian (const GDate
*const_d
)
512 GDate
*d
= (GDate
*) const_d
;
516 g_return_if_fail (d
!= NULL
);
517 g_return_if_fail (d
->dmy
);
518 g_return_if_fail (!d
->julian
);
519 g_return_if_fail (g_date_valid_dmy (d
->day
, d
->month
, d
->year
));
521 /* What we actually do is: multiply years * 365 days in the year,
522 * add the number of years divided by 4, subtract the number of
523 * years divided by 100 and add the number of years divided by 400,
524 * which accounts for leap year stuff. Code from Steffen Beyer's
528 year
= d
->year
- 1; /* we know d->year > 0 since it's valid */
530 d
->julian_days
= year
* 365U;
531 d
->julian_days
+= (year
>>= 2); /* divide by 4 and add */
532 d
->julian_days
-= (year
/= 25); /* divides original # years by 100 */
533 d
->julian_days
+= year
>> 2; /* divides by 4, which divides original by 400 */
535 idx
= g_date_is_leap_year (d
->year
) ? 1 : 0;
537 d
->julian_days
+= days_in_year
[idx
][d
->month
] + d
->day
;
539 g_return_if_fail (g_date_valid_julian (d
->julian_days
));
545 g_date_update_dmy (const GDate
*const_d
)
547 GDate
*d
= (GDate
*) const_d
;
552 guint32 A
, B
, C
, D
, E
, M
;
554 g_return_if_fail (d
!= NULL
);
555 g_return_if_fail (d
->julian
);
556 g_return_if_fail (!d
->dmy
);
557 g_return_if_fail (g_date_valid_julian (d
->julian_days
));
559 /* Formula taken from the Calendar FAQ; the formula was for the
560 * Julian Period which starts on 1 January 4713 BC, so we add
561 * 1,721,425 to the number of days before doing the formula.
563 * I'm sure this can be simplified for our 1 January 1 AD period
564 * start, but I can't figure out how to unpack the formula.
567 A
= d
->julian_days
+ 1721425 + 32045;
568 B
= ( 4 *(A
+ 36524) )/ 146097 - 1;
569 C
= A
- (146097 * B
)/4;
570 D
= ( 4 * (C
+ 365) ) / 1461 - 1;
571 E
= C
- ((1461*D
) / 4);
572 M
= (5 * (E
- 1) + 2)/153;
574 m
= M
+ 3 - (12*(M
/10));
575 day
= E
- (153*M
+ 2)/5;
576 y
= 100 * B
+ D
- 4800 + (M
/10);
578 #ifdef G_ENABLE_DEBUG
579 if (!g_date_valid_dmy (day
, m
, y
))
580 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
581 d
->julian_days
, day
, m
, y
);
592 * g_date_get_weekday:
595 * Returns the day of the week for a #GDate. The date must be valid.
597 * Returns: day of the week as a #GDateWeekday.
600 g_date_get_weekday (const GDate
*d
)
602 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_WEEKDAY
);
605 g_date_update_julian (d
);
607 g_return_val_if_fail (d
->julian
, G_DATE_BAD_WEEKDAY
);
609 return ((d
->julian_days
- 1) % 7) + 1;
614 * @date: a #GDate to get the month from
616 * Returns the month of the year. The date must be valid.
618 * Returns: month of the year as a #GDateMonth
621 g_date_get_month (const GDate
*d
)
623 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_MONTH
);
626 g_date_update_dmy (d
);
628 g_return_val_if_fail (d
->dmy
, G_DATE_BAD_MONTH
);
637 * Returns the year of a #GDate. The date must be valid.
639 * Returns: year in which the date falls
642 g_date_get_year (const GDate
*d
)
644 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_YEAR
);
647 g_date_update_dmy (d
);
649 g_return_val_if_fail (d
->dmy
, G_DATE_BAD_YEAR
);
656 * @date: a #GDate to extract the day of the month from
658 * Returns the day of the month. The date must be valid.
660 * Returns: day of the month
663 g_date_get_day (const GDate
*d
)
665 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_DAY
);
668 g_date_update_dmy (d
);
670 g_return_val_if_fail (d
->dmy
, G_DATE_BAD_DAY
);
677 * @date: a #GDate to extract the Julian day from
679 * Returns the Julian day or "serial number" of the #GDate. The
680 * Julian day is simply the number of days since January 1, Year 1; i.e.,
681 * January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2,
682 * etc. The date must be valid.
684 * Returns: Julian day
687 g_date_get_julian (const GDate
*d
)
689 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_JULIAN
);
692 g_date_update_julian (d
);
694 g_return_val_if_fail (d
->julian
, G_DATE_BAD_JULIAN
);
696 return d
->julian_days
;
700 * g_date_get_day_of_year:
701 * @date: a #GDate to extract day of year from
703 * Returns the day of the year, where Jan 1 is the first day of the
704 * year. The date must be valid.
706 * Returns: day of the year
709 g_date_get_day_of_year (const GDate
*d
)
713 g_return_val_if_fail (g_date_valid (d
), 0);
716 g_date_update_dmy (d
);
718 g_return_val_if_fail (d
->dmy
, 0);
720 idx
= g_date_is_leap_year (d
->year
) ? 1 : 0;
722 return (days_in_year
[idx
][d
->month
] + d
->day
);
726 * g_date_get_monday_week_of_year:
729 * Returns the week of the year, where weeks are understood to start on
730 * Monday. If the date is before the first Monday of the year, return 0.
731 * The date must be valid.
733 * Returns: week of the year
736 g_date_get_monday_week_of_year (const GDate
*d
)
742 g_return_val_if_fail (g_date_valid (d
), 0);
745 g_date_update_dmy (d
);
747 g_return_val_if_fail (d
->dmy
, 0);
749 g_date_clear (&first
, 1);
751 g_date_set_dmy (&first
, 1, 1, d
->year
);
753 wd
= g_date_get_weekday (&first
) - 1; /* make Monday day 0 */
754 day
= g_date_get_day_of_year (d
) - 1;
756 return ((day
+ wd
)/7U + (wd
== 0 ? 1 : 0));
760 * g_date_get_sunday_week_of_year:
763 * Returns the week of the year during which this date falls, if
764 * weeks are understood to begin on Sunday. The date must be valid.
765 * Can return 0 if the day is before the first Sunday of the year.
767 * Returns: week number
770 g_date_get_sunday_week_of_year (const GDate
*d
)
776 g_return_val_if_fail (g_date_valid (d
), 0);
779 g_date_update_dmy (d
);
781 g_return_val_if_fail (d
->dmy
, 0);
783 g_date_clear (&first
, 1);
785 g_date_set_dmy (&first
, 1, 1, d
->year
);
787 wd
= g_date_get_weekday (&first
);
788 if (wd
== 7) wd
= 0; /* make Sunday day 0 */
789 day
= g_date_get_day_of_year (d
) - 1;
791 return ((day
+ wd
)/7U + (wd
== 0 ? 1 : 0));
795 * g_date_get_iso8601_week_of_year:
796 * @date: a valid #GDate
798 * Returns the week of the year, where weeks are interpreted according
801 * Returns: ISO 8601 week number of the year.
806 g_date_get_iso8601_week_of_year (const GDate
*d
)
808 guint j
, d4
, L
, d1
, w
;
810 g_return_val_if_fail (g_date_valid (d
), 0);
813 g_date_update_julian (d
);
815 g_return_val_if_fail (d
->julian
, 0);
817 /* Formula taken from the Calendar FAQ; the formula was for the
818 * Julian Period which starts on 1 January 4713 BC, so we add
819 * 1,721,425 to the number of days before doing the formula.
821 j
= d
->julian_days
+ 1721425;
822 d4
= (j
+ 31741 - (j
% 7)) % 146097 % 36524 % 1461;
824 d1
= ((d4
- L
) % 365) + L
;
831 * g_date_days_between:
832 * @date1: the first date
833 * @date2: the second date
835 * Computes the number of days between two dates.
836 * If @date2 is prior to @date1, the returned value is negative.
837 * Both dates must be valid.
839 * Returns: the number of days between @date1 and @date2
842 g_date_days_between (const GDate
*d1
,
845 g_return_val_if_fail (g_date_valid (d1
), 0);
846 g_return_val_if_fail (g_date_valid (d2
), 0);
848 return (gint
)g_date_get_julian (d2
) - (gint
)g_date_get_julian (d1
);
853 * @date: pointer to one or more dates to clear
854 * @n_dates: number of dates to clear
856 * Initializes one or more #GDate structs to a sane but invalid
857 * state. The cleared dates will not represent an existing date, but will
858 * not contain garbage. Useful to init a date declared on the stack.
859 * Validity can be tested with g_date_valid().
862 g_date_clear (GDate
*d
, guint ndates
)
864 g_return_if_fail (d
!= NULL
);
865 g_return_if_fail (ndates
!= 0);
867 memset (d
, 0x0, ndates
*sizeof (GDate
));
870 G_LOCK_DEFINE_STATIC (g_date_global
);
872 /* These are for the parser, output to the user should use *
873 * g_date_strftime () - this creates more never-freed memory to annoy
874 * all those memory debugger users. :-)
877 static gchar
*long_month_names
[13] =
882 static gchar
*short_month_names
[13] =
887 /* This tells us if we need to update the parse info */
888 static gchar
*current_locale
= NULL
;
890 /* order of these in the current locale */
891 static GDateDMY dmy_order
[3] =
893 G_DATE_DAY
, G_DATE_MONTH
, G_DATE_YEAR
896 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
897 * 29 and below are counted as in the year 2000, numbers 30 and above
898 * are counted as in the year 1900.
901 static const GDateYear twodigit_start_year
= 1930;
903 /* It is impossible to enter a year between 1 AD and 99 AD with this
906 static gboolean using_twodigit_years
= FALSE
;
908 /* Adjustment of locale era to AD, non-zero means using locale era
910 static gint locale_era_adjust
= 0;
912 struct _GDateParseTokens
{
918 typedef struct _GDateParseTokens GDateParseTokens
;
922 /* HOLDS: g_date_global_lock */
924 g_date_fill_parse_tokens (const gchar
*str
, GDateParseTokens
*pt
)
926 gchar num
[4][NUM_LEN
+1];
930 /* We count 4, but store 3; so we can give an error
933 num
[0][0] = num
[1][0] = num
[2][0] = num
[3][0] = '\0';
935 s
= (const guchar
*) str
;
937 while (*s
&& pt
->num_ints
< 4)
941 while (*s
&& g_ascii_isdigit (*s
) && i
< NUM_LEN
)
943 num
[pt
->num_ints
][i
] = *s
;
950 num
[pt
->num_ints
][i
] = '\0';
954 if (*s
== '\0') break;
959 pt
->n
[0] = pt
->num_ints
> 0 ? atoi (num
[0]) : 0;
960 pt
->n
[1] = pt
->num_ints
> 1 ? atoi (num
[1]) : 0;
961 pt
->n
[2] = pt
->num_ints
> 2 ? atoi (num
[2]) : 0;
963 pt
->month
= G_DATE_BAD_MONTH
;
965 if (pt
->num_ints
< 3)
970 casefold
= g_utf8_casefold (str
, -1);
971 normalized
= g_utf8_normalize (casefold
, -1, G_NORMALIZE_ALL
);
977 if (long_month_names
[i
] != NULL
)
979 const gchar
*found
= strstr (normalized
, long_month_names
[i
]);
988 if (short_month_names
[i
] != NULL
)
990 const gchar
*found
= strstr (normalized
, short_month_names
[i
]);
1002 g_free (normalized
);
1006 /* HOLDS: g_date_global_lock */
1008 g_date_prepare_to_parse (const gchar
*str
,
1009 GDateParseTokens
*pt
)
1011 const gchar
*locale
= setlocale (LC_TIME
, NULL
);
1012 gboolean recompute_localeinfo
= FALSE
;
1015 g_return_if_fail (locale
!= NULL
); /* should not happen */
1017 g_date_clear (&d
, 1); /* clear for scratch use */
1019 if ( (current_locale
== NULL
) || (strcmp (locale
, current_locale
) != 0) )
1020 recompute_localeinfo
= TRUE
; /* Uh, there used to be a reason for the temporary */
1022 if (recompute_localeinfo
)
1025 GDateParseTokens testpt
;
1028 g_free (current_locale
); /* still works if current_locale == NULL */
1030 current_locale
= g_strdup (locale
);
1032 short_month_names
[0] = "Error";
1033 long_month_names
[0] = "Error";
1039 g_date_set_dmy (&d
, 1, i
, 1);
1041 g_return_if_fail (g_date_valid (&d
));
1043 g_date_strftime (buf
, 127, "%b", &d
);
1045 casefold
= g_utf8_casefold (buf
, -1);
1046 g_free (short_month_names
[i
]);
1047 short_month_names
[i
] = g_utf8_normalize (casefold
, -1, G_NORMALIZE_ALL
);
1050 g_date_strftime (buf
, 127, "%B", &d
);
1051 casefold
= g_utf8_casefold (buf
, -1);
1052 g_free (long_month_names
[i
]);
1053 long_month_names
[i
] = g_utf8_normalize (casefold
, -1, G_NORMALIZE_ALL
);
1059 /* Determine DMY order */
1061 /* had to pick a random day - don't change this, some strftimes
1062 * are broken on some days, and this one is good so far. */
1063 g_date_set_dmy (&d
, 4, 7, 1976);
1065 g_date_strftime (buf
, 127, "%x", &d
);
1067 g_date_fill_parse_tokens (buf
, &testpt
);
1070 while (i
< testpt
.num_ints
)
1072 switch (testpt
.n
[i
])
1075 dmy_order
[i
] = G_DATE_MONTH
;
1078 dmy_order
[i
] = G_DATE_DAY
;
1081 using_twodigit_years
= TRUE
; /* FALL THRU */
1083 dmy_order
[i
] = G_DATE_YEAR
;
1086 /* assume locale era */
1087 locale_era_adjust
= 1976 - testpt
.n
[i
];
1088 dmy_order
[i
] = G_DATE_YEAR
;
1094 #if defined(G_ENABLE_DEBUG) && 0
1095 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
1099 DEBUG_MSG ((" %s %s", long_month_names
[i
], short_month_names
[i
]));
1102 if (using_twodigit_years
)
1104 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year
));
1111 switch (dmy_order
[i
])
1114 strings
[i
] = "Month";
1117 strings
[i
] = "Year";
1128 DEBUG_MSG (("**Order: %s, %s, %s", strings
[0], strings
[1], strings
[2]));
1129 DEBUG_MSG (("**Sample date in this locale: '%s'", buf
));
1134 g_date_fill_parse_tokens (str
, pt
);
1139 * @date: a #GDate to fill in
1140 * @str: string to parse
1142 * Parses a user-inputted string @str, and try to figure out what date it
1143 * represents, taking the [current locale][setlocale] into account. If the
1144 * string is successfully parsed, the date will be valid after the call.
1145 * Otherwise, it will be invalid. You should check using g_date_valid()
1146 * to see whether the parsing succeeded.
1148 * This function is not appropriate for file formats and the like; it
1149 * isn't very precise, and its exact behavior varies with the locale.
1150 * It's intended to be a heuristic routine that guesses what the user
1151 * means by a given string (and it does work pretty well in that
1155 g_date_set_parse (GDate
*d
,
1158 GDateParseTokens pt
;
1159 guint m
= G_DATE_BAD_MONTH
, day
= G_DATE_BAD_DAY
, y
= G_DATE_BAD_YEAR
;
1161 g_return_if_fail (d
!= NULL
);
1164 g_date_clear (d
, 1);
1166 G_LOCK (g_date_global
);
1168 g_date_prepare_to_parse (str
, &pt
);
1170 DEBUG_MSG (("Found %d ints, '%d' '%d' '%d' and written out month %d",
1171 pt
.num_ints
, pt
.n
[0], pt
.n
[1], pt
.n
[2], pt
.month
));
1174 if (pt
.num_ints
== 4)
1176 G_UNLOCK (g_date_global
);
1177 return; /* presumably a typo; bail out. */
1180 if (pt
.num_ints
> 1)
1185 g_assert (pt
.num_ints
< 4); /* i.e., it is 2 or 3 */
1187 while (i
< pt
.num_ints
&& j
< 3)
1189 switch (dmy_order
[j
])
1193 if (pt
.num_ints
== 2 && pt
.month
!= G_DATE_BAD_MONTH
)
1196 ++j
; /* skip months, but don't skip this number */
1205 if (pt
.num_ints
== 2 && pt
.month
== G_DATE_BAD_MONTH
)
1208 ++j
; /* skip days, since we may have month/year */
1218 if (locale_era_adjust
!= 0)
1220 y
+= locale_era_adjust
;
1222 else if (using_twodigit_years
&& y
< 100)
1224 guint two
= twodigit_start_year
% 100;
1225 guint century
= (twodigit_start_year
/ 100) * 100;
1243 if (pt
.num_ints
== 3 && !g_date_valid_dmy (day
, m
, y
))
1245 /* Try YYYY MM DD */
1250 if (using_twodigit_years
&& y
< 100)
1251 y
= G_DATE_BAD_YEAR
; /* avoids ambiguity */
1253 else if (pt
.num_ints
== 2)
1255 if (m
== G_DATE_BAD_MONTH
&& pt
.month
!= G_DATE_BAD_MONTH
)
1259 else if (pt
.num_ints
== 1)
1261 if (pt
.month
!= G_DATE_BAD_MONTH
)
1263 /* Month name and year? */
1270 /* Try yyyymmdd and yymmdd */
1272 m
= (pt
.n
[0]/100) % 100;
1273 day
= pt
.n
[0] % 100;
1276 /* FIXME move this into a separate function */
1277 if (using_twodigit_years
&& y
< 100)
1279 guint two
= twodigit_start_year
% 100;
1280 guint century
= (twodigit_start_year
/ 100) * 100;
1290 /* See if we got anything valid out of all this. */
1291 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
1292 if (y
< 8000 && g_date_valid_dmy (day
, m
, y
))
1299 #ifdef G_ENABLE_DEBUG
1302 DEBUG_MSG (("Rejected DMY %u %u %u", day
, m
, y
));
1305 G_UNLOCK (g_date_global
);
1309 * g_date_set_time_t:
1311 * @timet: time_t value to set
1313 * Sets the value of a date to the date corresponding to a time
1314 * specified as a time_t. The time to date conversion is done using
1315 * the user's current timezone.
1317 * To set the value of a date to the current day, you could write:
1318 * |[<!-- language="C" -->
1319 * g_date_set_time_t (date, time (NULL));
1325 g_date_set_time_t (GDate
*date
,
1330 g_return_if_fail (date
!= NULL
);
1332 #ifdef HAVE_LOCALTIME_R
1333 localtime_r (&timet
, &tm
);
1336 struct tm
*ptm
= localtime (&timet
);
1340 /* Happens at least in Microsoft's C library if you pass a
1341 * negative time_t. Use 2000-01-01 as default date.
1343 #ifndef G_DISABLE_CHECKS
1344 g_return_if_fail_warning (G_LOG_DOMAIN
, "g_date_set_time", "ptm != NULL");
1352 memcpy ((void *) &tm
, (void *) ptm
, sizeof(struct tm
));
1356 date
->julian
= FALSE
;
1358 date
->month
= tm
.tm_mon
+ 1;
1359 date
->day
= tm
.tm_mday
;
1360 date
->year
= tm
.tm_year
+ 1900;
1362 g_return_if_fail (g_date_valid_dmy (date
->day
, date
->month
, date
->year
));
1371 * @time_: #GTime value to set.
1373 * Sets the value of a date from a #GTime value.
1374 * The time to date conversion is done using the user's current timezone.
1376 * Deprecated: 2.10: Use g_date_set_time_t() instead.
1379 g_date_set_time (GDate
*date
,
1382 g_date_set_time_t (date
, (time_t) time_
);
1386 * g_date_set_time_val:
1388 * @timeval: #GTimeVal value to set
1390 * Sets the value of a date from a #GTimeVal value. Note that the
1391 * @tv_usec member is ignored, because #GDate can't make use of the
1392 * additional precision.
1394 * The time to date conversion is done using the user's current timezone.
1399 g_date_set_time_val (GDate
*date
,
1402 g_date_set_time_t (date
, (time_t) timeval
->tv_sec
);
1408 * @month: month to set
1410 * Sets the month of the year for a #GDate. If the resulting
1411 * day-month-year triplet is invalid, the date will be invalid.
1414 g_date_set_month (GDate
*d
,
1417 g_return_if_fail (d
!= NULL
);
1418 g_return_if_fail (g_date_valid_month (m
));
1420 if (d
->julian
&& !d
->dmy
) g_date_update_dmy(d
);
1425 if (g_date_valid_dmy (d
->day
, d
->month
, d
->year
))
1436 * Sets the day of the month for a #GDate. If the resulting
1437 * day-month-year triplet is invalid, the date will be invalid.
1440 g_date_set_day (GDate
*d
,
1443 g_return_if_fail (d
!= NULL
);
1444 g_return_if_fail (g_date_valid_day (day
));
1446 if (d
->julian
&& !d
->dmy
) g_date_update_dmy(d
);
1451 if (g_date_valid_dmy (d
->day
, d
->month
, d
->year
))
1460 * @year: year to set
1462 * Sets the year for a #GDate. If the resulting day-month-year
1463 * triplet is invalid, the date will be invalid.
1466 g_date_set_year (GDate
*d
,
1469 g_return_if_fail (d
!= NULL
);
1470 g_return_if_fail (g_date_valid_year (y
));
1472 if (d
->julian
&& !d
->dmy
) g_date_update_dmy(d
);
1477 if (g_date_valid_dmy (d
->day
, d
->month
, d
->year
))
1490 * Sets the value of a #GDate from a day, month, and year.
1491 * The day-month-year triplet must be valid; if you aren't
1492 * sure it is, call g_date_valid_dmy() to check before you
1496 g_date_set_dmy (GDate
*d
,
1501 g_return_if_fail (d
!= NULL
);
1502 g_return_if_fail (g_date_valid_dmy (day
, m
, y
));
1514 * g_date_set_julian:
1516 * @julian_date: Julian day number (days since January 1, Year 1)
1518 * Sets the value of a #GDate from a Julian day number.
1521 g_date_set_julian (GDate
*d
,
1524 g_return_if_fail (d
!= NULL
);
1525 g_return_if_fail (g_date_valid_julian (j
));
1533 * g_date_is_first_of_month:
1534 * @date: a #GDate to check
1536 * Returns %TRUE if the date is on the first of a month.
1537 * The date must be valid.
1539 * Returns: %TRUE if the date is the first of the month
1542 g_date_is_first_of_month (const GDate
*d
)
1544 g_return_val_if_fail (g_date_valid (d
), FALSE
);
1547 g_date_update_dmy (d
);
1549 g_return_val_if_fail (d
->dmy
, FALSE
);
1551 if (d
->day
== 1) return TRUE
;
1556 * g_date_is_last_of_month:
1557 * @date: a #GDate to check
1559 * Returns %TRUE if the date is the last day of the month.
1560 * The date must be valid.
1562 * Returns: %TRUE if the date is the last day of the month
1565 g_date_is_last_of_month (const GDate
*d
)
1569 g_return_val_if_fail (g_date_valid (d
), FALSE
);
1572 g_date_update_dmy (d
);
1574 g_return_val_if_fail (d
->dmy
, FALSE
);
1576 idx
= g_date_is_leap_year (d
->year
) ? 1 : 0;
1578 if (d
->day
== days_in_months
[idx
][d
->month
]) return TRUE
;
1584 * @date: a #GDate to increment
1585 * @n_days: number of days to move the date forward
1587 * Increments a date some number of days.
1588 * To move forward by weeks, add weeks*7 days.
1589 * The date must be valid.
1592 g_date_add_days (GDate
*d
,
1595 g_return_if_fail (g_date_valid (d
));
1598 g_date_update_julian (d
);
1600 g_return_if_fail (d
->julian
);
1602 d
->julian_days
+= ndays
;
1607 * g_date_subtract_days:
1608 * @date: a #GDate to decrement
1609 * @n_days: number of days to move
1611 * Moves a date some number of days into the past.
1612 * To move by weeks, just move by weeks*7 days.
1613 * The date must be valid.
1616 g_date_subtract_days (GDate
*d
,
1619 g_return_if_fail (g_date_valid (d
));
1622 g_date_update_julian (d
);
1624 g_return_if_fail (d
->julian
);
1625 g_return_if_fail (d
->julian_days
> ndays
);
1627 d
->julian_days
-= ndays
;
1632 * g_date_add_months:
1633 * @date: a #GDate to increment
1634 * @n_months: number of months to move forward
1636 * Increments a date by some number of months.
1637 * If the day of the month is greater than 28,
1638 * this routine may change the day of the month
1639 * (because the destination month may not have
1640 * the current day in it). The date must be valid.
1643 g_date_add_months (GDate
*d
,
1646 guint years
, months
;
1649 g_return_if_fail (g_date_valid (d
));
1652 g_date_update_dmy (d
);
1654 g_return_if_fail (d
->dmy
);
1656 nmonths
+= d
->month
- 1;
1659 months
= nmonths
%12;
1661 d
->month
= months
+ 1;
1664 idx
= g_date_is_leap_year (d
->year
) ? 1 : 0;
1666 if (d
->day
> days_in_months
[idx
][d
->month
])
1667 d
->day
= days_in_months
[idx
][d
->month
];
1671 g_return_if_fail (g_date_valid (d
));
1675 * g_date_subtract_months:
1676 * @date: a #GDate to decrement
1677 * @n_months: number of months to move
1679 * Moves a date some number of months into the past.
1680 * If the current day of the month doesn't exist in
1681 * the destination month, the day of the month
1682 * may change. The date must be valid.
1685 g_date_subtract_months (GDate
*d
,
1688 guint years
, months
;
1691 g_return_if_fail (g_date_valid (d
));
1694 g_date_update_dmy (d
);
1696 g_return_if_fail (d
->dmy
);
1699 months
= nmonths
%12;
1701 g_return_if_fail (d
->year
> years
);
1705 if (d
->month
> months
) d
->month
-= months
;
1709 d
->month
= 12 - months
;
1713 idx
= g_date_is_leap_year (d
->year
) ? 1 : 0;
1715 if (d
->day
> days_in_months
[idx
][d
->month
])
1716 d
->day
= days_in_months
[idx
][d
->month
];
1720 g_return_if_fail (g_date_valid (d
));
1725 * @date: a #GDate to increment
1726 * @n_years: number of years to move forward
1728 * Increments a date by some number of years.
1729 * If the date is February 29, and the destination
1730 * year is not a leap year, the date will be changed
1731 * to February 28. The date must be valid.
1734 g_date_add_years (GDate
*d
,
1737 g_return_if_fail (g_date_valid (d
));
1740 g_date_update_dmy (d
);
1742 g_return_if_fail (d
->dmy
);
1746 if (d
->month
== 2 && d
->day
== 29)
1748 if (!g_date_is_leap_year (d
->year
))
1756 * g_date_subtract_years:
1757 * @date: a #GDate to decrement
1758 * @n_years: number of years to move
1760 * Moves a date some number of years into the past.
1761 * If the current day doesn't exist in the destination
1762 * year (i.e. it's February 29 and you move to a non-leap-year)
1763 * then the day is changed to February 29. The date
1767 g_date_subtract_years (GDate
*d
,
1770 g_return_if_fail (g_date_valid (d
));
1773 g_date_update_dmy (d
);
1775 g_return_if_fail (d
->dmy
);
1776 g_return_if_fail (d
->year
> nyears
);
1780 if (d
->month
== 2 && d
->day
== 29)
1782 if (!g_date_is_leap_year (d
->year
))
1790 * g_date_is_leap_year:
1791 * @year: year to check
1793 * Returns %TRUE if the year is a leap year.
1795 * For the purposes of this function, leap year is every year
1796 * divisible by 4 unless that year is divisible by 100. If it
1797 * is divisible by 100 it would be a leap year only if that year
1798 * is also divisible by 400.
1800 * Returns: %TRUE if the year is a leap year
1803 g_date_is_leap_year (GDateYear year
)
1805 g_return_val_if_fail (g_date_valid_year (year
), FALSE
);
1807 return ( (((year
% 4) == 0) && ((year
% 100) != 0)) ||
1808 (year
% 400) == 0 );
1812 * g_date_get_days_in_month:
1816 * Returns the number of days in a month, taking leap
1817 * years into account.
1819 * Returns: number of days in @month during the @year
1822 g_date_get_days_in_month (GDateMonth month
,
1827 g_return_val_if_fail (g_date_valid_year (year
), 0);
1828 g_return_val_if_fail (g_date_valid_month (month
), 0);
1830 idx
= g_date_is_leap_year (year
) ? 1 : 0;
1832 return days_in_months
[idx
][month
];
1836 * g_date_get_monday_weeks_in_year:
1839 * Returns the number of weeks in the year, where weeks
1840 * are taken to start on Monday. Will be 52 or 53. The
1841 * date must be valid. (Years always have 52 7-day periods,
1842 * plus 1 or 2 extra days depending on whether it's a leap
1843 * year. This function is basically telling you how many
1844 * Mondays are in the year, i.e. there are 53 Mondays if
1845 * one of the extra days happens to be a Monday.)
1847 * Returns: number of Mondays in the year
1850 g_date_get_monday_weeks_in_year (GDateYear year
)
1854 g_return_val_if_fail (g_date_valid_year (year
), 0);
1856 g_date_clear (&d
, 1);
1857 g_date_set_dmy (&d
, 1, 1, year
);
1858 if (g_date_get_weekday (&d
) == G_DATE_MONDAY
) return 53;
1859 g_date_set_dmy (&d
, 31, 12, year
);
1860 if (g_date_get_weekday (&d
) == G_DATE_MONDAY
) return 53;
1861 if (g_date_is_leap_year (year
))
1863 g_date_set_dmy (&d
, 2, 1, year
);
1864 if (g_date_get_weekday (&d
) == G_DATE_MONDAY
) return 53;
1865 g_date_set_dmy (&d
, 30, 12, year
);
1866 if (g_date_get_weekday (&d
) == G_DATE_MONDAY
) return 53;
1872 * g_date_get_sunday_weeks_in_year:
1873 * @year: year to count weeks in
1875 * Returns the number of weeks in the year, where weeks
1876 * are taken to start on Sunday. Will be 52 or 53. The
1877 * date must be valid. (Years always have 52 7-day periods,
1878 * plus 1 or 2 extra days depending on whether it's a leap
1879 * year. This function is basically telling you how many
1880 * Sundays are in the year, i.e. there are 53 Sundays if
1881 * one of the extra days happens to be a Sunday.)
1883 * Returns: the number of weeks in @year
1886 g_date_get_sunday_weeks_in_year (GDateYear year
)
1890 g_return_val_if_fail (g_date_valid_year (year
), 0);
1892 g_date_clear (&d
, 1);
1893 g_date_set_dmy (&d
, 1, 1, year
);
1894 if (g_date_get_weekday (&d
) == G_DATE_SUNDAY
) return 53;
1895 g_date_set_dmy (&d
, 31, 12, year
);
1896 if (g_date_get_weekday (&d
) == G_DATE_SUNDAY
) return 53;
1897 if (g_date_is_leap_year (year
))
1899 g_date_set_dmy (&d
, 2, 1, year
);
1900 if (g_date_get_weekday (&d
) == G_DATE_SUNDAY
) return 53;
1901 g_date_set_dmy (&d
, 30, 12, year
);
1902 if (g_date_get_weekday (&d
) == G_DATE_SUNDAY
) return 53;
1909 * @lhs: first date to compare
1910 * @rhs: second date to compare
1912 * qsort()-style comparison function for dates.
1913 * Both dates must be valid.
1915 * Returns: 0 for equal, less than zero if @lhs is less than @rhs,
1916 * greater than zero if @lhs is greater than @rhs
1919 g_date_compare (const GDate
*lhs
,
1922 g_return_val_if_fail (lhs
!= NULL
, 0);
1923 g_return_val_if_fail (rhs
!= NULL
, 0);
1924 g_return_val_if_fail (g_date_valid (lhs
), 0);
1925 g_return_val_if_fail (g_date_valid (rhs
), 0);
1927 /* Remember the self-comparison case! I think it works right now. */
1931 if (lhs
->julian
&& rhs
->julian
)
1933 if (lhs
->julian_days
< rhs
->julian_days
) return -1;
1934 else if (lhs
->julian_days
> rhs
->julian_days
) return 1;
1937 else if (lhs
->dmy
&& rhs
->dmy
)
1939 if (lhs
->year
< rhs
->year
) return -1;
1940 else if (lhs
->year
> rhs
->year
) return 1;
1943 if (lhs
->month
< rhs
->month
) return -1;
1944 else if (lhs
->month
> rhs
->month
) return 1;
1947 if (lhs
->day
< rhs
->day
) return -1;
1948 else if (lhs
->day
> rhs
->day
) return 1;
1957 if (!lhs
->julian
) g_date_update_julian (lhs
);
1958 if (!rhs
->julian
) g_date_update_julian (rhs
);
1959 g_return_val_if_fail (lhs
->julian
, 0);
1960 g_return_val_if_fail (rhs
->julian
, 0);
1964 return 0; /* warnings */
1968 * g_date_to_struct_tm:
1969 * @date: a #GDate to set the struct tm from
1970 * @tm: (not nullable): struct tm to fill
1972 * Fills in the date-related bits of a struct tm using the @date value.
1973 * Initializes the non-date parts with something sane but meaningless.
1976 g_date_to_struct_tm (const GDate
*d
,
1981 g_return_if_fail (g_date_valid (d
));
1982 g_return_if_fail (tm
!= NULL
);
1985 g_date_update_dmy (d
);
1987 g_return_if_fail (d
->dmy
);
1989 /* zero all the irrelevant fields to be sure they're valid */
1991 /* On Linux and maybe other systems, there are weird non-POSIX
1992 * fields on the end of struct tm that choke strftime if they
1993 * contain garbage. So we need to 0 the entire struct, not just the
1994 * fields we know to exist.
1997 memset (tm
, 0x0, sizeof (struct tm
));
1999 tm
->tm_mday
= d
->day
;
2000 tm
->tm_mon
= d
->month
- 1; /* 0-11 goes in tm */
2001 tm
->tm_year
= ((int)d
->year
) - 1900; /* X/Open says tm_year can be negative */
2003 day
= g_date_get_weekday (d
);
2004 if (day
== 7) day
= 0; /* struct tm wants days since Sunday, so Sunday is 0 */
2006 tm
->tm_wday
= (int)day
;
2008 tm
->tm_yday
= g_date_get_day_of_year (d
) - 1; /* 0 to 365 */
2009 tm
->tm_isdst
= -1; /* -1 means "information not available" */
2014 * @date: a #GDate to clamp
2015 * @min_date: minimum accepted value for @date
2016 * @max_date: maximum accepted value for @date
2018 * If @date is prior to @min_date, sets @date equal to @min_date.
2019 * If @date falls after @max_date, sets @date equal to @max_date.
2020 * Otherwise, @date is unchanged.
2021 * Either of @min_date and @max_date may be %NULL.
2022 * All non-%NULL dates must be valid.
2025 g_date_clamp (GDate
*date
,
2026 const GDate
*min_date
,
2027 const GDate
*max_date
)
2029 g_return_if_fail (g_date_valid (date
));
2031 if (min_date
!= NULL
)
2032 g_return_if_fail (g_date_valid (min_date
));
2034 if (max_date
!= NULL
)
2035 g_return_if_fail (g_date_valid (max_date
));
2037 if (min_date
!= NULL
&& max_date
!= NULL
)
2038 g_return_if_fail (g_date_compare (min_date
, max_date
) <= 0);
2040 if (min_date
&& g_date_compare (date
, min_date
) < 0)
2043 if (max_date
&& g_date_compare (max_date
, date
) < 0)
2049 * @date1: the first date
2050 * @date2: the second date
2052 * Checks if @date1 is less than or equal to @date2,
2053 * and swap the values if this is not the case.
2056 g_date_order (GDate
*date1
,
2059 g_return_if_fail (g_date_valid (date1
));
2060 g_return_if_fail (g_date_valid (date2
));
2062 if (g_date_compare (date1
, date2
) > 0)
2072 win32_strftime_helper (const GDate
*d
,
2073 const gchar
*format
,
2074 const struct tm
*tm
,
2078 SYSTEMTIME systemtime
;
2079 TIME_ZONE_INFORMATION tzinfo
;
2085 const wchar_t digits
[] = L
"0123456789";
2090 systemtime
.wYear
= tm
->tm_year
+ 1900;
2091 systemtime
.wMonth
= tm
->tm_mon
+ 1;
2092 systemtime
.wDayOfWeek
= tm
->tm_wday
;
2093 systemtime
.wDay
= tm
->tm_mday
;
2094 systemtime
.wHour
= tm
->tm_hour
;
2095 systemtime
.wMinute
= tm
->tm_min
;
2096 systemtime
.wSecond
= tm
->tm_sec
;
2097 systemtime
.wMilliseconds
= 0;
2099 lcid
= GetThreadLocale ();
2100 result
= g_array_sized_new (FALSE
, FALSE
, sizeof (wchar_t), MAX (128, strlen (format
) * 2));
2105 c
= g_utf8_get_char (p
);
2108 p
= g_utf8_next_char (p
);
2112 g_array_free (result
, TRUE
);
2117 c
= g_utf8_get_char (p
);
2118 if (c
== 'E' || c
== 'O')
2120 /* Ignore modified conversion specifiers for now. */
2121 p
= g_utf8_next_char (p
);
2125 g_array_free (result
, TRUE
);
2130 c
= g_utf8_get_char (p
);
2136 if (systemtime
.wDayOfWeek
== 0)
2139 k
= systemtime
.wDayOfWeek
- 1;
2140 n
= GetLocaleInfoW (lcid
, LOCALE_SABBREVDAYNAME1
+k
, NULL
, 0);
2141 g_array_set_size (result
, result
->len
+ n
);
2142 GetLocaleInfoW (lcid
, LOCALE_SABBREVDAYNAME1
+k
, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
2143 g_array_set_size (result
, result
->len
- 1);
2146 if (systemtime
.wDayOfWeek
== 0)
2149 k
= systemtime
.wDayOfWeek
- 1;
2150 n
= GetLocaleInfoW (lcid
, LOCALE_SDAYNAME1
+k
, NULL
, 0);
2151 g_array_set_size (result
, result
->len
+ n
);
2152 GetLocaleInfoW (lcid
, LOCALE_SDAYNAME1
+k
, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
2153 g_array_set_size (result
, result
->len
- 1);
2157 n
= GetLocaleInfoW (lcid
, LOCALE_SABBREVMONTHNAME1
+systemtime
.wMonth
-1, NULL
, 0);
2158 g_array_set_size (result
, result
->len
+ n
);
2159 GetLocaleInfoW (lcid
, LOCALE_SABBREVMONTHNAME1
+systemtime
.wMonth
-1, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
2160 g_array_set_size (result
, result
->len
- 1);
2163 n
= GetLocaleInfoW (lcid
, LOCALE_SMONTHNAME1
+systemtime
.wMonth
-1, NULL
, 0);
2164 g_array_set_size (result
, result
->len
+ n
);
2165 GetLocaleInfoW (lcid
, LOCALE_SMONTHNAME1
+systemtime
.wMonth
-1, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
2166 g_array_set_size (result
, result
->len
- 1);
2169 n
= GetDateFormatW (lcid
, 0, &systemtime
, NULL
, NULL
, 0);
2172 g_array_set_size (result
, result
->len
+ n
);
2173 GetDateFormatW (lcid
, 0, &systemtime
, NULL
, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
2174 g_array_set_size (result
, result
->len
- 1);
2176 g_array_append_vals (result
, L
" ", 1);
2177 n
= GetTimeFormatW (lcid
, 0, &systemtime
, NULL
, NULL
, 0);
2180 g_array_set_size (result
, result
->len
+ n
);
2181 GetTimeFormatW (lcid
, 0, &systemtime
, NULL
, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
2182 g_array_set_size (result
, result
->len
- 1);
2186 g_array_append_vals (result
, digits
+ systemtime
.wYear
/1000, 1);
2187 g_array_append_vals (result
, digits
+ (systemtime
.wYear
/1000)%10, 1);
2190 g_array_append_vals (result
, digits
+ systemtime
.wDay
/10, 1);
2191 g_array_append_vals (result
, digits
+ systemtime
.wDay
%10, 1);
2194 g_array_append_vals (result
, digits
+ systemtime
.wMonth
/10, 1);
2195 g_array_append_vals (result
, digits
+ systemtime
.wMonth
%10, 1);
2196 g_array_append_vals (result
, L
"/", 1);
2197 g_array_append_vals (result
, digits
+ systemtime
.wDay
/10, 1);
2198 g_array_append_vals (result
, digits
+ systemtime
.wDay
%10, 1);
2199 g_array_append_vals (result
, L
"/", 1);
2200 g_array_append_vals (result
, digits
+ (systemtime
.wYear
/10)%10, 1);
2201 g_array_append_vals (result
, digits
+ systemtime
.wYear
%10, 1);
2204 if (systemtime
.wDay
>= 10)
2205 g_array_append_vals (result
, digits
+ systemtime
.wDay
/10, 1);
2207 g_array_append_vals (result
, L
" ", 1);
2208 g_array_append_vals (result
, digits
+ systemtime
.wDay
%10, 1);
2211 /* A GDate has no time fields, so for now we can
2212 * hardcode all time conversions into zeros (or 12 for
2213 * %I). The alternative code snippets in the #else
2214 * branches are here ready to be taken into use when
2215 * needed by a g_strftime() or g_date_and_time_format()
2220 g_array_append_vals (result
, L
"00", 2);
2222 g_array_append_vals (result
, digits
+ systemtime
.wHour
/10, 1);
2223 g_array_append_vals (result
, digits
+ systemtime
.wHour
%10, 1);
2228 g_array_append_vals (result
, L
"12", 2);
2230 if (systemtime
.wHour
== 0)
2231 g_array_append_vals (result
, L
"12", 2);
2234 g_array_append_vals (result
, digits
+ (systemtime
.wHour
%12)/10, 1);
2235 g_array_append_vals (result
, digits
+ (systemtime
.wHour
%12)%10, 1);
2240 g_array_append_vals (result
, digits
+ (tm
->tm_yday
+1)/100, 1);
2241 g_array_append_vals (result
, digits
+ ((tm
->tm_yday
+1)/10)%10, 1);
2242 g_array_append_vals (result
, digits
+ (tm
->tm_yday
+1)%10, 1);
2245 g_array_append_vals (result
, digits
+ systemtime
.wMonth
/10, 1);
2246 g_array_append_vals (result
, digits
+ systemtime
.wMonth
%10, 1);
2250 g_array_append_vals (result
, L
"00", 2);
2252 g_array_append_vals (result
, digits
+ systemtime
.wMinute
/10, 1);
2253 g_array_append_vals (result
, digits
+ systemtime
.wMinute
%10, 1);
2257 g_array_append_vals (result
, L
"\n", 1);
2260 n
= GetTimeFormatW (lcid
, 0, &systemtime
, L
"tt", NULL
, 0);
2263 g_array_set_size (result
, result
->len
+ n
);
2264 GetTimeFormatW (lcid
, 0, &systemtime
, L
"tt", ((wchar_t *) result
->data
) + result
->len
- n
, n
);
2265 g_array_set_size (result
, result
->len
- 1);
2269 /* This is a rather odd format. Hard to say what to do.
2270 * Let's always use the POSIX %I:%M:%S %p
2273 g_array_append_vals (result
, L
"12:00:00", 8);
2275 if (systemtime
.wHour
== 0)
2276 g_array_append_vals (result
, L
"12", 2);
2279 g_array_append_vals (result
, digits
+ (systemtime
.wHour
%12)/10, 1);
2280 g_array_append_vals (result
, digits
+ (systemtime
.wHour
%12)%10, 1);
2282 g_array_append_vals (result
, L
":", 1);
2283 g_array_append_vals (result
, digits
+ systemtime
.wMinute
/10, 1);
2284 g_array_append_vals (result
, digits
+ systemtime
.wMinute
%10, 1);
2285 g_array_append_vals (result
, L
":", 1);
2286 g_array_append_vals (result
, digits
+ systemtime
.wSecond
/10, 1);
2287 g_array_append_vals (result
, digits
+ systemtime
.wSecond
%10, 1);
2288 g_array_append_vals (result
, L
" ", 1);
2290 n
= GetTimeFormatW (lcid
, 0, &systemtime
, L
"tt", NULL
, 0);
2293 g_array_set_size (result
, result
->len
+ n
);
2294 GetTimeFormatW (lcid
, 0, &systemtime
, L
"tt", ((wchar_t *) result
->data
) + result
->len
- n
, n
);
2295 g_array_set_size (result
, result
->len
- 1);
2300 g_array_append_vals (result
, L
"00:00", 5);
2302 g_array_append_vals (result
, digits
+ systemtime
.wHour
/10, 1);
2303 g_array_append_vals (result
, digits
+ systemtime
.wHour
%10, 1);
2304 g_array_append_vals (result
, L
":", 1);
2305 g_array_append_vals (result
, digits
+ systemtime
.wMinute
/10, 1);
2306 g_array_append_vals (result
, digits
+ systemtime
.wMinute
%10, 1);
2311 g_array_append_vals (result
, L
"00", 2);
2313 g_array_append_vals (result
, digits
+ systemtime
.wSecond
/10, 1);
2314 g_array_append_vals (result
, digits
+ systemtime
.wSecond
%10, 1);
2318 g_array_append_vals (result
, L
"\t", 1);
2322 g_array_append_vals (result
, L
"00:00:00", 8);
2324 g_array_append_vals (result
, digits
+ systemtime
.wHour
/10, 1);
2325 g_array_append_vals (result
, digits
+ systemtime
.wHour
%10, 1);
2326 g_array_append_vals (result
, L
":", 1);
2327 g_array_append_vals (result
, digits
+ systemtime
.wMinute
/10, 1);
2328 g_array_append_vals (result
, digits
+ systemtime
.wMinute
%10, 1);
2329 g_array_append_vals (result
, L
":", 1);
2330 g_array_append_vals (result
, digits
+ systemtime
.wSecond
/10, 1);
2331 g_array_append_vals (result
, digits
+ systemtime
.wSecond
%10, 1);
2335 if (systemtime
.wDayOfWeek
== 0)
2336 g_array_append_vals (result
, L
"7", 1);
2338 g_array_append_vals (result
, digits
+ systemtime
.wDayOfWeek
, 1);
2341 n
= g_date_get_sunday_week_of_year (d
);
2342 g_array_append_vals (result
, digits
+ n
/10, 1);
2343 g_array_append_vals (result
, digits
+ n
%10, 1);
2346 n
= g_date_get_iso8601_week_of_year (d
);
2347 g_array_append_vals (result
, digits
+ n
/10, 1);
2348 g_array_append_vals (result
, digits
+ n
%10, 1);
2351 g_array_append_vals (result
, digits
+ systemtime
.wDayOfWeek
, 1);
2354 n
= g_date_get_monday_week_of_year (d
);
2355 g_array_append_vals (result
, digits
+ n
/10, 1);
2356 g_array_append_vals (result
, digits
+ n
%10, 1);
2359 n
= GetDateFormatW (lcid
, 0, &systemtime
, NULL
, NULL
, 0);
2362 g_array_set_size (result
, result
->len
+ n
);
2363 GetDateFormatW (lcid
, 0, &systemtime
, NULL
, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
2364 g_array_set_size (result
, result
->len
- 1);
2368 n
= GetTimeFormatW (lcid
, 0, &systemtime
, NULL
, NULL
, 0);
2371 g_array_set_size (result
, result
->len
+ n
);
2372 GetTimeFormatW (lcid
, 0, &systemtime
, NULL
, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
2373 g_array_set_size (result
, result
->len
- 1);
2377 g_array_append_vals (result
, digits
+ (systemtime
.wYear
/10)%10, 1);
2378 g_array_append_vals (result
, digits
+ systemtime
.wYear
%10, 1);
2381 g_array_append_vals (result
, digits
+ systemtime
.wYear
/1000, 1);
2382 g_array_append_vals (result
, digits
+ (systemtime
.wYear
/100)%10, 1);
2383 g_array_append_vals (result
, digits
+ (systemtime
.wYear
/10)%10, 1);
2384 g_array_append_vals (result
, digits
+ systemtime
.wYear
%10, 1);
2387 n
= GetTimeZoneInformation (&tzinfo
);
2388 if (n
== TIME_ZONE_ID_UNKNOWN
)
2390 else if (n
== TIME_ZONE_ID_STANDARD
)
2391 g_array_append_vals (result
, tzinfo
.StandardName
, wcslen (tzinfo
.StandardName
));
2392 else if (n
== TIME_ZONE_ID_DAYLIGHT
)
2393 g_array_append_vals (result
, tzinfo
.DaylightName
, wcslen (tzinfo
.DaylightName
));
2396 g_array_append_vals (result
, L
"%", 1);
2400 else if (c
<= 0xFFFF)
2403 g_array_append_vals (result
, &wc
, 1);
2410 ws
= g_ucs4_to_utf16 (&c
, 1, NULL
, &nwc
, NULL
);
2411 g_array_append_vals (result
, ws
, nwc
);
2414 p
= g_utf8_next_char (p
);
2417 convbuf
= g_utf16_to_utf8 ((wchar_t *) result
->data
, result
->len
, NULL
, &convlen
, NULL
);
2418 g_array_free (result
, TRUE
);
2426 if (slen
<= convlen
)
2428 /* Ensure only whole characters are copied into the buffer. */
2429 gchar
*end
= g_utf8_find_prev_char (convbuf
, convbuf
+ slen
);
2430 g_assert (end
!= NULL
);
2431 convlen
= end
- convbuf
;
2433 /* Return 0 because the buffer isn't large enough. */
2439 memcpy (s
, convbuf
, convlen
);
2450 * @s: destination buffer
2451 * @slen: buffer size
2452 * @format: format string
2453 * @date: valid #GDate
2455 * Generates a printed representation of the date, in a
2456 * [locale][setlocale]-specific way.
2457 * Works just like the platform's C library strftime() function,
2458 * but only accepts date-related formats; time-related formats
2459 * give undefined results. Date must be valid. Unlike strftime()
2460 * (which uses the locale encoding), works on a UTF-8 format
2461 * string and stores a UTF-8 result.
2463 * This function does not provide any conversion specifiers in
2464 * addition to those implemented by the platform's C library.
2465 * For example, don't expect that using g_date_strftime() would
2466 * make the \%F provided by the C99 strftime() work on Windows
2467 * where the C library only complies to C89.
2469 * Returns: number of characters written to the buffer, or 0 the buffer was too small
2471 #pragma GCC diagnostic push
2472 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
2475 g_date_strftime (gchar
*s
,
2477 const gchar
*format
,
2482 gsize locale_format_len
= 0;
2483 gchar
*locale_format
;
2489 GError
*error
= NULL
;
2493 g_return_val_if_fail (g_date_valid (d
), 0);
2494 g_return_val_if_fail (slen
> 0, 0);
2495 g_return_val_if_fail (format
!= NULL
, 0);
2496 g_return_val_if_fail (s
!= NULL
, 0);
2498 g_date_to_struct_tm (d
, &tm
);
2501 if (!g_utf8_validate (format
, -1, NULL
))
2506 return win32_strftime_helper (d
, format
, &tm
, s
, slen
);
2509 locale_format
= g_locale_from_utf8 (format
, -1, NULL
, &locale_format_len
, &error
);
2513 g_warning (G_STRLOC
"Error converting format to locale encoding: %s\n", error
->message
);
2514 g_error_free (error
);
2520 tmpbufsize
= MAX (128, locale_format_len
* 2);
2523 tmpbuf
= g_malloc (tmpbufsize
);
2525 /* Set the first byte to something other than '\0', to be able to
2526 * recognize whether strftime actually failed or just returned "".
2529 tmplen
= strftime (tmpbuf
, tmpbufsize
, locale_format
, &tm
);
2531 if (tmplen
== 0 && tmpbuf
[0] != '\0')
2536 if (tmpbufsize
> 65536)
2538 g_warning (G_STRLOC
"Maximum buffer size for g_date_strftime exceeded: giving up\n");
2539 g_free (locale_format
);
2548 g_free (locale_format
);
2550 convbuf
= g_locale_to_utf8 (tmpbuf
, tmplen
, NULL
, &convlen
, &error
);
2555 g_warning (G_STRLOC
"Error converting results of strftime to UTF-8: %s\n", error
->message
);
2556 g_error_free (error
);
2562 if (slen
<= convlen
)
2564 /* Ensure only whole characters are copied into the buffer.
2566 gchar
*end
= g_utf8_find_prev_char (convbuf
, convbuf
+ slen
);
2567 g_assert (end
!= NULL
);
2568 convlen
= end
- convbuf
;
2570 /* Return 0 because the buffer isn't large enough.
2577 memcpy (s
, convbuf
, convlen
);
2585 #pragma GCC diagnostic pop