3 * Copyright (C) 2009-2010 Christian Hergert <chris@dronelabs.com>
4 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
5 * Copyright (C) 2010 Emmanuele Bassi <ebassi@linux.intel.com>
6 * Copyright © 2010 Codethink Limited
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * licence, or (at your option) any later version.
13 * This is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 * License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 * Authors: Christian Hergert <chris@dronelabs.com>
24 * Thiago Santos <thiago.sousa.santos@collabora.co.uk>
25 * Emmanuele Bassi <ebassi@linux.intel.com>
26 * Ryan Lortie <desrt@desrt.ca>
29 /* Algorithms within this file are based on the Calendar FAQ by
30 * Claus Tondering. It can be found at
31 * http://www.tondering.dk/claus/cal/calendar29.txt
33 * Copyright and disclaimer
34 * ------------------------
35 * This document is Copyright (C) 2008 by Claus Tondering.
36 * E-mail: claus@tondering.dk. (Please include the word
37 * "calendar" in the subject line.)
38 * The document may be freely distributed, provided this
39 * copyright notice is included and no money is charged for
42 * This document is provided "as is". No warranties are made as
57 #ifdef HAVE_LANGINFO_TIME
61 #include "gdatetime.h"
64 #include "gfileutils.h"
67 #include "gmappedfile.h"
68 #include "gstrfuncs.h"
69 #include "gtestutils.h"
71 #include "gtimezone.h"
78 #endif /* !G_OS_WIN32 */
83 * @short_description: A structure representing Date and Time
84 * @see_also: #GTimeZone
86 * #GDateTime is a structure that combines a Gregorian date and time
87 * into a single structure. It provides many conversion and methods to
88 * manipulate dates and times. Time precision is provided down to
89 * microseconds and the time can range (proleptically) from 0001-01-01
90 * 00:00:00 to 9999-12-31 23:59:59.999999. #GDateTime follows POSIX
91 * time in the sense that it is oblivious to leap seconds.
93 * #GDateTime is an immutable object; once it has been created it cannot
94 * be modified further. All modifiers will create a new #GDateTime.
95 * Nearly all such functions can fail due to the date or time going out
96 * of range, in which case %NULL will be returned.
98 * #GDateTime is reference counted: the reference count is increased by calling
99 * g_date_time_ref() and decreased by calling g_date_time_unref(). When the
100 * reference count drops to 0, the resources allocated by the #GDateTime
101 * structure are released.
103 * Many parts of the API may produce non-obvious results. As an
104 * example, adding two months to January 31st will yield March 31st
105 * whereas adding one month and then one month again will yield either
106 * March 28th or March 29th. Also note that adding 24 hours is not
107 * always the same as adding one day (since days containing daylight
108 * savings time transitions are either 23 or 25 hours in length).
110 * #GDateTime is available since GLib 2.26.
115 /* 1 is 0001-01-01 in Proleptic Gregorian */
118 /* Microsecond timekeeping within Day */
121 /* TimeZone information */
125 volatile gint ref_count;
128 /* Time conversion {{{1 */
130 #define UNIX_EPOCH_START 719163
131 #define INSTANT_TO_UNIX(instant) \
132 ((instant)/USEC_PER_SECOND - UNIX_EPOCH_START * SEC_PER_DAY)
133 #define UNIX_TO_INSTANT(unix) \
134 (((unix) + UNIX_EPOCH_START * SEC_PER_DAY) * USEC_PER_SECOND)
136 #define DAYS_IN_4YEARS 1461 /* days in 4 years */
137 #define DAYS_IN_100YEARS 36524 /* days in 100 years */
138 #define DAYS_IN_400YEARS 146097 /* days in 400 years */
140 #define USEC_PER_SECOND (G_GINT64_CONSTANT (1000000))
141 #define USEC_PER_MINUTE (G_GINT64_CONSTANT (60000000))
142 #define USEC_PER_HOUR (G_GINT64_CONSTANT (3600000000))
143 #define USEC_PER_MILLISECOND (G_GINT64_CONSTANT (1000))
144 #define USEC_PER_DAY (G_GINT64_CONSTANT (86400000000))
145 #define SEC_PER_DAY (G_GINT64_CONSTANT (86400))
147 #define SECS_PER_MINUTE (60)
148 #define SECS_PER_HOUR (60 * SECS_PER_MINUTE)
149 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
150 #define SECS_PER_YEAR (365 * SECS_PER_DAY)
151 #define SECS_PER_JULIAN (DAYS_PER_PERIOD * SECS_PER_DAY)
153 #define GREGORIAN_LEAP(y) ((((y) % 4) == 0) && (!((((y) % 100) == 0) && (((y) % 400) != 0))))
154 #define JULIAN_YEAR(d) ((d)->julian / 365.25)
155 #define DAYS_PER_PERIOD (G_GINT64_CONSTANT (2914695))
157 static const guint16 days_in_months[2][13] =
159 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
160 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
163 static const guint16 days_in_year[2][13] =
165 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
166 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
169 #ifdef HAVE_LANGINFO_TIME
171 #define GET_AMPM(d) ((g_date_time_get_hour (d) < 12) ? \
172 nl_langinfo (AM_STR) : \
173 nl_langinfo (PM_STR))
175 #define PREFERRED_DATE_TIME_FMT nl_langinfo (D_T_FMT)
176 #define PREFERRED_DATE_FMT nl_langinfo (D_FMT)
177 #define PREFERRED_TIME_FMT nl_langinfo (T_FMT)
178 #define PREFERRED_TIME_FMT nl_langinfo (T_FMT)
179 #define PREFERRED_12HR_TIME_FMT nl_langinfo (T_FMT_AMPM)
181 static const gint weekday_item[2][7] =
183 { ABDAY_2, ABDAY_3, ABDAY_4, ABDAY_5, ABDAY_6, ABDAY_7, ABDAY_1 },
184 { DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7, DAY_1 }
187 static const gint month_item[2][12] =
189 { ABMON_1, ABMON_2, ABMON_3, ABMON_4, ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10, ABMON_11, ABMON_12 },
190 { MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7, MON_8, MON_9, MON_10, MON_11, MON_12 },
193 #define WEEKDAY_ABBR(d) nl_langinfo (weekday_item[0][g_date_time_get_day_of_week (d) - 1])
194 #define WEEKDAY_FULL(d) nl_langinfo (weekday_item[1][g_date_time_get_day_of_week (d) - 1])
195 #define MONTH_ABBR(d) nl_langinfo (month_item[0][g_date_time_get_month (d) - 1])
196 #define MONTH_FULL(d) nl_langinfo (month_item[1][g_date_time_get_month (d) - 1])
200 #define GET_AMPM(d) ((g_date_time_get_hour (d) < 12) \
201 /* Translators: 'before midday' indicator */ \
202 ? C_("GDateTime", "AM") \
203 /* Translators: 'after midday' indicator */ \
204 : C_("GDateTime", "PM"))
206 /* Translators: this is the preferred format for expressing the date and the time */
207 #define PREFERRED_DATE_TIME_FMT C_("GDateTime", "%a %b %e %H:%M:%S %Y")
209 /* Translators: this is the preferred format for expressing the date */
210 #define PREFERRED_DATE_FMT C_("GDateTime", "%m/%d/%y")
212 /* Translators: this is the preferred format for expressing the time */
213 #define PREFERRED_TIME_FMT C_("GDateTime", "%H:%M:%S")
215 /* Translators: this is the preferred format for expressing 12 hour time */
216 #define PREFERRED_12HR_TIME_FMT C_("GDateTime", "%I:%M:%S %p")
218 #define WEEKDAY_ABBR(d) (get_weekday_name_abbr (g_date_time_get_day_of_week (d)))
219 #define WEEKDAY_FULL(d) (get_weekday_name (g_date_time_get_day_of_week (d)))
220 #define MONTH_ABBR(d) (get_month_name_abbr (g_date_time_get_month (d)))
221 #define MONTH_FULL(d) (get_month_name (g_date_time_get_month (d)))
224 get_month_name (gint month)
229 return C_("full month name", "January");
231 return C_("full month name", "February");
233 return C_("full month name", "March");
235 return C_("full month name", "April");
237 return C_("full month name", "May");
239 return C_("full month name", "June");
241 return C_("full month name", "July");
243 return C_("full month name", "August");
245 return C_("full month name", "September");
247 return C_("full month name", "October");
249 return C_("full month name", "November");
251 return C_("full month name", "December");
254 g_warning ("Invalid month number %d", month);
261 get_month_name_abbr (gint month)
266 return C_("abbreviated month name", "Jan");
268 return C_("abbreviated month name", "Feb");
270 return C_("abbreviated month name", "Mar");
272 return C_("abbreviated month name", "Apr");
274 return C_("abbreviated month name", "May");
276 return C_("abbreviated month name", "Jun");
278 return C_("abbreviated month name", "Jul");
280 return C_("abbreviated month name", "Aug");
282 return C_("abbreviated month name", "Sep");
284 return C_("abbreviated month name", "Oct");
286 return C_("abbreviated month name", "Nov");
288 return C_("abbreviated month name", "Dec");
291 g_warning ("Invalid month number %d", month);
298 get_weekday_name (gint day)
303 return C_("full weekday name", "Monday");
305 return C_("full weekday name", "Tuesday");
307 return C_("full weekday name", "Wednesday");
309 return C_("full weekday name", "Thursday");
311 return C_("full weekday name", "Friday");
313 return C_("full weekday name", "Saturday");
315 return C_("full weekday name", "Sunday");
318 g_warning ("Invalid week day number %d", day);
325 get_weekday_name_abbr (gint day)
330 return C_("abbreviated weekday name", "Mon");
332 return C_("abbreviated weekday name", "Tue");
334 return C_("abbreviated weekday name", "Wed");
336 return C_("abbreviated weekday name", "Thu");
338 return C_("abbreviated weekday name", "Fri");
340 return C_("abbreviated weekday name", "Sat");
342 return C_("abbreviated weekday name", "Sun");
345 g_warning ("Invalid week day number %d", day);
351 #endif /* HAVE_LANGINFO_TIME */
354 ymd_to_days (gint year,
360 days = (year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100)
361 + ((year - 1) / 400);
363 days += days_in_year[0][month - 1];
364 if (GREGORIAN_LEAP (year) && month > 2)
373 g_date_time_get_week_number (GDateTime *datetime,
378 gint a, b, c, d, e, f, g, n, s, month, day, year;
380 g_date_time_get_ymd (datetime, &year, &month, &day);
384 a = g_date_time_get_year (datetime) - 1;
385 b = (a / 4) - (a / 100) + (a / 400);
386 c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
389 f = day - 1 + (31 * (month - 1));
394 b = (a / 4) - (a / 100) + (a / 400);
395 c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
398 f = day + (((153 * (month - 3)) + 2) / 5) + 58 + s;
408 *week_number = 53 - ((g - s) / 5);
409 else if (n > 364 + s)
412 *week_number = (n / 7) + 1;
416 *day_of_week = d + 1;
419 *day_of_year = f + 1;
425 g_date_time_alloc (GTimeZone *tz)
429 datetime = g_slice_new0 (GDateTime);
430 datetime->tz = g_time_zone_ref (tz);
431 datetime->ref_count = 1;
438 * @datetime: a #GDateTime
440 * Atomically increments the reference count of @datetime by one.
442 * Return value: the #GDateTime with the reference count increased
447 g_date_time_ref (GDateTime *datetime)
449 g_return_val_if_fail (datetime != NULL, NULL);
450 g_return_val_if_fail (datetime->ref_count > 0, NULL);
452 g_atomic_int_inc (&datetime->ref_count);
459 * @datetime: a #GDateTime
461 * Atomically decrements the reference count of @datetime by one.
463 * When the reference count reaches zero, the resources allocated by
464 * @datetime are freed
469 g_date_time_unref (GDateTime *datetime)
471 g_return_if_fail (datetime != NULL);
472 g_return_if_fail (datetime->ref_count > 0);
474 if (g_atomic_int_dec_and_test (&datetime->ref_count))
476 g_time_zone_unref (datetime->tz);
477 g_slice_free (GDateTime, datetime);
481 /* Internal state transformers {{{1 */
483 * g_date_time_to_instant:
484 * @datetime: a #GDateTime
486 * Convert a @datetime into an instant.
488 * An instant is a number that uniquely describes a particular
489 * microsecond in time, taking time zone considerations into account.
490 * (ie: "03:00 -0400" is the same instant as "02:00 -0500").
492 * An instant is always positive but we use a signed return value to
493 * avoid troubles with C.
496 g_date_time_to_instant (GDateTime *datetime)
500 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
501 offset *= USEC_PER_SECOND;
503 return datetime->days * USEC_PER_DAY + datetime->usec - offset;
507 * g_date_time_from_instant:
509 * @instant: a instant in time
511 * Creates a #GDateTime from a time zone and an instant.
513 * This might fail if the time ends up being out of range.
516 g_date_time_from_instant (GTimeZone *tz,
522 if (instant < 0 || instant > G_GINT64_CONSTANT (1000000000000000000))
525 datetime = g_date_time_alloc (tz);
526 datetime->interval = g_time_zone_find_interval (tz,
527 G_TIME_TYPE_UNIVERSAL,
528 INSTANT_TO_UNIX (instant));
529 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
530 offset *= USEC_PER_SECOND;
534 datetime->days = instant / USEC_PER_DAY;
535 datetime->usec = instant % USEC_PER_DAY;
537 if (datetime->days < 1 || 3652059 < datetime->days)
539 g_date_time_unref (datetime);
548 * g_date_time_deal_with_date_change:
549 * @datetime: a #GDateTime
551 * This function should be called whenever the date changes by adding
552 * days, months or years. It does three things.
554 * First, we ensure that the date falls between 0001-01-01 and
555 * 9999-12-31 and return %FALSE if it does not.
557 * Next we update the ->interval field.
559 * Finally, we ensure that the resulting date and time pair exists (by
560 * ensuring that our time zone has an interval containing it) and
561 * adjusting as required. For example, if we have the time 02:30:00 on
562 * March 13 2010 in Toronto and we add 1 day to it, we would end up with
563 * 2:30am on March 14th, which doesn't exist. In that case, we bump the
567 g_date_time_deal_with_date_change (GDateTime *datetime)
573 if (datetime->days < 1 || datetime->days > 3652059)
576 was_dst = g_time_zone_is_dst (datetime->tz, datetime->interval);
578 full_time = datetime->days * USEC_PER_DAY + datetime->usec;
581 usec = full_time % USEC_PER_SECOND;
582 full_time /= USEC_PER_SECOND;
583 full_time -= UNIX_EPOCH_START * SEC_PER_DAY;
585 datetime->interval = g_time_zone_adjust_time (datetime->tz,
588 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
589 full_time *= USEC_PER_SECOND;
592 datetime->days = full_time / USEC_PER_DAY;
593 datetime->usec = full_time % USEC_PER_DAY;
595 /* maybe daylight time caused us to shift to a different day,
596 * but it definitely didn't push us into a different year */
601 g_date_time_replace_days (GDateTime *datetime,
606 new = g_date_time_alloc (datetime->tz);
607 new->interval = datetime->interval;
608 new->usec = datetime->usec;
611 if (!g_date_time_deal_with_date_change (new))
613 g_date_time_unref (new);
620 /* now/unix/timeval Constructors {{{1 */
623 * g_date_time_new_from_timeval:
627 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
628 * given time zone @tz.
630 * The time contained in a #GTimeVal is always stored in the form of
631 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
634 * This call can fail (returning %NULL) if @tv represents a time outside
635 * of the supported range of #GDateTime.
637 * You should release the return value by calling g_date_time_unref()
638 * when you are done with it.
640 * Returns: a new #GDateTime, or %NULL
645 g_date_time_new_from_timeval (GTimeZone *tz,
648 return g_date_time_from_instant (tz, tv->tv_usec +
649 UNIX_TO_INSTANT (tv->tv_sec));
653 * g_date_time_new_from_unix:
657 * Creates a #GDateTime corresponding to the given Unix time @t in the
658 * given time zone @tz.
660 * Unix time is the number of seconds that have elapsed since 1970-01-01
661 * 00:00:00 UTC, regardless of the time zone given.
663 * This call can fail (returning %NULL) if @t represents a time outside
664 * of the supported range of #GDateTime.
666 * You should release the return value by calling g_date_time_unref()
667 * when you are done with it.
669 * Returns: a new #GDateTime, or %NULL
674 g_date_time_new_from_unix (GTimeZone *tz,
677 return g_date_time_from_instant (tz, UNIX_TO_INSTANT (secs));
681 * g_date_time_new_now:
684 * Creates a #GDateTime corresponding to this exact instant in the given
685 * time zone @tz. The time is as accurate as the system allows, to a
686 * maximum accuracy of 1 microsecond.
688 * This function will always succeed unless the system clock is set to
689 * truly insane values (or unless GLib is still being used after the
692 * You should release the return value by calling g_date_time_unref()
693 * when you are done with it.
695 * Returns: a new #GDateTime, or %NULL
700 g_date_time_new_now (GTimeZone *tz)
704 g_get_current_time (&tv);
706 return g_date_time_new_from_timeval (tz, &tv);
710 * g_date_time_new_now_local:
712 * Creates a #GDateTime corresponding to this exact instant in the local
715 * This is equivalent to calling g_date_time_new_now() with the time
716 * zone returned by g_time_zone_new_local().
718 * Returns: a new #GDateTime, or %NULL
723 g_date_time_new_now_local (void)
728 local = g_time_zone_new_local ();
729 datetime = g_date_time_new_now (local);
730 g_time_zone_unref (local);
736 * g_date_time_new_now_utc:
738 * Creates a #GDateTime corresponding to this exact instant in UTC.
740 * This is equivalent to calling g_date_time_new_now() with the time
741 * zone returned by g_time_zone_new_utc().
743 * Returns: a new #GDateTime, or %NULL
748 g_date_time_new_now_utc (void)
753 utc = g_time_zone_new_utc ();
754 datetime = g_date_time_new_now (utc);
755 g_time_zone_unref (utc);
761 * g_date_time_new_from_unix_local:
764 * Creates a #GDateTime corresponding to the given Unix time @t in the
767 * Unix time is the number of seconds that have elapsed since 1970-01-01
768 * 00:00:00 UTC, regardless of the local time offset.
770 * This call can fail (returning %NULL) if @t represents a time outside
771 * of the supported range of #GDateTime.
773 * You should release the return value by calling g_date_time_unref()
774 * when you are done with it.
776 * Returns: a new #GDateTime, or %NULL
781 g_date_time_new_from_unix_local (gint64 t)
786 local = g_time_zone_new_local ();
787 datetime = g_date_time_new_from_unix (local, t);
788 g_time_zone_unref (local);
794 * g_date_time_new_from_unix_utc:
797 * Creates a #GDateTime corresponding to the given Unix time @t in UTC.
799 * Unix time is the number of seconds that have elapsed since 1970-01-01
802 * This call can fail (returning %NULL) if @t represents a time outside
803 * of the supported range of #GDateTime.
805 * You should release the return value by calling g_date_time_unref()
806 * when you are done with it.
808 * Returns: a new #GDateTime, or %NULL
813 g_date_time_new_from_unix_utc (gint64 t)
818 utc = g_time_zone_new_utc ();
819 datetime = g_date_time_new_from_unix (utc, t);
820 g_time_zone_unref (utc);
826 * g_date_time_new_from_timeval_local:
829 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
832 * The time contained in a #GTimeVal is always stored in the form of
833 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
836 * This call can fail (returning %NULL) if @tv represents a time outside
837 * of the supported range of #GDateTime.
839 * You should release the return value by calling g_date_time_unref()
840 * when you are done with it.
842 * Returns: a new #GDateTime, or %NULL
847 g_date_time_new_from_timeval_local (const GTimeVal *tv)
852 local = g_time_zone_new_local ();
853 datetime = g_date_time_new_from_timeval (local, tv);
854 g_time_zone_unref (local);
860 * g_date_time_new_from_timeval_utc:
863 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in UTC.
865 * The time contained in a #GTimeVal is always stored in the form of
866 * seconds elapsed since 1970-01-01 00:00:00 UTC.
868 * This call can fail (returning %NULL) if @tv represents a time outside
869 * of the supported range of #GDateTime.
871 * You should release the return value by calling g_date_time_unref()
872 * when you are done with it.
874 * Returns: a new #GDateTime, or %NULL
879 g_date_time_new_from_timeval_utc (const GTimeVal *tv)
884 utc = g_time_zone_new_utc ();
885 datetime = g_date_time_new_from_timeval (utc, tv);
886 g_time_zone_unref (utc);
891 /* full new functions {{{1 */
896 * @year: the year component of the date
897 * @month: the month component of the date
898 * @day: the day component of the date
899 * @hour: the hour component of the date
900 * @minute: the minute component of the date
901 * @seconds: the number of seconds past the minute
903 * Creates a new #GDateTime corresponding to the given date and time in
906 * The @year must be between 1 and 9999, @month between 1 and 12 and @day
907 * between 1 and 28, 29, 30 or 31 depending on the month and the year.
909 * @hour must be between 0 and 23 and @minute must be between 0 and 59.
911 * @seconds must be at least 0.0 and must be strictly less than 60.0.
912 * It will be rounded down to the nearest microsecond.
914 * If the given time is not representable in the given time zone (for
915 * example, 02:30 on March 14th 2010 in Toronto, due to daylight savings
916 * time) then the time will be rounded up to the nearest existing time
917 * (in this case, 03:00). If this matters to you then you should verify
918 * the return value for containing the same as the numbers you gave.
920 * In the case that the given time is ambiguous in the given time zone
921 * (for example, 01:30 on November 7th 2010 in Toronto, due to daylight
922 * savings time) then the time falling within standard (ie:
923 * non-daylight) time is taken.
925 * It not considered a programmer error for the values to this function
926 * to be out of range, but in the case that they are, the function will
929 * You should release the return value by calling g_date_time_unref()
930 * when you are done with it.
932 * Returns: a new #GDateTime, or %NULL
937 g_date_time_new (GTimeZone *tz,
948 datetime = g_date_time_alloc (tz);
949 datetime->days = ymd_to_days (year, month, day);
950 datetime->usec = (hour * USEC_PER_HOUR)
951 + (minute * USEC_PER_MINUTE)
952 + (gint64) (seconds * USEC_PER_SECOND);
954 full_time = SEC_PER_DAY *
955 (ymd_to_days (year, month, day) - UNIX_EPOCH_START) +
956 SECS_PER_HOUR * hour +
957 SECS_PER_MINUTE * minute +
960 datetime->interval = g_time_zone_adjust_time (datetime->tz,
961 G_TIME_TYPE_STANDARD,
964 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
965 datetime->days = full_time / SEC_PER_DAY;
966 datetime->usec = (full_time % SEC_PER_DAY) * USEC_PER_SECOND;
967 datetime->usec += ((int) (seconds * USEC_PER_SECOND)) % USEC_PER_SECOND;
973 * g_date_time_new_local:
974 * @year: the year component of the date
975 * @month: the month component of the date
976 * @day: the day component of the date
977 * @hour: the hour component of the date
978 * @minute: the minute component of the date
979 * @seconds: the number of seconds past the minute
981 * Creates a new #GDateTime corresponding to the given date and time in
982 * the local time zone.
984 * This call is equivalent to calling g_date_time_new() with the time
985 * zone returned by g_time_zone_new_local().
987 * Returns: a #GDateTime, or %NULL
992 g_date_time_new_local (gint year,
1002 local = g_time_zone_new_local ();
1003 datetime = g_date_time_new (local, year, month, day, hour, minute, seconds);
1004 g_time_zone_unref (local);
1010 * g_date_time_new_utc:
1011 * @year: the year component of the date
1012 * @month: the month component of the date
1013 * @day: the day component of the date
1014 * @hour: the hour component of the date
1015 * @minute: the minute component of the date
1016 * @seconds: the number of seconds past the minute
1018 * Creates a new #GDateTime corresponding to the given date and time in
1021 * This call is equivalent to calling g_date_time_new() with the time
1022 * zone returned by g_time_zone_new_utc().
1024 * Returns: a #GDateTime, or %NULL
1029 g_date_time_new_utc (gint year,
1036 GDateTime *datetime;
1039 utc = g_time_zone_new_utc ();
1040 datetime = g_date_time_new (utc, year, month, day, hour, minute, seconds);
1041 g_time_zone_unref (utc);
1050 * @datetime: a #GDateTime
1051 * @timespan: a #GTimeSpan
1053 * Creates a copy of @datetime and adds the specified timespan to the copy.
1055 * Return value: the newly created #GDateTime which should be freed with
1056 * g_date_time_unref().
1061 g_date_time_add (GDateTime *datetime,
1064 return g_date_time_from_instant (datetime->tz, timespan +
1065 g_date_time_to_instant (datetime));
1069 * g_date_time_add_years:
1070 * @datetime: a #GDateTime
1071 * @years: the number of years
1073 * Creates a copy of @datetime and adds the specified number of years to the
1076 * Return value: the newly created #GDateTime which should be freed with
1077 * g_date_time_unref().
1082 g_date_time_add_years (GDateTime *datetime,
1085 gint year, month, day;
1087 g_return_val_if_fail (datetime != NULL, NULL);
1089 if (years < -10000 || years > 10000)
1092 g_date_time_get_ymd (datetime, &year, &month, &day);
1095 /* only possible issue is if we've entered a year with no February 29
1097 if (month == 2 && day == 29 && !GREGORIAN_LEAP (year))
1100 return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
1104 * g_date_time_add_months:
1105 * @datetime: a #GDateTime
1106 * @months: the number of months
1108 * Creates a copy of @datetime and adds the specified number of months to the
1111 * Return value: the newly created #GDateTime which should be freed with
1112 * g_date_time_unref().
1117 g_date_time_add_months (GDateTime *datetime,
1120 gint year, month, day;
1122 g_return_val_if_fail (datetime != NULL, NULL);
1123 g_date_time_get_ymd (datetime, &year, &month, &day);
1125 if (months < -120000 || months > 120000)
1128 year += months / 12;
1129 month += months % 12;
1135 else if (month > 12)
1141 day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
1143 return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
1147 * g_date_time_add_weeks:
1148 * @datetime: a #GDateTime
1149 * @weeks: the number of weeks
1151 * Creates a copy of @datetime and adds the specified number of weeks to the
1154 * Return value: the newly created #GDateTime which should be freed with
1155 * g_date_time_unref().
1160 g_date_time_add_weeks (GDateTime *datetime,
1163 g_return_val_if_fail (datetime != NULL, NULL);
1165 return g_date_time_add_days (datetime, weeks * 7);
1169 * g_date_time_add_days:
1170 * @datetime: a #GDateTime
1171 * @days: the number of days
1173 * Creates a copy of @datetime and adds the specified number of days to the
1176 * Return value: the newly created #GDateTime which should be freed with
1177 * g_date_time_unref().
1182 g_date_time_add_days (GDateTime *datetime,
1185 g_return_val_if_fail (datetime != NULL, NULL);
1187 if (days < -3660000 || days > 3660000)
1190 return g_date_time_replace_days (datetime, datetime->days + days);
1194 * g_date_time_add_hours:
1195 * @datetime: a #GDateTime
1196 * @hours: the number of hours to add
1198 * Creates a copy of @datetime and adds the specified number of hours
1200 * Return value: the newly created #GDateTime which should be freed with
1201 * g_date_time_unref().
1206 g_date_time_add_hours (GDateTime *datetime,
1209 return g_date_time_add (datetime, hours * USEC_PER_HOUR);
1213 * g_date_time_add_minutes:
1214 * @datetime: a #GDateTime
1215 * @minutes: the number of minutes to add
1217 * Creates a copy of @datetime adding the specified number of minutes.
1219 * Return value: the newly created #GDateTime which should be freed with
1220 * g_date_time_unref().
1225 g_date_time_add_minutes (GDateTime *datetime,
1228 return g_date_time_add (datetime, minutes * USEC_PER_MINUTE);
1233 * g_date_time_add_seconds:
1234 * @datetime: a #GDateTime
1235 * @seconds: the number of seconds to add
1237 * Creates a copy of @datetime and adds the specified number of seconds.
1239 * Return value: the newly created #GDateTime which should be freed with
1240 * g_date_time_unref().
1245 g_date_time_add_seconds (GDateTime *datetime,
1248 return g_date_time_add (datetime, seconds * USEC_PER_SECOND);
1252 * g_date_time_add_full:
1253 * @datetime: a #GDateTime
1254 * @years: the number of years to add
1255 * @months: the number of months to add
1256 * @days: the number of days to add
1257 * @hours: the number of hours to add
1258 * @minutes: the number of minutes to add
1259 * @seconds: the number of seconds to add
1261 * Creates a new #GDateTime adding the specified values to the current date and
1262 * time in @datetime.
1264 * Return value: the newly created #GDateTime that should be freed with
1265 * g_date_time_unref().
1270 g_date_time_add_full (GDateTime *datetime,
1278 gint year, month, day;
1283 g_return_val_if_fail (datetime != NULL, NULL);
1284 g_date_time_get_ymd (datetime, &year, &month, &day);
1286 months += years * 12;
1288 if (months < -120000 || months > 120000)
1291 if (days < -3660000 || days > 3660000)
1294 year += months / 12;
1295 month += months % 12;
1301 else if (month > 12)
1307 day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
1309 /* full_time is now in unix (local) time */
1310 full_time = datetime->usec / USEC_PER_SECOND + SEC_PER_DAY *
1311 (ymd_to_days (year, month, day) + days - UNIX_EPOCH_START);
1313 interval = g_time_zone_adjust_time (datetime->tz,
1314 g_time_zone_is_dst (datetime->tz,
1315 datetime->interval),
1318 /* move to UTC unix time */
1319 full_time -= g_time_zone_get_offset (datetime->tz, interval);
1321 /* convert back to an instant, add back fractional seconds */
1322 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
1323 full_time = full_time * USEC_PER_SECOND +
1324 datetime->usec % USEC_PER_SECOND;
1326 /* do the actual addition now */
1327 full_time += (hours * USEC_PER_HOUR) +
1328 (minutes * USEC_PER_MINUTE) +
1329 (gint64) (seconds * USEC_PER_SECOND);
1331 /* find the new interval */
1332 interval = g_time_zone_find_interval (datetime->tz,
1333 G_TIME_TYPE_UNIVERSAL,
1334 INSTANT_TO_UNIX (full_time));
1336 /* convert back into local time */
1337 full_time += USEC_PER_SECOND *
1338 g_time_zone_get_offset (datetime->tz, interval);
1340 /* split into days and usec of a new datetime */
1341 new = g_date_time_alloc (datetime->tz);
1342 new->interval = interval;
1343 new->days = full_time / USEC_PER_DAY;
1344 new->usec = full_time % USEC_PER_DAY;
1351 /* Compare, difference, hash, equal {{{1 */
1353 * g_date_time_compare:
1354 * @dt1: first #GDateTime to compare
1355 * @dt2: second #GDateTime to compare
1357 * A comparison function for #GDateTimes that is suitable
1358 * as a #GCompareFunc. Both #GDateTimes must be non-%NULL.
1360 * Return value: -1, 0 or 1 if @dt1 is less than, equal to or greater
1366 g_date_time_compare (gconstpointer dt1,
1371 difference = g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2);
1376 else if (difference > 0)
1384 * g_date_time_difference:
1385 * @end: a #GDateTime
1386 * @begin: a #GDateTime
1388 * Calculates the difference in time between @end and @begin. The
1389 * #GTimeSpan that is returned is effectively @end - @begin (ie:
1390 * positive if the first simparameter is larger).
1392 * Return value: the difference between the two #GDateTime, as a time
1393 * span expressed in microseconds.
1398 g_date_time_difference (GDateTime *end,
1401 g_return_val_if_fail (begin != NULL, 0);
1402 g_return_val_if_fail (end != NULL, 0);
1404 return g_date_time_to_instant (end) -
1405 g_date_time_to_instant (begin);
1410 * @datetime: a #GDateTime
1412 * Hashes @datetime into a #guint, suitable for use within #GHashTable.
1414 * Return value: a #guint containing the hash
1419 g_date_time_hash (gconstpointer datetime)
1421 return g_date_time_to_instant ((GDateTime *) datetime);
1425 * g_date_time_equal:
1426 * @dt1: a #GDateTime
1427 * @dt2: a #GDateTime
1429 * Checks to see if @dt1 and @dt2 are equal.
1431 * Equal here means that they represent the same moment after converting
1432 * them to the same time zone.
1434 * Return value: %TRUE if @dt1 and @dt2 are equal
1439 g_date_time_equal (gconstpointer dt1,
1442 return g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2) == 0;
1445 /* Year, Month, Day Getters {{{1 */
1447 * g_date_time_get_ymd:
1448 * @datetime: a #GDateTime.
1449 * @year: (out): the return location for the gregorian year, or %NULL.
1450 * @month: (out): the return location for the month of the year, or %NULL.
1451 * @day: (out): the return location for the day of the month, or %NULL.
1453 * Retrieves the Gregorian day, month, and year of a given #GDateTime.
1458 g_date_time_get_ymd (GDateTime *datetime,
1466 gint remaining_days;
1473 g_return_if_fail (datetime != NULL);
1475 remaining_days = datetime->days;
1478 * We need to convert an offset in days to its year/month/day representation.
1479 * Leap years makes this a little trickier than it should be, so we use
1480 * 400, 100 and 4 years cycles here to get to the correct year.
1483 /* Our days offset starts sets 0001-01-01 as day 1, if it was day 0 our
1484 * math would be simpler, so let's do it */
1487 the_year = (remaining_days / DAYS_IN_400YEARS) * 400 + 1;
1488 remaining_days = remaining_days % DAYS_IN_400YEARS;
1490 y100_cycles = remaining_days / DAYS_IN_100YEARS;
1491 remaining_days = remaining_days % DAYS_IN_100YEARS;
1492 the_year += y100_cycles * 100;
1494 y4_cycles = remaining_days / DAYS_IN_4YEARS;
1495 remaining_days = remaining_days % DAYS_IN_4YEARS;
1496 the_year += y4_cycles * 4;
1498 y1_cycles = remaining_days / 365;
1499 the_year += y1_cycles;
1500 remaining_days = remaining_days % 365;
1502 if (y1_cycles == 4 || y100_cycles == 4) {
1503 g_assert (remaining_days == 0);
1505 /* special case that indicates that the date is actually one year before,
1506 * in the 31th of December */
1513 /* now get the month and the day */
1514 leap = y1_cycles == 3 && (y4_cycles != 24 || y100_cycles == 3);
1516 g_assert (leap == GREGORIAN_LEAP(the_year));
1518 the_month = (remaining_days + 50) >> 5;
1519 preceding = (days_in_year[0][the_month - 1] + (the_month > 2 && leap));
1520 if (preceding > remaining_days)
1522 /* estimate is too large */
1524 preceding -= leap ? days_in_months[1][the_month]
1525 : days_in_months[0][the_month];
1528 remaining_days -= preceding;
1529 g_assert(0 <= remaining_days);
1531 the_day = remaining_days + 1;
1543 * g_date_time_get_year:
1544 * @datetime: A #GDateTime
1546 * Retrieves the year represented by @datetime in the Gregorian calendar.
1548 * Return value: the year represented by @datetime
1553 g_date_time_get_year (GDateTime *datetime)
1557 g_return_val_if_fail (datetime != NULL, 0);
1559 g_date_time_get_ymd (datetime, &year, NULL, NULL);
1565 * g_date_time_get_month:
1566 * @datetime: a #GDateTime
1568 * Retrieves the month of the year represented by @datetime in the Gregorian
1571 * Return value: the month represented by @datetime
1576 g_date_time_get_month (GDateTime *datetime)
1580 g_return_val_if_fail (datetime != NULL, 0);
1582 g_date_time_get_ymd (datetime, NULL, &month, NULL);
1588 * g_date_time_get_day_of_month:
1589 * @datetime: a #GDateTime
1591 * Retrieves the day of the month represented by @datetime in the gregorian
1594 * Return value: the day of the month
1599 g_date_time_get_day_of_month (GDateTime *datetime)
1603 const guint16 *days;
1606 g_return_val_if_fail (datetime != NULL, 0);
1608 days = days_in_year[GREGORIAN_LEAP (g_date_time_get_year (datetime)) ? 1 : 0];
1609 g_date_time_get_week_number (datetime, NULL, NULL, &day_of_year);
1611 for (i = 1; i <= 12; i++)
1613 if (days [i] >= day_of_year)
1614 return day_of_year - last;
1618 g_warn_if_reached ();
1622 /* Week of year / day of week getters {{{1 */
1624 * g_date_time_get_week_numbering_year:
1625 * @datetime: a #GDateTime
1627 * Returns the ISO 8601 week-numbering year in which the week containing
1630 * This function, taken together with g_date_time_get_week_of_year() and
1631 * g_date_time_get_day_of_week() can be used to determine the full ISO
1632 * week date on which @datetime falls.
1634 * This is usually equal to the normal Gregorian year (as returned by
1635 * g_date_time_get_year()), except as detailed below:
1637 * For Thursday, the week-numbering year is always equal to the usual
1638 * calendar year. For other days, the number is such that every day
1639 * within a complete week (Monday to Sunday) is contained within the
1640 * same week-numbering year.
1642 * For Monday, Tuesday and Wednesday occurring near the end of the year,
1643 * this may mean that the week-numbering year is one greater than the
1644 * calendar year (so that these days have the same week-numbering year
1645 * as the Thursday occurring early in the next year).
1647 * For Friday, Saturaday and Sunday occurring near the start of the year,
1648 * this may mean that the week-numbering year is one less than the
1649 * calendar year (so that these days have the same week-numbering year
1650 * as the Thursday occurring late in the previous year).
1652 * An equivalent description is that the week-numbering year is equal to
1653 * the calendar year containing the majority of the days in the current
1654 * week (Monday to Sunday).
1656 * Note that January 1 0001 in the proleptic Gregorian calendar is a
1657 * Monday, so this function never returns 0.
1659 * Returns: the ISO 8601 week-numbering year for @datetime
1664 g_date_time_get_week_numbering_year (GDateTime *datetime)
1666 gint year, month, day, weekday;
1668 g_date_time_get_ymd (datetime, &year, &month, &day);
1669 weekday = g_date_time_get_day_of_week (datetime);
1671 /* January 1, 2, 3 might be in the previous year if they occur after
1674 * Jan 1: Friday, Saturday, Sunday => day 1: weekday 5, 6, 7
1675 * Jan 2: Saturday, Sunday => day 2: weekday 6, 7
1676 * Jan 3: Sunday => day 3: weekday 7
1678 * So we have a special case if (day - weekday) <= -4
1680 if (month == 1 && (day - weekday) <= -4)
1683 /* December 29, 30, 31 might be in the next year if they occur before
1686 * Dec 31: Monday, Tuesday, Wednesday => day 31: weekday 1, 2, 3
1687 * Dec 30: Monday, Tuesday => day 30: weekday 1, 2
1688 * Dec 29: Monday => day 29: weekday 1
1690 * So we have a special case if (day - weekday) >= 28
1692 else if (month == 12 && (day - weekday) >= 28)
1700 * g_date_time_get_week_of_year:
1701 * @datetime: a #GDateTime
1703 * Returns the ISO 8601 week number for the week containing @datetime.
1704 * The ISO 8601 week number is the same for every day of the week (from
1705 * Moday through Sunday). That can produce some unusual results
1706 * (described below).
1708 * The first week of the year is week 1. This is the week that contains
1709 * the first Thursday of the year. Equivalently, this is the first week
1710 * that has more than 4 of its days falling within the calendar year.
1712 * The value 0 is never returned by this function. Days contained
1713 * within a year but occurring before the first ISO 8601 week of that
1714 * year are considered as being contained in the last week of the
1715 * previous year. Similarly, the final days of a calendar year may be
1716 * considered as being part of the first ISO 8601 week of the next year
1717 * if 4 or more days of that week are contained within the new year.
1719 * Returns: the ISO 8601 week number for @datetime.
1724 g_date_time_get_week_of_year (GDateTime *datetime)
1728 g_return_val_if_fail (datetime != NULL, 0);
1730 g_date_time_get_week_number (datetime, &weeknum, NULL, NULL);
1736 * g_date_time_get_day_of_week:
1737 * @datetime: a #GDateTime
1739 * Retrieves the ISO 8601 day of the week on which @datetime falls (1 is
1740 * Monday, 2 is Tuesday... 7 is Sunday).
1742 * Return value: the day of the week
1747 g_date_time_get_day_of_week (GDateTime *datetime)
1749 g_return_val_if_fail (datetime != NULL, 0);
1751 return (datetime->days - 1) % 7 + 1;
1754 /* Day of year getter {{{1 */
1756 * g_date_time_get_day_of_year:
1757 * @datetime: a #GDateTime
1759 * Retrieves the day of the year represented by @datetime in the Gregorian
1762 * Return value: the day of the year
1767 g_date_time_get_day_of_year (GDateTime *datetime)
1771 g_return_val_if_fail (datetime != NULL, 0);
1773 g_date_time_get_week_number (datetime, NULL, NULL, &doy);
1777 /* Time component getters {{{1 */
1780 * g_date_time_get_hour:
1781 * @datetime: a #GDateTime
1783 * Retrieves the hour of the day represented by @datetime
1785 * Return value: the hour of the day
1790 g_date_time_get_hour (GDateTime *datetime)
1792 g_return_val_if_fail (datetime != NULL, 0);
1794 return (datetime->usec / USEC_PER_HOUR);
1798 * g_date_time_get_minute:
1799 * @datetime: a #GDateTime
1801 * Retrieves the minute of the hour represented by @datetime
1803 * Return value: the minute of the hour
1808 g_date_time_get_minute (GDateTime *datetime)
1810 g_return_val_if_fail (datetime != NULL, 0);
1812 return (datetime->usec % USEC_PER_HOUR) / USEC_PER_MINUTE;
1816 * g_date_time_get_second:
1817 * @datetime: a #GDateTime
1819 * Retrieves the second of the minute represented by @datetime
1821 * Return value: the second represented by @datetime
1826 g_date_time_get_second (GDateTime *datetime)
1828 g_return_val_if_fail (datetime != NULL, 0);
1830 return (datetime->usec % USEC_PER_MINUTE) / USEC_PER_SECOND;
1834 * g_date_time_get_microsecond:
1835 * @datetime: a #GDateTime
1837 * Retrieves the microsecond of the date represented by @datetime
1839 * Return value: the microsecond of the second
1844 g_date_time_get_microsecond (GDateTime *datetime)
1846 g_return_val_if_fail (datetime != NULL, 0);
1848 return (datetime->usec % USEC_PER_SECOND);
1852 * g_date_time_get_seconds:
1853 * @datetime: a #GDateTime
1855 * Retrieves the number of seconds since the start of the last minute,
1856 * including the fractional part.
1858 * Returns: the number of seconds
1863 g_date_time_get_seconds (GDateTime *datetime)
1865 g_return_val_if_fail (datetime != NULL, 0);
1867 return (datetime->usec % USEC_PER_MINUTE) / 1000000.0;
1870 /* Exporters {{{1 */
1872 * g_date_time_to_unix:
1873 * @datetime: a #GDateTime
1875 * Gives the Unix time corresponding to @datetime, rounding down to the
1878 * Unix time is the number of seconds that have elapsed since 1970-01-01
1879 * 00:00:00 UTC, regardless of the time zone associated with @datetime.
1881 * Returns: the Unix time corresponding to @datetime
1886 g_date_time_to_unix (GDateTime *datetime)
1888 return INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
1892 * g_date_time_to_timeval:
1893 * @datetime: a #GDateTime
1894 * @tv: a #GTimeVal to modify
1896 * Stores the instant in time that @datetime represents into @tv.
1898 * The time contained in a #GTimeVal is always stored in the form of
1899 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the time
1900 * zone associated with @datetime.
1902 * On systems where 'long' is 32bit (ie: all 32bit systems and all
1903 * Windows systems), a #GTimeVal is incapable of storing the entire
1904 * range of values that #GDateTime is capable of expressing. On those
1905 * systems, this function returns %FALSE to indicate that the time is
1908 * On systems where 'long' is 64bit, this function never fails.
1910 * Returns: %TRUE if successful, else %FALSE
1915 g_date_time_to_timeval (GDateTime *datetime,
1918 tv->tv_sec = INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
1919 tv->tv_usec = datetime->usec % USEC_PER_SECOND;
1924 /* Timezone queries {{{1 */
1926 * g_date_time_get_utc_offset:
1927 * @datetime: a #GDateTime
1929 * Determines the offset to UTC in effect at the time and in the time
1930 * zone of @datetime.
1932 * The offset is the number of microseconds that you add to UTC time to
1933 * arrive at local time for the time zone (ie: negative numbers for time
1934 * zones west of GMT, positive numbers for east).
1936 * If @datetime represents UTC time, then the offset is always zero.
1938 * Returns: the number of microseconds that should be added to UTC to
1939 * get the local time
1944 g_date_time_get_utc_offset (GDateTime *datetime)
1948 g_return_val_if_fail (datetime != NULL, 0);
1950 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
1952 return (gint64) offset * USEC_PER_SECOND;
1956 * g_date_time_get_timezone_abbreviation:
1957 * @datetime: a #GDateTime
1959 * Determines the time zone abbreviation to be used at the time and in
1960 * the time zone of @datetime.
1962 * For example, in Toronto this is currently "EST" during the winter
1963 * months and "EDT" during the summer months when daylight savings
1964 * time is in effect.
1966 * Returns: (transfer none): the time zone abbreviation. The returned
1967 * string is owned by the #GDateTime and it should not be
1973 g_date_time_get_timezone_abbreviation (GDateTime *datetime)
1975 g_return_val_if_fail (datetime != NULL, NULL);
1977 return g_time_zone_get_abbreviation (datetime->tz, datetime->interval);
1981 * g_date_time_is_daylight_savings:
1982 * @datetime: a #GDateTime
1984 * Determines if daylight savings time is in effect at the time and in
1985 * the time zone of @datetime.
1987 * Returns: %TRUE if daylight savings time is in effect
1992 g_date_time_is_daylight_savings (GDateTime *datetime)
1994 g_return_val_if_fail (datetime != NULL, FALSE);
1996 return g_time_zone_is_dst (datetime->tz, datetime->interval);
1999 /* Timezone convert {{{1 */
2001 * g_date_time_to_timezone:
2002 * @datetime: a #GDateTime
2003 * @tz: the new #GTimeZone
2005 * Create a new #GDateTime corresponding to the same instant in time as
2006 * @datetime, but in the time zone @tz.
2008 * This call can fail in the case that the time goes out of bounds. For
2009 * example, converting 0001-01-01 00:00:00 UTC to a time zone west of
2010 * Greenwich will fail (due to the year 0 being out of range).
2012 * You should release the return value by calling g_date_time_unref()
2013 * when you are done with it.
2015 * Returns: a new #GDateTime, or %NULL
2020 g_date_time_to_timezone (GDateTime *datetime,
2023 return g_date_time_from_instant (tz, g_date_time_to_instant (datetime));
2027 * g_date_time_to_local:
2028 * @datetime: a #GDateTime
2030 * Creates a new #GDateTime corresponding to the same instant in time as
2031 * @datetime, but in the local time zone.
2033 * This call is equivalent to calling g_date_time_to_timezone() with the
2034 * time zone returned by g_time_zone_new_local().
2036 * Returns: the newly created #GDateTime
2041 g_date_time_to_local (GDateTime *datetime)
2046 local = g_time_zone_new_local ();
2047 new = g_date_time_to_timezone (datetime, local);
2048 g_time_zone_unref (local);
2054 * g_date_time_to_utc:
2055 * @datetime: a #GDateTime
2057 * Creates a new #GDateTime corresponding to the same instant in time as
2058 * @datetime, but in UTC.
2060 * This call is equivalent to calling g_date_time_to_timezone() with the
2061 * time zone returned by g_time_zone_new_utc().
2063 * Returns: the newly created #GDateTime
2068 g_date_time_to_utc (GDateTime *datetime)
2073 utc = g_time_zone_new_utc ();
2074 new = g_date_time_to_timezone (datetime, utc);
2075 g_time_zone_unref (utc);
2083 format_number (GString *str,
2084 gboolean use_alt_digits,
2089 const gunichar ascii_digits[10] = {
2090 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
2092 const gunichar *digits = ascii_digits;
2096 g_return_if_fail (width <= 10);
2098 #ifdef HAVE_LANGINFO_OUTDIGIT
2101 static gunichar alt_digits[10];
2102 static gsize initialised;
2103 /* 2^32 has 10 digits */
2105 if G_UNLIKELY (g_once_init_enter (&initialised))
2107 #define DO_DIGIT(n) \
2109 union { guint integer; char *pointer; } val; \
2110 val.pointer = nl_langinfo (_NL_CTYPE_OUTDIGIT## n ##_WC); \
2111 alt_digits[n] = val.integer; \
2113 DO_DIGIT(0); DO_DIGIT(1); DO_DIGIT(2); DO_DIGIT(3); DO_DIGIT(4);
2114 DO_DIGIT(5); DO_DIGIT(6); DO_DIGIT(7); DO_DIGIT(8); DO_DIGIT(9);
2116 g_once_init_leave (&initialised, TRUE);
2119 digits = alt_digits;
2121 #endif /* HAVE_LANGINFO_OUTDIGIT */
2125 tmp[i++] = digits[number % 10];
2130 while (pad && i < width)
2131 tmp[i++] = pad == '0' ? digits[0] : pad;
2133 /* should really be impossible */
2137 g_string_append_unichar (str, tmp[--i]);
2141 * g_date_time_format:
2142 * @datetime: A #GDateTime
2143 * @format: a valid UTF-8 string, containing the format for the
2146 * Creates a newly allocated string representing the requested @format.
2148 * The format strings understood by this function are a subset of the
2149 * strftime() format language as specified by C99. The \%D, \%U and \%W
2150 * conversions are not supported, nor is the 'E' modifier. The GNU
2151 * extensions \%k, \%l, \%s and \%P are supported, however, as are the
2152 * '0', '_' and '-' modifiers.
2154 * In contrast to strftime(), this function always produces a UTF-8
2155 * string, regardless of the current locale. Note that the rendering of
2156 * many formats is locale-dependent and may not match the strftime()
2159 * The following format specifiers are supported:
2162 * <varlistentry><term>
2163 * <literal>\%a</literal>:
2164 * </term><listitem><simpara>
2165 * the abbreviated weekday name according to the current locale
2166 * </simpara></listitem></varlistentry>
2167 * <varlistentry><term>
2168 * <literal>\%A</literal>:
2169 * </term><listitem><simpara>
2170 * the full weekday name according to the current locale
2171 * </simpara></listitem></varlistentry>
2172 * <varlistentry><term>
2173 * <literal>\%b</literal>:
2174 * </term><listitem><simpara>
2175 * the abbreviated month name according to the current locale
2176 * </simpara></listitem></varlistentry>
2177 * <varlistentry><term>
2178 * <literal>\%B</literal>:
2179 * </term><listitem><simpara>
2180 * the full month name according to the current locale
2181 * </simpara></listitem></varlistentry>
2182 * <varlistentry><term>
2183 * <literal>\%c</literal>:
2184 * </term><listitem><simpara>
2185 * the preferred date and time representation for the current locale
2186 * </simpara></listitem></varlistentry>
2187 * <varlistentry><term>
2188 * <literal>\%C</literal>:
2189 * </term><listitem><simpara>
2190 * The century number (year/100) as a 2-digit integer (00-99)
2191 * </simpara></listitem></varlistentry>
2192 * <varlistentry><term>
2193 * <literal>\%d</literal>:
2194 * </term><listitem><simpara>
2195 * the day of the month as a decimal number (range 01 to 31)
2196 * </simpara></listitem></varlistentry>
2197 * <varlistentry><term>
2198 * <literal>\%e</literal>:
2199 * </term><listitem><simpara>
2200 * the day of the month as a decimal number (range 1 to 31)
2201 * </simpara></listitem></varlistentry>
2202 * <varlistentry><term>
2203 * <literal>\%F</literal>:
2204 * </term><listitem><simpara>
2205 * equivalent to <literal>\%Y-\%m-\%d</literal> (the ISO 8601 date
2207 * </simpara></listitem></varlistentry>
2208 * <varlistentry><term>
2209 * <literal>\%g</literal>:
2210 * </term><listitem><simpara>
2211 * the last two digits of the ISO 8601 week-based year as a decimal
2212 * number (00-99). This works well with \%V and \%u.
2213 * </simpara></listitem></varlistentry>
2214 * <varlistentry><term>
2215 * <literal>\%G</literal>:
2216 * </term><listitem><simpara>
2217 * the ISO 8601 week-based year as a decimal number. This works well
2219 * </simpara></listitem></varlistentry>
2220 * <varlistentry><term>
2221 * <literal>\%h</literal>:
2222 * </term><listitem><simpara>
2223 * equivalent to <literal>\%b</literal>
2224 * </simpara></listitem></varlistentry>
2225 * <varlistentry><term>
2226 * <literal>\%H</literal>:
2227 * </term><listitem><simpara>
2228 * the hour as a decimal number using a 24-hour clock (range 00 to
2230 * </simpara></listitem></varlistentry>
2231 * <varlistentry><term>
2232 * <literal>\%I</literal>:
2233 * </term><listitem><simpara>
2234 * the hour as a decimal number using a 12-hour clock (range 01 to
2236 * </simpara></listitem></varlistentry>
2237 * <varlistentry><term>
2238 * <literal>\%j</literal>:
2239 * </term><listitem><simpara>
2240 * the day of the year as a decimal number (range 001 to 366)
2241 * </simpara></listitem></varlistentry>
2242 * <varlistentry><term>
2243 * <literal>\%k</literal>:
2244 * </term><listitem><simpara>
2245 * the hour (24-hour clock) as a decimal number (range 0 to 23);
2246 * single digits are preceded by a blank
2247 * </simpara></listitem></varlistentry>
2248 * <varlistentry><term>
2249 * <literal>\%l</literal>:
2250 * </term><listitem><simpara>
2251 * the hour (12-hour clock) as a decimal number (range 1 to 12);
2252 * single digits are preceded by a blank
2253 * </simpara></listitem></varlistentry>
2254 * <varlistentry><term>
2255 * <literal>\%m</literal>:
2256 * </term><listitem><simpara>
2257 * the month as a decimal number (range 01 to 12)
2258 * </simpara></listitem></varlistentry>
2259 * <varlistentry><term>
2260 * <literal>\%M</literal>:
2261 * </term><listitem><simpara>
2262 * the minute as a decimal number (range 00 to 59)
2263 * </simpara></listitem></varlistentry>
2264 * <varlistentry><term>
2265 * <literal>\%p</literal>:
2266 * </term><listitem><simpara>
2267 * either "AM" or "PM" according to the given time value, or the
2268 * corresponding strings for the current locale. Noon is treated as
2269 * "PM" and midnight as "AM".
2270 * </simpara></listitem></varlistentry>
2271 * <varlistentry><term>
2272 * <literal>\%P</literal>:
2273 * </term><listitem><simpara>
2274 * like \%p but lowercase: "am" or "pm" or a corresponding string for
2275 * the current locale
2276 * </simpara></listitem></varlistentry>
2277 * <varlistentry><term>
2278 * <literal>\%r</literal>:
2279 * </term><listitem><simpara>
2280 * the time in a.m. or p.m. notation
2281 * </simpara></listitem></varlistentry>
2282 * <varlistentry><term>
2283 * <literal>\%R</literal>:
2284 * </term><listitem><simpara>
2285 * the time in 24-hour notation (<literal>\%H:\%M</literal>)
2286 * </simpara></listitem></varlistentry>
2287 * <varlistentry><term>
2288 * <literal>\%s</literal>:
2289 * </term><listitem><simpara>
2290 * the number of seconds since the Epoch, that is, since 1970-01-01
2292 * </simpara></listitem></varlistentry>
2293 * <varlistentry><term>
2294 * <literal>\%S</literal>:
2295 * </term><listitem><simpara>
2296 * the second as a decimal number (range 00 to 60)
2297 * </simpara></listitem></varlistentry>
2298 * <varlistentry><term>
2299 * <literal>\%t</literal>:
2300 * </term><listitem><simpara>
2302 * </simpara></listitem></varlistentry>
2303 * <varlistentry><term>
2304 * <literal>\%T</literal>:
2305 * </term><listitem><simpara>
2306 * the time in 24-hour notation with seconds (<literal>\%H:\%M:\%S</literal>)
2307 * </simpara></listitem></varlistentry>
2308 * <varlistentry><term>
2309 * <literal>\%u</literal>:
2310 * </term><listitem><simpara>
2311 * the ISO 8601 standard day of the week as a decimal, range 1 to 7,
2312 * Monday being 1. This works well with \%G and \%V.
2313 * </simpara></listitem></varlistentry>
2314 * <varlistentry><term>
2315 * <literal>\%V</literal>:
2316 * </term><listitem><simpara>
2317 * the ISO 8601 standard week number of the current year as a decimal
2318 * number, range 01 to 53, where week 1 is the first week that has at
2319 * least 4 days in the new year. See g_date_time_get_week_of_year().
2320 * This works well with \%G and \%u.
2321 * </simpara></listitem></varlistentry>
2322 * <varlistentry><term>
2323 * <literal>\%w</literal>:
2324 * </term><listitem><simpara>
2325 * the day of the week as a decimal, range 0 to 6, Sunday being 0.
2326 * This is not the ISO 8601 standard format -- use \%u instead.
2327 * </simpara></listitem></varlistentry>
2328 * <varlistentry><term>
2329 * <literal>\%x</literal>:
2330 * </term><listitem><simpara>
2331 * the preferred date representation for the current locale without
2333 * </simpara></listitem></varlistentry>
2334 * <varlistentry><term>
2335 * <literal>\%X</literal>:
2336 * </term><listitem><simpara>
2337 * the preferred time representation for the current locale without
2339 * </simpara></listitem></varlistentry>
2340 * <varlistentry><term>
2341 * <literal>\%y</literal>:
2342 * </term><listitem><simpara>
2343 * the year as a decimal number without the century
2344 * </simpara></listitem></varlistentry>
2345 * <varlistentry><term>
2346 * <literal>\%Y</literal>:
2347 * </term><listitem><simpara>
2348 * the year as a decimal number including the century
2349 * </simpara></listitem></varlistentry>
2350 * <varlistentry><term>
2351 * <literal>\%z</literal>:
2352 * </term><listitem><simpara>
2353 * the time-zone as hour offset from UTC
2354 * </simpara></listitem></varlistentry>
2355 * <varlistentry><term>
2356 * <literal>\%Z</literal>:
2357 * </term><listitem><simpara>
2358 * the time zone or name or abbreviation
2359 * </simpara></listitem></varlistentry>
2360 * <varlistentry><term>
2361 * <literal>\%\%</literal>:
2362 * </term><listitem><simpara>
2363 * a literal <literal>\%</literal> character
2364 * </simpara></listitem></varlistentry>
2367 * Some conversion specifications can be modified by preceding the
2368 * conversion specifier by one or more modifier characters. The
2369 * following modifiers are supported for many of the numeric
2375 * Use alternative numeric symbols, if the current locale
2382 * Pad a numeric result with spaces.
2383 * This overrides the default padding for the specifier.
2389 * Do not pad a numeric result.
2390 * This overrides the default padding for the specifier.
2396 * Pad a numeric result with zeros.
2397 * This overrides the default padding for the specifier.
2402 * Returns: a newly allocated string formatted to the requested format
2403 * or %NULL in the case that there was an error. The string
2404 * should be freed with g_free().
2409 g_date_time_format (GDateTime *datetime,
2410 const gchar *format)
2415 gboolean in_mod = FALSE;
2416 gboolean alt_digits = FALSE;
2417 gboolean pad_set = FALSE;
2421 g_return_val_if_fail (datetime != NULL, NULL);
2422 g_return_val_if_fail (format != NULL, NULL);
2423 g_return_val_if_fail (g_utf8_validate (format, -1, NULL), NULL);
2425 outstr = g_string_sized_new (strlen (format) * 2);
2428 for (; *format; format = g_utf8_next_char (format))
2430 c = g_utf8_get_char (format);
2449 g_string_append (outstr, WEEKDAY_ABBR (datetime));
2452 g_string_append (outstr, WEEKDAY_FULL (datetime));
2455 g_string_append (outstr, MONTH_ABBR (datetime));
2458 g_string_append (outstr, MONTH_FULL (datetime));
2462 tmp = g_date_time_format (datetime, PREFERRED_DATE_TIME_FMT);
2463 g_string_append (outstr, tmp);
2468 format_number (outstr, alt_digits, pad_set ? pad : '0', 2,
2469 g_date_time_get_year (datetime) / 100);
2472 format_number (outstr, alt_digits, pad_set ? pad : '0', 2,
2473 g_date_time_get_day_of_month (datetime));
2476 format_number (outstr, alt_digits, pad_set ? pad : ' ', 2,
2477 g_date_time_get_day_of_month (datetime));
2480 g_string_append_printf (outstr, "%d-%02d-%02d",
2481 g_date_time_get_year (datetime),
2482 g_date_time_get_month (datetime),
2483 g_date_time_get_day_of_month (datetime));
2486 format_number (outstr, alt_digits, pad_set ? pad : '0', 2,
2487 g_date_time_get_week_numbering_year (datetime) % 100);
2490 format_number (outstr, alt_digits, pad_set ? pad : 0, 0,
2491 g_date_time_get_week_numbering_year (datetime));
2494 g_string_append (outstr, MONTH_ABBR (datetime));
2497 format_number (outstr, alt_digits, pad_set ? pad : '0', 2,
2498 g_date_time_get_hour (datetime));
2501 format_number (outstr, alt_digits, pad_set ? pad : '0', 2,
2502 (g_date_time_get_hour (datetime) + 11) % 12 + 1);
2505 format_number (outstr, alt_digits, pad_set ? pad : '0', 3,
2506 g_date_time_get_day_of_year (datetime));
2509 format_number (outstr, alt_digits, pad_set ? pad : ' ', 2,
2510 g_date_time_get_hour (datetime));
2513 format_number (outstr, alt_digits, pad_set ? pad : ' ', 2,
2514 (g_date_time_get_hour (datetime) + 11) % 12 + 1);
2517 g_string_append_c (outstr, '\n');
2520 format_number (outstr, alt_digits, pad_set ? pad : '0', 2,
2521 g_date_time_get_month (datetime));
2524 format_number (outstr, alt_digits, pad_set ? pad : '0', 2,
2525 g_date_time_get_minute (datetime));
2531 ampm = g_utf8_strup (GET_AMPM (datetime), -1);
2532 g_string_append (outstr, ampm);
2536 ampm = g_utf8_strdown (GET_AMPM (datetime), -1);
2537 g_string_append (outstr, ampm);
2542 tmp = g_date_time_format (datetime, PREFERRED_12HR_TIME_FMT);
2543 g_string_append (outstr, tmp);
2548 g_string_append_printf (outstr, "%02d:%02d",
2549 g_date_time_get_hour (datetime),
2550 g_date_time_get_minute (datetime));
2553 g_string_append_printf (outstr, "%" G_GINT64_FORMAT, g_date_time_to_unix (datetime));
2556 format_number (outstr, alt_digits, pad_set ? pad : '0', 2,
2557 g_date_time_get_second (datetime));
2560 g_string_append_c (outstr, '\t');
2563 g_string_append_printf (outstr, "%02d:%02d:%02d",
2564 g_date_time_get_hour (datetime),
2565 g_date_time_get_minute (datetime),
2566 g_date_time_get_second (datetime));
2569 format_number (outstr, alt_digits, 0, 0,
2570 g_date_time_get_day_of_week (datetime));
2573 format_number (outstr, alt_digits, pad_set ? pad : '0', 2,
2574 g_date_time_get_week_of_year (datetime));
2577 format_number (outstr, alt_digits, 0, 0,
2578 g_date_time_get_day_of_week (datetime) % 7);
2582 tmp = g_date_time_format (datetime, PREFERRED_DATE_FMT);
2583 g_string_append (outstr, tmp);
2589 tmp = g_date_time_format (datetime, PREFERRED_TIME_FMT);
2590 g_string_append (outstr, tmp);
2595 format_number (outstr, alt_digits, pad_set ? pad : '0', 2,
2596 g_date_time_get_year (datetime) % 100);
2599 format_number (outstr, alt_digits, 0, 0,
2600 g_date_time_get_year (datetime));
2603 if (datetime->tz != NULL)
2605 gint64 offset = g_date_time_get_utc_offset (datetime)
2608 g_string_append_printf (outstr, "%+03d%02d",
2609 (int) offset / 3600,
2610 (int) abs(offset) / 60 % 60);
2613 g_string_append (outstr, "+0000");
2616 g_string_append (outstr, g_date_time_get_timezone_abbreviation (datetime));
2619 g_string_append_c (outstr, '%');
2639 g_string_append_unichar (outstr, c);
2644 return g_string_free (outstr, FALSE);
2647 g_string_free (outstr, TRUE);
2653 /* vim:set foldmethod=marker: */