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_FMT nl_langinfo (D_FMT)
176 #define PREFERRED_TIME_FMT nl_langinfo (T_FMT)
178 static const gint weekday_item[2][7] =
180 { ABDAY_2, ABDAY_3, ABDAY_4, ABDAY_5, ABDAY_6, ABDAY_7, ABDAY_1 },
181 { DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7, DAY_1 }
184 static const gint month_item[2][12] =
186 { ABMON_1, ABMON_2, ABMON_3, ABMON_4, ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10, ABMON_11, ABMON_12 },
187 { MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7, MON_8, MON_9, MON_10, MON_11, MON_12 },
190 #define WEEKDAY_ABBR(d) nl_langinfo (weekday_item[0][g_date_time_get_day_of_week (d) - 1])
191 #define WEEKDAY_FULL(d) nl_langinfo (weekday_item[1][g_date_time_get_day_of_week (d) - 1])
192 #define MONTH_ABBR(d) nl_langinfo (month_item[0][g_date_time_get_month (d) - 1])
193 #define MONTH_FULL(d) nl_langinfo (month_item[1][g_date_time_get_month (d) - 1])
197 #define GET_AMPM(d) ((g_date_time_get_hour (d) < 12) \
198 /* Translators: 'before midday' indicator */ \
199 ? C_("GDateTime", "am") \
200 /* Translators: 'after midday' indicator */ \
201 : C_("GDateTime", "pm"))
203 /* Translators: this is the preferred format for expressing the date */
204 #define PREFERRED_DATE_FMT C_("GDateTime", "%m/%d/%y")
206 /* Translators: this is the preferred format for expressing the time */
207 #define PREFERRED_TIME_FMT C_("GDateTime", "%H:%M:%S")
210 #define WEEKDAY_ABBR(d) (get_weekday_name_abbr (g_date_time_get_day_of_week (d)))
211 #define WEEKDAY_FULL(d) (get_weekday_name (g_date_time_get_day_of_week (d)))
212 #define MONTH_ABBR(d) (get_month_name_abbr (g_date_time_get_month (d)))
213 #define MONTH_FULL(d) (get_month_name (g_date_time_get_month (d)))
216 get_month_name (gint month)
221 return C_("full month name", "January");
223 return C_("full month name", "February");
225 return C_("full month name", "March");
227 return C_("full month name", "April");
229 return C_("full month name", "May");
231 return C_("full month name", "June");
233 return C_("full month name", "July");
235 return C_("full month name", "August");
237 return C_("full month name", "September");
239 return C_("full month name", "October");
241 return C_("full month name", "November");
243 return C_("full month name", "December");
246 g_warning ("Invalid month number %d", month);
253 get_month_name_abbr (gint month)
258 return C_("abbreviated month name", "Jan");
260 return C_("abbreviated month name", "Feb");
262 return C_("abbreviated month name", "Mar");
264 return C_("abbreviated month name", "Apr");
266 return C_("abbreviated month name", "May");
268 return C_("abbreviated month name", "Jun");
270 return C_("abbreviated month name", "Jul");
272 return C_("abbreviated month name", "Aug");
274 return C_("abbreviated month name", "Sep");
276 return C_("abbreviated month name", "Oct");
278 return C_("abbreviated month name", "Nov");
280 return C_("abbreviated month name", "Dec");
283 g_warning ("Invalid month number %d", month);
290 get_weekday_name (gint day)
295 return C_("full weekday name", "Monday");
297 return C_("full weekday name", "Tuesday");
299 return C_("full weekday name", "Wednesday");
301 return C_("full weekday name", "Thursday");
303 return C_("full weekday name", "Friday");
305 return C_("full weekday name", "Saturday");
307 return C_("full weekday name", "Sunday");
310 g_warning ("Invalid week day number %d", day);
317 get_weekday_name_abbr (gint day)
322 return C_("abbreviated weekday name", "Mon");
324 return C_("abbreviated weekday name", "Tue");
326 return C_("abbreviated weekday name", "Wed");
328 return C_("abbreviated weekday name", "Thu");
330 return C_("abbreviated weekday name", "Fri");
332 return C_("abbreviated weekday name", "Sat");
334 return C_("abbreviated weekday name", "Sun");
337 g_warning ("Invalid week day number %d", day);
343 #endif /* HAVE_LANGINFO_TIME */
346 ymd_to_days (gint year,
352 days = (year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100)
353 + ((year - 1) / 400);
355 days += days_in_year[0][month - 1];
356 if (GREGORIAN_LEAP (year) && month > 2)
365 g_date_time_get_week_number (GDateTime *datetime,
370 gint a, b, c, d, e, f, g, n, s, month, day, year;
372 g_date_time_get_ymd (datetime, &year, &month, &day);
376 a = g_date_time_get_year (datetime) - 1;
377 b = (a / 4) - (a / 100) + (a / 400);
378 c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
381 f = day - 1 + (31 * (month - 1));
386 b = (a / 4) - (a / 100) + (a / 400);
387 c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
390 f = day + (((153 * (month - 3)) + 2) / 5) + 58 + s;
400 *week_number = 53 - ((g - s) / 5);
401 else if (n > 364 + s)
404 *week_number = (n / 7) + 1;
408 *day_of_week = d + 1;
411 *day_of_year = f + 1;
417 g_date_time_alloc (GTimeZone *tz)
421 datetime = g_slice_new0 (GDateTime);
422 datetime->tz = g_time_zone_ref (tz);
423 datetime->ref_count = 1;
430 * @datetime: a #GDateTime
432 * Atomically increments the reference count of @datetime by one.
434 * Return value: the #GDateTime with the reference count increased
439 g_date_time_ref (GDateTime *datetime)
441 g_return_val_if_fail (datetime != NULL, NULL);
442 g_return_val_if_fail (datetime->ref_count > 0, NULL);
444 g_atomic_int_inc (&datetime->ref_count);
451 * @datetime: a #GDateTime
453 * Atomically decrements the reference count of @datetime by one.
455 * When the reference count reaches zero, the resources allocated by
456 * @datetime are freed
461 g_date_time_unref (GDateTime *datetime)
463 g_return_if_fail (datetime != NULL);
464 g_return_if_fail (datetime->ref_count > 0);
466 if (g_atomic_int_dec_and_test (&datetime->ref_count))
468 g_time_zone_unref (datetime->tz);
469 g_slice_free (GDateTime, datetime);
473 /* Internal state transformers {{{1 */
475 * g_date_time_to_instant:
476 * @datetime: a #GDateTime
478 * Convert a @datetime into an instant.
480 * An instant is a number that uniquely describes a particular
481 * microsecond in time, taking time zone considerations into account.
482 * (ie: "03:00 -0400" is the same instant as "02:00 -0500").
484 * An instant is always positive but we use a signed return value to
485 * avoid troubles with C.
488 g_date_time_to_instant (GDateTime *datetime)
492 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
493 offset *= USEC_PER_SECOND;
495 return datetime->days * USEC_PER_DAY + datetime->usec - offset;
499 * g_date_time_from_instant:
501 * @instant: a instant in time
503 * Creates a #GDateTime from a time zone and an instant.
505 * This might fail if the time ends up being out of range.
508 g_date_time_from_instant (GTimeZone *tz,
514 if (instant < 0 || instant > G_GINT64_CONSTANT (1000000000000000000))
517 datetime = g_date_time_alloc (tz);
518 datetime->interval = g_time_zone_find_interval (tz,
519 G_TIME_TYPE_UNIVERSAL,
520 INSTANT_TO_UNIX (instant));
521 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
522 offset *= USEC_PER_SECOND;
526 datetime->days = instant / USEC_PER_DAY;
527 datetime->usec = instant % USEC_PER_DAY;
529 if (datetime->days < 1 || 3652059 < datetime->days)
531 g_date_time_unref (datetime);
540 * g_date_time_deal_with_date_change:
541 * @datetime: a #GDateTime
543 * This function should be called whenever the date changes by adding
544 * days, months or years. It does three things.
546 * First, we ensure that the date falls between 0001-01-01 and
547 * 9999-12-31 and return %FALSE if it does not.
549 * Next we update the ->interval field.
551 * Finally, we ensure that the resulting date and time pair exists (by
552 * ensuring that our time zone has an interval containing it) and
553 * adjusting as required. For example, if we have the time 02:30:00 on
554 * March 13 2010 in Toronto and we add 1 day to it, we would end up with
555 * 2:30am on March 14th, which doesn't exist. In that case, we bump the
559 g_date_time_deal_with_date_change (GDateTime *datetime)
565 if (datetime->days < 1 || datetime->days > 3652059)
568 was_dst = g_time_zone_is_dst (datetime->tz, datetime->interval);
570 full_time = datetime->days * USEC_PER_DAY + datetime->usec;
573 usec = full_time % USEC_PER_SECOND;
574 full_time /= USEC_PER_SECOND;
575 full_time -= UNIX_EPOCH_START * SEC_PER_DAY;
577 datetime->interval = g_time_zone_adjust_time (datetime->tz,
580 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
581 full_time *= USEC_PER_SECOND;
584 datetime->days = full_time / USEC_PER_DAY;
585 datetime->usec = full_time % USEC_PER_DAY;
587 /* maybe daylight time caused us to shift to a different day,
588 * but it definitely didn't push us into a different year */
593 g_date_time_replace_days (GDateTime *datetime,
598 new = g_date_time_alloc (datetime->tz);
599 new->interval = datetime->interval;
600 new->usec = datetime->usec;
603 if (!g_date_time_deal_with_date_change (new))
605 g_date_time_unref (new);
612 /* now/unix/timeval Constructors {{{1 */
615 * g_date_time_new_from_timeval:
619 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
620 * given time zone @tz.
622 * The time contained in a #GTimeVal is always stored in the form of
623 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
626 * This call can fail (returning %NULL) if @tv represents a time outside
627 * of the supported range of #GDateTime.
629 * You should release the return value by calling g_date_time_unref()
630 * when you are done with it.
632 * Returns: a new #GDateTime, or %NULL
637 g_date_time_new_from_timeval (GTimeZone *tz,
640 return g_date_time_from_instant (tz, tv->tv_usec +
641 UNIX_TO_INSTANT (tv->tv_sec));
645 * g_date_time_new_from_unix:
649 * Creates a #GDateTime corresponding to the given Unix time @t in the
650 * given time zone @tz.
652 * Unix time is the number of seconds that have elapsed since 1970-01-01
653 * 00:00:00 UTC, regardless of the time zone given.
655 * This call can fail (returning %NULL) if @t represents a time outside
656 * of the supported range of #GDateTime.
658 * You should release the return value by calling g_date_time_unref()
659 * when you are done with it.
661 * Returns: a new #GDateTime, or %NULL
666 g_date_time_new_from_unix (GTimeZone *tz,
669 return g_date_time_from_instant (tz, UNIX_TO_INSTANT (secs));
673 * g_date_time_new_now:
676 * Creates a #GDateTime corresponding to this exact instant in the given
677 * time zone @tz. The time is as accurate as the system allows, to a
678 * maximum accuracy of 1 microsecond.
680 * This function will always succeed unless the system clock is set to
681 * truly insane values (or unless GLib is still being used after the
684 * You should release the return value by calling g_date_time_unref()
685 * when you are done with it.
687 * Returns: a new #GDateTime, or %NULL
692 g_date_time_new_now (GTimeZone *tz)
696 g_get_current_time (&tv);
698 return g_date_time_new_from_timeval (tz, &tv);
702 * g_date_time_new_now_local:
704 * Creates a #GDateTime corresponding to this exact instant in the local
707 * This is equivalent to calling g_date_time_new_now() with the time
708 * zone returned by g_time_zone_new_local().
710 * Returns: a new #GDateTime, or %NULL
715 g_date_time_new_now_local (void)
720 local = g_time_zone_new_local ();
721 datetime = g_date_time_new_now (local);
722 g_time_zone_unref (local);
728 * g_date_time_new_now_utc:
730 * Creates a #GDateTime corresponding to this exact instant in UTC.
732 * This is equivalent to calling g_date_time_new_now() with the time
733 * zone returned by g_time_zone_new_utc().
735 * Returns: a new #GDateTime, or %NULL
740 g_date_time_new_now_utc (void)
745 utc = g_time_zone_new_utc ();
746 datetime = g_date_time_new_now (utc);
747 g_time_zone_unref (utc);
753 * g_date_time_new_from_unix_local:
756 * Creates a #GDateTime corresponding to the given Unix time @t in the
759 * Unix time is the number of seconds that have elapsed since 1970-01-01
760 * 00:00:00 UTC, regardless of the local time offset.
762 * This call can fail (returning %NULL) if @t represents a time outside
763 * of the supported range of #GDateTime.
765 * You should release the return value by calling g_date_time_unref()
766 * when you are done with it.
768 * Returns: a new #GDateTime, or %NULL
773 g_date_time_new_from_unix_local (gint64 t)
778 local = g_time_zone_new_local ();
779 datetime = g_date_time_new_from_unix (local, t);
780 g_time_zone_unref (local);
786 * g_date_time_new_from_unix_utc:
789 * Creates a #GDateTime corresponding to the given Unix time @t in UTC.
791 * Unix time is the number of seconds that have elapsed since 1970-01-01
794 * This call can fail (returning %NULL) if @t represents a time outside
795 * of the supported range of #GDateTime.
797 * You should release the return value by calling g_date_time_unref()
798 * when you are done with it.
800 * Returns: a new #GDateTime, or %NULL
805 g_date_time_new_from_unix_utc (gint64 t)
810 utc = g_time_zone_new_utc ();
811 datetime = g_date_time_new_from_unix (utc, t);
812 g_time_zone_unref (utc);
818 * g_date_time_new_from_timeval_local:
821 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
824 * The time contained in a #GTimeVal is always stored in the form of
825 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
828 * This call can fail (returning %NULL) if @tv represents a time outside
829 * of the supported range of #GDateTime.
831 * You should release the return value by calling g_date_time_unref()
832 * when you are done with it.
834 * Returns: a new #GDateTime, or %NULL
839 g_date_time_new_from_timeval_local (const GTimeVal *tv)
844 local = g_time_zone_new_local ();
845 datetime = g_date_time_new_from_timeval (local, tv);
846 g_time_zone_unref (local);
852 * g_date_time_new_from_timeval_utc:
855 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in UTC.
857 * The time contained in a #GTimeVal is always stored in the form of
858 * seconds elapsed since 1970-01-01 00:00:00 UTC.
860 * This call can fail (returning %NULL) if @tv represents a time outside
861 * of the supported range of #GDateTime.
863 * You should release the return value by calling g_date_time_unref()
864 * when you are done with it.
866 * Returns: a new #GDateTime, or %NULL
871 g_date_time_new_from_timeval_utc (const GTimeVal *tv)
876 utc = g_time_zone_new_utc ();
877 datetime = g_date_time_new_from_timeval (utc, tv);
878 g_time_zone_unref (utc);
883 /* full new functions {{{1 */
888 * @year: the year component of the date
889 * @month: the month component of the date
890 * @day: the day component of the date
891 * @hour: the hour component of the date
892 * @minute: the minute component of the date
893 * @seconds: the number of seconds past the minute
895 * Creates a new #GDateTime corresponding to the given date and time in
898 * The @year must be between 1 and 9999, @month between 1 and 12 and @day
899 * between 1 and 28, 29, 30 or 31 depending on the month and the year.
901 * @hour must be between 0 and 23 and @minute must be between 0 and 59.
903 * @seconds must be at least 0.0 and must be strictly less than 60.0.
904 * It will be rounded down to the nearest microsecond.
906 * If the given time is not representable in the given time zone (for
907 * example, 02:30 on March 14th 2010 in Toronto, due to daylight savings
908 * time) then the time will be rounded up to the nearest existing time
909 * (in this case, 03:00). If this matters to you then you should verify
910 * the return value for containing the same as the numbers you gave.
912 * In the case that the given time is ambiguous in the given time zone
913 * (for example, 01:30 on November 7th 2010 in Toronto, due to daylight
914 * savings time) then the time falling within standard (ie:
915 * non-daylight) time is taken.
917 * It not considered a programmer error for the values to this function
918 * to be out of range, but in the case that they are, the function will
921 * You should release the return value by calling g_date_time_unref()
922 * when you are done with it.
924 * Returns: a new #GDateTime, or %NULL
929 g_date_time_new (GTimeZone *tz,
940 datetime = g_date_time_alloc (tz);
941 datetime->days = ymd_to_days (year, month, day);
942 datetime->usec = (hour * USEC_PER_HOUR)
943 + (minute * USEC_PER_MINUTE)
944 + (gint64) (seconds * USEC_PER_SECOND);
946 full_time = SEC_PER_DAY *
947 (ymd_to_days (year, month, day) - UNIX_EPOCH_START) +
948 SECS_PER_HOUR * hour +
949 SECS_PER_MINUTE * minute +
952 datetime->interval = g_time_zone_adjust_time (datetime->tz,
953 G_TIME_TYPE_STANDARD,
956 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
957 datetime->days = full_time / SEC_PER_DAY;
958 datetime->usec = (full_time % SEC_PER_DAY) * USEC_PER_SECOND;
959 datetime->usec += ((int) (seconds * USEC_PER_SECOND)) % USEC_PER_SECOND;
965 * g_date_time_new_local:
966 * @year: the year component of the date
967 * @month: the month component of the date
968 * @day: the day component of the date
969 * @hour: the hour component of the date
970 * @minute: the minute component of the date
971 * @seconds: the number of seconds past the minute
973 * Creates a new #GDateTime corresponding to the given date and time in
974 * the local time zone.
976 * This call is equivalent to calling g_date_time_new() with the time
977 * zone returned by g_time_zone_new_local().
979 * Returns: a #GDateTime, or %NULL
984 g_date_time_new_local (gint year,
994 local = g_time_zone_new_local ();
995 datetime = g_date_time_new (local, year, month, day, hour, minute, seconds);
996 g_time_zone_unref (local);
1002 * g_date_time_new_utc:
1003 * @year: the year component of the date
1004 * @month: the month component of the date
1005 * @day: the day component of the date
1006 * @hour: the hour component of the date
1007 * @minute: the minute component of the date
1008 * @seconds: the number of seconds past the minute
1010 * Creates a new #GDateTime corresponding to the given date and time in
1013 * This call is equivalent to calling g_date_time_new() with the time
1014 * zone returned by g_time_zone_new_utc().
1016 * Returns: a #GDateTime, or %NULL
1021 g_date_time_new_utc (gint year,
1028 GDateTime *datetime;
1031 utc = g_time_zone_new_utc ();
1032 datetime = g_date_time_new (utc, year, month, day, hour, minute, seconds);
1033 g_time_zone_unref (utc);
1042 * @datetime: a #GDateTime
1043 * @timespan: a #GTimeSpan
1045 * Creates a copy of @datetime and adds the specified timespan to the copy.
1047 * Return value: the newly created #GDateTime which should be freed with
1048 * g_date_time_unref().
1053 g_date_time_add (GDateTime *datetime,
1056 return g_date_time_from_instant (datetime->tz, timespan +
1057 g_date_time_to_instant (datetime));
1061 * g_date_time_add_years:
1062 * @datetime: a #GDateTime
1063 * @years: the number of years
1065 * Creates a copy of @datetime and adds the specified number of years to the
1068 * Return value: the newly created #GDateTime which should be freed with
1069 * g_date_time_unref().
1074 g_date_time_add_years (GDateTime *datetime,
1077 gint year, month, day;
1079 g_return_val_if_fail (datetime != NULL, NULL);
1081 if (years < -10000 || years > 10000)
1084 g_date_time_get_ymd (datetime, &year, &month, &day);
1087 /* only possible issue is if we've entered a year with no February 29
1089 if (month == 2 && day == 29 && !GREGORIAN_LEAP (year))
1092 return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
1096 * g_date_time_add_months:
1097 * @datetime: a #GDateTime
1098 * @months: the number of months
1100 * Creates a copy of @datetime and adds the specified number of months to the
1103 * Return value: the newly created #GDateTime which should be freed with
1104 * g_date_time_unref().
1109 g_date_time_add_months (GDateTime *datetime,
1112 gint year, month, day;
1114 g_return_val_if_fail (datetime != NULL, NULL);
1115 g_date_time_get_ymd (datetime, &year, &month, &day);
1117 if (months < -120000 || months > 120000)
1120 year += months / 12;
1121 month += months % 12;
1127 else if (month > 12)
1133 day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
1135 return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
1139 * g_date_time_add_weeks:
1140 * @datetime: a #GDateTime
1141 * @weeks: the number of weeks
1143 * Creates a copy of @datetime and adds the specified number of weeks to the
1146 * Return value: the newly created #GDateTime which should be freed with
1147 * g_date_time_unref().
1152 g_date_time_add_weeks (GDateTime *datetime,
1155 g_return_val_if_fail (datetime != NULL, NULL);
1157 return g_date_time_add_days (datetime, weeks * 7);
1161 * g_date_time_add_days:
1162 * @datetime: a #GDateTime
1163 * @days: the number of days
1165 * Creates a copy of @datetime and adds the specified number of days to the
1168 * Return value: the newly created #GDateTime which should be freed with
1169 * g_date_time_unref().
1174 g_date_time_add_days (GDateTime *datetime,
1177 g_return_val_if_fail (datetime != NULL, NULL);
1179 if (days < -3660000 || days > 3660000)
1182 return g_date_time_replace_days (datetime, datetime->days + days);
1186 * g_date_time_add_hours:
1187 * @datetime: a #GDateTime
1188 * @hours: the number of hours to add
1190 * Creates a copy of @datetime and adds the specified number of hours
1192 * Return value: the newly created #GDateTime which should be freed with
1193 * g_date_time_unref().
1198 g_date_time_add_hours (GDateTime *datetime,
1201 return g_date_time_add (datetime, hours * USEC_PER_HOUR);
1205 * g_date_time_add_minutes:
1206 * @datetime: a #GDateTime
1207 * @minutes: the number of minutes to add
1209 * Creates a copy of @datetime adding the specified number of minutes.
1211 * Return value: the newly created #GDateTime which should be freed with
1212 * g_date_time_unref().
1217 g_date_time_add_minutes (GDateTime *datetime,
1220 return g_date_time_add (datetime, minutes * USEC_PER_MINUTE);
1225 * g_date_time_add_seconds:
1226 * @datetime: a #GDateTime
1227 * @seconds: the number of seconds to add
1229 * Creates a copy of @datetime and adds the specified number of seconds.
1231 * Return value: the newly created #GDateTime which should be freed with
1232 * g_date_time_unref().
1237 g_date_time_add_seconds (GDateTime *datetime,
1240 return g_date_time_add (datetime, seconds * USEC_PER_SECOND);
1244 * g_date_time_add_full:
1245 * @datetime: a #GDateTime
1246 * @years: the number of years to add
1247 * @months: the number of months to add
1248 * @days: the number of days to add
1249 * @hours: the number of hours to add
1250 * @minutes: the number of minutes to add
1251 * @seconds: the number of seconds to add
1253 * Creates a new #GDateTime adding the specified values to the current date and
1254 * time in @datetime.
1256 * Return value: the newly created #GDateTime that should be freed with
1257 * g_date_time_unref().
1262 g_date_time_add_full (GDateTime *datetime,
1270 gint year, month, day;
1275 g_return_val_if_fail (datetime != NULL, NULL);
1276 g_date_time_get_ymd (datetime, &year, &month, &day);
1278 months += years * 12;
1280 if (months < -120000 || months > 120000)
1283 if (days < -3660000 || days > 3660000)
1286 year += months / 12;
1287 month += months % 12;
1293 else if (month > 12)
1299 day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
1301 /* full_time is now in unix (local) time */
1302 full_time = datetime->usec / USEC_PER_SECOND + SEC_PER_DAY *
1303 (ymd_to_days (year, month, day) + days - UNIX_EPOCH_START);
1305 interval = g_time_zone_adjust_time (datetime->tz,
1306 g_time_zone_is_dst (datetime->tz,
1307 datetime->interval),
1310 /* move to UTC unix time */
1311 full_time -= g_time_zone_get_offset (datetime->tz, interval);
1313 /* convert back to an instant, add back fractional seconds */
1314 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
1315 full_time = full_time * USEC_PER_SECOND +
1316 datetime->usec % USEC_PER_SECOND;
1318 /* do the actual addition now */
1319 full_time += (hours * USEC_PER_HOUR) +
1320 (minutes * USEC_PER_MINUTE) +
1321 (gint64) (seconds * USEC_PER_SECOND);
1323 /* find the new interval */
1324 interval = g_time_zone_find_interval (datetime->tz,
1325 G_TIME_TYPE_UNIVERSAL,
1326 INSTANT_TO_UNIX (full_time));
1328 /* convert back into local time */
1329 full_time += USEC_PER_SECOND *
1330 g_time_zone_get_offset (datetime->tz, interval);
1332 /* split into days and usec of a new datetime */
1333 new = g_date_time_alloc (datetime->tz);
1334 new->interval = interval;
1335 new->days = full_time / USEC_PER_DAY;
1336 new->usec = full_time % USEC_PER_DAY;
1343 /* Compare, difference, hash, equal {{{1 */
1345 * g_date_time_compare:
1346 * @dt1: first #GDateTime to compare
1347 * @dt2: second #GDateTime to compare
1349 * A comparison function for #GDateTimes that is suitable
1350 * as a #GCompareFunc. Both #GDateTimes must be non-%NULL.
1352 * Return value: -1, 0 or 1 if @dt1 is less than, equal to or greater
1358 g_date_time_compare (gconstpointer dt1,
1363 difference = g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2);
1368 else if (difference > 0)
1376 * g_date_time_difference:
1377 * @end: a #GDateTime
1378 * @begin: a #GDateTime
1380 * Calculates the difference in time between @end and @begin. The
1381 * #GTimeSpan that is returned is effectively @end - @begin (ie:
1382 * positive if the first simparameter is larger).
1384 * Return value: the difference between the two #GDateTime, as a time
1385 * span expressed in microseconds.
1390 g_date_time_difference (GDateTime *end,
1393 g_return_val_if_fail (begin != NULL, 0);
1394 g_return_val_if_fail (end != NULL, 0);
1396 return g_date_time_to_instant (end) -
1397 g_date_time_to_instant (begin);
1402 * @datetime: a #GDateTime
1404 * Hashes @datetime into a #guint, suitable for use within #GHashTable.
1406 * Return value: a #guint containing the hash
1411 g_date_time_hash (gconstpointer datetime)
1413 return g_date_time_to_instant ((GDateTime *) datetime);
1417 * g_date_time_equal:
1418 * @dt1: a #GDateTime
1419 * @dt2: a #GDateTime
1421 * Checks to see if @dt1 and @dt2 are equal.
1423 * Equal here means that they represent the same moment after converting
1424 * them to the same time zone.
1426 * Return value: %TRUE if @dt1 and @dt2 are equal
1431 g_date_time_equal (gconstpointer dt1,
1434 return g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2) == 0;
1437 /* Year, Month, Day Getters {{{1 */
1439 * g_date_time_get_ymd:
1440 * @datetime: a #GDateTime.
1441 * @year: (out): the return location for the gregorian year, or %NULL.
1442 * @month: (out): the return location for the month of the year, or %NULL.
1443 * @day: (out): the return location for the day of the month, or %NULL.
1445 * Retrieves the Gregorian day, month, and year of a given #GDateTime.
1450 g_date_time_get_ymd (GDateTime *datetime,
1458 gint remaining_days;
1465 g_return_if_fail (datetime != NULL);
1467 remaining_days = datetime->days;
1470 * We need to convert an offset in days to its year/month/day representation.
1471 * Leap years makes this a little trickier than it should be, so we use
1472 * 400, 100 and 4 years cycles here to get to the correct year.
1475 /* Our days offset starts sets 0001-01-01 as day 1, if it was day 0 our
1476 * math would be simpler, so let's do it */
1479 the_year = (remaining_days / DAYS_IN_400YEARS) * 400 + 1;
1480 remaining_days = remaining_days % DAYS_IN_400YEARS;
1482 y100_cycles = remaining_days / DAYS_IN_100YEARS;
1483 remaining_days = remaining_days % DAYS_IN_100YEARS;
1484 the_year += y100_cycles * 100;
1486 y4_cycles = remaining_days / DAYS_IN_4YEARS;
1487 remaining_days = remaining_days % DAYS_IN_4YEARS;
1488 the_year += y4_cycles * 4;
1490 y1_cycles = remaining_days / 365;
1491 the_year += y1_cycles;
1492 remaining_days = remaining_days % 365;
1494 if (y1_cycles == 4 || y100_cycles == 4) {
1495 g_assert (remaining_days == 0);
1497 /* special case that indicates that the date is actually one year before,
1498 * in the 31th of December */
1505 /* now get the month and the day */
1506 leap = y1_cycles == 3 && (y4_cycles != 24 || y100_cycles == 3);
1508 g_assert (leap == GREGORIAN_LEAP(the_year));
1510 the_month = (remaining_days + 50) >> 5;
1511 preceding = (days_in_year[0][the_month - 1] + (the_month > 2 && leap));
1512 if (preceding > remaining_days)
1514 /* estimate is too large */
1516 preceding -= leap ? days_in_months[1][the_month]
1517 : days_in_months[0][the_month];
1520 remaining_days -= preceding;
1521 g_assert(0 <= remaining_days);
1523 the_day = remaining_days + 1;
1535 * g_date_time_get_year:
1536 * @datetime: A #GDateTime
1538 * Retrieves the year represented by @datetime in the Gregorian calendar.
1540 * Return value: the year represented by @datetime
1545 g_date_time_get_year (GDateTime *datetime)
1549 g_return_val_if_fail (datetime != NULL, 0);
1551 g_date_time_get_ymd (datetime, &year, NULL, NULL);
1557 * g_date_time_get_month:
1558 * @datetime: a #GDateTime
1560 * Retrieves the month of the year represented by @datetime in the Gregorian
1563 * Return value: the month represented by @datetime
1568 g_date_time_get_month (GDateTime *datetime)
1572 g_return_val_if_fail (datetime != NULL, 0);
1574 g_date_time_get_ymd (datetime, NULL, &month, NULL);
1580 * g_date_time_get_day_of_month:
1581 * @datetime: a #GDateTime
1583 * Retrieves the day of the month represented by @datetime in the gregorian
1586 * Return value: the day of the month
1591 g_date_time_get_day_of_month (GDateTime *datetime)
1595 const guint16 *days;
1598 g_return_val_if_fail (datetime != NULL, 0);
1600 days = days_in_year[GREGORIAN_LEAP (g_date_time_get_year (datetime)) ? 1 : 0];
1601 g_date_time_get_week_number (datetime, NULL, NULL, &day_of_year);
1603 for (i = 1; i <= 12; i++)
1605 if (days [i] >= day_of_year)
1606 return day_of_year - last;
1610 g_warn_if_reached ();
1614 /* Week of year / day of week getters {{{1 */
1616 * g_date_time_get_week_numbering_year:
1617 * @datetime: a #GDateTime
1619 * Returns the ISO 8601 week-numbering year in which the week containing
1622 * This function, taken together with g_date_time_get_week_of_year() and
1623 * g_date_time_get_day_of_week() can be used to determine the full ISO
1624 * week date on which @datetime falls.
1626 * This is usually equal to the normal Gregorian year (as returned by
1627 * g_date_time_get_year()), except as detailed below:
1629 * For Thursday, the week-numbering year is always equal to the usual
1630 * calendar year. For other days, the number is such that every day
1631 * within a complete week (Monday to Sunday) is contained within the
1632 * same week-numbering year.
1634 * For Monday, Tuesday and Wednesday occurring near the end of the year,
1635 * this may mean that the week-numbering year is one greater than the
1636 * calendar year (so that these days have the same week-numbering year
1637 * as the Thursday occurring early in the next year).
1639 * For Friday, Saturaday and Sunday occurring near the start of the year,
1640 * this may mean that the week-numbering year is one less than the
1641 * calendar year (so that these days have the same week-numbering year
1642 * as the Thursday occurring late in the previous year).
1644 * An equivalent description is that the week-numbering year is equal to
1645 * the calendar year containing the majority of the days in the current
1646 * week (Monday to Sunday).
1648 * Note that January 1 0001 in the proleptic Gregorian calendar is a
1649 * Monday, so this function never returns 0.
1651 * Returns: the ISO 8601 week-numbering year for @datetime
1656 g_date_time_get_week_numbering_year (GDateTime *datetime)
1658 gint year, month, day, weekday;
1660 g_date_time_get_ymd (datetime, &year, &month, &day);
1661 weekday = g_date_time_get_day_of_week (datetime);
1663 /* January 1, 2, 3 might be in the previous year if they occur after
1666 * Jan 1: Friday, Saturday, Sunday => day 1: weekday 5, 6, 7
1667 * Jan 2: Saturday, Sunday => day 2: weekday 6, 7
1668 * Jan 3: Sunday => day 3: weekday 7
1670 * So we have a special case if (day - weekday) <= -4
1672 if (month == 1 && (day - weekday) <= -4)
1675 /* December 29, 30, 31 might be in the next year if they occur before
1678 * Dec 31: Monday, Tuesday, Wednesday => day 31: weekday 1, 2, 3
1679 * Dec 30: Monday, Tuesday => day 30: weekday 1, 2
1680 * Dec 29: Monday => day 29: weekday 1
1682 * So we have a special case if (day - weekday) >= 28
1684 else if (month == 12 && (day - weekday) >= 28)
1692 * g_date_time_get_week_of_year:
1693 * @datetime: a #GDateTime
1695 * Returns the ISO 8601 week number for the week containing @datetime.
1696 * The ISO 8601 week number is the same for every day of the week (from
1697 * Moday through Sunday). That can produce some unusual results
1698 * (described below).
1700 * The first week of the year is week 1. This is the week that contains
1701 * the first Thursday of the year. Equivalently, this is the first week
1702 * that has more than 4 of its days falling within the calendar year.
1704 * The value 0 is never returned by this function. Days contained
1705 * within a year but occurring before the first ISO 8601 week of that
1706 * year are considered as being contained in the last week of the
1707 * previous year. Similarly, the final days of a calendar year may be
1708 * considered as being part of the first ISO 8601 week of the next year
1709 * if 4 or more days of that week are contained within the new year.
1711 * Returns: the ISO 8601 week number for @datetime.
1716 g_date_time_get_week_of_year (GDateTime *datetime)
1720 g_return_val_if_fail (datetime != NULL, 0);
1722 g_date_time_get_week_number (datetime, &weeknum, NULL, NULL);
1728 * g_date_time_get_day_of_week:
1729 * @datetime: a #GDateTime
1731 * Retrieves the ISO 8601 day of the week on which @datetime falls (1 is
1732 * Monday, 2 is Tuesday... 7 is Sunday).
1734 * Return value: the day of the week
1739 g_date_time_get_day_of_week (GDateTime *datetime)
1741 g_return_val_if_fail (datetime != NULL, 0);
1743 return (datetime->days - 1) % 7 + 1;
1746 /* Day of year getter {{{1 */
1748 * g_date_time_get_day_of_year:
1749 * @datetime: a #GDateTime
1751 * Retrieves the day of the year represented by @datetime in the Gregorian
1754 * Return value: the day of the year
1759 g_date_time_get_day_of_year (GDateTime *datetime)
1763 g_return_val_if_fail (datetime != NULL, 0);
1765 g_date_time_get_week_number (datetime, NULL, NULL, &doy);
1769 /* Time component getters {{{1 */
1772 * g_date_time_get_hour:
1773 * @datetime: a #GDateTime
1775 * Retrieves the hour of the day represented by @datetime
1777 * Return value: the hour of the day
1782 g_date_time_get_hour (GDateTime *datetime)
1784 g_return_val_if_fail (datetime != NULL, 0);
1786 return (datetime->usec / USEC_PER_HOUR);
1790 * g_date_time_get_minute:
1791 * @datetime: a #GDateTime
1793 * Retrieves the minute of the hour represented by @datetime
1795 * Return value: the minute of the hour
1800 g_date_time_get_minute (GDateTime *datetime)
1802 g_return_val_if_fail (datetime != NULL, 0);
1804 return (datetime->usec % USEC_PER_HOUR) / USEC_PER_MINUTE;
1808 * g_date_time_get_second:
1809 * @datetime: a #GDateTime
1811 * Retrieves the second of the minute represented by @datetime
1813 * Return value: the second represented by @datetime
1818 g_date_time_get_second (GDateTime *datetime)
1820 g_return_val_if_fail (datetime != NULL, 0);
1822 return (datetime->usec % USEC_PER_MINUTE) / USEC_PER_SECOND;
1826 * g_date_time_get_microsecond:
1827 * @datetime: a #GDateTime
1829 * Retrieves the microsecond of the date represented by @datetime
1831 * Return value: the microsecond of the second
1836 g_date_time_get_microsecond (GDateTime *datetime)
1838 g_return_val_if_fail (datetime != NULL, 0);
1840 return (datetime->usec % USEC_PER_SECOND);
1844 * g_date_time_get_seconds:
1845 * @datetime: a #GDateTime
1847 * Retrieves the number of seconds since the start of the last minute,
1848 * including the fractional part.
1850 * Returns: the number of seconds
1855 g_date_time_get_seconds (GDateTime *datetime)
1857 g_return_val_if_fail (datetime != NULL, 0);
1859 return (datetime->usec % USEC_PER_MINUTE) / 1000000.0;
1862 /* Exporters {{{1 */
1864 * g_date_time_to_unix:
1865 * @datetime: a #GDateTime
1867 * Gives the Unix time corresponding to @datetime, rounding down to the
1870 * Unix time is the number of seconds that have elapsed since 1970-01-01
1871 * 00:00:00 UTC, regardless of the time zone associated with @datetime.
1873 * Returns: the Unix time corresponding to @datetime
1878 g_date_time_to_unix (GDateTime *datetime)
1880 return INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
1884 * g_date_time_to_timeval:
1885 * @datetime: a #GDateTime
1886 * @tv: a #GTimeVal to modify
1888 * Stores the instant in time that @datetime represents into @tv.
1890 * The time contained in a #GTimeVal is always stored in the form of
1891 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the time
1892 * zone associated with @datetime.
1894 * On systems where 'long' is 32bit (ie: all 32bit systems and all
1895 * Windows systems), a #GTimeVal is incapable of storing the entire
1896 * range of values that #GDateTime is capable of expressing. On those
1897 * systems, this function returns %FALSE to indicate that the time is
1900 * On systems where 'long' is 64bit, this function never fails.
1902 * Returns: %TRUE if successful, else %FALSE
1907 g_date_time_to_timeval (GDateTime *datetime,
1910 tv->tv_sec = INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
1911 tv->tv_usec = datetime->usec % USEC_PER_SECOND;
1916 /* Timezone queries {{{1 */
1918 * g_date_time_get_utc_offset:
1919 * @datetime: a #GDateTime
1921 * Determines the offset to UTC in effect at the time and in the time
1922 * zone of @datetime.
1924 * The offset is the number of microseconds that you add to UTC time to
1925 * arrive at local time for the time zone (ie: negative numbers for time
1926 * zones west of GMT, positive numbers for east).
1928 * If @datetime represents UTC time, then the offset is always zero.
1930 * Returns: the number of microseconds that should be added to UTC to
1931 * get the local time
1936 g_date_time_get_utc_offset (GDateTime *datetime)
1940 g_return_val_if_fail (datetime != NULL, 0);
1942 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
1944 return (gint64) offset * USEC_PER_SECOND;
1948 * g_date_time_get_timezone_abbreviation:
1949 * @datetime: a #GDateTime
1951 * Determines the time zone abbreviation to be used at the time and in
1952 * the time zone of @datetime.
1954 * For example, in Toronto this is currently "EST" during the winter
1955 * months and "EDT" during the summer months when daylight savings
1956 * time is in effect.
1958 * Returns: (transfer none): the time zone abbreviation. The returned
1959 * string is owned by the #GDateTime and it should not be
1965 g_date_time_get_timezone_abbreviation (GDateTime *datetime)
1967 g_return_val_if_fail (datetime != NULL, NULL);
1969 return g_time_zone_get_abbreviation (datetime->tz, datetime->interval);
1973 * g_date_time_is_daylight_savings:
1974 * @datetime: a #GDateTime
1976 * Determines if daylight savings time is in effect at the time and in
1977 * the time zone of @datetime.
1979 * Returns: %TRUE if daylight savings time is in effect
1984 g_date_time_is_daylight_savings (GDateTime *datetime)
1986 g_return_val_if_fail (datetime != NULL, FALSE);
1988 return g_time_zone_is_dst (datetime->tz, datetime->interval);
1991 /* Timezone convert {{{1 */
1993 * g_date_time_to_timezone:
1994 * @datetime: a #GDateTime
1995 * @tz: the new #GTimeZone
1997 * Create a new #GDateTime corresponding to the same instant in time as
1998 * @datetime, but in the time zone @tz.
2000 * This call can fail in the case that the time goes out of bounds. For
2001 * example, converting 0001-01-01 00:00:00 UTC to a time zone west of
2002 * Greenwich will fail (due to the year 0 being out of range).
2004 * You should release the return value by calling g_date_time_unref()
2005 * when you are done with it.
2007 * Returns: a new #GDateTime, or %NULL
2012 g_date_time_to_timezone (GDateTime *datetime,
2015 return g_date_time_from_instant (tz, g_date_time_to_instant (datetime));
2019 * g_date_time_to_local:
2020 * @datetime: a #GDateTime
2022 * Creates a new #GDateTime corresponding to the same instant in time as
2023 * @datetime, but in the local time zone.
2025 * This call is equivalent to calling g_date_time_to_timezone() with the
2026 * time zone returned by g_time_zone_new_local().
2028 * Returns: the newly created #GDateTime
2033 g_date_time_to_local (GDateTime *datetime)
2038 local = g_time_zone_new_local ();
2039 new = g_date_time_to_timezone (datetime, local);
2040 g_time_zone_unref (local);
2046 * g_date_time_to_utc:
2047 * @datetime: a #GDateTime
2049 * Creates a new #GDateTime corresponding to the same instant in time as
2050 * @datetime, but in UTC.
2052 * This call is equivalent to calling g_date_time_to_timezone() with the
2053 * time zone returned by g_time_zone_new_utc().
2055 * Returns: the newly created #GDateTime
2060 g_date_time_to_utc (GDateTime *datetime)
2065 utc = g_time_zone_new_utc ();
2066 new = g_date_time_to_timezone (datetime, utc);
2067 g_time_zone_unref (utc);
2075 get_numeric_format (gchar *fmt,
2077 gboolean alt_digits,
2081 const gchar *width_str;
2093 g_warning ("get_numeric_format: width %d not handled", width);
2110 g_snprintf (fmt, len, "%%%s%sd", alt_digits ? "I": "", width_str);
2114 * g_date_time_format:
2115 * @datetime: A #GDateTime
2116 * @format: a valid UTF-8 string, containing the format for the
2119 * Creates a newly allocated string representing the requested @format.
2121 * The format strings understood by this function are a subset of the
2122 * strftime() format language. In contrast to strftime(), this function
2123 * always produces a UTF-8 string, regardless of the current locale.
2124 * Note that the rendering of many formats is locale-dependent and may
2125 * not match the strftime() output exactly.
2127 * The following format specifiers are supported:
2130 * <varlistentry><term>
2131 * <literal>%%a</literal>:
2132 * </term><listitem><simpara>
2133 * the abbreviated weekday name according to the current locale
2134 * </simpara></listitem></varlistentry>
2135 * <varlistentry><term>
2136 * <literal>%%A</literal>:
2137 * </term><listitem><simpara>
2138 * the full weekday name according to the current locale
2139 * </simpara></listitem></varlistentry>
2140 * <varlistentry><term>
2141 * <literal>%%b</literal>:
2142 * </term><listitem><simpara>
2143 * the abbreviated month name according to the current locale
2144 * </simpara></listitem></varlistentry>
2145 * <varlistentry><term>
2146 * <literal>%%B</literal>:
2147 * </term><listitem><simpara>
2148 * the full month name according to the current locale
2149 * </simpara></listitem></varlistentry>
2150 * <varlistentry><term>
2151 * <literal>%%d</literal>:
2152 * </term><listitem><simpara>
2153 * the day of the month as a decimal number (range 01 to 31)
2154 * </simpara></listitem></varlistentry>
2155 * <varlistentry><term>
2156 * <literal>%%e</literal>:
2157 * </term><listitem><simpara>
2158 * the day of the month as a decimal number (range 1 to 31)
2159 * </simpara></listitem></varlistentry>
2160 * <varlistentry><term>
2161 * <literal>%%F</literal>:
2162 * </term><listitem><simpara>
2163 * equivalent to <literal>%%Y-%%m-%%d</literal> (the ISO 8601 date
2165 * </simpara></listitem></varlistentry>
2166 * <varlistentry><term>
2167 * <literal>%%h</literal>:
2168 * </term><listitem><simpara>
2169 * equivalent to <literal>%%b</literal>
2170 * </simpara></listitem></varlistentry>
2171 * <varlistentry><term>
2172 * <literal>%%H</literal>:
2173 * </term><listitem><simpara>
2174 * the hour as a decimal number using a 24-hour clock (range 00 to
2176 * </simpara></listitem></varlistentry>
2177 * <varlistentry><term>
2178 * <literal>%%I</literal>:
2179 * </term><listitem><simpara>
2180 * the hour as a decimal number using a 12-hour clock (range 01 to
2182 * </simpara></listitem></varlistentry>
2183 * <varlistentry><term>
2184 * <literal>%%j</literal>:
2185 * </term><listitem><simpara>
2186 * the day of the year as a decimal number (range 001 to 366)
2187 * </simpara></listitem></varlistentry>
2188 * <varlistentry><term>
2189 * <literal>%%k</literal>:
2190 * </term><listitem><simpara>
2191 * the hour (24-hour clock) as a decimal number (range 0 to 23);
2192 * single digits are preceded by a blank
2193 * </simpara></listitem></varlistentry>
2194 * <varlistentry><term>
2195 * <literal>%%l</literal>:
2196 * </term><listitem><simpara>
2197 * the hour (12-hour clock) as a decimal number (range 1 to 12);
2198 * single digits are preceded by a blank
2199 * </simpara></listitem></varlistentry>
2200 * <varlistentry><term>
2201 * <literal>%%m</literal>:
2202 * </term><listitem><simpara>
2203 * the month as a decimal number (range 01 to 12)
2204 * </simpara></listitem></varlistentry>
2205 * <varlistentry><term>
2206 * <literal>%%M</literal>:
2207 * </term><listitem><simpara>
2208 * the minute as a decimal number (range 00 to 59)
2209 * </simpara></listitem></varlistentry>
2210 * <varlistentry><term>
2211 * <literal>%%N</literal>:
2212 * </term><listitem><simpara>
2213 * the micro-seconds as a decimal number
2214 * </simpara></listitem></varlistentry>
2215 * <varlistentry><term>
2216 * <literal>%%p</literal>:
2217 * </term><listitem><simpara>
2218 * either "AM" or "PM" according to the given time value, or the
2219 * corresponding strings for the current locale. Noon is treated as
2220 * "PM" and midnight as "AM".
2221 * </simpara></listitem></varlistentry>
2222 * <varlistentry><term>
2223 * <literal>%%P</literal>:
2224 * </term><listitem><simpara>
2225 * like %%p but lowercase: "am" or "pm" or a corresponding string for
2226 * the current locale
2227 * </simpara></listitem></varlistentry>
2228 * <varlistentry><term>
2229 * <literal>%%r</literal>:
2230 * </term><listitem><simpara>
2231 * the time in a.m. or p.m. notation
2232 * </simpara></listitem></varlistentry>
2233 * <varlistentry><term>
2234 * <literal>%%R</literal>:
2235 * </term><listitem><simpara>
2236 * the time in 24-hour notation (<literal>%%H:%%M</literal>)
2237 * </simpara></listitem></varlistentry>
2238 * <varlistentry><term>
2239 * <literal>%%s</literal>:
2240 * </term><listitem><simpara>
2241 * the number of seconds since the Epoch, that is, since 1970-01-01
2243 * </simpara></listitem></varlistentry>
2244 * <varlistentry><term>
2245 * <literal>%%S</literal>:
2246 * </term><listitem><simpara>
2247 * the second as a decimal number (range 00 to 60)
2248 * </simpara></listitem></varlistentry>
2249 * <varlistentry><term>
2250 * <literal>%%t</literal>:
2251 * </term><listitem><simpara>
2253 * </simpara></listitem></varlistentry>
2254 * <varlistentry><term>
2255 * <literal>%%T</literal>:
2256 * </term><listitem><simpara>
2257 * the time in 24-hour notation with seconds (<literal>%%H:%%M:%%S</literal>)
2258 * </simpara></listitem></varlistentry>
2259 * <varlistentry><term>
2260 * <literal>%%u</literal>:
2261 * </term><listitem><simpara>
2262 * the day of the week as a decimal, range 1 to 7, Monday being 1
2263 * </simpara></listitem></varlistentry>
2264 * <varlistentry><term>
2265 * <literal>%%V</literal>:
2266 * </term><listitem><simpara>
2267 * the ISO 8601 week number of the current year as a decimal number,
2268 * range 01 to 53, where week 1 is the first week that has at least
2269 * 4 days in the new year. See g_date_time_get_week_of_year().
2270 * </simpara></listitem></varlistentry>
2271 * <varlistentry><term>
2272 * <literal>%%W</literal>:
2273 * </term><listitem><simpara>
2274 * the week number of the current year as a decimal number
2275 * </simpara></listitem></varlistentry>
2276 * <varlistentry><term>
2277 * <literal>%%x</literal>:
2278 * </term><listitem><simpara>
2279 * the preferred date representation for the current locale without
2281 * </simpara></listitem></varlistentry>
2282 * <varlistentry><term>
2283 * <literal>%%X</literal>:
2284 * </term><listitem><simpara>
2285 * the preferred time representation for the current locale without
2287 * </simpara></listitem></varlistentry>
2288 * <varlistentry><term>
2289 * <literal>%%y</literal>:
2290 * </term><listitem><simpara>
2291 * the year as a decimal number without the century
2292 * </simpara></listitem></varlistentry>
2293 * <varlistentry><term>
2294 * <literal>%%Y</literal>:
2295 * </term><listitem><simpara>
2296 * the year as a decimal number including the century
2297 * </simpara></listitem></varlistentry>
2298 * <varlistentry><term>
2299 * <literal>%%z</literal>:
2300 * </term><listitem><simpara>
2301 * the time-zone as hour offset from UTC
2302 * </simpara></listitem></varlistentry>
2303 * <varlistentry><term>
2304 * <literal>%%Z</literal>:
2305 * </term><listitem><simpara>
2306 * the time zone or name or abbreviation
2307 * </simpara></listitem></varlistentry>
2308 * <varlistentry><term>
2309 * <literal>%%%</literal>:
2310 * </term><listitem><simpara>
2311 * a literal <literal>%%</literal> character
2312 * </simpara></listitem></varlistentry>
2315 * Some conversion specifications can be modified by preceding the
2316 * conversion specifier by one or more modifier characters. The
2317 * following modifiers are supported for many of the numeric
2323 * Use alternative numeric symbols, if the current locale
2330 * Pad a numeric result with spaces.
2331 * This overrides the default padding for the specifier.
2337 * Do not pad a numeric result.
2338 * This overrides the default padding for the specifier.
2344 * Pad a numeric result with zeros.
2345 * This overrides the default padding for the specifier.
2350 * Returns: a newly allocated string formatted to the requested format
2351 * or %NULL in the case that there was an error. The string
2352 * should be freed with g_free().
2357 g_date_time_format (GDateTime *datetime,
2358 const gchar *format)
2363 gboolean in_mod = FALSE;
2364 gboolean alt_digits = FALSE;
2365 gboolean pad_set = FALSE;
2370 g_return_val_if_fail (datetime != NULL, NULL);
2371 g_return_val_if_fail (format != NULL, NULL);
2372 g_return_val_if_fail (g_utf8_validate (format, -1, NULL), NULL);
2374 outstr = g_string_sized_new (strlen (format) * 2);
2377 for (; *format; format = g_utf8_next_char (format))
2379 c = g_utf8_get_char (format);
2398 g_string_append (outstr, WEEKDAY_ABBR (datetime));
2401 g_string_append (outstr, WEEKDAY_FULL (datetime));
2404 g_string_append (outstr, MONTH_ABBR (datetime));
2407 g_string_append (outstr, MONTH_FULL (datetime));
2410 get_numeric_format (fmt, sizeof(fmt), alt_digits, pad_set ? pad : '0', 2);
2411 g_string_append_printf (outstr, fmt, g_date_time_get_day_of_month (datetime));
2414 get_numeric_format (fmt, sizeof(fmt), alt_digits, pad_set ? pad : 0, 2);
2415 g_string_append_printf (outstr, fmt, g_date_time_get_day_of_month (datetime));
2418 g_string_append_printf (outstr, "%d-%02d-%02d",
2419 g_date_time_get_year (datetime),
2420 g_date_time_get_month (datetime),
2421 g_date_time_get_day_of_month (datetime));
2424 g_string_append (outstr, MONTH_ABBR (datetime));
2427 get_numeric_format (fmt, sizeof(fmt), alt_digits, pad_set ? pad : '0', 2);
2428 g_string_append_printf (outstr, fmt, g_date_time_get_hour (datetime));
2432 gint hour = g_date_time_get_hour (datetime) % 12;
2435 get_numeric_format (fmt, sizeof(fmt), alt_digits, pad_set ? pad : '0', 2);
2436 g_string_append_printf (outstr, fmt, hour);
2440 get_numeric_format (fmt, sizeof(fmt), FALSE, pad_set ? pad : '0', 3);
2441 g_string_append_printf (outstr, fmt, g_date_time_get_day_of_year (datetime));
2444 get_numeric_format (fmt, sizeof(fmt), alt_digits, pad_set ? pad : ' ', 2);
2445 g_string_append_printf (outstr, fmt, g_date_time_get_hour (datetime));
2449 gint hour = g_date_time_get_hour (datetime) % 12;
2452 get_numeric_format (fmt, sizeof(fmt), alt_digits, pad_set ? pad : ' ', 2);
2453 g_string_append_printf (outstr, fmt, hour);
2457 get_numeric_format (fmt, sizeof(fmt), alt_digits, pad_set ? pad : '0', 2);
2458 g_string_append_printf (outstr, fmt, g_date_time_get_month (datetime));
2461 get_numeric_format (fmt, sizeof(fmt), alt_digits, pad_set ? pad : '0', 2);
2462 g_string_append_printf (outstr, fmt, g_date_time_get_minute (datetime));
2465 g_string_append_printf (outstr, "%"G_GUINT64_FORMAT, datetime->usec % USEC_PER_SECOND);
2471 ampm = g_utf8_strup (GET_AMPM (datetime), -1);
2472 g_string_append (outstr, ampm);
2476 ampm = g_utf8_strdown (GET_AMPM (datetime), -1);
2477 g_string_append (outstr, ampm);
2482 gint hour = g_date_time_get_hour (datetime) % 12;
2485 ampm = g_utf8_strup (GET_AMPM (datetime), -1);
2486 g_string_append_printf (outstr, "%02d:%02d:%02d %s",
2488 g_date_time_get_minute (datetime),
2489 g_date_time_get_second (datetime),
2495 g_string_append_printf (outstr, "%02d:%02d",
2496 g_date_time_get_hour (datetime),
2497 g_date_time_get_minute (datetime));
2500 g_string_append_printf (outstr, "%" G_GINT64_FORMAT, g_date_time_to_unix (datetime));
2503 get_numeric_format (fmt, sizeof(fmt), alt_digits, pad_set ? pad : '0', 2);
2504 g_string_append_printf (outstr, fmt, g_date_time_get_second (datetime));
2507 g_string_append_c (outstr, '\t');
2510 g_string_append_printf (outstr, "%02d:%02d:%02d",
2511 g_date_time_get_hour (datetime),
2512 g_date_time_get_minute (datetime),
2513 g_date_time_get_second (datetime));
2516 get_numeric_format (fmt, sizeof(fmt), alt_digits, 0, 0);
2517 g_string_append_printf (outstr, fmt, g_date_time_get_day_of_week (datetime));
2520 get_numeric_format (fmt, sizeof(fmt), alt_digits, '0', 2);
2521 g_string_append_printf (outstr, fmt, g_date_time_get_week_of_year (datetime));
2524 get_numeric_format (fmt, sizeof(fmt), alt_digits, 0, 0);
2525 g_string_append_printf (outstr, fmt, g_date_time_get_day_of_year (datetime) / 7);
2529 tmp = g_date_time_format (datetime, PREFERRED_DATE_FMT);
2530 g_string_append (outstr, tmp);
2536 tmp = g_date_time_format (datetime, PREFERRED_TIME_FMT);
2537 g_string_append (outstr, tmp);
2542 get_numeric_format (fmt, sizeof(fmt), alt_digits, pad_set ? pad : '0', 2);
2543 g_string_append_printf (outstr, fmt, g_date_time_get_year (datetime) % 100);
2546 get_numeric_format (fmt, sizeof(fmt), alt_digits, 0, 0);
2547 g_string_append_printf (outstr, fmt, g_date_time_get_year (datetime));
2550 if (datetime->tz != NULL)
2552 gint64 offset = g_date_time_get_utc_offset (datetime)
2555 g_string_append_printf (outstr, "%+03d%02d",
2556 (int) offset / 3600,
2557 (int) abs(offset) / 60 % 60);
2560 g_string_append (outstr, "+0000");
2563 g_string_append (outstr, g_date_time_get_timezone_abbreviation (datetime));
2566 g_string_append_c (outstr, '%');
2569 g_string_append_c (outstr, '\n');
2589 g_string_append_unichar (outstr, c);
2594 return g_string_free (outstr, FALSE);
2597 g_string_free (outstr, TRUE);
2603 /* vim:set foldmethod=marker: */