Add support for g_auto[s]list(Type)
[glib.git] / glib / gdatetime.c
blob88af308e9ca1cbaa79d502c5524718d7cbb052da
1 /* gdatetime.c
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 License
19 * along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Authors: Christian Hergert <chris@dronelabs.com>
22 * Thiago Santos <thiago.sousa.santos@collabora.co.uk>
23 * Emmanuele Bassi <ebassi@linux.intel.com>
24 * Ryan Lortie <desrt@desrt.ca>
25 * Robert Ancell <robert.ancell@canonical.com>
28 /* Algorithms within this file are based on the Calendar FAQ by
29 * Claus Tondering. It can be found at
30 * http://www.tondering.dk/claus/cal/calendar29.txt
32 * Copyright and disclaimer
33 * ------------------------
34 * This document is Copyright (C) 2008 by Claus Tondering.
35 * E-mail: claus@tondering.dk. (Please include the word
36 * "calendar" in the subject line.)
37 * The document may be freely distributed, provided this
38 * copyright notice is included and no money is charged for
39 * the document.
41 * This document is provided "as is". No warranties are made as
42 * to its correctness.
45 /* Prologue {{{1 */
47 #include "config.h"
49 #include <stdlib.h>
50 #include <string.h>
52 #ifdef HAVE_LANGINFO_TIME
53 #include <langinfo.h>
54 #endif
56 #include "gdatetime.h"
58 #include "gslice.h"
59 #include "gatomic.h"
60 #include "gcharset.h"
61 #include "gconvert.h"
62 #include "gfileutils.h"
63 #include "ghash.h"
64 #include "gmain.h"
65 #include "gmappedfile.h"
66 #include "gstrfuncs.h"
67 #include "gtestutils.h"
68 #include "gthread.h"
69 #include "gtimezone.h"
71 #include "glibintl.h"
73 #ifndef G_OS_WIN32
74 #include <sys/time.h>
75 #include <time.h>
76 #endif /* !G_OS_WIN32 */
78 /**
79 * SECTION:date-time
80 * @title: GDateTime
81 * @short_description: a structure representing Date and Time
82 * @see_also: #GTimeZone
84 * #GDateTime is a structure that combines a Gregorian date and time
85 * into a single structure. It provides many conversion and methods to
86 * manipulate dates and times. Time precision is provided down to
87 * microseconds and the time can range (proleptically) from 0001-01-01
88 * 00:00:00 to 9999-12-31 23:59:59.999999. #GDateTime follows POSIX
89 * time in the sense that it is oblivious to leap seconds.
91 * #GDateTime is an immutable object; once it has been created it cannot
92 * be modified further. All modifiers will create a new #GDateTime.
93 * Nearly all such functions can fail due to the date or time going out
94 * of range, in which case %NULL will be returned.
96 * #GDateTime is reference counted: the reference count is increased by calling
97 * g_date_time_ref() and decreased by calling g_date_time_unref(). When the
98 * reference count drops to 0, the resources allocated by the #GDateTime
99 * structure are released.
101 * Many parts of the API may produce non-obvious results. As an
102 * example, adding two months to January 31st will yield March 31st
103 * whereas adding one month and then one month again will yield either
104 * March 28th or March 29th. Also note that adding 24 hours is not
105 * always the same as adding one day (since days containing daylight
106 * savings time transitions are either 23 or 25 hours in length).
108 * #GDateTime is available since GLib 2.26.
111 struct _GDateTime
113 /* Microsecond timekeeping within Day */
114 guint64 usec;
116 /* TimeZone information */
117 GTimeZone *tz;
118 gint interval;
120 /* 1 is 0001-01-01 in Proleptic Gregorian */
121 gint32 days;
123 volatile gint ref_count;
126 /* Time conversion {{{1 */
128 #define UNIX_EPOCH_START 719163
129 #define INSTANT_TO_UNIX(instant) \
130 ((instant)/USEC_PER_SECOND - UNIX_EPOCH_START * SEC_PER_DAY)
131 #define UNIX_TO_INSTANT(unix) \
132 (((gint64) (unix) + UNIX_EPOCH_START * SEC_PER_DAY) * USEC_PER_SECOND)
133 #define UNIX_TO_INSTANT_IS_VALID(unix) \
134 ((gint64) (unix) <= INSTANT_TO_UNIX (G_MAXINT64))
136 #define DAYS_IN_4YEARS 1461 /* days in 4 years */
137 #define DAYS_IN_100YEARS 36524 /* days in 100 years */
138 #define DAYS_IN_400YEARS 146097 /* days in 400 years */
140 #define USEC_PER_SECOND (G_GINT64_CONSTANT (1000000))
141 #define USEC_PER_MINUTE (G_GINT64_CONSTANT (60000000))
142 #define USEC_PER_HOUR (G_GINT64_CONSTANT (3600000000))
143 #define USEC_PER_MILLISECOND (G_GINT64_CONSTANT (1000))
144 #define USEC_PER_DAY (G_GINT64_CONSTANT (86400000000))
145 #define SEC_PER_DAY (G_GINT64_CONSTANT (86400))
147 #define SECS_PER_MINUTE (60)
148 #define SECS_PER_HOUR (60 * SECS_PER_MINUTE)
149 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
150 #define SECS_PER_YEAR (365 * SECS_PER_DAY)
151 #define SECS_PER_JULIAN (DAYS_PER_PERIOD * SECS_PER_DAY)
153 #define GREGORIAN_LEAP(y) ((((y) % 4) == 0) && (!((((y) % 100) == 0) && (((y) % 400) != 0))))
154 #define JULIAN_YEAR(d) ((d)->julian / 365.25)
155 #define DAYS_PER_PERIOD (G_GINT64_CONSTANT (2914695))
157 static const guint16 days_in_months[2][13] =
159 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
160 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
163 static const guint16 days_in_year[2][13] =
165 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
166 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
169 #ifdef HAVE_LANGINFO_TIME
171 #define GET_AMPM(d) ((g_date_time_get_hour (d) < 12) ? \
172 nl_langinfo (AM_STR) : \
173 nl_langinfo (PM_STR))
175 #define PREFERRED_DATE_TIME_FMT nl_langinfo (D_T_FMT)
176 #define PREFERRED_DATE_FMT nl_langinfo (D_FMT)
177 #define PREFERRED_TIME_FMT nl_langinfo (T_FMT)
178 #define PREFERRED_12HR_TIME_FMT nl_langinfo (T_FMT_AMPM)
180 static const gint weekday_item[2][7] =
182 { ABDAY_2, ABDAY_3, ABDAY_4, ABDAY_5, ABDAY_6, ABDAY_7, ABDAY_1 },
183 { DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7, DAY_1 }
186 static const gint month_item[2][12] =
188 { ABMON_1, ABMON_2, ABMON_3, ABMON_4, ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10, ABMON_11, ABMON_12 },
189 { MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7, MON_8, MON_9, MON_10, MON_11, MON_12 },
192 #define WEEKDAY_ABBR(d) nl_langinfo (weekday_item[0][g_date_time_get_day_of_week (d) - 1])
193 #define WEEKDAY_FULL(d) nl_langinfo (weekday_item[1][g_date_time_get_day_of_week (d) - 1])
194 #define MONTH_ABBR(d) nl_langinfo (month_item[0][g_date_time_get_month (d) - 1])
195 #define MONTH_FULL(d) nl_langinfo (month_item[1][g_date_time_get_month (d) - 1])
197 #else
199 #define GET_AMPM(d) (get_fallback_ampm (g_date_time_get_hour (d)))
201 /* Translators: this is the preferred format for expressing the date and the time */
202 #define PREFERRED_DATE_TIME_FMT C_("GDateTime", "%a %b %e %H:%M:%S %Y")
204 /* Translators: this is the preferred format for expressing the date */
205 #define PREFERRED_DATE_FMT C_("GDateTime", "%m/%d/%y")
207 /* Translators: this is the preferred format for expressing the time */
208 #define PREFERRED_TIME_FMT C_("GDateTime", "%H:%M:%S")
210 /* Translators: this is the preferred format for expressing 12 hour time */
211 #define PREFERRED_12HR_TIME_FMT C_("GDateTime", "%I:%M:%S %p")
213 #define WEEKDAY_ABBR(d) (get_weekday_name_abbr (g_date_time_get_day_of_week (d)))
214 #define WEEKDAY_FULL(d) (get_weekday_name (g_date_time_get_day_of_week (d)))
215 #define MONTH_ABBR(d) (get_month_name_abbr (g_date_time_get_month (d)))
216 #define MONTH_FULL(d) (get_month_name (g_date_time_get_month (d)))
218 static const gchar *
219 get_month_name (gint month)
221 switch (month)
223 case 1:
224 return C_("full month name", "January");
225 case 2:
226 return C_("full month name", "February");
227 case 3:
228 return C_("full month name", "March");
229 case 4:
230 return C_("full month name", "April");
231 case 5:
232 return C_("full month name", "May");
233 case 6:
234 return C_("full month name", "June");
235 case 7:
236 return C_("full month name", "July");
237 case 8:
238 return C_("full month name", "August");
239 case 9:
240 return C_("full month name", "September");
241 case 10:
242 return C_("full month name", "October");
243 case 11:
244 return C_("full month name", "November");
245 case 12:
246 return C_("full month name", "December");
248 default:
249 g_warning ("Invalid month number %d", month);
252 return NULL;
255 static const gchar *
256 get_month_name_abbr (gint month)
258 switch (month)
260 case 1:
261 return C_("abbreviated month name", "Jan");
262 case 2:
263 return C_("abbreviated month name", "Feb");
264 case 3:
265 return C_("abbreviated month name", "Mar");
266 case 4:
267 return C_("abbreviated month name", "Apr");
268 case 5:
269 return C_("abbreviated month name", "May");
270 case 6:
271 return C_("abbreviated month name", "Jun");
272 case 7:
273 return C_("abbreviated month name", "Jul");
274 case 8:
275 return C_("abbreviated month name", "Aug");
276 case 9:
277 return C_("abbreviated month name", "Sep");
278 case 10:
279 return C_("abbreviated month name", "Oct");
280 case 11:
281 return C_("abbreviated month name", "Nov");
282 case 12:
283 return C_("abbreviated month name", "Dec");
285 default:
286 g_warning ("Invalid month number %d", month);
289 return NULL;
292 static const gchar *
293 get_weekday_name (gint day)
295 switch (day)
297 case 1:
298 return C_("full weekday name", "Monday");
299 case 2:
300 return C_("full weekday name", "Tuesday");
301 case 3:
302 return C_("full weekday name", "Wednesday");
303 case 4:
304 return C_("full weekday name", "Thursday");
305 case 5:
306 return C_("full weekday name", "Friday");
307 case 6:
308 return C_("full weekday name", "Saturday");
309 case 7:
310 return C_("full weekday name", "Sunday");
312 default:
313 g_warning ("Invalid week day number %d", day);
316 return NULL;
319 static const gchar *
320 get_weekday_name_abbr (gint day)
322 switch (day)
324 case 1:
325 return C_("abbreviated weekday name", "Mon");
326 case 2:
327 return C_("abbreviated weekday name", "Tue");
328 case 3:
329 return C_("abbreviated weekday name", "Wed");
330 case 4:
331 return C_("abbreviated weekday name", "Thu");
332 case 5:
333 return C_("abbreviated weekday name", "Fri");
334 case 6:
335 return C_("abbreviated weekday name", "Sat");
336 case 7:
337 return C_("abbreviated weekday name", "Sun");
339 default:
340 g_warning ("Invalid week day number %d", day);
343 return NULL;
346 #endif /* HAVE_LANGINFO_TIME */
348 /* Format AM/PM indicator if the locale does not have a localized version. */
349 static const gchar *
350 get_fallback_ampm (gint hour)
352 if (hour < 12)
353 /* Translators: 'before midday' indicator */
354 return C_("GDateTime", "AM");
355 else
356 /* Translators: 'after midday' indicator */
357 return C_("GDateTime", "PM");
360 static inline gint
361 ymd_to_days (gint year,
362 gint month,
363 gint day)
365 gint64 days;
367 days = (year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100)
368 + ((year - 1) / 400);
370 days += days_in_year[0][month - 1];
371 if (GREGORIAN_LEAP (year) && month > 2)
372 day++;
374 days += day;
376 return days;
379 static void
380 g_date_time_get_week_number (GDateTime *datetime,
381 gint *week_number,
382 gint *day_of_week,
383 gint *day_of_year)
385 gint a, b, c, d, e, f, g, n, s, month, day, year;
387 g_date_time_get_ymd (datetime, &year, &month, &day);
389 if (month <= 2)
391 a = g_date_time_get_year (datetime) - 1;
392 b = (a / 4) - (a / 100) + (a / 400);
393 c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
394 s = b - c;
395 e = 0;
396 f = day - 1 + (31 * (month - 1));
398 else
400 a = year;
401 b = (a / 4) - (a / 100) + (a / 400);
402 c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
403 s = b - c;
404 e = s + 1;
405 f = day + (((153 * (month - 3)) + 2) / 5) + 58 + s;
408 g = (a + b) % 7;
409 d = (f + g - e) % 7;
410 n = f + 3 - d;
412 if (week_number)
414 if (n < 0)
415 *week_number = 53 - ((g - s) / 5);
416 else if (n > 364 + s)
417 *week_number = 1;
418 else
419 *week_number = (n / 7) + 1;
422 if (day_of_week)
423 *day_of_week = d + 1;
425 if (day_of_year)
426 *day_of_year = f + 1;
429 /* Lifecycle {{{1 */
431 static GDateTime *
432 g_date_time_alloc (GTimeZone *tz)
434 GDateTime *datetime;
436 datetime = g_slice_new0 (GDateTime);
437 datetime->tz = g_time_zone_ref (tz);
438 datetime->ref_count = 1;
440 return datetime;
444 * g_date_time_ref:
445 * @datetime: a #GDateTime
447 * Atomically increments the reference count of @datetime by one.
449 * Returns: the #GDateTime with the reference count increased
451 * Since: 2.26
453 GDateTime *
454 g_date_time_ref (GDateTime *datetime)
456 g_return_val_if_fail (datetime != NULL, NULL);
457 g_return_val_if_fail (datetime->ref_count > 0, NULL);
459 g_atomic_int_inc (&datetime->ref_count);
461 return datetime;
465 * g_date_time_unref:
466 * @datetime: a #GDateTime
468 * Atomically decrements the reference count of @datetime by one.
470 * When the reference count reaches zero, the resources allocated by
471 * @datetime are freed
473 * Since: 2.26
475 void
476 g_date_time_unref (GDateTime *datetime)
478 g_return_if_fail (datetime != NULL);
479 g_return_if_fail (datetime->ref_count > 0);
481 if (g_atomic_int_dec_and_test (&datetime->ref_count))
483 g_time_zone_unref (datetime->tz);
484 g_slice_free (GDateTime, datetime);
488 /* Internal state transformers {{{1 */
489 /*< internal >
490 * g_date_time_to_instant:
491 * @datetime: a #GDateTime
493 * Convert a @datetime into an instant.
495 * An instant is a number that uniquely describes a particular
496 * microsecond in time, taking time zone considerations into account.
497 * (ie: "03:00 -0400" is the same instant as "02:00 -0500").
499 * An instant is always positive but we use a signed return value to
500 * avoid troubles with C.
502 static gint64
503 g_date_time_to_instant (GDateTime *datetime)
505 gint64 offset;
507 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
508 offset *= USEC_PER_SECOND;
510 return datetime->days * USEC_PER_DAY + datetime->usec - offset;
513 /*< internal >
514 * g_date_time_from_instant:
515 * @tz: a #GTimeZone
516 * @instant: a instant in time
518 * Creates a #GDateTime from a time zone and an instant.
520 * This might fail if the time ends up being out of range.
522 static GDateTime *
523 g_date_time_from_instant (GTimeZone *tz,
524 gint64 instant)
526 GDateTime *datetime;
527 gint64 offset;
529 if (instant < 0 || instant > G_GINT64_CONSTANT (1000000000000000000))
530 return NULL;
532 datetime = g_date_time_alloc (tz);
533 datetime->interval = g_time_zone_find_interval (tz,
534 G_TIME_TYPE_UNIVERSAL,
535 INSTANT_TO_UNIX (instant));
536 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
537 offset *= USEC_PER_SECOND;
539 instant += offset;
541 datetime->days = instant / USEC_PER_DAY;
542 datetime->usec = instant % USEC_PER_DAY;
544 if (datetime->days < 1 || 3652059 < datetime->days)
546 g_date_time_unref (datetime);
547 datetime = NULL;
550 return datetime;
554 /*< internal >
555 * g_date_time_deal_with_date_change:
556 * @datetime: a #GDateTime
558 * This function should be called whenever the date changes by adding
559 * days, months or years. It does three things.
561 * First, we ensure that the date falls between 0001-01-01 and
562 * 9999-12-31 and return %FALSE if it does not.
564 * Next we update the ->interval field.
566 * Finally, we ensure that the resulting date and time pair exists (by
567 * ensuring that our time zone has an interval containing it) and
568 * adjusting as required. For example, if we have the time 02:30:00 on
569 * March 13 2010 in Toronto and we add 1 day to it, we would end up with
570 * 2:30am on March 14th, which doesn't exist. In that case, we bump the
571 * time up to 3:00am.
573 static gboolean
574 g_date_time_deal_with_date_change (GDateTime *datetime)
576 GTimeType was_dst;
577 gint64 full_time;
578 gint64 usec;
580 if (datetime->days < 1 || datetime->days > 3652059)
581 return FALSE;
583 was_dst = g_time_zone_is_dst (datetime->tz, datetime->interval);
585 full_time = datetime->days * USEC_PER_DAY + datetime->usec;
588 usec = full_time % USEC_PER_SECOND;
589 full_time /= USEC_PER_SECOND;
590 full_time -= UNIX_EPOCH_START * SEC_PER_DAY;
592 datetime->interval = g_time_zone_adjust_time (datetime->tz,
593 was_dst,
594 &full_time);
595 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
596 full_time *= USEC_PER_SECOND;
597 full_time += usec;
599 datetime->days = full_time / USEC_PER_DAY;
600 datetime->usec = full_time % USEC_PER_DAY;
602 /* maybe daylight time caused us to shift to a different day,
603 * but it definitely didn't push us into a different year */
604 return TRUE;
607 static GDateTime *
608 g_date_time_replace_days (GDateTime *datetime,
609 gint days)
611 GDateTime *new;
613 new = g_date_time_alloc (datetime->tz);
614 new->interval = datetime->interval;
615 new->usec = datetime->usec;
616 new->days = days;
618 if (!g_date_time_deal_with_date_change (new))
620 g_date_time_unref (new);
621 new = NULL;
624 return new;
627 /* now/unix/timeval Constructors {{{1 */
629 /*< internal >
630 * g_date_time_new_from_timeval:
631 * @tz: a #GTimeZone
632 * @tv: a #GTimeVal
634 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
635 * given time zone @tz.
637 * The time contained in a #GTimeVal is always stored in the form of
638 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
639 * given time zone.
641 * This call can fail (returning %NULL) if @tv represents a time outside
642 * of the supported range of #GDateTime.
644 * You should release the return value by calling g_date_time_unref()
645 * when you are done with it.
647 * Returns: a new #GDateTime, or %NULL
649 * Since: 2.26
651 static GDateTime *
652 g_date_time_new_from_timeval (GTimeZone *tz,
653 const GTimeVal *tv)
655 if ((gint64) tv->tv_sec > G_MAXINT64 - 1 ||
656 !UNIX_TO_INSTANT_IS_VALID ((gint64) tv->tv_sec + 1))
657 return NULL;
659 return g_date_time_from_instant (tz, tv->tv_usec +
660 UNIX_TO_INSTANT (tv->tv_sec));
663 /*< internal >
664 * g_date_time_new_from_unix:
665 * @tz: a #GTimeZone
666 * @t: the Unix time
668 * Creates a #GDateTime corresponding to the given Unix time @t in the
669 * given time zone @tz.
671 * Unix time is the number of seconds that have elapsed since 1970-01-01
672 * 00:00:00 UTC, regardless of the time zone given.
674 * This call can fail (returning %NULL) if @t represents a time outside
675 * of the supported range of #GDateTime.
677 * You should release the return value by calling g_date_time_unref()
678 * when you are done with it.
680 * Returns: a new #GDateTime, or %NULL
682 * Since: 2.26
684 static GDateTime *
685 g_date_time_new_from_unix (GTimeZone *tz,
686 gint64 secs)
688 if (!UNIX_TO_INSTANT_IS_VALID (secs))
689 return NULL;
691 return g_date_time_from_instant (tz, UNIX_TO_INSTANT (secs));
695 * g_date_time_new_now:
696 * @tz: a #GTimeZone
698 * Creates a #GDateTime corresponding to this exact instant in the given
699 * time zone @tz. The time is as accurate as the system allows, to a
700 * maximum accuracy of 1 microsecond.
702 * This function will always succeed unless the system clock is set to
703 * truly insane values (or unless GLib is still being used after the
704 * year 9999).
706 * You should release the return value by calling g_date_time_unref()
707 * when you are done with it.
709 * Returns: a new #GDateTime, or %NULL
711 * Since: 2.26
713 GDateTime *
714 g_date_time_new_now (GTimeZone *tz)
716 GTimeVal tv;
718 g_get_current_time (&tv);
720 return g_date_time_new_from_timeval (tz, &tv);
724 * g_date_time_new_now_local:
726 * Creates a #GDateTime corresponding to this exact instant in the local
727 * time zone.
729 * This is equivalent to calling g_date_time_new_now() with the time
730 * zone returned by g_time_zone_new_local().
732 * Returns: a new #GDateTime, or %NULL
734 * Since: 2.26
736 GDateTime *
737 g_date_time_new_now_local (void)
739 GDateTime *datetime;
740 GTimeZone *local;
742 local = g_time_zone_new_local ();
743 datetime = g_date_time_new_now (local);
744 g_time_zone_unref (local);
746 return datetime;
750 * g_date_time_new_now_utc:
752 * Creates a #GDateTime corresponding to this exact instant in UTC.
754 * This is equivalent to calling g_date_time_new_now() with the time
755 * zone returned by g_time_zone_new_utc().
757 * Returns: a new #GDateTime, or %NULL
759 * Since: 2.26
761 GDateTime *
762 g_date_time_new_now_utc (void)
764 GDateTime *datetime;
765 GTimeZone *utc;
767 utc = g_time_zone_new_utc ();
768 datetime = g_date_time_new_now (utc);
769 g_time_zone_unref (utc);
771 return datetime;
775 * g_date_time_new_from_unix_local:
776 * @t: the Unix time
778 * Creates a #GDateTime corresponding to the given Unix time @t in the
779 * local time zone.
781 * Unix time is the number of seconds that have elapsed since 1970-01-01
782 * 00:00:00 UTC, regardless of the local time offset.
784 * This call can fail (returning %NULL) if @t represents a time outside
785 * of the supported range of #GDateTime.
787 * You should release the return value by calling g_date_time_unref()
788 * when you are done with it.
790 * Returns: a new #GDateTime, or %NULL
792 * Since: 2.26
794 GDateTime *
795 g_date_time_new_from_unix_local (gint64 t)
797 GDateTime *datetime;
798 GTimeZone *local;
800 local = g_time_zone_new_local ();
801 datetime = g_date_time_new_from_unix (local, t);
802 g_time_zone_unref (local);
804 return datetime;
808 * g_date_time_new_from_unix_utc:
809 * @t: the Unix time
811 * Creates a #GDateTime corresponding to the given Unix time @t in UTC.
813 * Unix time is the number of seconds that have elapsed since 1970-01-01
814 * 00:00:00 UTC.
816 * This call can fail (returning %NULL) if @t represents a time outside
817 * of the supported range of #GDateTime.
819 * You should release the return value by calling g_date_time_unref()
820 * when you are done with it.
822 * Returns: a new #GDateTime, or %NULL
824 * Since: 2.26
826 GDateTime *
827 g_date_time_new_from_unix_utc (gint64 t)
829 GDateTime *datetime;
830 GTimeZone *utc;
832 utc = g_time_zone_new_utc ();
833 datetime = g_date_time_new_from_unix (utc, t);
834 g_time_zone_unref (utc);
836 return datetime;
840 * g_date_time_new_from_timeval_local:
841 * @tv: a #GTimeVal
843 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
844 * local time zone.
846 * The time contained in a #GTimeVal is always stored in the form of
847 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
848 * local time offset.
850 * This call can fail (returning %NULL) if @tv represents a time outside
851 * of the supported range of #GDateTime.
853 * You should release the return value by calling g_date_time_unref()
854 * when you are done with it.
856 * Returns: a new #GDateTime, or %NULL
858 * Since: 2.26
860 GDateTime *
861 g_date_time_new_from_timeval_local (const GTimeVal *tv)
863 GDateTime *datetime;
864 GTimeZone *local;
866 local = g_time_zone_new_local ();
867 datetime = g_date_time_new_from_timeval (local, tv);
868 g_time_zone_unref (local);
870 return datetime;
874 * g_date_time_new_from_timeval_utc:
875 * @tv: a #GTimeVal
877 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in UTC.
879 * The time contained in a #GTimeVal is always stored in the form of
880 * seconds elapsed since 1970-01-01 00:00:00 UTC.
882 * This call can fail (returning %NULL) if @tv represents a time outside
883 * of the supported range of #GDateTime.
885 * You should release the return value by calling g_date_time_unref()
886 * when you are done with it.
888 * Returns: a new #GDateTime, or %NULL
890 * Since: 2.26
892 GDateTime *
893 g_date_time_new_from_timeval_utc (const GTimeVal *tv)
895 GDateTime *datetime;
896 GTimeZone *utc;
898 utc = g_time_zone_new_utc ();
899 datetime = g_date_time_new_from_timeval (utc, tv);
900 g_time_zone_unref (utc);
902 return datetime;
905 /* Parse integers in the form d (week days), dd (hours etc), ddd (ordinal days) or dddd (years) */
906 static gboolean
907 get_iso8601_int (const gchar *text, gsize length, gint *value)
909 gint i, v = 0;
911 if (length < 1 || length > 4)
912 return FALSE;
914 for (i = 0; i < length; i++)
916 const gchar c = text[i];
917 if (c < '0' || c > '9')
918 return FALSE;
919 v = v * 10 + (c - '0');
922 *value = v;
923 return TRUE;
926 /* Parse seconds in the form ss or ss.sss (variable length decimal) */
927 static gboolean
928 get_iso8601_seconds (const gchar *text, gsize length, gdouble *value)
930 gint i;
931 gdouble multiplier = 0.1, v = 0;
933 if (length < 2)
934 return FALSE;
936 for (i = 0; i < 2; i++)
938 const gchar c = text[i];
939 if (c < '0' || c > '9')
940 return FALSE;
941 v = v * 10 + (c - '0');
944 if (length > 2 && !(text[i] == '.' || text[i] == ','))
945 return FALSE;
946 i++;
947 if (i == length)
948 return FALSE;
950 for (; i < length; i++)
952 const gchar c = text[i];
953 if (c < '0' || c > '9')
954 return FALSE;
955 v += (c - '0') * multiplier;
956 multiplier *= 0.1;
959 *value = v;
960 return TRUE;
963 static GDateTime *
964 g_date_time_new_ordinal (GTimeZone *tz, gint year, gint ordinal_day, gint hour, gint minute, gdouble seconds)
966 GDateTime *dt;
968 if (ordinal_day < 1 || ordinal_day > (GREGORIAN_LEAP (year) ? 366 : 365))
969 return NULL;
971 dt = g_date_time_new (tz, year, 1, 1, hour, minute, seconds);
972 dt->days += ordinal_day - 1;
974 return dt;
977 static GDateTime *
978 g_date_time_new_week (GTimeZone *tz, gint year, gint week, gint week_day, gint hour, gint minute, gdouble seconds)
980 gint64 p;
981 gint max_week, jan4_week_day, ordinal_day;
982 GDateTime *dt;
984 p = (year * 365 + (year / 4) - (year / 100) + (year / 400)) % 7;
985 max_week = p == 4 ? 53 : 52;
987 if (week < 1 || week > max_week || week_day < 1 || week_day > 7)
988 return NULL;
990 dt = g_date_time_new (tz, year, 1, 4, 0, 0, 0);
991 g_date_time_get_week_number (dt, NULL, &jan4_week_day, NULL);
992 ordinal_day = (week * 7) + week_day - (jan4_week_day + 3);
993 if (ordinal_day < 0)
995 year--;
996 ordinal_day += GREGORIAN_LEAP (year) ? 366 : 365;
998 else if (ordinal_day > (GREGORIAN_LEAP (year) ? 366 : 365))
1000 ordinal_day -= (GREGORIAN_LEAP (year) ? 366 : 365);
1001 year++;
1004 return g_date_time_new_ordinal (tz, year, ordinal_day, hour, minute, seconds);
1007 static GDateTime *
1008 parse_iso8601_date (const gchar *text, gsize length,
1009 gint hour, gint minute, gdouble seconds, GTimeZone *tz)
1011 /* YYYY-MM-DD */
1012 if (length == 10 && text[4] == '-' && text[7] == '-')
1014 int year, month, day;
1015 if (!get_iso8601_int (text, 4, &year) ||
1016 !get_iso8601_int (text + 5, 2, &month) ||
1017 !get_iso8601_int (text + 8, 2, &day))
1018 return NULL;
1019 return g_date_time_new (tz, year, month, day, hour, minute, seconds);
1021 /* YYYY-DDD */
1022 else if (length == 8 && text[4] == '-')
1024 gint year, ordinal_day;
1025 if (!get_iso8601_int (text, 4, &year) ||
1026 !get_iso8601_int (text + 5, 3, &ordinal_day))
1027 return NULL;
1028 return g_date_time_new_ordinal (tz, year, ordinal_day, hour, minute, seconds);
1030 /* YYYY-Www-D */
1031 else if (length == 10 && text[4] == '-' && text[5] == 'W' && text[8] == '-')
1033 gint year, week, week_day;
1034 if (!get_iso8601_int (text, 4, &year) ||
1035 !get_iso8601_int (text + 6, 2, &week) ||
1036 !get_iso8601_int (text + 9, 1, &week_day))
1037 return NULL;
1038 return g_date_time_new_week (tz, year, week, week_day, hour, minute, seconds);
1040 /* YYYYWwwD */
1041 else if (length == 8 && text[4] == 'W')
1043 gint year, week, week_day;
1044 if (!get_iso8601_int (text, 4, &year) ||
1045 !get_iso8601_int (text + 5, 2, &week) ||
1046 !get_iso8601_int (text + 7, 1, &week_day))
1047 return NULL;
1048 return g_date_time_new_week (tz, year, week, week_day, hour, minute, seconds);
1050 /* YYYYMMDD */
1051 else if (length == 8)
1053 int year, month, day;
1054 if (!get_iso8601_int (text, 4, &year) ||
1055 !get_iso8601_int (text + 4, 2, &month) ||
1056 !get_iso8601_int (text + 6, 2, &day))
1057 return NULL;
1058 return g_date_time_new (tz, year, month, day, hour, minute, seconds);
1060 /* YYYYDDD */
1061 else if (length == 7)
1063 gint year, ordinal_day;
1064 if (!get_iso8601_int (text, 4, &year) ||
1065 !get_iso8601_int (text + 4, 3, &ordinal_day))
1066 return NULL;
1067 return g_date_time_new_ordinal (tz, year, ordinal_day, hour, minute, seconds);
1069 else
1070 return FALSE;
1073 static GTimeZone *
1074 parse_iso8601_timezone (const gchar *text, gsize length, gssize *tz_offset)
1076 gint i, tz_length, offset_sign = 1, offset_hours, offset_minutes;
1077 GTimeZone *tz;
1079 /* UTC uses Z suffix */
1080 if (length > 0 && text[length - 1] == 'Z')
1082 *tz_offset = length - 1;
1083 return g_time_zone_new_utc ();
1086 /* Look for '+' or '-' of offset */
1087 for (i = length - 1; i >= 0; i--)
1088 if (text[i] == '+' || text[i] == '-')
1090 offset_sign = text[i] == '-' ? -1 : 1;
1091 break;
1093 if (i < 0)
1094 return NULL;
1095 tz_length = length - i;
1097 /* +hh:mm or -hh:mm */
1098 if (tz_length == 6 && text[i+3] == ':')
1100 if (!get_iso8601_int (text + i + 1, 2, &offset_hours) ||
1101 !get_iso8601_int (text + i + 4, 2, &offset_minutes))
1102 return NULL;
1104 /* +hhmm or -hhmm */
1105 else if (tz_length == 5)
1107 if (!get_iso8601_int (text + i + 1, 2, &offset_hours) ||
1108 !get_iso8601_int (text + i + 3, 2, &offset_minutes))
1109 return NULL;
1111 /* +hh or -hh */
1112 else if (tz_length == 3)
1114 if (!get_iso8601_int (text + i + 1, 2, &offset_hours))
1115 return NULL;
1116 offset_minutes = 0;
1118 else
1119 return NULL;
1121 *tz_offset = i;
1122 tz = g_time_zone_new (text + i);
1124 /* Double-check that the GTimeZone matches our interpretation of the timezone.
1125 * Failure would indicate a bug either here of in the GTimeZone code. */
1126 g_assert (g_time_zone_get_offset (tz, 0) == offset_sign * (offset_hours * 3600 + offset_minutes * 60));
1128 return tz;
1131 static gboolean
1132 parse_iso8601_time (const gchar *text, gsize length,
1133 gint *hour, gint *minute, gdouble *seconds, GTimeZone **tz)
1135 gssize tz_offset = -1;
1137 /* Check for timezone suffix */
1138 *tz = parse_iso8601_timezone (text, length, &tz_offset);
1139 if (tz_offset >= 0)
1140 length = tz_offset;
1142 /* hh:mm:ss(.sss) */
1143 if (length >= 8 && text[2] == ':' && text[5] == ':')
1145 return get_iso8601_int (text, 2, hour) &&
1146 get_iso8601_int (text + 3, 2, minute) &&
1147 get_iso8601_seconds (text + 6, length - 6, seconds);
1149 /* hhmmss(.sss) */
1150 else if (length >= 6)
1152 return get_iso8601_int (text, 2, hour) &&
1153 get_iso8601_int (text + 2, 2, minute) &&
1154 get_iso8601_seconds (text + 4, length - 4, seconds);
1156 else
1157 return FALSE;
1161 * g_date_time_new_from_iso8601:
1162 * @text: an ISO 8601 formatted time string.
1163 * @default_tz: (nullable): a #GTimeZone to use if the text doesn't contain a
1164 * timezone, or %NULL.
1166 * Creates a #GDateTime corresponding to the given
1167 * [ISO 8601 formatted string](https://en.wikipedia.org/wiki/ISO_8601)
1168 * @text. ISO 8601 strings of the form <date><sep><time><tz> are supported.
1170 * <sep> is the separator and can be either 'T', 't' or ' '.
1172 * <date> is in the form:
1174 * - `YYYY-MM-DD` - Year/month/day, e.g. 2016-08-24.
1175 * - `YYYYMMDD` - Same as above without dividers.
1176 * - `YYYY-DDD` - Ordinal day where DDD is from 001 to 366, e.g. 2016-237.
1177 * - `YYYYDDD` - Same as above without dividers.
1178 * - `YYYY-Www-D` - Week day where ww is from 01 to 52 and D from 1-7,
1179 * e.g. 2016-W34-3.
1180 * - `YYYYWwwD` - Same as above without dividers.
1182 * <time> is in the form:
1184 * - `hh:mm:ss(.sss)` - Hours, minutes, seconds (subseconds), e.g. 22:10:42.123.
1185 * - `hhmmss(.sss)` - Same as above without dividers.
1187 * <tz> is an optional timezone suffix of the form:
1189 * - `Z` - UTC.
1190 * - `+hh:mm` or `-hh:mm` - Offset from UTC in hours and minutes, e.g. +12:00.
1191 * - `+hh` or `-hh` - Offset from UTC in hours, e.g. +12.
1193 * If the timezone is not provided in @text it must be provided in @default_tz
1194 * (this field is otherwise ignored).
1196 * This call can fail (returning %NULL) if @text is not a valid ISO 8601
1197 * formatted string.
1199 * You should release the return value by calling g_date_time_unref()
1200 * when you are done with it.
1202 * Returns: (transfer full) (nullable): a new #GDateTime, or %NULL
1204 * Since: 2.56
1206 GDateTime *
1207 g_date_time_new_from_iso8601 (const gchar *text, GTimeZone *default_tz)
1209 gint length, date_length = -1;
1210 gint hour = 0, minute = 0;
1211 gdouble seconds = 0.0;
1212 GTimeZone *tz = NULL;
1213 GDateTime *datetime = NULL;
1215 g_return_val_if_fail (text != NULL, NULL);
1217 /* Count length of string and find date / time separator ('T', 't', or ' ') */
1218 for (length = 0; text[length] != '\0'; length++)
1220 if (date_length < 0 && (text[length] == 'T' || text[length] == 't' || text[length] == ' '))
1221 date_length = length;
1224 if (date_length < 0)
1225 return NULL;
1227 if (!parse_iso8601_time (text + date_length + 1, length - (date_length + 1),
1228 &hour, &minute, &seconds, &tz))
1229 goto out;
1230 if (tz == NULL && default_tz == NULL)
1231 return NULL;
1233 datetime = parse_iso8601_date (text, date_length, hour, minute, seconds, tz ? tz : default_tz);
1235 out:
1236 if (tz != NULL)
1237 g_time_zone_unref (tz);
1238 return datetime;
1241 /* full new functions {{{1 */
1244 * g_date_time_new:
1245 * @tz: a #GTimeZone
1246 * @year: the year component of the date
1247 * @month: the month component of the date
1248 * @day: the day component of the date
1249 * @hour: the hour component of the date
1250 * @minute: the minute component of the date
1251 * @seconds: the number of seconds past the minute
1253 * Creates a new #GDateTime corresponding to the given date and time in
1254 * the time zone @tz.
1256 * The @year must be between 1 and 9999, @month between 1 and 12 and @day
1257 * between 1 and 28, 29, 30 or 31 depending on the month and the year.
1259 * @hour must be between 0 and 23 and @minute must be between 0 and 59.
1261 * @seconds must be at least 0.0 and must be strictly less than 60.0.
1262 * It will be rounded down to the nearest microsecond.
1264 * If the given time is not representable in the given time zone (for
1265 * example, 02:30 on March 14th 2010 in Toronto, due to daylight savings
1266 * time) then the time will be rounded up to the nearest existing time
1267 * (in this case, 03:00). If this matters to you then you should verify
1268 * the return value for containing the same as the numbers you gave.
1270 * In the case that the given time is ambiguous in the given time zone
1271 * (for example, 01:30 on November 7th 2010 in Toronto, due to daylight
1272 * savings time) then the time falling within standard (ie:
1273 * non-daylight) time is taken.
1275 * It not considered a programmer error for the values to this function
1276 * to be out of range, but in the case that they are, the function will
1277 * return %NULL.
1279 * You should release the return value by calling g_date_time_unref()
1280 * when you are done with it.
1282 * Returns: a new #GDateTime, or %NULL
1284 * Since: 2.26
1286 GDateTime *
1287 g_date_time_new (GTimeZone *tz,
1288 gint year,
1289 gint month,
1290 gint day,
1291 gint hour,
1292 gint minute,
1293 gdouble seconds)
1295 GDateTime *datetime;
1296 gint64 full_time;
1297 gint64 usec;
1299 g_return_val_if_fail (tz != NULL, NULL);
1301 if (year < 1 || year > 9999 ||
1302 month < 1 || month > 12 ||
1303 day < 1 || day > days_in_months[GREGORIAN_LEAP (year)][month] ||
1304 hour < 0 || hour > 23 ||
1305 minute < 0 || minute > 59 ||
1306 seconds < 0.0 || seconds >= 60.0)
1307 return NULL;
1309 datetime = g_date_time_alloc (tz);
1310 datetime->days = ymd_to_days (year, month, day);
1311 datetime->usec = (hour * USEC_PER_HOUR)
1312 + (minute * USEC_PER_MINUTE)
1313 + (gint64) (seconds * USEC_PER_SECOND);
1315 full_time = SEC_PER_DAY *
1316 (ymd_to_days (year, month, day) - UNIX_EPOCH_START) +
1317 SECS_PER_HOUR * hour +
1318 SECS_PER_MINUTE * minute +
1319 (int) seconds;
1321 datetime->interval = g_time_zone_adjust_time (datetime->tz,
1322 G_TIME_TYPE_STANDARD,
1323 &full_time);
1325 /* This is the correct way to convert a scaled FP value to integer.
1326 * If this surprises you, please observe that (int)(1.000001 * 1e6)
1327 * is 1000000. This is not a problem with precision, it's just how
1328 * FP numbers work.
1329 * See https://bugzilla.gnome.org/show_bug.cgi?id=697715. */
1330 usec = seconds * USEC_PER_SECOND;
1331 if ((usec + 1) * 1e-6 <= seconds) {
1332 usec++;
1335 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
1336 datetime->days = full_time / SEC_PER_DAY;
1337 datetime->usec = (full_time % SEC_PER_DAY) * USEC_PER_SECOND;
1338 datetime->usec += usec % USEC_PER_SECOND;
1340 return datetime;
1344 * g_date_time_new_local:
1345 * @year: the year component of the date
1346 * @month: the month component of the date
1347 * @day: the day component of the date
1348 * @hour: the hour component of the date
1349 * @minute: the minute component of the date
1350 * @seconds: the number of seconds past the minute
1352 * Creates a new #GDateTime corresponding to the given date and time in
1353 * the local time zone.
1355 * This call is equivalent to calling g_date_time_new() with the time
1356 * zone returned by g_time_zone_new_local().
1358 * Returns: a #GDateTime, or %NULL
1360 * Since: 2.26
1362 GDateTime *
1363 g_date_time_new_local (gint year,
1364 gint month,
1365 gint day,
1366 gint hour,
1367 gint minute,
1368 gdouble seconds)
1370 GDateTime *datetime;
1371 GTimeZone *local;
1373 local = g_time_zone_new_local ();
1374 datetime = g_date_time_new (local, year, month, day, hour, minute, seconds);
1375 g_time_zone_unref (local);
1377 return datetime;
1381 * g_date_time_new_utc:
1382 * @year: the year component of the date
1383 * @month: the month component of the date
1384 * @day: the day component of the date
1385 * @hour: the hour component of the date
1386 * @minute: the minute component of the date
1387 * @seconds: the number of seconds past the minute
1389 * Creates a new #GDateTime corresponding to the given date and time in
1390 * UTC.
1392 * This call is equivalent to calling g_date_time_new() with the time
1393 * zone returned by g_time_zone_new_utc().
1395 * Returns: a #GDateTime, or %NULL
1397 * Since: 2.26
1399 GDateTime *
1400 g_date_time_new_utc (gint year,
1401 gint month,
1402 gint day,
1403 gint hour,
1404 gint minute,
1405 gdouble seconds)
1407 GDateTime *datetime;
1408 GTimeZone *utc;
1410 utc = g_time_zone_new_utc ();
1411 datetime = g_date_time_new (utc, year, month, day, hour, minute, seconds);
1412 g_time_zone_unref (utc);
1414 return datetime;
1417 /* Adders {{{1 */
1420 * g_date_time_add:
1421 * @datetime: a #GDateTime
1422 * @timespan: a #GTimeSpan
1424 * Creates a copy of @datetime and adds the specified timespan to the copy.
1426 * Returns: the newly created #GDateTime which should be freed with
1427 * g_date_time_unref().
1429 * Since: 2.26
1431 GDateTime*
1432 g_date_time_add (GDateTime *datetime,
1433 GTimeSpan timespan)
1435 return g_date_time_from_instant (datetime->tz, timespan +
1436 g_date_time_to_instant (datetime));
1440 * g_date_time_add_years:
1441 * @datetime: a #GDateTime
1442 * @years: the number of years
1444 * Creates a copy of @datetime and adds the specified number of years to the
1445 * copy. Add negative values to subtract years.
1447 * Returns: the newly created #GDateTime which should be freed with
1448 * g_date_time_unref().
1450 * Since: 2.26
1452 GDateTime *
1453 g_date_time_add_years (GDateTime *datetime,
1454 gint years)
1456 gint year, month, day;
1458 g_return_val_if_fail (datetime != NULL, NULL);
1460 if (years < -10000 || years > 10000)
1461 return NULL;
1463 g_date_time_get_ymd (datetime, &year, &month, &day);
1464 year += years;
1466 /* only possible issue is if we've entered a year with no February 29
1468 if (month == 2 && day == 29 && !GREGORIAN_LEAP (year))
1469 day = 28;
1471 return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
1475 * g_date_time_add_months:
1476 * @datetime: a #GDateTime
1477 * @months: the number of months
1479 * Creates a copy of @datetime and adds the specified number of months to the
1480 * copy. Add negative values to subtract months.
1482 * Returns: the newly created #GDateTime which should be freed with
1483 * g_date_time_unref().
1485 * Since: 2.26
1487 GDateTime*
1488 g_date_time_add_months (GDateTime *datetime,
1489 gint months)
1491 gint year, month, day;
1493 g_return_val_if_fail (datetime != NULL, NULL);
1494 g_date_time_get_ymd (datetime, &year, &month, &day);
1496 if (months < -120000 || months > 120000)
1497 return NULL;
1499 year += months / 12;
1500 month += months % 12;
1501 if (month < 1)
1503 month += 12;
1504 year--;
1506 else if (month > 12)
1508 month -= 12;
1509 year++;
1512 day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
1514 return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
1518 * g_date_time_add_weeks:
1519 * @datetime: a #GDateTime
1520 * @weeks: the number of weeks
1522 * Creates a copy of @datetime and adds the specified number of weeks to the
1523 * copy. Add negative values to subtract weeks.
1525 * Returns: the newly created #GDateTime which should be freed with
1526 * g_date_time_unref().
1528 * Since: 2.26
1530 GDateTime*
1531 g_date_time_add_weeks (GDateTime *datetime,
1532 gint weeks)
1534 g_return_val_if_fail (datetime != NULL, NULL);
1536 return g_date_time_add_days (datetime, weeks * 7);
1540 * g_date_time_add_days:
1541 * @datetime: a #GDateTime
1542 * @days: the number of days
1544 * Creates a copy of @datetime and adds the specified number of days to the
1545 * copy. Add negative values to subtract days.
1547 * Returns: the newly created #GDateTime which should be freed with
1548 * g_date_time_unref().
1550 * Since: 2.26
1552 GDateTime*
1553 g_date_time_add_days (GDateTime *datetime,
1554 gint days)
1556 g_return_val_if_fail (datetime != NULL, NULL);
1558 if (days < -3660000 || days > 3660000)
1559 return NULL;
1561 return g_date_time_replace_days (datetime, datetime->days + days);
1565 * g_date_time_add_hours:
1566 * @datetime: a #GDateTime
1567 * @hours: the number of hours to add
1569 * Creates a copy of @datetime and adds the specified number of hours.
1570 * Add negative values to subtract hours.
1572 * Returns: the newly created #GDateTime which should be freed with
1573 * g_date_time_unref().
1575 * Since: 2.26
1577 GDateTime*
1578 g_date_time_add_hours (GDateTime *datetime,
1579 gint hours)
1581 return g_date_time_add (datetime, hours * USEC_PER_HOUR);
1585 * g_date_time_add_minutes:
1586 * @datetime: a #GDateTime
1587 * @minutes: the number of minutes to add
1589 * Creates a copy of @datetime adding the specified number of minutes.
1590 * Add negative values to subtract minutes.
1592 * Returns: the newly created #GDateTime which should be freed with
1593 * g_date_time_unref().
1595 * Since: 2.26
1597 GDateTime*
1598 g_date_time_add_minutes (GDateTime *datetime,
1599 gint minutes)
1601 return g_date_time_add (datetime, minutes * USEC_PER_MINUTE);
1606 * g_date_time_add_seconds:
1607 * @datetime: a #GDateTime
1608 * @seconds: the number of seconds to add
1610 * Creates a copy of @datetime and adds the specified number of seconds.
1611 * Add negative values to subtract seconds.
1613 * Returns: the newly created #GDateTime which should be freed with
1614 * g_date_time_unref().
1616 * Since: 2.26
1618 GDateTime*
1619 g_date_time_add_seconds (GDateTime *datetime,
1620 gdouble seconds)
1622 return g_date_time_add (datetime, seconds * USEC_PER_SECOND);
1626 * g_date_time_add_full:
1627 * @datetime: a #GDateTime
1628 * @years: the number of years to add
1629 * @months: the number of months to add
1630 * @days: the number of days to add
1631 * @hours: the number of hours to add
1632 * @minutes: the number of minutes to add
1633 * @seconds: the number of seconds to add
1635 * Creates a new #GDateTime adding the specified values to the current date and
1636 * time in @datetime. Add negative values to subtract.
1638 * Returns: the newly created #GDateTime that should be freed with
1639 * g_date_time_unref().
1641 * Since: 2.26
1643 GDateTime *
1644 g_date_time_add_full (GDateTime *datetime,
1645 gint years,
1646 gint months,
1647 gint days,
1648 gint hours,
1649 gint minutes,
1650 gdouble seconds)
1652 gint year, month, day;
1653 gint64 full_time;
1654 GDateTime *new;
1655 gint interval;
1657 g_return_val_if_fail (datetime != NULL, NULL);
1658 g_date_time_get_ymd (datetime, &year, &month, &day);
1660 months += years * 12;
1662 if (months < -120000 || months > 120000)
1663 return NULL;
1665 if (days < -3660000 || days > 3660000)
1666 return NULL;
1668 year += months / 12;
1669 month += months % 12;
1670 if (month < 1)
1672 month += 12;
1673 year--;
1675 else if (month > 12)
1677 month -= 12;
1678 year++;
1681 day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
1683 /* full_time is now in unix (local) time */
1684 full_time = datetime->usec / USEC_PER_SECOND + SEC_PER_DAY *
1685 (ymd_to_days (year, month, day) + days - UNIX_EPOCH_START);
1687 interval = g_time_zone_adjust_time (datetime->tz,
1688 g_time_zone_is_dst (datetime->tz,
1689 datetime->interval),
1690 &full_time);
1692 /* move to UTC unix time */
1693 full_time -= g_time_zone_get_offset (datetime->tz, interval);
1695 /* convert back to an instant, add back fractional seconds */
1696 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
1697 full_time = full_time * USEC_PER_SECOND +
1698 datetime->usec % USEC_PER_SECOND;
1700 /* do the actual addition now */
1701 full_time += (hours * USEC_PER_HOUR) +
1702 (minutes * USEC_PER_MINUTE) +
1703 (gint64) (seconds * USEC_PER_SECOND);
1705 /* find the new interval */
1706 interval = g_time_zone_find_interval (datetime->tz,
1707 G_TIME_TYPE_UNIVERSAL,
1708 INSTANT_TO_UNIX (full_time));
1710 /* convert back into local time */
1711 full_time += USEC_PER_SECOND *
1712 g_time_zone_get_offset (datetime->tz, interval);
1714 /* split into days and usec of a new datetime */
1715 new = g_date_time_alloc (datetime->tz);
1716 new->interval = interval;
1717 new->days = full_time / USEC_PER_DAY;
1718 new->usec = full_time % USEC_PER_DAY;
1720 /* XXX validate */
1722 return new;
1725 /* Compare, difference, hash, equal {{{1 */
1727 * g_date_time_compare:
1728 * @dt1: (not nullable): first #GDateTime to compare
1729 * @dt2: (not nullable): second #GDateTime to compare
1731 * A comparison function for #GDateTimes that is suitable
1732 * as a #GCompareFunc. Both #GDateTimes must be non-%NULL.
1734 * Returns: -1, 0 or 1 if @dt1 is less than, equal to or greater
1735 * than @dt2.
1737 * Since: 2.26
1739 gint
1740 g_date_time_compare (gconstpointer dt1,
1741 gconstpointer dt2)
1743 gint64 difference;
1745 difference = g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2);
1747 if (difference < 0)
1748 return -1;
1750 else if (difference > 0)
1751 return 1;
1753 else
1754 return 0;
1758 * g_date_time_difference:
1759 * @end: a #GDateTime
1760 * @begin: a #GDateTime
1762 * Calculates the difference in time between @end and @begin. The
1763 * #GTimeSpan that is returned is effectively @end - @begin (ie:
1764 * positive if the first parameter is larger).
1766 * Returns: the difference between the two #GDateTime, as a time
1767 * span expressed in microseconds.
1769 * Since: 2.26
1771 GTimeSpan
1772 g_date_time_difference (GDateTime *end,
1773 GDateTime *begin)
1775 g_return_val_if_fail (begin != NULL, 0);
1776 g_return_val_if_fail (end != NULL, 0);
1778 return g_date_time_to_instant (end) -
1779 g_date_time_to_instant (begin);
1783 * g_date_time_hash:
1784 * @datetime: (not nullable): a #GDateTime
1786 * Hashes @datetime into a #guint, suitable for use within #GHashTable.
1788 * Returns: a #guint containing the hash
1790 * Since: 2.26
1792 guint
1793 g_date_time_hash (gconstpointer datetime)
1795 return g_date_time_to_instant ((GDateTime *) datetime);
1799 * g_date_time_equal:
1800 * @dt1: (not nullable): a #GDateTime
1801 * @dt2: (not nullable): a #GDateTime
1803 * Checks to see if @dt1 and @dt2 are equal.
1805 * Equal here means that they represent the same moment after converting
1806 * them to the same time zone.
1808 * Returns: %TRUE if @dt1 and @dt2 are equal
1810 * Since: 2.26
1812 gboolean
1813 g_date_time_equal (gconstpointer dt1,
1814 gconstpointer dt2)
1816 return g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2) == 0;
1819 /* Year, Month, Day Getters {{{1 */
1821 * g_date_time_get_ymd:
1822 * @datetime: a #GDateTime.
1823 * @year: (out) (optional): the return location for the gregorian year, or %NULL.
1824 * @month: (out) (optional): the return location for the month of the year, or %NULL.
1825 * @day: (out) (optional): the return location for the day of the month, or %NULL.
1827 * Retrieves the Gregorian day, month, and year of a given #GDateTime.
1829 * Since: 2.26
1831 void
1832 g_date_time_get_ymd (GDateTime *datetime,
1833 gint *year,
1834 gint *month,
1835 gint *day)
1837 gint the_year;
1838 gint the_month;
1839 gint the_day;
1840 gint remaining_days;
1841 gint y100_cycles;
1842 gint y4_cycles;
1843 gint y1_cycles;
1844 gint preceding;
1845 gboolean leap;
1847 g_return_if_fail (datetime != NULL);
1849 remaining_days = datetime->days;
1852 * We need to convert an offset in days to its year/month/day representation.
1853 * Leap years makes this a little trickier than it should be, so we use
1854 * 400, 100 and 4 years cycles here to get to the correct year.
1857 /* Our days offset starts sets 0001-01-01 as day 1, if it was day 0 our
1858 * math would be simpler, so let's do it */
1859 remaining_days--;
1861 the_year = (remaining_days / DAYS_IN_400YEARS) * 400 + 1;
1862 remaining_days = remaining_days % DAYS_IN_400YEARS;
1864 y100_cycles = remaining_days / DAYS_IN_100YEARS;
1865 remaining_days = remaining_days % DAYS_IN_100YEARS;
1866 the_year += y100_cycles * 100;
1868 y4_cycles = remaining_days / DAYS_IN_4YEARS;
1869 remaining_days = remaining_days % DAYS_IN_4YEARS;
1870 the_year += y4_cycles * 4;
1872 y1_cycles = remaining_days / 365;
1873 the_year += y1_cycles;
1874 remaining_days = remaining_days % 365;
1876 if (y1_cycles == 4 || y100_cycles == 4) {
1877 g_assert (remaining_days == 0);
1879 /* special case that indicates that the date is actually one year before,
1880 * in the 31th of December */
1881 the_year--;
1882 the_month = 12;
1883 the_day = 31;
1884 goto end;
1887 /* now get the month and the day */
1888 leap = y1_cycles == 3 && (y4_cycles != 24 || y100_cycles == 3);
1890 g_assert (leap == GREGORIAN_LEAP(the_year));
1892 the_month = (remaining_days + 50) >> 5;
1893 preceding = (days_in_year[0][the_month - 1] + (the_month > 2 && leap));
1894 if (preceding > remaining_days)
1896 /* estimate is too large */
1897 the_month -= 1;
1898 preceding -= leap ? days_in_months[1][the_month]
1899 : days_in_months[0][the_month];
1902 remaining_days -= preceding;
1903 g_assert(0 <= remaining_days);
1905 the_day = remaining_days + 1;
1907 end:
1908 if (year)
1909 *year = the_year;
1910 if (month)
1911 *month = the_month;
1912 if (day)
1913 *day = the_day;
1917 * g_date_time_get_year:
1918 * @datetime: A #GDateTime
1920 * Retrieves the year represented by @datetime in the Gregorian calendar.
1922 * Returns: the year represented by @datetime
1924 * Since: 2.26
1926 gint
1927 g_date_time_get_year (GDateTime *datetime)
1929 gint year;
1931 g_return_val_if_fail (datetime != NULL, 0);
1933 g_date_time_get_ymd (datetime, &year, NULL, NULL);
1935 return year;
1939 * g_date_time_get_month:
1940 * @datetime: a #GDateTime
1942 * Retrieves the month of the year represented by @datetime in the Gregorian
1943 * calendar.
1945 * Returns: the month represented by @datetime
1947 * Since: 2.26
1949 gint
1950 g_date_time_get_month (GDateTime *datetime)
1952 gint month;
1954 g_return_val_if_fail (datetime != NULL, 0);
1956 g_date_time_get_ymd (datetime, NULL, &month, NULL);
1958 return month;
1962 * g_date_time_get_day_of_month:
1963 * @datetime: a #GDateTime
1965 * Retrieves the day of the month represented by @datetime in the gregorian
1966 * calendar.
1968 * Returns: the day of the month
1970 * Since: 2.26
1972 gint
1973 g_date_time_get_day_of_month (GDateTime *datetime)
1975 gint day_of_year,
1977 const guint16 *days;
1978 guint16 last = 0;
1980 g_return_val_if_fail (datetime != NULL, 0);
1982 days = days_in_year[GREGORIAN_LEAP (g_date_time_get_year (datetime)) ? 1 : 0];
1983 g_date_time_get_week_number (datetime, NULL, NULL, &day_of_year);
1985 for (i = 1; i <= 12; i++)
1987 if (days [i] >= day_of_year)
1988 return day_of_year - last;
1989 last = days [i];
1992 g_warn_if_reached ();
1993 return 0;
1996 /* Week of year / day of week getters {{{1 */
1998 * g_date_time_get_week_numbering_year:
1999 * @datetime: a #GDateTime
2001 * Returns the ISO 8601 week-numbering year in which the week containing
2002 * @datetime falls.
2004 * This function, taken together with g_date_time_get_week_of_year() and
2005 * g_date_time_get_day_of_week() can be used to determine the full ISO
2006 * week date on which @datetime falls.
2008 * This is usually equal to the normal Gregorian year (as returned by
2009 * g_date_time_get_year()), except as detailed below:
2011 * For Thursday, the week-numbering year is always equal to the usual
2012 * calendar year. For other days, the number is such that every day
2013 * within a complete week (Monday to Sunday) is contained within the
2014 * same week-numbering year.
2016 * For Monday, Tuesday and Wednesday occurring near the end of the year,
2017 * this may mean that the week-numbering year is one greater than the
2018 * calendar year (so that these days have the same week-numbering year
2019 * as the Thursday occurring early in the next year).
2021 * For Friday, Saturday and Sunday occurring near the start of the year,
2022 * this may mean that the week-numbering year is one less than the
2023 * calendar year (so that these days have the same week-numbering year
2024 * as the Thursday occurring late in the previous year).
2026 * An equivalent description is that the week-numbering year is equal to
2027 * the calendar year containing the majority of the days in the current
2028 * week (Monday to Sunday).
2030 * Note that January 1 0001 in the proleptic Gregorian calendar is a
2031 * Monday, so this function never returns 0.
2033 * Returns: the ISO 8601 week-numbering year for @datetime
2035 * Since: 2.26
2037 gint
2038 g_date_time_get_week_numbering_year (GDateTime *datetime)
2040 gint year, month, day, weekday;
2042 g_date_time_get_ymd (datetime, &year, &month, &day);
2043 weekday = g_date_time_get_day_of_week (datetime);
2045 /* January 1, 2, 3 might be in the previous year if they occur after
2046 * Thursday.
2048 * Jan 1: Friday, Saturday, Sunday => day 1: weekday 5, 6, 7
2049 * Jan 2: Saturday, Sunday => day 2: weekday 6, 7
2050 * Jan 3: Sunday => day 3: weekday 7
2052 * So we have a special case if (day - weekday) <= -4
2054 if (month == 1 && (day - weekday) <= -4)
2055 return year - 1;
2057 /* December 29, 30, 31 might be in the next year if they occur before
2058 * Thursday.
2060 * Dec 31: Monday, Tuesday, Wednesday => day 31: weekday 1, 2, 3
2061 * Dec 30: Monday, Tuesday => day 30: weekday 1, 2
2062 * Dec 29: Monday => day 29: weekday 1
2064 * So we have a special case if (day - weekday) >= 28
2066 else if (month == 12 && (day - weekday) >= 28)
2067 return year + 1;
2069 else
2070 return year;
2074 * g_date_time_get_week_of_year:
2075 * @datetime: a #GDateTime
2077 * Returns the ISO 8601 week number for the week containing @datetime.
2078 * The ISO 8601 week number is the same for every day of the week (from
2079 * Moday through Sunday). That can produce some unusual results
2080 * (described below).
2082 * The first week of the year is week 1. This is the week that contains
2083 * the first Thursday of the year. Equivalently, this is the first week
2084 * that has more than 4 of its days falling within the calendar year.
2086 * The value 0 is never returned by this function. Days contained
2087 * within a year but occurring before the first ISO 8601 week of that
2088 * year are considered as being contained in the last week of the
2089 * previous year. Similarly, the final days of a calendar year may be
2090 * considered as being part of the first ISO 8601 week of the next year
2091 * if 4 or more days of that week are contained within the new year.
2093 * Returns: the ISO 8601 week number for @datetime.
2095 * Since: 2.26
2097 gint
2098 g_date_time_get_week_of_year (GDateTime *datetime)
2100 gint weeknum;
2102 g_return_val_if_fail (datetime != NULL, 0);
2104 g_date_time_get_week_number (datetime, &weeknum, NULL, NULL);
2106 return weeknum;
2110 * g_date_time_get_day_of_week:
2111 * @datetime: a #GDateTime
2113 * Retrieves the ISO 8601 day of the week on which @datetime falls (1 is
2114 * Monday, 2 is Tuesday... 7 is Sunday).
2116 * Returns: the day of the week
2118 * Since: 2.26
2120 gint
2121 g_date_time_get_day_of_week (GDateTime *datetime)
2123 g_return_val_if_fail (datetime != NULL, 0);
2125 return (datetime->days - 1) % 7 + 1;
2128 /* Day of year getter {{{1 */
2130 * g_date_time_get_day_of_year:
2131 * @datetime: a #GDateTime
2133 * Retrieves the day of the year represented by @datetime in the Gregorian
2134 * calendar.
2136 * Returns: the day of the year
2138 * Since: 2.26
2140 gint
2141 g_date_time_get_day_of_year (GDateTime *datetime)
2143 gint doy = 0;
2145 g_return_val_if_fail (datetime != NULL, 0);
2147 g_date_time_get_week_number (datetime, NULL, NULL, &doy);
2148 return doy;
2151 /* Time component getters {{{1 */
2154 * g_date_time_get_hour:
2155 * @datetime: a #GDateTime
2157 * Retrieves the hour of the day represented by @datetime
2159 * Returns: the hour of the day
2161 * Since: 2.26
2163 gint
2164 g_date_time_get_hour (GDateTime *datetime)
2166 g_return_val_if_fail (datetime != NULL, 0);
2168 return (datetime->usec / USEC_PER_HOUR);
2172 * g_date_time_get_minute:
2173 * @datetime: a #GDateTime
2175 * Retrieves the minute of the hour represented by @datetime
2177 * Returns: the minute of the hour
2179 * Since: 2.26
2181 gint
2182 g_date_time_get_minute (GDateTime *datetime)
2184 g_return_val_if_fail (datetime != NULL, 0);
2186 return (datetime->usec % USEC_PER_HOUR) / USEC_PER_MINUTE;
2190 * g_date_time_get_second:
2191 * @datetime: a #GDateTime
2193 * Retrieves the second of the minute represented by @datetime
2195 * Returns: the second represented by @datetime
2197 * Since: 2.26
2199 gint
2200 g_date_time_get_second (GDateTime *datetime)
2202 g_return_val_if_fail (datetime != NULL, 0);
2204 return (datetime->usec % USEC_PER_MINUTE) / USEC_PER_SECOND;
2208 * g_date_time_get_microsecond:
2209 * @datetime: a #GDateTime
2211 * Retrieves the microsecond of the date represented by @datetime
2213 * Returns: the microsecond of the second
2215 * Since: 2.26
2217 gint
2218 g_date_time_get_microsecond (GDateTime *datetime)
2220 g_return_val_if_fail (datetime != NULL, 0);
2222 return (datetime->usec % USEC_PER_SECOND);
2226 * g_date_time_get_seconds:
2227 * @datetime: a #GDateTime
2229 * Retrieves the number of seconds since the start of the last minute,
2230 * including the fractional part.
2232 * Returns: the number of seconds
2234 * Since: 2.26
2236 gdouble
2237 g_date_time_get_seconds (GDateTime *datetime)
2239 g_return_val_if_fail (datetime != NULL, 0);
2241 return (datetime->usec % USEC_PER_MINUTE) / 1000000.0;
2244 /* Exporters {{{1 */
2246 * g_date_time_to_unix:
2247 * @datetime: a #GDateTime
2249 * Gives the Unix time corresponding to @datetime, rounding down to the
2250 * nearest second.
2252 * Unix time is the number of seconds that have elapsed since 1970-01-01
2253 * 00:00:00 UTC, regardless of the time zone associated with @datetime.
2255 * Returns: the Unix time corresponding to @datetime
2257 * Since: 2.26
2259 gint64
2260 g_date_time_to_unix (GDateTime *datetime)
2262 return INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
2266 * g_date_time_to_timeval:
2267 * @datetime: a #GDateTime
2268 * @tv: a #GTimeVal to modify
2270 * Stores the instant in time that @datetime represents into @tv.
2272 * The time contained in a #GTimeVal is always stored in the form of
2273 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the time
2274 * zone associated with @datetime.
2276 * On systems where 'long' is 32bit (ie: all 32bit systems and all
2277 * Windows systems), a #GTimeVal is incapable of storing the entire
2278 * range of values that #GDateTime is capable of expressing. On those
2279 * systems, this function returns %FALSE to indicate that the time is
2280 * out of range.
2282 * On systems where 'long' is 64bit, this function never fails.
2284 * Returns: %TRUE if successful, else %FALSE
2286 * Since: 2.26
2288 gboolean
2289 g_date_time_to_timeval (GDateTime *datetime,
2290 GTimeVal *tv)
2292 tv->tv_sec = INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
2293 tv->tv_usec = datetime->usec % USEC_PER_SECOND;
2295 return TRUE;
2298 /* Timezone queries {{{1 */
2300 * g_date_time_get_utc_offset:
2301 * @datetime: a #GDateTime
2303 * Determines the offset to UTC in effect at the time and in the time
2304 * zone of @datetime.
2306 * The offset is the number of microseconds that you add to UTC time to
2307 * arrive at local time for the time zone (ie: negative numbers for time
2308 * zones west of GMT, positive numbers for east).
2310 * If @datetime represents UTC time, then the offset is always zero.
2312 * Returns: the number of microseconds that should be added to UTC to
2313 * get the local time
2315 * Since: 2.26
2317 GTimeSpan
2318 g_date_time_get_utc_offset (GDateTime *datetime)
2320 gint offset;
2322 g_return_val_if_fail (datetime != NULL, 0);
2324 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
2326 return (gint64) offset * USEC_PER_SECOND;
2330 * g_date_time_get_timezone_abbreviation:
2331 * @datetime: a #GDateTime
2333 * Determines the time zone abbreviation to be used at the time and in
2334 * the time zone of @datetime.
2336 * For example, in Toronto this is currently "EST" during the winter
2337 * months and "EDT" during the summer months when daylight savings
2338 * time is in effect.
2340 * Returns: (transfer none): the time zone abbreviation. The returned
2341 * string is owned by the #GDateTime and it should not be
2342 * modified or freed
2344 * Since: 2.26
2346 const gchar *
2347 g_date_time_get_timezone_abbreviation (GDateTime *datetime)
2349 g_return_val_if_fail (datetime != NULL, NULL);
2351 return g_time_zone_get_abbreviation (datetime->tz, datetime->interval);
2355 * g_date_time_is_daylight_savings:
2356 * @datetime: a #GDateTime
2358 * Determines if daylight savings time is in effect at the time and in
2359 * the time zone of @datetime.
2361 * Returns: %TRUE if daylight savings time is in effect
2363 * Since: 2.26
2365 gboolean
2366 g_date_time_is_daylight_savings (GDateTime *datetime)
2368 g_return_val_if_fail (datetime != NULL, FALSE);
2370 return g_time_zone_is_dst (datetime->tz, datetime->interval);
2373 /* Timezone convert {{{1 */
2375 * g_date_time_to_timezone:
2376 * @datetime: a #GDateTime
2377 * @tz: the new #GTimeZone
2379 * Create a new #GDateTime corresponding to the same instant in time as
2380 * @datetime, but in the time zone @tz.
2382 * This call can fail in the case that the time goes out of bounds. For
2383 * example, converting 0001-01-01 00:00:00 UTC to a time zone west of
2384 * Greenwich will fail (due to the year 0 being out of range).
2386 * You should release the return value by calling g_date_time_unref()
2387 * when you are done with it.
2389 * Returns: a new #GDateTime, or %NULL
2391 * Since: 2.26
2393 GDateTime *
2394 g_date_time_to_timezone (GDateTime *datetime,
2395 GTimeZone *tz)
2397 return g_date_time_from_instant (tz, g_date_time_to_instant (datetime));
2401 * g_date_time_to_local:
2402 * @datetime: a #GDateTime
2404 * Creates a new #GDateTime corresponding to the same instant in time as
2405 * @datetime, but in the local time zone.
2407 * This call is equivalent to calling g_date_time_to_timezone() with the
2408 * time zone returned by g_time_zone_new_local().
2410 * Returns: the newly created #GDateTime
2412 * Since: 2.26
2414 GDateTime *
2415 g_date_time_to_local (GDateTime *datetime)
2417 GDateTime *new;
2418 GTimeZone *local;
2420 local = g_time_zone_new_local ();
2421 new = g_date_time_to_timezone (datetime, local);
2422 g_time_zone_unref (local);
2424 return new;
2428 * g_date_time_to_utc:
2429 * @datetime: a #GDateTime
2431 * Creates a new #GDateTime corresponding to the same instant in time as
2432 * @datetime, but in UTC.
2434 * This call is equivalent to calling g_date_time_to_timezone() with the
2435 * time zone returned by g_time_zone_new_utc().
2437 * Returns: the newly created #GDateTime
2439 * Since: 2.26
2441 GDateTime *
2442 g_date_time_to_utc (GDateTime *datetime)
2444 GDateTime *new;
2445 GTimeZone *utc;
2447 utc = g_time_zone_new_utc ();
2448 new = g_date_time_to_timezone (datetime, utc);
2449 g_time_zone_unref (utc);
2451 return new;
2454 /* Format {{{1 */
2456 static gboolean
2457 format_z (GString *outstr,
2458 gint offset,
2459 guint colons)
2461 gint hours;
2462 gint minutes;
2463 gint seconds;
2465 hours = offset / 3600;
2466 minutes = ABS (offset) / 60 % 60;
2467 seconds = ABS (offset) % 60;
2469 switch (colons)
2471 case 0:
2472 g_string_append_printf (outstr, "%+03d%02d",
2473 hours,
2474 minutes);
2475 break;
2477 case 1:
2478 g_string_append_printf (outstr, "%+03d:%02d",
2479 hours,
2480 minutes);
2481 break;
2483 case 2:
2484 g_string_append_printf (outstr, "%+03d:%02d:%02d",
2485 hours,
2486 minutes,
2487 seconds);
2488 break;
2490 case 3:
2491 g_string_append_printf (outstr, "%+03d", hours);
2493 if (minutes != 0 || seconds != 0)
2495 g_string_append_printf (outstr, ":%02d", minutes);
2497 if (seconds != 0)
2498 g_string_append_printf (outstr, ":%02d", seconds);
2500 break;
2502 default:
2503 return FALSE;
2506 return TRUE;
2509 static void
2510 format_number (GString *str,
2511 gboolean use_alt_digits,
2512 gchar *pad,
2513 gint width,
2514 guint32 number)
2516 const gchar *ascii_digits[10] = {
2517 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
2519 const gchar **digits = ascii_digits;
2520 const gchar *tmp[10];
2521 gint i = 0;
2523 g_return_if_fail (width <= 10);
2525 #ifdef HAVE_LANGINFO_OUTDIGIT
2526 if (use_alt_digits)
2528 static const gchar *alt_digits[10];
2529 static gsize initialised;
2530 /* 2^32 has 10 digits */
2532 if G_UNLIKELY (g_once_init_enter (&initialised))
2534 #define DO_DIGIT(n) \
2535 alt_digits[n] = nl_langinfo (_NL_CTYPE_OUTDIGIT## n ##_MB)
2536 DO_DIGIT(0); DO_DIGIT(1); DO_DIGIT(2); DO_DIGIT(3); DO_DIGIT(4);
2537 DO_DIGIT(5); DO_DIGIT(6); DO_DIGIT(7); DO_DIGIT(8); DO_DIGIT(9);
2538 #undef DO_DIGIT
2539 g_once_init_leave (&initialised, TRUE);
2542 digits = alt_digits;
2544 #endif /* HAVE_LANGINFO_OUTDIGIT */
2548 tmp[i++] = digits[number % 10];
2549 number /= 10;
2551 while (number);
2553 while (pad && i < width)
2554 tmp[i++] = *pad == '0' ? digits[0] : pad;
2556 /* should really be impossible */
2557 g_assert (i <= 10);
2559 while (i)
2560 g_string_append (str, tmp[--i]);
2563 static gboolean
2564 format_ampm (GDateTime *datetime,
2565 GString *outstr,
2566 gboolean locale_is_utf8,
2567 gboolean uppercase)
2569 const gchar *ampm;
2570 gchar *tmp = NULL, *ampm_dup;
2571 gsize len;
2573 ampm = GET_AMPM (datetime);
2575 if (!ampm || ampm[0] == '\0')
2576 ampm = get_fallback_ampm (g_date_time_get_hour (datetime));
2578 #if defined (HAVE_LANGINFO_TIME)
2579 if (!locale_is_utf8)
2581 /* This assumes that locale encoding can't have embedded NULs */
2582 ampm = tmp = g_locale_to_utf8 (ampm, -1, NULL, NULL, NULL);
2583 if (!tmp)
2584 return FALSE;
2586 #endif
2587 if (uppercase)
2588 ampm_dup = g_utf8_strup (ampm, -1);
2589 else
2590 ampm_dup = g_utf8_strdown (ampm, -1);
2591 len = strlen (ampm_dup);
2592 if (!locale_is_utf8)
2594 #if defined (HAVE_LANGINFO_TIME)
2595 g_free (tmp);
2596 #endif
2597 tmp = g_locale_from_utf8 (ampm_dup, -1, NULL, &len, NULL);
2598 g_free (ampm_dup);
2599 if (!tmp)
2600 return FALSE;
2601 ampm_dup = tmp;
2603 g_string_append_len (outstr, ampm_dup, len);
2604 g_free (ampm_dup);
2606 return TRUE;
2609 static gboolean g_date_time_format_locale (GDateTime *datetime,
2610 const gchar *format,
2611 GString *outstr,
2612 gboolean locale_is_utf8);
2614 /* g_date_time_format() subroutine that takes a locale-encoded format
2615 * string and produces a locale-encoded date/time string.
2617 static gboolean
2618 g_date_time_locale_format_locale (GDateTime *datetime,
2619 const gchar *format,
2620 GString *outstr,
2621 gboolean locale_is_utf8)
2623 gchar *utf8_format;
2624 gboolean success;
2626 if (locale_is_utf8)
2627 return g_date_time_format_locale (datetime, format, outstr,
2628 locale_is_utf8);
2630 utf8_format = g_locale_to_utf8 (format, -1, NULL, NULL, NULL);
2631 if (!utf8_format)
2632 return FALSE;
2634 success = g_date_time_format_locale (datetime, utf8_format, outstr,
2635 locale_is_utf8);
2636 g_free (utf8_format);
2637 return success;
2640 /* g_date_time_format() subroutine that takes a UTF-8 format
2641 * string and produces a locale-encoded date/time string.
2643 static gboolean
2644 g_date_time_format_locale (GDateTime *datetime,
2645 const gchar *format,
2646 GString *outstr,
2647 gboolean locale_is_utf8)
2649 guint len;
2650 guint colons;
2651 gchar *tmp;
2652 gsize tmp_len;
2653 gunichar c;
2654 gboolean alt_digits = FALSE;
2655 gboolean pad_set = FALSE;
2656 gchar *pad = "";
2657 const gchar *name;
2658 const gchar *tz;
2660 while (*format)
2662 len = strcspn (format, "%");
2663 if (len)
2665 if (locale_is_utf8)
2666 g_string_append_len (outstr, format, len);
2667 else
2669 tmp = g_locale_from_utf8 (format, len, NULL, &tmp_len, NULL);
2670 if (!tmp)
2671 return FALSE;
2672 g_string_append_len (outstr, tmp, tmp_len);
2673 g_free (tmp);
2677 format += len;
2678 if (!*format)
2679 break;
2681 g_assert (*format == '%');
2682 format++;
2683 if (!*format)
2684 break;
2686 colons = 0;
2687 alt_digits = FALSE;
2688 pad_set = FALSE;
2690 next_mod:
2691 c = g_utf8_get_char (format);
2692 format = g_utf8_next_char (format);
2693 switch (c)
2695 case 'a':
2696 name = WEEKDAY_ABBR (datetime);
2697 if (g_strcmp0 (name, "") == 0)
2698 return FALSE;
2699 #if !defined (HAVE_LANGINFO_TIME)
2700 if (!locale_is_utf8)
2702 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2703 if (!tmp)
2704 return FALSE;
2705 g_string_append_len (outstr, tmp, tmp_len);
2706 g_free (tmp);
2708 else
2709 #endif
2711 g_string_append (outstr, name);
2713 break;
2714 case 'A':
2715 name = WEEKDAY_FULL (datetime);
2716 if (g_strcmp0 (name, "") == 0)
2717 return FALSE;
2718 #if !defined (HAVE_LANGINFO_TIME)
2719 if (!locale_is_utf8)
2721 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2722 if (!tmp)
2723 return FALSE;
2724 g_string_append_len (outstr, tmp, tmp_len);
2725 g_free (tmp);
2727 else
2728 #endif
2730 g_string_append (outstr, name);
2732 break;
2733 case 'b':
2734 name = MONTH_ABBR (datetime);
2735 if (g_strcmp0 (name, "") == 0)
2736 return FALSE;
2737 #if !defined (HAVE_LANGINFO_TIME)
2738 if (!locale_is_utf8)
2740 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2741 if (!tmp)
2742 return FALSE;
2743 g_string_append_len (outstr, tmp, tmp_len);
2744 g_free (tmp);
2746 else
2747 #endif
2749 g_string_append (outstr, name);
2751 break;
2752 case 'B':
2753 name = MONTH_FULL (datetime);
2754 if (g_strcmp0 (name, "") == 0)
2755 return FALSE;
2756 #if !defined (HAVE_LANGINFO_TIME)
2757 if (!locale_is_utf8)
2759 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2760 if (!tmp)
2761 return FALSE;
2762 g_string_append_len (outstr, tmp, tmp_len);
2763 g_free (tmp);
2765 else
2766 #endif
2768 g_string_append (outstr, name);
2770 break;
2771 case 'c':
2773 if (g_strcmp0 (PREFERRED_DATE_TIME_FMT, "") == 0)
2774 return FALSE;
2775 if (!g_date_time_locale_format_locale (datetime, PREFERRED_DATE_TIME_FMT,
2776 outstr, locale_is_utf8))
2777 return FALSE;
2779 break;
2780 case 'C':
2781 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2782 g_date_time_get_year (datetime) / 100);
2783 break;
2784 case 'd':
2785 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2786 g_date_time_get_day_of_month (datetime));
2787 break;
2788 case 'e':
2789 format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
2790 g_date_time_get_day_of_month (datetime));
2791 break;
2792 case 'F':
2793 g_string_append_printf (outstr, "%d-%02d-%02d",
2794 g_date_time_get_year (datetime),
2795 g_date_time_get_month (datetime),
2796 g_date_time_get_day_of_month (datetime));
2797 break;
2798 case 'g':
2799 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2800 g_date_time_get_week_numbering_year (datetime) % 100);
2801 break;
2802 case 'G':
2803 format_number (outstr, alt_digits, pad_set ? pad : 0, 0,
2804 g_date_time_get_week_numbering_year (datetime));
2805 break;
2806 case 'h':
2807 name = MONTH_ABBR (datetime);
2808 if (g_strcmp0 (name, "") == 0)
2809 return FALSE;
2810 #if !defined (HAVE_LANGINFO_TIME)
2811 if (!locale_is_utf8)
2813 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2814 if (!tmp)
2815 return FALSE;
2816 g_string_append_len (outstr, tmp, tmp_len);
2817 g_free (tmp);
2819 else
2820 #endif
2822 g_string_append (outstr, name);
2824 break;
2825 case 'H':
2826 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2827 g_date_time_get_hour (datetime));
2828 break;
2829 case 'I':
2830 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2831 (g_date_time_get_hour (datetime) + 11) % 12 + 1);
2832 break;
2833 case 'j':
2834 format_number (outstr, alt_digits, pad_set ? pad : "0", 3,
2835 g_date_time_get_day_of_year (datetime));
2836 break;
2837 case 'k':
2838 format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
2839 g_date_time_get_hour (datetime));
2840 break;
2841 case 'l':
2842 format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
2843 (g_date_time_get_hour (datetime) + 11) % 12 + 1);
2844 break;
2845 case 'n':
2846 g_string_append_c (outstr, '\n');
2847 break;
2848 case 'm':
2849 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2850 g_date_time_get_month (datetime));
2851 break;
2852 case 'M':
2853 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2854 g_date_time_get_minute (datetime));
2855 break;
2856 case 'O':
2857 alt_digits = TRUE;
2858 goto next_mod;
2859 case 'p':
2860 if (!format_ampm (datetime, outstr, locale_is_utf8, TRUE))
2861 return FALSE;
2862 break;
2863 case 'P':
2864 if (!format_ampm (datetime, outstr, locale_is_utf8, FALSE))
2865 return FALSE;
2866 break;
2867 case 'r':
2869 if (g_strcmp0 (PREFERRED_12HR_TIME_FMT, "") == 0)
2870 return FALSE;
2871 if (!g_date_time_locale_format_locale (datetime, PREFERRED_12HR_TIME_FMT,
2872 outstr, locale_is_utf8))
2873 return FALSE;
2875 break;
2876 case 'R':
2877 g_string_append_printf (outstr, "%02d:%02d",
2878 g_date_time_get_hour (datetime),
2879 g_date_time_get_minute (datetime));
2880 break;
2881 case 's':
2882 g_string_append_printf (outstr, "%" G_GINT64_FORMAT, g_date_time_to_unix (datetime));
2883 break;
2884 case 'S':
2885 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2886 g_date_time_get_second (datetime));
2887 break;
2888 case 't':
2889 g_string_append_c (outstr, '\t');
2890 break;
2891 case 'T':
2892 g_string_append_printf (outstr, "%02d:%02d:%02d",
2893 g_date_time_get_hour (datetime),
2894 g_date_time_get_minute (datetime),
2895 g_date_time_get_second (datetime));
2896 break;
2897 case 'u':
2898 format_number (outstr, alt_digits, 0, 0,
2899 g_date_time_get_day_of_week (datetime));
2900 break;
2901 case 'V':
2902 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2903 g_date_time_get_week_of_year (datetime));
2904 break;
2905 case 'w':
2906 format_number (outstr, alt_digits, 0, 0,
2907 g_date_time_get_day_of_week (datetime) % 7);
2908 break;
2909 case 'x':
2911 if (g_strcmp0 (PREFERRED_DATE_FMT, "") == 0)
2912 return FALSE;
2913 if (!g_date_time_locale_format_locale (datetime, PREFERRED_DATE_FMT,
2914 outstr, locale_is_utf8))
2915 return FALSE;
2917 break;
2918 case 'X':
2920 if (g_strcmp0 (PREFERRED_TIME_FMT, "") == 0)
2921 return FALSE;
2922 if (!g_date_time_locale_format_locale (datetime, PREFERRED_TIME_FMT,
2923 outstr, locale_is_utf8))
2924 return FALSE;
2926 break;
2927 case 'y':
2928 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2929 g_date_time_get_year (datetime) % 100);
2930 break;
2931 case 'Y':
2932 format_number (outstr, alt_digits, 0, 0,
2933 g_date_time_get_year (datetime));
2934 break;
2935 case 'z':
2937 gint64 offset;
2938 offset = g_date_time_get_utc_offset (datetime) / USEC_PER_SECOND;
2939 if (!format_z (outstr, (int) offset, colons))
2940 return FALSE;
2942 break;
2943 case 'Z':
2944 tz = g_date_time_get_timezone_abbreviation (datetime);
2945 tmp_len = strlen (tz);
2946 if (!locale_is_utf8)
2948 tz = tmp = g_locale_from_utf8 (tz, -1, NULL, &tmp_len, NULL);
2949 if (!tmp)
2950 return FALSE;
2952 g_string_append_len (outstr, tz, tmp_len);
2953 if (!locale_is_utf8)
2954 g_free (tmp);
2955 break;
2956 case '%':
2957 g_string_append_c (outstr, '%');
2958 break;
2959 case '-':
2960 pad_set = TRUE;
2961 pad = "";
2962 goto next_mod;
2963 case '_':
2964 pad_set = TRUE;
2965 pad = " ";
2966 goto next_mod;
2967 case '0':
2968 pad_set = TRUE;
2969 pad = "0";
2970 goto next_mod;
2971 case ':':
2972 /* Colons are only allowed before 'z' */
2973 if (*format && *format != 'z' && *format != ':')
2974 return FALSE;
2975 colons++;
2976 goto next_mod;
2977 default:
2978 return FALSE;
2982 return TRUE;
2986 * g_date_time_format:
2987 * @datetime: A #GDateTime
2988 * @format: a valid UTF-8 string, containing the format for the
2989 * #GDateTime
2991 * Creates a newly allocated string representing the requested @format.
2993 * The format strings understood by this function are a subset of the
2994 * strftime() format language as specified by C99. The \%D, \%U and \%W
2995 * conversions are not supported, nor is the 'E' modifier. The GNU
2996 * extensions \%k, \%l, \%s and \%P are supported, however, as are the
2997 * '0', '_' and '-' modifiers.
2999 * In contrast to strftime(), this function always produces a UTF-8
3000 * string, regardless of the current locale. Note that the rendering of
3001 * many formats is locale-dependent and may not match the strftime()
3002 * output exactly.
3004 * The following format specifiers are supported:
3006 * - \%a: the abbreviated weekday name according to the current locale
3007 * - \%A: the full weekday name according to the current locale
3008 * - \%b: the abbreviated month name according to the current locale
3009 * - \%B: the full month name according to the current locale
3010 * - \%c: the preferred date and time representation for the current locale
3011 * - \%C: the century number (year/100) as a 2-digit integer (00-99)
3012 * - \%d: the day of the month as a decimal number (range 01 to 31)
3013 * - \%e: the day of the month as a decimal number (range 1 to 31)
3014 * - \%F: equivalent to `%Y-%m-%d` (the ISO 8601 date format)
3015 * - \%g: the last two digits of the ISO 8601 week-based year as a
3016 * decimal number (00-99). This works well with \%V and \%u.
3017 * - \%G: the ISO 8601 week-based year as a decimal number. This works
3018 * well with \%V and \%u.
3019 * - \%h: equivalent to \%b
3020 * - \%H: the hour as a decimal number using a 24-hour clock (range 00 to 23)
3021 * - \%I: the hour as a decimal number using a 12-hour clock (range 01 to 12)
3022 * - \%j: the day of the year as a decimal number (range 001 to 366)
3023 * - \%k: the hour (24-hour clock) as a decimal number (range 0 to 23);
3024 * single digits are preceded by a blank
3025 * - \%l: the hour (12-hour clock) as a decimal number (range 1 to 12);
3026 * single digits are preceded by a blank
3027 * - \%m: the month as a decimal number (range 01 to 12)
3028 * - \%M: the minute as a decimal number (range 00 to 59)
3029 * - \%p: either "AM" or "PM" according to the given time value, or the
3030 * corresponding strings for the current locale. Noon is treated as
3031 * "PM" and midnight as "AM".
3032 * - \%P: like \%p but lowercase: "am" or "pm" or a corresponding string for
3033 * the current locale
3034 * - \%r: the time in a.m. or p.m. notation
3035 * - \%R: the time in 24-hour notation (\%H:\%M)
3036 * - \%s: the number of seconds since the Epoch, that is, since 1970-01-01
3037 * 00:00:00 UTC
3038 * - \%S: the second as a decimal number (range 00 to 60)
3039 * - \%t: a tab character
3040 * - \%T: the time in 24-hour notation with seconds (\%H:\%M:\%S)
3041 * - \%u: the ISO 8601 standard day of the week as a decimal, range 1 to 7,
3042 * Monday being 1. This works well with \%G and \%V.
3043 * - \%V: the ISO 8601 standard week number of the current year as a decimal
3044 * number, range 01 to 53, where week 1 is the first week that has at
3045 * least 4 days in the new year. See g_date_time_get_week_of_year().
3046 * This works well with \%G and \%u.
3047 * - \%w: the day of the week as a decimal, range 0 to 6, Sunday being 0.
3048 * This is not the ISO 8601 standard format -- use \%u instead.
3049 * - \%x: the preferred date representation for the current locale without
3050 * the time
3051 * - \%X: the preferred time representation for the current locale without
3052 * the date
3053 * - \%y: the year as a decimal number without the century
3054 * - \%Y: the year as a decimal number including the century
3055 * - \%z: the time zone as an offset from UTC (+hhmm)
3056 * - \%:z: the time zone as an offset from UTC (+hh:mm).
3057 * This is a gnulib strftime() extension. Since: 2.38
3058 * - \%::z: the time zone as an offset from UTC (+hh:mm:ss). This is a
3059 * gnulib strftime() extension. Since: 2.38
3060 * - \%:::z: the time zone as an offset from UTC, with : to necessary
3061 * precision (e.g., -04, +05:30). This is a gnulib strftime() extension. Since: 2.38
3062 * - \%Z: the time zone or name or abbreviation
3063 * - \%\%: a literal \% character
3065 * Some conversion specifications can be modified by preceding the
3066 * conversion specifier by one or more modifier characters. The
3067 * following modifiers are supported for many of the numeric
3068 * conversions:
3070 * - O: Use alternative numeric symbols, if the current locale supports those.
3071 * - _: Pad a numeric result with spaces. This overrides the default padding
3072 * for the specifier.
3073 * - -: Do not pad a numeric result. This overrides the default padding
3074 * for the specifier.
3075 * - 0: Pad a numeric result with zeros. This overrides the default padding
3076 * for the specifier.
3078 * Returns: a newly allocated string formatted to the requested format
3079 * or %NULL in the case that there was an error (such as a format specifier
3080 * not being supported in the current locale). The string
3081 * should be freed with g_free().
3083 * Since: 2.26
3085 gchar *
3086 g_date_time_format (GDateTime *datetime,
3087 const gchar *format)
3089 GString *outstr;
3090 gchar *utf8;
3091 gboolean locale_is_utf8 = g_get_charset (NULL);
3093 g_return_val_if_fail (datetime != NULL, NULL);
3094 g_return_val_if_fail (format != NULL, NULL);
3095 g_return_val_if_fail (g_utf8_validate (format, -1, NULL), NULL);
3097 outstr = g_string_sized_new (strlen (format) * 2);
3099 if (!g_date_time_format_locale (datetime, format, outstr, locale_is_utf8))
3101 g_string_free (outstr, TRUE);
3102 return NULL;
3105 if (locale_is_utf8)
3106 return g_string_free (outstr, FALSE);
3108 utf8 = g_locale_to_utf8 (outstr->str, outstr->len, NULL, NULL, NULL);
3109 g_string_free (outstr, TRUE);
3110 return utf8;
3114 /* Epilogue {{{1 */
3115 /* vim:set foldmethod=marker: */