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>
7 * This is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 /* Algorithms within this file are based on the Calendar FAQ by
23 * Claus Tondering. It can be found at
24 * http://www.tondering.dk/claus/cal/calendar29.txt
26 * Copyright and disclaimer
27 * ------------------------
28 * This document is Copyright (C) 2008 by Claus Tondering.
29 * E-mail: claus@tondering.dk. (Please include the word
30 * "calendar" in the subject line.)
31 * The document may be freely distributed, provided this
32 * copyright notice is included and no money is charged for
35 * This document is provided "as is". No warranties are made as
51 #endif /* !G_OS_WIN32 */
53 #include "gdatetime.h"
56 #include "gfileutils.h"
58 #include "gstrfuncs.h"
59 #include "gtestutils.h"
65 * @short_description: A structure representing Date and Time
67 * #GDateTime is a structure that combines a date and time into a single
68 * structure. It provides many conversion and methods to manipulate dates
69 * and times. Time precision is provided down to microseconds.
71 * #GDateTime is an immutable object: once it has been created it cannot be
72 * modified further. All modifiers will create a new #GDateTime.
74 * #GDateTime is reference counted: the reference count is increased by calling
75 * g_date_time_ref() and decreased by calling g_date_time_unref(). When the
76 * reference count drops to 0, the resources allocated by the #GDateTime
77 * structure are released.
79 * Internally, #GDateTime uses the Julian Day Number since the
80 * initial Julian Period (-4712 BC). However, the public API uses the
81 * internationally accepted Gregorian Calendar.
83 * #GDateTime is available since GLib 2.26.
86 #define USEC_PER_SECOND (G_GINT64_CONSTANT (1000000))
87 #define USEC_PER_MINUTE (G_GINT64_CONSTANT (60000000))
88 #define USEC_PER_HOUR (G_GINT64_CONSTANT (3600000000))
89 #define USEC_PER_MILLISECOND (G_GINT64_CONSTANT (1000))
90 #define USEC_PER_DAY (G_GINT64_CONSTANT (86400000000))
92 #define GREGORIAN_LEAP(y) ((((y) % 4) == 0) && (!((((y) % 100) == 0) && (((y) % 400) != 0))))
93 #define JULIAN_YEAR(d) ((d)->julian / 365.25)
94 #define DAYS_PER_PERIOD (G_GINT64_CONSTANT (2914695))
96 #define GET_AMPM(d,l) ((g_date_time_get_hour (d) < 12) \
97 ? (l ? C_("GDateTime", "am") : C_("GDateTime", "AM")) \
98 : (l ? C_("GDateTime", "pm") : C_("GDateTime", "PM")))
100 #define WEEKDAY_ABBR(d) (get_weekday_name_abbr (g_date_time_get_day_of_week (datetime)))
101 #define WEEKDAY_FULL(d) (get_weekday_name (g_date_time_get_day_of_week (datetime)))
103 #define MONTH_ABBR(d) (get_month_name_abbr (g_date_time_get_month (datetime)))
104 #define MONTH_FULL(d) (get_month_name (g_date_time_get_month (datetime)))
106 /* Translators: this is the preferred format for expressing the date */
107 #define GET_PREFERRED_DATE(d) (g_date_time_printf ((d), C_("GDateTime", "%m/%d/%y")))
109 /* Translators: this is the preferred format for expressing the time */
110 #define GET_PREFERRED_TIME(d) (g_date_time_printf ((d), C_("GDateTime", "%H:%M:%S")))
112 typedef struct _GTimeZone GTimeZone;
116 /* Julian Period, 0 is Initial Epoch */
119 /* Day within Julian Period */
122 /* Microsecond timekeeping within Day */
127 /* TimeZone information, NULL is UTC */
130 volatile gint ref_count;
135 /* TZ abbreviation (e.g. PST) */
143 static const guint16 days_in_months[2][13] =
145 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
146 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
149 static const guint16 days_in_year[2][13] =
151 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
152 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
156 get_month_name (gint month)
161 return C_("GDateTime", "January");
163 return C_("GDateTime", "February");
165 return C_("GDateTime", "March");
167 return C_("GDateTime", "April");
169 return C_("GDateTime", "May");
171 return C_("GDateTime", "June");
173 return C_("GDateTime", "July");
175 return C_("GDateTime", "August");
177 return C_("GDateTime", "September");
179 return C_("GDateTime", "October");
181 return C_("GDateTime", "November");
183 return C_("GDateTime", "December");
186 g_warning ("Invalid month number %d", month);
193 get_month_name_abbr (gint month)
198 return C_("GDateTime", "Jan");
200 return C_("GDateTime", "Feb");
202 return C_("GDateTime", "Mar");
204 return C_("GDateTime", "Apr");
206 return C_("GDateTime", "May");
208 return C_("GDateTime", "Jun");
210 return C_("GDateTime", "Jul");
212 return C_("GDateTime", "Aug");
214 return C_("GDateTime", "Sep");
216 return C_("GDateTime", "Oct");
218 return C_("GDateTime", "Nov");
220 return C_("GDateTime", "Dec");
223 g_warning ("Invalid month number %d", month);
230 get_weekday_name (gint day)
235 return C_("GDateTime", "Monday");
237 return C_("GDateTime", "Tuesday");
239 return C_("GDateTime", "Wednesday");
241 return C_("GDateTime", "Thursday");
243 return C_("GDateTime", "Friday");
245 return C_("GDateTime", "Saturday");
247 return C_("GDateTime", "Sunday");
250 g_warning ("Invalid week day number %d", day);
257 get_weekday_name_abbr (gint day)
262 return C_("GDateTime", "Mon");
264 return C_("GDateTime", "Tue");
266 return C_("GDateTime", "Wed");
268 return C_("GDateTime", "Thu");
270 return C_("GDateTime", "Fri");
272 return C_("GDateTime", "Sat");
274 return C_("GDateTime", "Sun");
277 g_warning ("Invalid week day number %d", day);
284 date_to_julian (gint year,
288 gint a = (14 - month) / 12;
289 gint y = year + 4800 - a;
290 gint m = month + (12 * a) - 3;
293 + (((153 * m) + 2) / 5)
302 g_date_time_add_days_internal (GDateTime *datetime,
305 gint __day = datetime->julian + days;
308 datetime->period += -1 + (__day / DAYS_PER_PERIOD);
309 datetime->period += DAYS_PER_PERIOD + (__day % DAYS_PER_PERIOD);
311 else if (__day > DAYS_PER_PERIOD)
313 datetime->period += (datetime->julian + days) / DAYS_PER_PERIOD;
314 datetime->julian = (datetime->julian + days) % DAYS_PER_PERIOD;
317 datetime->julian += days;
321 g_date_time_add_usec (GDateTime *datetime,
324 gint64 __usec = datetime->usec + usecs;
325 gint __days = __usec / USEC_PER_DAY;
331 g_date_time_add_days_internal (datetime, __days);
334 datetime->usec = USEC_PER_DAY + (__usec % USEC_PER_DAY);
336 datetime->usec = __usec % USEC_PER_DAY;
340 * g_date_time_add_ymd:
341 * @datetime: a #GDateTime
342 * @years: years to add, in the Gregorian calendar
343 * @months: months to add, in the Gregorian calendar
344 * @days: days to add, in the Gregorian calendar
346 * Updates @datetime by adding @years, @months and @days to it
348 * This function modifies the passed #GDateTime so public accessors
349 * should make always pass a copy
352 g_date_time_add_ymd (GDateTime *datetime,
357 gint __year = g_date_time_get_year (datetime);
358 gint __month = g_date_time_get_month (datetime);
359 gint __day = g_date_time_get_day_of_month (datetime);
361 const guint16 *max_days;
365 /* subtract one day for leap years */
366 if (GREGORIAN_LEAP (__year) && __month == 2)
373 step = months > 0 ? 1 : -1;
374 for (i = 0; i < ABS (months); i++)
383 else if (__month > 12)
391 max_days = days_in_months[GREGORIAN_LEAP (__year) ? 1 : 0];
392 if (max_days[__month] < __day)
393 __day = max_days[__month];
395 /* since the add_days_internal() uses the julian date we need to
396 * update it using the new year/month/day and then add the days
398 datetime->julian = date_to_julian (__year, __month, __day);
399 g_date_time_add_days_internal (datetime, days);
402 #define ZONEINFO_DIR "zoneinfo"
403 #define TZ_MAGIC "TZif"
404 #define TZ_MAGIC_LEN (strlen (TZ_MAGIC))
405 #define TZ_HEADER_SIZE 44
406 #define TZ_TIMECNT_OFFSET 32
407 #define TZ_TYPECNT_OFFSET 36
408 #define TZ_TRANSITIONS_OFFSET 44
410 #define TZ_TTINFO_SIZE 6
411 #define TZ_TTINFO_GMTOFF_OFFSET 0
412 #define TZ_TTINFO_ISDST_OFFSET 4
413 #define TZ_TTINFO_NAME_OFFSET 5
416 get_tzdata_path (const gchar *tz_name)
422 const gchar *tz_dir = g_getenv ("TZDIR");
425 retval = g_build_filename (tz_dir, tz_name, NULL);
427 retval = g_build_filename ("/", "usr", "share", ZONEINFO_DIR, tz_name, NULL);
431 /* an empty tz_name means "the current timezone file". tzset(3) defines
432 * it to be /usr/share/zoneinfo/localtime, and it also allows an
433 * /etc/localtime as a symlink to the localtime file under
434 * /usr/share/zoneinfo or to the correct timezone file. Fedora does not
435 * have /usr/share/zoneinfo/localtime, but it does have a real
438 * in any case, this path should resolve correctly.
440 retval = g_build_filename ("/", "etc", "localtime", NULL);
447 * Parses tzdata database times to get timezone info.
449 * @tzname: Olson database name for the timezone
450 * @start: Time offset from epoch we want to know the timezone
451 * @_is_dst: Returns if this time in the timezone is in DST
452 * @_offset: Returns the offset from UTC for this timezone
453 * @_name: Returns the abreviated name for thie timezone
456 parse_tzdata (const gchar *tzname,
463 gchar *filename, *contents;
465 guint32 timecnt, typecnt;
466 gint transitions_size, ttinfo_map_size;
467 guint8 *ttinfo_map, *ttinfos;
468 gint start_transition = -1;
469 guint32 *transitions;
476 filename = get_tzdata_path (tzname);
478 /* XXX: should we be caching this in memory for faster access?
479 * and if so, how do we expire the cache?
482 if (!g_file_get_contents (filename, &contents, &length, &error))
490 if (length < TZ_HEADER_SIZE ||
491 (strncmp (contents, TZ_MAGIC, TZ_MAGIC_LEN) != 0))
497 timecnt = GUINT32_FROM_BE (*(guint32 *)(contents + TZ_TIMECNT_OFFSET));
498 typecnt = GUINT32_FROM_BE (*(guint32 *)(contents + TZ_TYPECNT_OFFSET));
500 transitions = (guint32 *)(contents + TZ_TRANSITIONS_OFFSET);
501 transitions_size = timecnt * sizeof (*transitions);
502 ttinfo_map = (void *)(contents + TZ_TRANSITIONS_OFFSET + transitions_size);
503 ttinfo_map_size = timecnt;
504 ttinfos = (void *)(ttinfo_map + ttinfo_map_size);
507 * Find the first transition that contains the 'start' time
509 for (i = 1; i < timecnt && start_transition == -1; i++)
511 gint32 transition_time = GINT32_FROM_BE (transitions[i]);
513 /* if is_utc is not set, we need to add this time offset to compare with
514 * start, because it is already on the timezone time */
519 off = *(gint32 *)(ttinfos + ttinfo_map[i] * TZ_TTINFO_SIZE +
520 TZ_TTINFO_GMTOFF_OFFSET);
521 off = GINT32_FROM_BE (off);
523 transition_time += off;
526 if (transition_time > start)
528 start_transition = ttinfo_map[i - 1];
533 if (start_transition == -1)
536 start_transition = ttinfo_map[timecnt - 1];
538 start_transition = 0;
541 /* Copy the data out of the corresponding ttinfo structs */
542 offset = *(gint32 *)(ttinfos + start_transition * TZ_TTINFO_SIZE +
543 TZ_TTINFO_GMTOFF_OFFSET);
544 offset = GINT32_FROM_BE (offset);
545 isdst = *(ttinfos + start_transition * TZ_TTINFO_SIZE +
546 TZ_TTINFO_ISDST_OFFSET);
547 name_offset = *(ttinfos + start_transition * TZ_TTINFO_SIZE +
548 TZ_TTINFO_NAME_OFFSET);
551 *_name = g_strdup ((gchar*) (ttinfos + TZ_TTINFO_SIZE * typecnt + name_offset));
565 * g_time_zone_new_from_epoc:
566 * @tzname: The Olson's database timezone name
567 * @epoch: The epoch offset
568 * @is_utc: If the @epoch is in UTC or already in the @tzname timezone
570 * Creates a new timezone
573 g_time_zone_new_from_epoch (const gchar *tzname,
577 GTimeZone *tz = NULL;
582 if (parse_tzdata (tzname, epoch, is_utc, &is_dst, &offset, &name))
584 tz = g_slice_new (GTimeZone);
593 #define SECS_PER_MINUTE (60)
594 #define SECS_PER_HOUR (60 * SECS_PER_MINUTE)
595 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
596 #define SECS_PER_YEAR (365 * SECS_PER_DAY)
597 #define SECS_PER_JULIAN (DAYS_PER_PERIOD * SECS_PER_DAY)
600 g_date_time_secs_offset (const GDateTime * dt)
606 y = g_date_time_get_year (dt);
607 d = g_date_time_get_day_of_year (dt);
608 h = g_date_time_get_hour (dt);
609 m = g_date_time_get_minute (dt);
610 s = g_date_time_get_second (dt);
612 leaps = GREGORIAN_LEAP (y) ? 1 : 0;
613 for (i = 1970; i < y; i++)
615 if (GREGORIAN_LEAP (i))
620 secs += (y - 1970) * SECS_PER_YEAR;
621 secs += d * SECS_PER_DAY;
622 secs += (leaps - 1) * SECS_PER_DAY;
623 secs += h * SECS_PER_HOUR;
624 secs += m * SECS_PER_MINUTE;
628 secs -= dt->tz->offset;
634 * g_date_time_create_time_zone:
636 * @tzname: the name of the timezone
638 * Creates a timezone from a #GDateTime (disregarding its own timezone).
639 * This function transforms the #GDateTime into seconds since the epoch
640 * and creates a timezone for it in the @tzname zone.
642 * Return value: a newly created #GTimeZone
645 g_date_time_create_time_zone (GDateTime *dt,
650 secs = g_date_time_secs_offset (dt);
652 return g_time_zone_new_from_epoch (tzname, secs, FALSE);
656 g_date_time_new (void)
660 datetime = g_slice_new0 (GDateTime);
661 datetime->ref_count = 1;
667 g_time_zone_copy (const GTimeZone *timezone)
671 if (G_UNLIKELY (timezone == NULL))
674 tz = g_slice_new (GTimeZone);
675 memcpy (tz, timezone, sizeof (GTimeZone));
677 tz->name = g_strdup (timezone->name);
683 g_time_zone_free (GTimeZone *timezone)
685 if (G_LIKELY (timezone != NULL))
687 g_free (timezone->name);
688 g_slice_free (GTimeZone, timezone);
693 g_date_time_free (GDateTime *datetime)
695 if (G_UNLIKELY (datetime == NULL))
699 g_time_zone_free (datetime->tz);
701 g_slice_free (GDateTime, datetime);
705 g_date_time_get_week_number (const GDateTime *datetime,
710 gint a, b, c, d, e, f, g, n, s, month, day, year;
712 g_date_time_get_dmy (datetime, &day, &month, &year);
716 a = g_date_time_get_year (datetime) - 1;
717 b = (a / 4) - (a / 100) + (a / 400);
718 c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
721 f = day - 1 + (31 * (month - 1));
726 b = (a / 4) - (a / 100) + (a / 400);
727 c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
730 f = day + (((153 * (month - 3)) + 2) / 5) + 58 + s;
740 *week_number = 53 - ((g - s) / 5);
741 else if (n > 364 + s)
744 *week_number = (n / 7) + 1;
748 *day_of_week = d + 1;
751 *day_of_year = f + 1;
756 * @datetime: a #GDateTime
757 * @timespan: a #GTimeSpan
759 * Creates a copy of @datetime and adds the specified timespan to the copy.
761 * Return value: the newly created #GDateTime which should be freed with
762 * g_date_time_unref().
767 g_date_time_add (const GDateTime *datetime,
772 g_return_val_if_fail (datetime != NULL, NULL);
774 dt = g_date_time_copy (datetime);
775 g_date_time_add_usec (dt, timespan);
781 * g_date_time_add_years:
782 * @datetime: a #GDateTime
783 * @years: the number of years
785 * Creates a copy of @datetime and adds the specified number of years to the
788 * Return value: the newly created #GDateTime which should be freed with
789 * g_date_time_unref().
794 g_date_time_add_years (const GDateTime *datetime,
799 g_return_val_if_fail (datetime != NULL, NULL);
801 dt = g_date_time_copy (datetime);
802 g_date_time_add_ymd (dt, years, 0, 0);
808 * g_date_time_add_months:
809 * @datetime: a #GDateTime
810 * @months: the number of months
812 * Creates a copy of @datetime and adds the specified number of months to the
815 * Return value: the newly created #GDateTime which should be freed with
816 * g_date_time_unref().
821 g_date_time_add_months (const GDateTime *datetime,
826 g_return_val_if_fail (datetime != NULL, NULL);
828 dt = g_date_time_copy (datetime);
829 g_date_time_add_ymd (dt, 0, months, 0);
835 * g_date_time_add_weeks:
836 * @datetime: a #GDateTime
837 * @weeks: the number of weeks
839 * Creates a copy of @datetime and adds the specified number of weeks to the
842 * Return value: the newly created #GDateTime which should be freed with
843 * g_date_time_unref().
848 g_date_time_add_weeks (const GDateTime *datetime,
851 g_return_val_if_fail (datetime != NULL, NULL);
853 return g_date_time_add_days (datetime, weeks * 7);
857 * g_date_time_add_days:
858 * @datetime: a #GDateTime
859 * @days: the number of days
861 * Creates a copy of @datetime and adds the specified number of days to the
864 * Return value: the newly created #GDateTime which should be freed with
865 * g_date_time_unref().
870 g_date_time_add_days (const GDateTime *datetime,
875 g_return_val_if_fail (datetime != NULL, NULL);
877 dt = g_date_time_copy (datetime);
878 g_date_time_add_ymd (dt, 0, 0, days);
884 * g_date_time_add_hours:
885 * @datetime: a #GDateTime
886 * @hours: the number of hours to add
888 * Creates a copy of @datetime and adds the specified number of hours
890 * Return value: the newly created #GDateTime which should be freed with
891 * g_date_time_unref().
896 g_date_time_add_hours (const GDateTime *datetime,
901 g_return_val_if_fail (datetime != NULL, NULL);
903 dt = g_date_time_copy (datetime);
904 g_date_time_add_usec (dt, (gint64) hours * USEC_PER_HOUR);
910 * g_date_time_add_seconds:
911 * @datetime: a #GDateTime
912 * @seconds: the number of seconds to add
914 * Creates a copy of @datetime and adds the specified number of seconds.
916 * Return value: the newly created #GDateTime which should be freed with
917 * g_date_time_unref().
922 g_date_time_add_seconds (const GDateTime *datetime,
927 g_return_val_if_fail (datetime != NULL, NULL);
929 dt = g_date_time_copy (datetime);
930 g_date_time_add_usec (dt, (gint64) seconds * USEC_PER_SECOND);
936 * g_date_time_add_milliseconds:
937 * @datetime: a #GDateTime
938 * @milliseconds: the number of milliseconds to add
940 * Creates a copy of @datetime adding the specified number of milliseconds.
942 * Return value: the newly created #GDateTime which should be freed with
943 * g_date_time_unref().
948 g_date_time_add_milliseconds (const GDateTime *datetime,
953 g_return_val_if_fail (datetime != NULL, NULL);
955 dt = g_date_time_copy (datetime);
956 g_date_time_add_usec (dt, (gint64) milliseconds * USEC_PER_MILLISECOND);
962 * g_date_time_add_minutes:
963 * @datetime: a #GDateTime
964 * @minutes: the number of minutes to add
966 * Creates a copy of @datetime adding the specified number of minutes.
968 * Return value: the newly created #GDateTime which should be freed with
969 * g_date_time_unref().
974 g_date_time_add_minutes (const GDateTime *datetime,
979 g_return_val_if_fail (datetime != NULL, NULL);
981 dt = g_date_time_copy (datetime);
982 g_date_time_add_usec (dt, (gint64) minutes * USEC_PER_MINUTE);
988 * g_date_time_add_full:
989 * @datetime: a #GDateTime
990 * @years: the number of years to add
991 * @months: the number of months to add
992 * @days: the number of days to add
993 * @hours: the number of hours to add
994 * @minutes: the number of minutes to add
995 * @seconds: the number of seconds to add
997 * Creates a new #GDateTime adding the specified values to the current date and
1000 * Return value: the newly created #GDateTime that should be freed with
1001 * g_date_time_unref().
1006 g_date_time_add_full (const GDateTime *datetime,
1017 g_return_val_if_fail (datetime != NULL, NULL);
1019 dt = g_date_time_copy (datetime);
1022 g_date_time_add_ymd (dt, years, months, days);
1025 usecs = (hours * USEC_PER_HOUR)
1026 + (minutes * USEC_PER_MINUTE)
1027 + (seconds * USEC_PER_SECOND);
1028 g_date_time_add_usec (dt, usecs);
1034 * g_date_time_compare:
1035 * @dt1: first #GDateTime to compare
1036 * @dt2: second #GDateTime to compare
1038 * qsort()-style comparison for #GDateTime<!-- -->'s. Both #GDateTime<-- -->'s
1039 * must be non-%NULL.
1041 * Return value: 0 for equal, less than zero if dt1 is less than dt2, greater
1042 * than zero if dt2 is greator than dt1.
1047 g_date_time_compare (gconstpointer dt1,
1050 const GDateTime *a, *b;
1055 if ((a->period == b->period) &&
1056 (a->julian == b->julian) &&
1057 (a->usec == b->usec))
1061 else if ((a->period > b->period) ||
1062 ((a->period == b->period) && (a->julian > b->julian)) ||
1063 ((a->period == b->period) && (a->julian == b->julian) && a->usec > b->usec))
1073 * @datetime: a #GDateTime
1075 * Creates a copy of @datetime.
1077 * Return value: the newly created #GDateTime which should be freed with
1078 * g_date_time_unref().
1083 g_date_time_copy (const GDateTime *datetime)
1087 g_return_val_if_fail (datetime != NULL, NULL);
1089 copied = g_date_time_new ();
1090 copied->period = datetime->period;
1091 copied->julian = datetime->julian;
1092 copied->usec = datetime->usec;
1093 copied->tz = g_time_zone_copy (datetime->tz);
1100 * @datetime: a #GDateTime
1102 * Creates a new #GDateTime at Midnight on the date represented by @datetime.
1104 * Return value: the newly created #GDateTime which should be freed with
1105 * g_date_time_unref().
1110 g_date_time_day (const GDateTime *datetime)
1114 g_return_val_if_fail (datetime != NULL, NULL);
1116 date = g_date_time_copy (datetime);
1123 * g_date_time_difference:
1124 * @begin: a #GDateTime
1125 * @end: a #GDateTime
1127 * Calculates the known difference in time between @begin and @end.
1129 * Since the exact precision cannot always be known due to incomplete
1130 * historic information, an attempt is made to calculate the difference.
1132 * Return value: the difference between the two #GDateTime, as a time
1133 * span expressed in microseconds.
1138 g_date_time_difference (const GDateTime *begin,
1139 const GDateTime *end)
1141 g_return_val_if_fail (begin != NULL, 0);
1142 g_return_val_if_fail (end != NULL, 0);
1144 if (begin->period != 0 || end->period != 0)
1146 g_warning ("GDateTime only supports the current Julian period");
1150 return ((end->julian - begin->julian) * USEC_PER_DAY) + (end->usec - begin->usec);
1154 * g_date_time_equal:
1155 * @dt1: a #GDateTime
1156 * @dt2: a #GDateTime
1158 * Checks to see if @dt1 and @dt2 are equal.
1160 * Equal here means that they represent the same moment after converting
1161 * them to the same timezone.
1163 * Return value: %TRUE if @dt1 and @dt2 are equal
1168 g_date_time_equal (gconstpointer dt1,
1171 const GDateTime *a, *b;
1172 GDateTime *a_utc, *b_utc;
1173 gint64 a_epoch, b_epoch;
1178 a_utc = g_date_time_to_utc (a);
1179 b_utc = g_date_time_to_utc (b);
1181 a_epoch = g_date_time_to_epoch (a_utc);
1182 b_epoch = g_date_time_to_epoch (b_utc);
1184 g_date_time_unref (a_utc);
1185 g_date_time_unref (b_utc);
1187 return a_epoch == b_epoch;
1191 * g_date_time_get_day_of_week:
1192 * @datetime: a #GDateTime
1194 * Retrieves the day of the week represented by @datetime within the gregorian
1195 * calendar. 1 is Sunday, 2 is Monday, etc.
1197 * Return value: the day of the week
1202 g_date_time_get_day_of_week (const GDateTime *datetime)
1210 g_return_val_if_fail (datetime != NULL, 0);
1213 * See Calendar FAQ Section 2.6 for algorithm information
1214 * http://www.tondering.dk/claus/cal/calendar29.txt
1217 g_date_time_get_dmy (datetime, &day, &month, &year);
1218 a = (14 - month) / 12;
1220 m = month + (12 * a) - 2;
1221 dow = ((day + y + (y / 4) - (y / 100) + (y / 400) + (31 * m) / 12) % 7);
1223 /* 1 is Monday and 7 is Sunday */
1224 return (dow == 0) ? 7 : dow;
1228 * g_date_time_get_day_of_month:
1229 * @datetime: a #GDateTime
1231 * Retrieves the day of the month represented by @datetime in the gregorian
1234 * Return value: the day of the month
1239 g_date_time_get_day_of_month (const GDateTime *datetime)
1243 const guint16 *days;
1246 g_return_val_if_fail (datetime != NULL, 0);
1248 days = days_in_year[g_date_time_is_leap_year (datetime) ? 1 : 0];
1249 g_date_time_get_week_number (datetime, NULL, NULL, &day_of_year);
1251 for (i = 1; i <= 12; i++)
1253 if (days [i] >= day_of_year)
1254 return day_of_year - last;
1258 g_warn_if_reached ();
1263 * g_date_time_get_day_of_year:
1264 * @datetime: a #GDateTime
1266 * Retrieves the day of the year represented by @datetime in the Gregorian
1269 * Return value: the day of the year
1274 g_date_time_get_day_of_year (const GDateTime *datetime)
1278 g_return_val_if_fail (datetime != NULL, 0);
1280 g_date_time_get_week_number (datetime, NULL, NULL, &doy);
1285 * g_date_time_get_dmy:
1286 * @datetime: a #GDateTime.
1287 * @day: (out): the return location for the day of the month, or %NULL.
1288 * @month: (out): the return location for the monty of the year, or %NULL.
1289 * @year: (out): the return location for the gregorian year, or %NULL.
1291 * Retrieves the Gregorian day, month, and year of a given #GDateTime.
1296 g_date_time_get_dmy (const GDateTime *datetime,
1301 gint a, b, c, d, e, m;
1303 a = datetime->julian + 32044;
1304 b = ((4 * a) + 3) / 146097;
1305 c = a - ((b * 146097) / 4);
1306 d = ((4 * c) + 3) / 1461;
1307 e = c - (1461 * d) / 4;
1308 m = (5 * e + 2) / 153;
1311 *day = e - (((153 * m) + 2) / 5) + 1;
1314 *month = m + 3 - (12 * (m / 10));
1317 *year = (b * 100) + d - 4800 + (m / 10);
1321 * g_date_time_get_hour:
1322 * @datetime: a #GDateTime
1324 * Retrieves the hour of the day represented by @datetime
1326 * Return value: the hour of the day
1331 g_date_time_get_hour (const GDateTime *datetime)
1333 g_return_val_if_fail (datetime != NULL, 0);
1335 return (datetime->usec / USEC_PER_HOUR);
1339 * g_date_time_get_julian:
1340 * @datetime: a #GDateTime
1341 * @period: (out): a location for the Julian period
1342 * @julian: (out): a location for the day in the Julian period
1343 * @hour: (out): a location for the hour of the day
1344 * @minute: (out): a location for the minute of the hour
1345 * @second: (out): a location for hte second of the minute
1347 * Retrieves the Julian period, day, hour, minute, and second which @datetime
1348 * represents in the Julian calendar.
1353 g_date_time_get_julian (const GDateTime *datetime,
1360 g_return_if_fail (datetime != NULL);
1363 *period = datetime->period;
1366 *julian = datetime->julian;
1369 *hour = (datetime->usec / USEC_PER_HOUR);
1372 *minute = (datetime->usec % USEC_PER_HOUR) / USEC_PER_MINUTE;
1375 *second = (datetime->usec % USEC_PER_MINUTE) / USEC_PER_SECOND;
1379 * g_date_time_get_microsecond:
1380 * @datetime: a #GDateTime
1382 * Retrieves the microsecond of the date represented by @datetime
1384 * Return value: the microsecond of the second
1389 g_date_time_get_microsecond (const GDateTime *datetime)
1391 g_return_val_if_fail (datetime != NULL, 0);
1393 return (datetime->usec % USEC_PER_SECOND);
1397 * g_date_time_get_millisecond:
1398 * @datetime: a #GDateTime
1400 * Retrieves the millisecond of the date represented by @datetime
1402 * Return value: the millisecond of the second
1407 g_date_time_get_millisecond (const GDateTime *datetime)
1409 g_return_val_if_fail (datetime != NULL, 0);
1411 return (datetime->usec % USEC_PER_SECOND) / USEC_PER_MILLISECOND;
1415 * g_date_time_get_minute:
1416 * @datetime: a #GDateTime
1418 * Retrieves the minute of the hour represented by @datetime
1420 * Return value: the minute of the hour
1425 g_date_time_get_minute (const GDateTime *datetime)
1427 g_return_val_if_fail (datetime != NULL, 0);
1429 return (datetime->usec % USEC_PER_HOUR) / USEC_PER_MINUTE;
1433 * g_date_time_get_month:
1434 * @datetime: a #GDateTime
1436 * Retrieves the month of the year represented by @datetime in the Gregorian
1439 * Return value: the month represented by @datetime
1444 g_date_time_get_month (const GDateTime *datetime)
1448 g_return_val_if_fail (datetime != NULL, 0);
1450 g_date_time_get_dmy (datetime, NULL, &month, NULL);
1456 * g_date_time_get_second:
1457 * @datetime: a #GDateTime
1459 * Retrieves the second of the minute represented by @datetime
1461 * Return value: the second represented by @datetime
1466 g_date_time_get_second (const GDateTime *datetime)
1468 g_return_val_if_fail (datetime != NULL, 0);
1470 return (datetime->usec % USEC_PER_MINUTE) / USEC_PER_SECOND;
1474 * g_date_time_get_utc_offset:
1475 * @datetime: a #GDateTime
1477 * Retrieves the offset from UTC that the local timezone specified by
1478 * @datetime represents.
1480 * If @datetime represents UTC time, then the offset is zero.
1482 * Return value: the offset, expressed as a time span expressed in
1488 g_date_time_get_utc_offset (const GDateTime *datetime)
1492 g_return_val_if_fail (datetime != NULL, 0);
1494 if (datetime->tz != NULL)
1495 offset = datetime->tz->offset;
1497 return (gint64) offset * USEC_PER_SECOND;
1501 * g_date_time_get_timezone_name:
1502 * @datetime: a #GDateTime
1504 * Retrieves the Olson's database timezone name of the timezone specified
1507 * Return value: (transfer none): the name of the timezone. The returned
1508 * string is owned by the #GDateTime and it should not be modified or
1513 G_CONST_RETURN gchar *
1514 g_date_time_get_timezone_name (const GDateTime *datetime)
1516 g_return_val_if_fail (datetime != NULL, NULL);
1518 if (datetime->tz != NULL)
1519 return datetime->tz->name;
1525 * g_date_time_get_year:
1526 * @datetime: A #GDateTime
1528 * Retrieves the year represented by @datetime in the Gregorian calendar.
1530 * Return value: the year represented by @datetime
1535 g_date_time_get_year (const GDateTime *datetime)
1539 g_return_val_if_fail (datetime != NULL, 0);
1541 g_date_time_get_dmy (datetime, NULL, NULL, &year);
1548 * @datetime: a #GDateTime
1550 * Hashes @datetime into a #guint, suitable for use within #GHashTable.
1552 * Return value: a #guint containing the hash
1557 g_date_time_hash (gconstpointer datetime)
1559 return (guint) (*((guint64 *) datetime));
1563 * g_date_time_is_leap_year:
1564 * @datetime: a #GDateTime
1566 * Determines if @datetime represents a date known to fall within
1567 * a leap year in the Gregorian calendar.
1569 * Return value: %TRUE if @datetime is a leap year.
1574 g_date_time_is_leap_year (const GDateTime *datetime)
1578 g_return_val_if_fail (datetime != NULL, FALSE);
1580 year = g_date_time_get_year (datetime);
1582 return GREGORIAN_LEAP (year);
1586 * g_date_time_is_daylight_savings:
1587 * @datetime: a #GDateTime
1589 * Determines if @datetime represents a date known to fall within daylight
1590 * savings time in the gregorian calendar.
1592 * Return value: %TRUE if @datetime falls within daylight savings time.
1597 g_date_time_is_daylight_savings (const GDateTime *datetime)
1599 g_return_val_if_fail (datetime != NULL, FALSE);
1604 return datetime->tz->is_dst;
1608 * g_date_time_new_from_date:
1609 * @year: the Gregorian year
1610 * @month: the Gregorian month
1611 * @day: the day in the Gregorian month
1613 * Creates a new #GDateTime using the specified date within the Gregorian
1616 * Return value: the newly created #GDateTime or %NULL if it is outside of
1617 * the representable range.
1622 g_date_time_new_from_date (gint year,
1628 g_return_val_if_fail (year > -4712 && year <= 3268, NULL);
1629 g_return_val_if_fail (month > 0 && month <= 12, NULL);
1630 g_return_val_if_fail (day > 0 && day <= 31, NULL);
1632 dt = g_date_time_new ();
1633 dt->julian = date_to_julian (year, month, day);
1634 dt->tz = g_date_time_create_time_zone (dt, NULL);
1640 * g_date_time_new_from_epoch:
1641 * @t: seconds from the Unix epoch
1643 * Creates a new #GDateTime using the time since Jan 1, 1970 specified by @t.
1645 * Return value: the newly created #GDateTime
1650 g_date_time_new_from_epoch (gint64 t) /* IN */
1656 memset (&tm, 0, sizeof (tm));
1658 /* XXX: GLib should probably have a wrapper for this */
1659 #ifdef HAVE_LOCALTIME_R
1660 localtime_r (&tt, &tm);
1663 struct tm *ptm = localtime (&tt);
1667 /* Happens at least in Microsoft's C library if you pass a
1668 * negative time_t. Use 2000-01-01 as default date.
1670 #ifndef G_DISABLE_CHECKS
1671 g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, "ptm != NULL");
1679 memcpy ((void *) &tm, (void *) ptm, sizeof (struct tm));
1681 #endif /* HAVE_LOCALTIME_R */
1683 return g_date_time_new_full (tm.tm_year + 1900,
1693 * g_date_time_new_from_timeval:
1696 * Creates a new #GDateTime using the date and time specified by #GTimeVal.
1698 * Return value: the newly created #GDateTime
1703 g_date_time_new_from_timeval (GTimeVal *tv)
1705 GDateTime *datetime;
1707 g_return_val_if_fail (tv != NULL, NULL);
1709 datetime = g_date_time_new_from_epoch (tv->tv_sec);
1710 datetime->usec += tv->tv_usec;
1711 datetime->tz = g_date_time_create_time_zone (datetime, NULL);
1717 * g_date_time_new_full:
1718 * @year: the Gregorian year
1719 * @month: the Gregorian month
1720 * @day: the day of the Gregorian month
1721 * @hour: the hour of the day
1722 * @minute: the minute of the hour
1723 * @second: the second of the minute
1724 * @timezone: (allow-none): the Olson's database timezone name, or %NULL
1725 * for local (e.g. America/New_York)
1727 * Creates a new #GDateTime using the date and times in the Gregorian calendar.
1729 * Return value: the newly created #GDateTime
1734 g_date_time_new_full (gint year,
1740 const gchar *timezone)
1744 g_return_val_if_fail (hour >= 0 && hour < 24, NULL);
1745 g_return_val_if_fail (minute >= 0 && minute < 60, NULL);
1746 g_return_val_if_fail (second >= 0 && second <= 60, NULL);
1748 if ((dt = g_date_time_new_from_date (year, month, day)) == NULL)
1751 dt->usec = (hour * USEC_PER_HOUR)
1752 + (minute * USEC_PER_MINUTE)
1753 + (second * USEC_PER_SECOND);
1755 dt->tz = g_date_time_create_time_zone (dt, timezone);
1756 if (timezone != NULL && dt->tz == NULL)
1758 /* timezone creation failed */
1759 g_date_time_unref (dt);
1767 * g_date_time_new_now:
1769 * Creates a new #GDateTime representing the current date and time.
1771 * Return value: the newly created #GDateTime which should be freed with
1772 * g_date_time_unref().
1777 g_date_time_new_now (void)
1781 g_get_current_time (&tv);
1783 return g_date_time_new_from_timeval (&tv);
1787 * g_date_time_printf:
1788 * @datetime: A #GDateTime
1789 * @format: a valid UTF-8 string, containing the format for the #GDateTime
1791 * Creates a newly allocated string representing the requested @format.
1793 * The following format specifiers are supported:
1795 * %%a The abbreviated weekday name according to the current locale.
1796 * %%A The full weekday name according to the current locale.
1797 * %%b The abbreviated month name according to the current locale.
1798 * %%B The full month name according to the current locale.
1799 * %%d The day of the month as a decimal number (range 01 to 31).
1800 * %%e The day of the month as a decimal number (range 1 to 31).
1801 * %%F Equivalent to %Y-%m-%d (the ISO 8601 date format).
1802 * %%h Equivalent to %b.
1803 * %%H The hour as a decimal number using a 24-hour clock (range 00 to 23).
1804 * %%I The hour as a decimal number using a 12-hour clock (range 01 to 12).
1805 * %%j The day of the year as a decimal number (range 001 to 366).
1806 * %%k The hour (24-hour clock) as a decimal number (range 0 to 23);
1807 * single digits are preceded by a blank.
1808 * %%l The hour (12-hour clock) as a decimal number (range 1 to 12);
1809 * single digits are preceded by a blank.
1810 * %%m The month as a decimal number (range 01 to 12).
1811 * %%M The minute as a decimal number (range 00 to 59).
1812 * %%N The micro-seconds as a decimal number.
1813 * %%p Either "AM" or "PM" according to the given time value, or the
1814 * corresponding strings for the current locale. Noon is treated
1815 * as "PM" and midnight as "AM".
1816 * %%P Like %%p but lowercase: "am" or "pm" or a corresponding string for
1817 * the current locale.
1818 * %%r The time in a.m. or p.m. notation.
1819 * %%R The time in 24-hour notation (%H:%M).
1820 * %%s The number of seconds since the Epoch, that is, since 1970-01-01
1822 * %%S The second as a decimal number (range 00 to 60).
1823 * %%t A tab character.
1824 * %%u The day of the week as a decimal, range 1 to 7, Monday being 1.
1825 * %%W The week number of the current year as a decimal number.
1826 * %%x The preferred date representation for the current locale without
1828 * %%X The preferred date representation for the current locale without
1830 * %%y The year as a decimal number without the century.
1831 * %%Y The year as a decimal number including the century.
1832 * %%Z Alphabetic time zone abbreviation (e.g. EDT).
1833 * %%% A literal %% character.
1835 * Return value: a newly allocated string formatted to the requested format or
1836 * %NULL in the case that there was an error. The string should be freed
1842 g_date_time_printf (const GDateTime *datetime,
1843 const gchar *format)
1853 g_return_val_if_fail (datetime != NULL, NULL);
1854 g_return_val_if_fail (format != NULL, NULL);
1855 g_return_val_if_fail (g_utf8_validate (format, -1, NULL), NULL);
1857 outstr = g_string_sized_new (strlen (format) * 2);
1858 utf8len = g_utf8_strlen (format, -1);
1861 for (i = 0; i < utf8len; i++)
1863 tmp = g_utf8_offset_to_pointer (format, i);
1864 c = g_utf8_get_char (tmp);
1881 g_string_append (outstr, WEEKDAY_ABBR (datetime));
1884 g_string_append (outstr, WEEKDAY_FULL (datetime));
1887 g_string_append (outstr, MONTH_ABBR (datetime));
1890 g_string_append (outstr, MONTH_FULL (datetime));
1893 g_string_append_printf (outstr, "%02d", g_date_time_get_day_of_month (datetime));
1896 g_string_append_printf (outstr, "%2d", g_date_time_get_day_of_month (datetime));
1899 g_string_append_printf (outstr, "%d-%02d-%02d",
1900 g_date_time_get_year (datetime),
1901 g_date_time_get_month (datetime),
1902 g_date_time_get_day_of_month (datetime));
1905 g_string_append (outstr, MONTH_ABBR (datetime));
1908 g_string_append_printf (outstr, "%02d", g_date_time_get_hour (datetime));
1911 if (g_date_time_get_hour (datetime) == 0)
1912 g_string_append (outstr, "12");
1914 g_string_append_printf (outstr, "%02d", g_date_time_get_hour (datetime) % 12);
1917 g_string_append_printf (outstr, "%03d", g_date_time_get_day_of_year (datetime));
1920 g_string_append_printf (outstr, "%2d", g_date_time_get_hour (datetime));
1923 if (g_date_time_get_hour (datetime) == 0)
1924 g_string_append (outstr, "12");
1926 g_string_append_printf (outstr, "%2d", g_date_time_get_hour (datetime) % 12);
1929 g_string_append_printf (outstr, "%02d", g_date_time_get_month (datetime));
1932 g_string_append_printf (outstr, "%02d", g_date_time_get_minute (datetime));
1935 g_string_append_printf (outstr, "%"G_GUINT64_FORMAT, datetime->usec % USEC_PER_SECOND);
1938 g_string_append (outstr, GET_AMPM (datetime, FALSE));
1941 g_string_append (outstr, GET_AMPM (datetime, TRUE));
1945 gint hour = g_date_time_get_hour (datetime) % 12;
1948 g_string_append_printf (outstr, "%02d:%02d:%02d %s",
1950 g_date_time_get_minute (datetime),
1951 g_date_time_get_second (datetime),
1952 GET_AMPM (datetime, FALSE));
1956 g_string_append_printf (outstr, "%02d:%02d",
1957 g_date_time_get_hour (datetime),
1958 g_date_time_get_minute (datetime));
1961 g_string_append_printf (outstr, "%" G_GINT64_FORMAT, g_date_time_to_epoch (datetime));
1964 g_string_append_printf (outstr, "%02d", g_date_time_get_second (datetime));
1967 g_string_append_c (outstr, '\t');
1970 g_string_append_printf (outstr, "%d", g_date_time_get_day_of_week (datetime));
1973 g_string_append_printf (outstr, "%d", g_date_time_get_day_of_year (datetime) / 7);
1977 tmp2 = GET_PREFERRED_DATE (datetime);
1978 g_string_append (outstr, tmp2);
1984 tmp2 = GET_PREFERRED_TIME (datetime);
1985 g_string_append (outstr, tmp2);
1990 g_string_append_printf (outstr, "%02d", g_date_time_get_year (datetime) % 100);
1993 g_string_append_printf (outstr, "%d", g_date_time_get_year (datetime));
1996 if (datetime->tz != NULL)
1997 g_string_append_printf (outstr, "%s", datetime->tz->name);
1999 g_string_append_printf (outstr, "UTC");
2002 g_string_append_c (outstr, '%');
2005 g_string_append_c (outstr, '\n');
2013 g_string_append_unichar (outstr, c);
2017 return g_string_free (outstr, FALSE);
2020 g_string_free (outstr, TRUE);
2026 * @datetime: a #GDateTime
2028 * Atomically increments the reference count of @datetime by one.
2030 * Return value: the #GDateTime with the reference count increased
2035 g_date_time_ref (GDateTime *datetime)
2037 g_return_val_if_fail (datetime != NULL, NULL);
2038 g_return_val_if_fail (datetime->ref_count > 0, NULL);
2040 g_atomic_int_inc (&datetime->ref_count);
2046 * g_date_time_unref:
2047 * @datetime: a #GDateTime
2049 * Atomically decrements the reference count of @datetime by one.
2051 * When the reference count reaches zero, the resources allocated by
2052 * @datetime are freed
2057 g_date_time_unref (GDateTime *datetime)
2059 g_return_if_fail (datetime != NULL);
2060 g_return_if_fail (datetime->ref_count > 0);
2062 if (g_atomic_int_dec_and_test (&datetime->ref_count))
2063 g_date_time_free (datetime);
2067 * g_date_time_to_local:
2068 * @datetime: a #GDateTime
2070 * Creates a new #GDateTime with @datetime converted to local time.
2072 * Return value: the newly created #GDateTime
2077 g_date_time_to_local (const GDateTime *datetime)
2081 g_return_val_if_fail (datetime != NULL, NULL);
2083 dt = g_date_time_copy (datetime);
2086 dt->tz = g_date_time_create_time_zone (dt, NULL);
2090 g_date_time_add_usec (dt, dt->tz->offset * USEC_PER_SECOND);
2097 * g_date_time_to_epoch:
2098 * @datetime: a #GDateTime
2100 * Converts @datetime into an integer representing seconds since the
2103 * Return value: @datetime as seconds since the Unix epoch
2108 g_date_time_to_epoch (const GDateTime *datetime)
2115 g_return_val_if_fail (datetime != NULL, 0);
2116 g_return_val_if_fail (datetime->period == 0, 0);
2118 g_date_time_get_dmy (datetime, &day, &month, &year);
2120 /* FIXME we use gint64, we shold expand these limits */
2123 else if (year > 2037)
2126 memset (&tm, 0, sizeof (tm));
2128 tm.tm_year = year - 1900;
2129 tm.tm_mon = month - 1;
2131 tm.tm_hour = g_date_time_get_hour (datetime);
2132 tm.tm_min = g_date_time_get_minute (datetime);
2133 tm.tm_sec = g_date_time_get_second (datetime);
2136 return (gint64) mktime (&tm);
2140 * g_date_time_to_timeval:
2141 * @datetime: a #GDateTime
2144 * Converts @datetime into a #GTimeVal and stores the result into @timeval.
2149 g_date_time_to_timeval (const GDateTime *datetime,
2152 g_return_if_fail (datetime != NULL);
2157 if (G_LIKELY (datetime->period == 0))
2159 tv->tv_sec = g_date_time_to_epoch (datetime);
2160 tv->tv_usec = datetime->usec % USEC_PER_SECOND;
2165 * g_date_time_to_utc:
2166 * @datetime: a #GDateTime
2168 * Creates a new #GDateTime that reprents @datetime in Universal coordinated
2171 * Return value: the newly created #GDateTime which should be freed with
2172 * g_date_time_unref().
2177 g_date_time_to_utc (const GDateTime *datetime)
2182 g_return_val_if_fail (datetime != NULL, NULL);
2184 ts = g_date_time_get_utc_offset (datetime) * -1;
2185 dt = g_date_time_add (datetime, ts);
2192 * g_date_time_new_today:
2194 * Createsa new #GDateTime that represents Midnight on the current day.
2196 * Return value: the newly created #GDateTime which should be freed with
2197 * g_date_time_unref().
2202 g_date_time_new_today (void)
2206 dt = g_date_time_new_now ();
2213 * g_date_time_new_utc_now:
2215 * Creates a new #GDateTime that represents the current instant in Universal
2216 * Coordinated Time (UTC).
2218 * Return value: the newly created #GDateTime which should be freed with
2219 * g_date_time_unref().
2224 g_date_time_new_utc_now (void)
2226 GDateTime *utc, *now;
2228 now = g_date_time_new_now ();
2229 utc = g_date_time_to_utc (now);
2230 g_date_time_unref (now);