genmarshal Only wrap body prototypes in C++ guards
[glib.git] / glib / gdatetime.c
blobc78bc4cac7b2c2d19baefa1d4fd2ea5b89e3c6c0
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>
27 /* Algorithms within this file are based on the Calendar FAQ by
28 * Claus Tondering. It can be found at
29 * http://www.tondering.dk/claus/cal/calendar29.txt
31 * Copyright and disclaimer
32 * ------------------------
33 * This document is Copyright (C) 2008 by Claus Tondering.
34 * E-mail: claus@tondering.dk. (Please include the word
35 * "calendar" in the subject line.)
36 * The document may be freely distributed, provided this
37 * copyright notice is included and no money is charged for
38 * the document.
40 * This document is provided "as is". No warranties are made as
41 * to its correctness.
44 /* Prologue {{{1 */
46 #include "config.h"
48 #include <stdlib.h>
49 #include <string.h>
51 #ifdef HAVE_LANGINFO_TIME
52 #include <langinfo.h>
53 #endif
55 #include "gdatetime.h"
57 #include "gslice.h"
58 #include "gatomic.h"
59 #include "gcharset.h"
60 #include "gconvert.h"
61 #include "gfileutils.h"
62 #include "ghash.h"
63 #include "gmain.h"
64 #include "gmappedfile.h"
65 #include "gstrfuncs.h"
66 #include "gtestutils.h"
67 #include "gthread.h"
68 #include "gtimezone.h"
70 #include "glibintl.h"
72 #ifndef G_OS_WIN32
73 #include <sys/time.h>
74 #include <time.h>
75 #endif /* !G_OS_WIN32 */
77 /**
78 * SECTION:date-time
79 * @title: GDateTime
80 * @short_description: a structure representing Date and Time
81 * @see_also: #GTimeZone
83 * #GDateTime is a structure that combines a Gregorian date and time
84 * into a single structure. It provides many conversion and methods to
85 * manipulate dates and times. Time precision is provided down to
86 * microseconds and the time can range (proleptically) from 0001-01-01
87 * 00:00:00 to 9999-12-31 23:59:59.999999. #GDateTime follows POSIX
88 * time in the sense that it is oblivious to leap seconds.
90 * #GDateTime is an immutable object; once it has been created it cannot
91 * be modified further. All modifiers will create a new #GDateTime.
92 * Nearly all such functions can fail due to the date or time going out
93 * of range, in which case %NULL will be returned.
95 * #GDateTime is reference counted: the reference count is increased by calling
96 * g_date_time_ref() and decreased by calling g_date_time_unref(). When the
97 * reference count drops to 0, the resources allocated by the #GDateTime
98 * structure are released.
100 * Many parts of the API may produce non-obvious results. As an
101 * example, adding two months to January 31st will yield March 31st
102 * whereas adding one month and then one month again will yield either
103 * March 28th or March 29th. Also note that adding 24 hours is not
104 * always the same as adding one day (since days containing daylight
105 * savings time transitions are either 23 or 25 hours in length).
107 * #GDateTime is available since GLib 2.26.
110 struct _GDateTime
112 /* Microsecond timekeeping within Day */
113 guint64 usec;
115 /* TimeZone information */
116 GTimeZone *tz;
117 gint interval;
119 /* 1 is 0001-01-01 in Proleptic Gregorian */
120 gint32 days;
122 volatile gint ref_count;
125 /* Time conversion {{{1 */
127 #define UNIX_EPOCH_START 719163
128 #define INSTANT_TO_UNIX(instant) \
129 ((instant)/USEC_PER_SECOND - UNIX_EPOCH_START * SEC_PER_DAY)
130 #define UNIX_TO_INSTANT(unix) \
131 (((gint64) (unix) + UNIX_EPOCH_START * SEC_PER_DAY) * USEC_PER_SECOND)
132 #define UNIX_TO_INSTANT_IS_VALID(unix) \
133 ((gint64) (unix) <= INSTANT_TO_UNIX (G_MAXINT64))
135 #define DAYS_IN_4YEARS 1461 /* days in 4 years */
136 #define DAYS_IN_100YEARS 36524 /* days in 100 years */
137 #define DAYS_IN_400YEARS 146097 /* days in 400 years */
139 #define USEC_PER_SECOND (G_GINT64_CONSTANT (1000000))
140 #define USEC_PER_MINUTE (G_GINT64_CONSTANT (60000000))
141 #define USEC_PER_HOUR (G_GINT64_CONSTANT (3600000000))
142 #define USEC_PER_MILLISECOND (G_GINT64_CONSTANT (1000))
143 #define USEC_PER_DAY (G_GINT64_CONSTANT (86400000000))
144 #define SEC_PER_DAY (G_GINT64_CONSTANT (86400))
146 #define SECS_PER_MINUTE (60)
147 #define SECS_PER_HOUR (60 * SECS_PER_MINUTE)
148 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
149 #define SECS_PER_YEAR (365 * SECS_PER_DAY)
150 #define SECS_PER_JULIAN (DAYS_PER_PERIOD * SECS_PER_DAY)
152 #define GREGORIAN_LEAP(y) ((((y) % 4) == 0) && (!((((y) % 100) == 0) && (((y) % 400) != 0))))
153 #define JULIAN_YEAR(d) ((d)->julian / 365.25)
154 #define DAYS_PER_PERIOD (G_GINT64_CONSTANT (2914695))
156 static const guint16 days_in_months[2][13] =
158 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
159 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
162 static const guint16 days_in_year[2][13] =
164 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
165 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
168 #ifdef HAVE_LANGINFO_TIME
170 #define GET_AMPM(d) ((g_date_time_get_hour (d) < 12) ? \
171 nl_langinfo (AM_STR) : \
172 nl_langinfo (PM_STR))
174 #define PREFERRED_DATE_TIME_FMT nl_langinfo (D_T_FMT)
175 #define PREFERRED_DATE_FMT nl_langinfo (D_FMT)
176 #define PREFERRED_TIME_FMT nl_langinfo (T_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 /* full new functions {{{1 */
908 * g_date_time_new:
909 * @tz: a #GTimeZone
910 * @year: the year component of the date
911 * @month: the month component of the date
912 * @day: the day component of the date
913 * @hour: the hour component of the date
914 * @minute: the minute component of the date
915 * @seconds: the number of seconds past the minute
917 * Creates a new #GDateTime corresponding to the given date and time in
918 * the time zone @tz.
920 * The @year must be between 1 and 9999, @month between 1 and 12 and @day
921 * between 1 and 28, 29, 30 or 31 depending on the month and the year.
923 * @hour must be between 0 and 23 and @minute must be between 0 and 59.
925 * @seconds must be at least 0.0 and must be strictly less than 60.0.
926 * It will be rounded down to the nearest microsecond.
928 * If the given time is not representable in the given time zone (for
929 * example, 02:30 on March 14th 2010 in Toronto, due to daylight savings
930 * time) then the time will be rounded up to the nearest existing time
931 * (in this case, 03:00). If this matters to you then you should verify
932 * the return value for containing the same as the numbers you gave.
934 * In the case that the given time is ambiguous in the given time zone
935 * (for example, 01:30 on November 7th 2010 in Toronto, due to daylight
936 * savings time) then the time falling within standard (ie:
937 * non-daylight) time is taken.
939 * It not considered a programmer error for the values to this function
940 * to be out of range, but in the case that they are, the function will
941 * return %NULL.
943 * You should release the return value by calling g_date_time_unref()
944 * when you are done with it.
946 * Returns: a new #GDateTime, or %NULL
948 * Since: 2.26
950 GDateTime *
951 g_date_time_new (GTimeZone *tz,
952 gint year,
953 gint month,
954 gint day,
955 gint hour,
956 gint minute,
957 gdouble seconds)
959 GDateTime *datetime;
960 gint64 full_time;
962 g_return_val_if_fail (tz != NULL, NULL);
964 if (year < 1 || year > 9999 ||
965 month < 1 || month > 12 ||
966 day < 1 || day > days_in_months[GREGORIAN_LEAP (year)][month] ||
967 hour < 0 || hour > 23 ||
968 minute < 0 || minute > 59 ||
969 seconds < 0.0 || seconds >= 60.0)
970 return NULL;
972 datetime = g_date_time_alloc (tz);
973 datetime->days = ymd_to_days (year, month, day);
974 datetime->usec = (hour * USEC_PER_HOUR)
975 + (minute * USEC_PER_MINUTE)
976 + (gint64) (seconds * USEC_PER_SECOND);
978 full_time = SEC_PER_DAY *
979 (ymd_to_days (year, month, day) - UNIX_EPOCH_START) +
980 SECS_PER_HOUR * hour +
981 SECS_PER_MINUTE * minute +
982 (int) seconds;
984 datetime->interval = g_time_zone_adjust_time (datetime->tz,
985 G_TIME_TYPE_STANDARD,
986 &full_time);
988 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
989 datetime->days = full_time / SEC_PER_DAY;
990 datetime->usec = (full_time % SEC_PER_DAY) * USEC_PER_SECOND;
991 datetime->usec += ((int) (seconds * USEC_PER_SECOND)) % USEC_PER_SECOND;
993 return datetime;
997 * g_date_time_new_local:
998 * @year: the year component of the date
999 * @month: the month component of the date
1000 * @day: the day component of the date
1001 * @hour: the hour component of the date
1002 * @minute: the minute component of the date
1003 * @seconds: the number of seconds past the minute
1005 * Creates a new #GDateTime corresponding to the given date and time in
1006 * the local time zone.
1008 * This call is equivalent to calling g_date_time_new() with the time
1009 * zone returned by g_time_zone_new_local().
1011 * Returns: a #GDateTime, or %NULL
1013 * Since: 2.26
1015 GDateTime *
1016 g_date_time_new_local (gint year,
1017 gint month,
1018 gint day,
1019 gint hour,
1020 gint minute,
1021 gdouble seconds)
1023 GDateTime *datetime;
1024 GTimeZone *local;
1026 local = g_time_zone_new_local ();
1027 datetime = g_date_time_new (local, year, month, day, hour, minute, seconds);
1028 g_time_zone_unref (local);
1030 return datetime;
1034 * g_date_time_new_utc:
1035 * @year: the year component of the date
1036 * @month: the month component of the date
1037 * @day: the day component of the date
1038 * @hour: the hour component of the date
1039 * @minute: the minute component of the date
1040 * @seconds: the number of seconds past the minute
1042 * Creates a new #GDateTime corresponding to the given date and time in
1043 * UTC.
1045 * This call is equivalent to calling g_date_time_new() with the time
1046 * zone returned by g_time_zone_new_utc().
1048 * Returns: a #GDateTime, or %NULL
1050 * Since: 2.26
1052 GDateTime *
1053 g_date_time_new_utc (gint year,
1054 gint month,
1055 gint day,
1056 gint hour,
1057 gint minute,
1058 gdouble seconds)
1060 GDateTime *datetime;
1061 GTimeZone *utc;
1063 utc = g_time_zone_new_utc ();
1064 datetime = g_date_time_new (utc, year, month, day, hour, minute, seconds);
1065 g_time_zone_unref (utc);
1067 return datetime;
1070 /* Adders {{{1 */
1073 * g_date_time_add:
1074 * @datetime: a #GDateTime
1075 * @timespan: a #GTimeSpan
1077 * Creates a copy of @datetime and adds the specified timespan to the copy.
1079 * Returns: the newly created #GDateTime which should be freed with
1080 * g_date_time_unref().
1082 * Since: 2.26
1084 GDateTime*
1085 g_date_time_add (GDateTime *datetime,
1086 GTimeSpan timespan)
1088 return g_date_time_from_instant (datetime->tz, timespan +
1089 g_date_time_to_instant (datetime));
1093 * g_date_time_add_years:
1094 * @datetime: a #GDateTime
1095 * @years: the number of years
1097 * Creates a copy of @datetime and adds the specified number of years to the
1098 * copy. Add negative values to subtract years.
1100 * Returns: the newly created #GDateTime which should be freed with
1101 * g_date_time_unref().
1103 * Since: 2.26
1105 GDateTime *
1106 g_date_time_add_years (GDateTime *datetime,
1107 gint years)
1109 gint year, month, day;
1111 g_return_val_if_fail (datetime != NULL, NULL);
1113 if (years < -10000 || years > 10000)
1114 return NULL;
1116 g_date_time_get_ymd (datetime, &year, &month, &day);
1117 year += years;
1119 /* only possible issue is if we've entered a year with no February 29
1121 if (month == 2 && day == 29 && !GREGORIAN_LEAP (year))
1122 day = 28;
1124 return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
1128 * g_date_time_add_months:
1129 * @datetime: a #GDateTime
1130 * @months: the number of months
1132 * Creates a copy of @datetime and adds the specified number of months to the
1133 * copy. Add negative values to subtract months.
1135 * Returns: the newly created #GDateTime which should be freed with
1136 * g_date_time_unref().
1138 * Since: 2.26
1140 GDateTime*
1141 g_date_time_add_months (GDateTime *datetime,
1142 gint months)
1144 gint year, month, day;
1146 g_return_val_if_fail (datetime != NULL, NULL);
1147 g_date_time_get_ymd (datetime, &year, &month, &day);
1149 if (months < -120000 || months > 120000)
1150 return NULL;
1152 year += months / 12;
1153 month += months % 12;
1154 if (month < 1)
1156 month += 12;
1157 year--;
1159 else if (month > 12)
1161 month -= 12;
1162 year++;
1165 day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
1167 return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
1171 * g_date_time_add_weeks:
1172 * @datetime: a #GDateTime
1173 * @weeks: the number of weeks
1175 * Creates a copy of @datetime and adds the specified number of weeks to the
1176 * copy. Add negative values to subtract weeks.
1178 * Returns: the newly created #GDateTime which should be freed with
1179 * g_date_time_unref().
1181 * Since: 2.26
1183 GDateTime*
1184 g_date_time_add_weeks (GDateTime *datetime,
1185 gint weeks)
1187 g_return_val_if_fail (datetime != NULL, NULL);
1189 return g_date_time_add_days (datetime, weeks * 7);
1193 * g_date_time_add_days:
1194 * @datetime: a #GDateTime
1195 * @days: the number of days
1197 * Creates a copy of @datetime and adds the specified number of days to the
1198 * copy. Add negative values to subtract days.
1200 * Returns: the newly created #GDateTime which should be freed with
1201 * g_date_time_unref().
1203 * Since: 2.26
1205 GDateTime*
1206 g_date_time_add_days (GDateTime *datetime,
1207 gint days)
1209 g_return_val_if_fail (datetime != NULL, NULL);
1211 if (days < -3660000 || days > 3660000)
1212 return NULL;
1214 return g_date_time_replace_days (datetime, datetime->days + days);
1218 * g_date_time_add_hours:
1219 * @datetime: a #GDateTime
1220 * @hours: the number of hours to add
1222 * Creates a copy of @datetime and adds the specified number of hours.
1223 * Add negative values to subtract hours.
1225 * Returns: the newly created #GDateTime which should be freed with
1226 * g_date_time_unref().
1228 * Since: 2.26
1230 GDateTime*
1231 g_date_time_add_hours (GDateTime *datetime,
1232 gint hours)
1234 return g_date_time_add (datetime, hours * USEC_PER_HOUR);
1238 * g_date_time_add_minutes:
1239 * @datetime: a #GDateTime
1240 * @minutes: the number of minutes to add
1242 * Creates a copy of @datetime adding the specified number of minutes.
1243 * Add negative values to subtract minutes.
1245 * Returns: the newly created #GDateTime which should be freed with
1246 * g_date_time_unref().
1248 * Since: 2.26
1250 GDateTime*
1251 g_date_time_add_minutes (GDateTime *datetime,
1252 gint minutes)
1254 return g_date_time_add (datetime, minutes * USEC_PER_MINUTE);
1259 * g_date_time_add_seconds:
1260 * @datetime: a #GDateTime
1261 * @seconds: the number of seconds to add
1263 * Creates a copy of @datetime and adds the specified number of seconds.
1264 * Add negative values to subtract seconds.
1266 * Returns: the newly created #GDateTime which should be freed with
1267 * g_date_time_unref().
1269 * Since: 2.26
1271 GDateTime*
1272 g_date_time_add_seconds (GDateTime *datetime,
1273 gdouble seconds)
1275 return g_date_time_add (datetime, seconds * USEC_PER_SECOND);
1279 * g_date_time_add_full:
1280 * @datetime: a #GDateTime
1281 * @years: the number of years to add
1282 * @months: the number of months to add
1283 * @days: the number of days to add
1284 * @hours: the number of hours to add
1285 * @minutes: the number of minutes to add
1286 * @seconds: the number of seconds to add
1288 * Creates a new #GDateTime adding the specified values to the current date and
1289 * time in @datetime. Add negative values to subtract.
1291 * Returns: the newly created #GDateTime that should be freed with
1292 * g_date_time_unref().
1294 * Since: 2.26
1296 GDateTime *
1297 g_date_time_add_full (GDateTime *datetime,
1298 gint years,
1299 gint months,
1300 gint days,
1301 gint hours,
1302 gint minutes,
1303 gdouble seconds)
1305 gint year, month, day;
1306 gint64 full_time;
1307 GDateTime *new;
1308 gint interval;
1310 g_return_val_if_fail (datetime != NULL, NULL);
1311 g_date_time_get_ymd (datetime, &year, &month, &day);
1313 months += years * 12;
1315 if (months < -120000 || months > 120000)
1316 return NULL;
1318 if (days < -3660000 || days > 3660000)
1319 return NULL;
1321 year += months / 12;
1322 month += months % 12;
1323 if (month < 1)
1325 month += 12;
1326 year--;
1328 else if (month > 12)
1330 month -= 12;
1331 year++;
1334 day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
1336 /* full_time is now in unix (local) time */
1337 full_time = datetime->usec / USEC_PER_SECOND + SEC_PER_DAY *
1338 (ymd_to_days (year, month, day) + days - UNIX_EPOCH_START);
1340 interval = g_time_zone_adjust_time (datetime->tz,
1341 g_time_zone_is_dst (datetime->tz,
1342 datetime->interval),
1343 &full_time);
1345 /* move to UTC unix time */
1346 full_time -= g_time_zone_get_offset (datetime->tz, interval);
1348 /* convert back to an instant, add back fractional seconds */
1349 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
1350 full_time = full_time * USEC_PER_SECOND +
1351 datetime->usec % USEC_PER_SECOND;
1353 /* do the actual addition now */
1354 full_time += (hours * USEC_PER_HOUR) +
1355 (minutes * USEC_PER_MINUTE) +
1356 (gint64) (seconds * USEC_PER_SECOND);
1358 /* find the new interval */
1359 interval = g_time_zone_find_interval (datetime->tz,
1360 G_TIME_TYPE_UNIVERSAL,
1361 INSTANT_TO_UNIX (full_time));
1363 /* convert back into local time */
1364 full_time += USEC_PER_SECOND *
1365 g_time_zone_get_offset (datetime->tz, interval);
1367 /* split into days and usec of a new datetime */
1368 new = g_date_time_alloc (datetime->tz);
1369 new->interval = interval;
1370 new->days = full_time / USEC_PER_DAY;
1371 new->usec = full_time % USEC_PER_DAY;
1373 /* XXX validate */
1375 return new;
1378 /* Compare, difference, hash, equal {{{1 */
1380 * g_date_time_compare:
1381 * @dt1: (not nullable): first #GDateTime to compare
1382 * @dt2: (not nullable): second #GDateTime to compare
1384 * A comparison function for #GDateTimes that is suitable
1385 * as a #GCompareFunc. Both #GDateTimes must be non-%NULL.
1387 * Returns: -1, 0 or 1 if @dt1 is less than, equal to or greater
1388 * than @dt2.
1390 * Since: 2.26
1392 gint
1393 g_date_time_compare (gconstpointer dt1,
1394 gconstpointer dt2)
1396 gint64 difference;
1398 difference = g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2);
1400 if (difference < 0)
1401 return -1;
1403 else if (difference > 0)
1404 return 1;
1406 else
1407 return 0;
1411 * g_date_time_difference:
1412 * @end: a #GDateTime
1413 * @begin: a #GDateTime
1415 * Calculates the difference in time between @end and @begin. The
1416 * #GTimeSpan that is returned is effectively @end - @begin (ie:
1417 * positive if the first parameter is larger).
1419 * Returns: the difference between the two #GDateTime, as a time
1420 * span expressed in microseconds.
1422 * Since: 2.26
1424 GTimeSpan
1425 g_date_time_difference (GDateTime *end,
1426 GDateTime *begin)
1428 g_return_val_if_fail (begin != NULL, 0);
1429 g_return_val_if_fail (end != NULL, 0);
1431 return g_date_time_to_instant (end) -
1432 g_date_time_to_instant (begin);
1436 * g_date_time_hash:
1437 * @datetime: (not nullable): a #GDateTime
1439 * Hashes @datetime into a #guint, suitable for use within #GHashTable.
1441 * Returns: a #guint containing the hash
1443 * Since: 2.26
1445 guint
1446 g_date_time_hash (gconstpointer datetime)
1448 return g_date_time_to_instant ((GDateTime *) datetime);
1452 * g_date_time_equal:
1453 * @dt1: (not nullable): a #GDateTime
1454 * @dt2: (not nullable): a #GDateTime
1456 * Checks to see if @dt1 and @dt2 are equal.
1458 * Equal here means that they represent the same moment after converting
1459 * them to the same time zone.
1461 * Returns: %TRUE if @dt1 and @dt2 are equal
1463 * Since: 2.26
1465 gboolean
1466 g_date_time_equal (gconstpointer dt1,
1467 gconstpointer dt2)
1469 return g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2) == 0;
1472 /* Year, Month, Day Getters {{{1 */
1474 * g_date_time_get_ymd:
1475 * @datetime: a #GDateTime.
1476 * @year: (out) (optional): the return location for the gregorian year, or %NULL.
1477 * @month: (out) (optional): the return location for the month of the year, or %NULL.
1478 * @day: (out) (optional): the return location for the day of the month, or %NULL.
1480 * Retrieves the Gregorian day, month, and year of a given #GDateTime.
1482 * Since: 2.26
1484 void
1485 g_date_time_get_ymd (GDateTime *datetime,
1486 gint *year,
1487 gint *month,
1488 gint *day)
1490 gint the_year;
1491 gint the_month;
1492 gint the_day;
1493 gint remaining_days;
1494 gint y100_cycles;
1495 gint y4_cycles;
1496 gint y1_cycles;
1497 gint preceding;
1498 gboolean leap;
1500 g_return_if_fail (datetime != NULL);
1502 remaining_days = datetime->days;
1505 * We need to convert an offset in days to its year/month/day representation.
1506 * Leap years makes this a little trickier than it should be, so we use
1507 * 400, 100 and 4 years cycles here to get to the correct year.
1510 /* Our days offset starts sets 0001-01-01 as day 1, if it was day 0 our
1511 * math would be simpler, so let's do it */
1512 remaining_days--;
1514 the_year = (remaining_days / DAYS_IN_400YEARS) * 400 + 1;
1515 remaining_days = remaining_days % DAYS_IN_400YEARS;
1517 y100_cycles = remaining_days / DAYS_IN_100YEARS;
1518 remaining_days = remaining_days % DAYS_IN_100YEARS;
1519 the_year += y100_cycles * 100;
1521 y4_cycles = remaining_days / DAYS_IN_4YEARS;
1522 remaining_days = remaining_days % DAYS_IN_4YEARS;
1523 the_year += y4_cycles * 4;
1525 y1_cycles = remaining_days / 365;
1526 the_year += y1_cycles;
1527 remaining_days = remaining_days % 365;
1529 if (y1_cycles == 4 || y100_cycles == 4) {
1530 g_assert (remaining_days == 0);
1532 /* special case that indicates that the date is actually one year before,
1533 * in the 31th of December */
1534 the_year--;
1535 the_month = 12;
1536 the_day = 31;
1537 goto end;
1540 /* now get the month and the day */
1541 leap = y1_cycles == 3 && (y4_cycles != 24 || y100_cycles == 3);
1543 g_assert (leap == GREGORIAN_LEAP(the_year));
1545 the_month = (remaining_days + 50) >> 5;
1546 preceding = (days_in_year[0][the_month - 1] + (the_month > 2 && leap));
1547 if (preceding > remaining_days)
1549 /* estimate is too large */
1550 the_month -= 1;
1551 preceding -= leap ? days_in_months[1][the_month]
1552 : days_in_months[0][the_month];
1555 remaining_days -= preceding;
1556 g_assert(0 <= remaining_days);
1558 the_day = remaining_days + 1;
1560 end:
1561 if (year)
1562 *year = the_year;
1563 if (month)
1564 *month = the_month;
1565 if (day)
1566 *day = the_day;
1570 * g_date_time_get_year:
1571 * @datetime: A #GDateTime
1573 * Retrieves the year represented by @datetime in the Gregorian calendar.
1575 * Returns: the year represented by @datetime
1577 * Since: 2.26
1579 gint
1580 g_date_time_get_year (GDateTime *datetime)
1582 gint year;
1584 g_return_val_if_fail (datetime != NULL, 0);
1586 g_date_time_get_ymd (datetime, &year, NULL, NULL);
1588 return year;
1592 * g_date_time_get_month:
1593 * @datetime: a #GDateTime
1595 * Retrieves the month of the year represented by @datetime in the Gregorian
1596 * calendar.
1598 * Returns: the month represented by @datetime
1600 * Since: 2.26
1602 gint
1603 g_date_time_get_month (GDateTime *datetime)
1605 gint month;
1607 g_return_val_if_fail (datetime != NULL, 0);
1609 g_date_time_get_ymd (datetime, NULL, &month, NULL);
1611 return month;
1615 * g_date_time_get_day_of_month:
1616 * @datetime: a #GDateTime
1618 * Retrieves the day of the month represented by @datetime in the gregorian
1619 * calendar.
1621 * Returns: the day of the month
1623 * Since: 2.26
1625 gint
1626 g_date_time_get_day_of_month (GDateTime *datetime)
1628 gint day_of_year,
1630 const guint16 *days;
1631 guint16 last = 0;
1633 g_return_val_if_fail (datetime != NULL, 0);
1635 days = days_in_year[GREGORIAN_LEAP (g_date_time_get_year (datetime)) ? 1 : 0];
1636 g_date_time_get_week_number (datetime, NULL, NULL, &day_of_year);
1638 for (i = 1; i <= 12; i++)
1640 if (days [i] >= day_of_year)
1641 return day_of_year - last;
1642 last = days [i];
1645 g_warn_if_reached ();
1646 return 0;
1649 /* Week of year / day of week getters {{{1 */
1651 * g_date_time_get_week_numbering_year:
1652 * @datetime: a #GDateTime
1654 * Returns the ISO 8601 week-numbering year in which the week containing
1655 * @datetime falls.
1657 * This function, taken together with g_date_time_get_week_of_year() and
1658 * g_date_time_get_day_of_week() can be used to determine the full ISO
1659 * week date on which @datetime falls.
1661 * This is usually equal to the normal Gregorian year (as returned by
1662 * g_date_time_get_year()), except as detailed below:
1664 * For Thursday, the week-numbering year is always equal to the usual
1665 * calendar year. For other days, the number is such that every day
1666 * within a complete week (Monday to Sunday) is contained within the
1667 * same week-numbering year.
1669 * For Monday, Tuesday and Wednesday occurring near the end of the year,
1670 * this may mean that the week-numbering year is one greater than the
1671 * calendar year (so that these days have the same week-numbering year
1672 * as the Thursday occurring early in the next year).
1674 * For Friday, Saturday and Sunday occurring near the start of the year,
1675 * this may mean that the week-numbering year is one less than the
1676 * calendar year (so that these days have the same week-numbering year
1677 * as the Thursday occurring late in the previous year).
1679 * An equivalent description is that the week-numbering year is equal to
1680 * the calendar year containing the majority of the days in the current
1681 * week (Monday to Sunday).
1683 * Note that January 1 0001 in the proleptic Gregorian calendar is a
1684 * Monday, so this function never returns 0.
1686 * Returns: the ISO 8601 week-numbering year for @datetime
1688 * Since: 2.26
1690 gint
1691 g_date_time_get_week_numbering_year (GDateTime *datetime)
1693 gint year, month, day, weekday;
1695 g_date_time_get_ymd (datetime, &year, &month, &day);
1696 weekday = g_date_time_get_day_of_week (datetime);
1698 /* January 1, 2, 3 might be in the previous year if they occur after
1699 * Thursday.
1701 * Jan 1: Friday, Saturday, Sunday => day 1: weekday 5, 6, 7
1702 * Jan 2: Saturday, Sunday => day 2: weekday 6, 7
1703 * Jan 3: Sunday => day 3: weekday 7
1705 * So we have a special case if (day - weekday) <= -4
1707 if (month == 1 && (day - weekday) <= -4)
1708 return year - 1;
1710 /* December 29, 30, 31 might be in the next year if they occur before
1711 * Thursday.
1713 * Dec 31: Monday, Tuesday, Wednesday => day 31: weekday 1, 2, 3
1714 * Dec 30: Monday, Tuesday => day 30: weekday 1, 2
1715 * Dec 29: Monday => day 29: weekday 1
1717 * So we have a special case if (day - weekday) >= 28
1719 else if (month == 12 && (day - weekday) >= 28)
1720 return year + 1;
1722 else
1723 return year;
1727 * g_date_time_get_week_of_year:
1728 * @datetime: a #GDateTime
1730 * Returns the ISO 8601 week number for the week containing @datetime.
1731 * The ISO 8601 week number is the same for every day of the week (from
1732 * Moday through Sunday). That can produce some unusual results
1733 * (described below).
1735 * The first week of the year is week 1. This is the week that contains
1736 * the first Thursday of the year. Equivalently, this is the first week
1737 * that has more than 4 of its days falling within the calendar year.
1739 * The value 0 is never returned by this function. Days contained
1740 * within a year but occurring before the first ISO 8601 week of that
1741 * year are considered as being contained in the last week of the
1742 * previous year. Similarly, the final days of a calendar year may be
1743 * considered as being part of the first ISO 8601 week of the next year
1744 * if 4 or more days of that week are contained within the new year.
1746 * Returns: the ISO 8601 week number for @datetime.
1748 * Since: 2.26
1750 gint
1751 g_date_time_get_week_of_year (GDateTime *datetime)
1753 gint weeknum;
1755 g_return_val_if_fail (datetime != NULL, 0);
1757 g_date_time_get_week_number (datetime, &weeknum, NULL, NULL);
1759 return weeknum;
1763 * g_date_time_get_day_of_week:
1764 * @datetime: a #GDateTime
1766 * Retrieves the ISO 8601 day of the week on which @datetime falls (1 is
1767 * Monday, 2 is Tuesday... 7 is Sunday).
1769 * Returns: the day of the week
1771 * Since: 2.26
1773 gint
1774 g_date_time_get_day_of_week (GDateTime *datetime)
1776 g_return_val_if_fail (datetime != NULL, 0);
1778 return (datetime->days - 1) % 7 + 1;
1781 /* Day of year getter {{{1 */
1783 * g_date_time_get_day_of_year:
1784 * @datetime: a #GDateTime
1786 * Retrieves the day of the year represented by @datetime in the Gregorian
1787 * calendar.
1789 * Returns: the day of the year
1791 * Since: 2.26
1793 gint
1794 g_date_time_get_day_of_year (GDateTime *datetime)
1796 gint doy = 0;
1798 g_return_val_if_fail (datetime != NULL, 0);
1800 g_date_time_get_week_number (datetime, NULL, NULL, &doy);
1801 return doy;
1804 /* Time component getters {{{1 */
1807 * g_date_time_get_hour:
1808 * @datetime: a #GDateTime
1810 * Retrieves the hour of the day represented by @datetime
1812 * Returns: the hour of the day
1814 * Since: 2.26
1816 gint
1817 g_date_time_get_hour (GDateTime *datetime)
1819 g_return_val_if_fail (datetime != NULL, 0);
1821 return (datetime->usec / USEC_PER_HOUR);
1825 * g_date_time_get_minute:
1826 * @datetime: a #GDateTime
1828 * Retrieves the minute of the hour represented by @datetime
1830 * Returns: the minute of the hour
1832 * Since: 2.26
1834 gint
1835 g_date_time_get_minute (GDateTime *datetime)
1837 g_return_val_if_fail (datetime != NULL, 0);
1839 return (datetime->usec % USEC_PER_HOUR) / USEC_PER_MINUTE;
1843 * g_date_time_get_second:
1844 * @datetime: a #GDateTime
1846 * Retrieves the second of the minute represented by @datetime
1848 * Returns: the second represented by @datetime
1850 * Since: 2.26
1852 gint
1853 g_date_time_get_second (GDateTime *datetime)
1855 g_return_val_if_fail (datetime != NULL, 0);
1857 return (datetime->usec % USEC_PER_MINUTE) / USEC_PER_SECOND;
1861 * g_date_time_get_microsecond:
1862 * @datetime: a #GDateTime
1864 * Retrieves the microsecond of the date represented by @datetime
1866 * Returns: the microsecond of the second
1868 * Since: 2.26
1870 gint
1871 g_date_time_get_microsecond (GDateTime *datetime)
1873 g_return_val_if_fail (datetime != NULL, 0);
1875 return (datetime->usec % USEC_PER_SECOND);
1879 * g_date_time_get_seconds:
1880 * @datetime: a #GDateTime
1882 * Retrieves the number of seconds since the start of the last minute,
1883 * including the fractional part.
1885 * Returns: the number of seconds
1887 * Since: 2.26
1889 gdouble
1890 g_date_time_get_seconds (GDateTime *datetime)
1892 g_return_val_if_fail (datetime != NULL, 0);
1894 return (datetime->usec % USEC_PER_MINUTE) / 1000000.0;
1897 /* Exporters {{{1 */
1899 * g_date_time_to_unix:
1900 * @datetime: a #GDateTime
1902 * Gives the Unix time corresponding to @datetime, rounding down to the
1903 * nearest second.
1905 * Unix time is the number of seconds that have elapsed since 1970-01-01
1906 * 00:00:00 UTC, regardless of the time zone associated with @datetime.
1908 * Returns: the Unix time corresponding to @datetime
1910 * Since: 2.26
1912 gint64
1913 g_date_time_to_unix (GDateTime *datetime)
1915 return INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
1919 * g_date_time_to_timeval:
1920 * @datetime: a #GDateTime
1921 * @tv: a #GTimeVal to modify
1923 * Stores the instant in time that @datetime represents into @tv.
1925 * The time contained in a #GTimeVal is always stored in the form of
1926 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the time
1927 * zone associated with @datetime.
1929 * On systems where 'long' is 32bit (ie: all 32bit systems and all
1930 * Windows systems), a #GTimeVal is incapable of storing the entire
1931 * range of values that #GDateTime is capable of expressing. On those
1932 * systems, this function returns %FALSE to indicate that the time is
1933 * out of range.
1935 * On systems where 'long' is 64bit, this function never fails.
1937 * Returns: %TRUE if successful, else %FALSE
1939 * Since: 2.26
1941 gboolean
1942 g_date_time_to_timeval (GDateTime *datetime,
1943 GTimeVal *tv)
1945 tv->tv_sec = INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
1946 tv->tv_usec = datetime->usec % USEC_PER_SECOND;
1948 return TRUE;
1951 /* Timezone queries {{{1 */
1953 * g_date_time_get_utc_offset:
1954 * @datetime: a #GDateTime
1956 * Determines the offset to UTC in effect at the time and in the time
1957 * zone of @datetime.
1959 * The offset is the number of microseconds that you add to UTC time to
1960 * arrive at local time for the time zone (ie: negative numbers for time
1961 * zones west of GMT, positive numbers for east).
1963 * If @datetime represents UTC time, then the offset is always zero.
1965 * Returns: the number of microseconds that should be added to UTC to
1966 * get the local time
1968 * Since: 2.26
1970 GTimeSpan
1971 g_date_time_get_utc_offset (GDateTime *datetime)
1973 gint offset;
1975 g_return_val_if_fail (datetime != NULL, 0);
1977 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
1979 return (gint64) offset * USEC_PER_SECOND;
1983 * g_date_time_get_timezone_abbreviation:
1984 * @datetime: a #GDateTime
1986 * Determines the time zone abbreviation to be used at the time and in
1987 * the time zone of @datetime.
1989 * For example, in Toronto this is currently "EST" during the winter
1990 * months and "EDT" during the summer months when daylight savings
1991 * time is in effect.
1993 * Returns: (transfer none): the time zone abbreviation. The returned
1994 * string is owned by the #GDateTime and it should not be
1995 * modified or freed
1997 * Since: 2.26
1999 const gchar *
2000 g_date_time_get_timezone_abbreviation (GDateTime *datetime)
2002 g_return_val_if_fail (datetime != NULL, NULL);
2004 return g_time_zone_get_abbreviation (datetime->tz, datetime->interval);
2008 * g_date_time_is_daylight_savings:
2009 * @datetime: a #GDateTime
2011 * Determines if daylight savings time is in effect at the time and in
2012 * the time zone of @datetime.
2014 * Returns: %TRUE if daylight savings time is in effect
2016 * Since: 2.26
2018 gboolean
2019 g_date_time_is_daylight_savings (GDateTime *datetime)
2021 g_return_val_if_fail (datetime != NULL, FALSE);
2023 return g_time_zone_is_dst (datetime->tz, datetime->interval);
2026 /* Timezone convert {{{1 */
2028 * g_date_time_to_timezone:
2029 * @datetime: a #GDateTime
2030 * @tz: the new #GTimeZone
2032 * Create a new #GDateTime corresponding to the same instant in time as
2033 * @datetime, but in the time zone @tz.
2035 * This call can fail in the case that the time goes out of bounds. For
2036 * example, converting 0001-01-01 00:00:00 UTC to a time zone west of
2037 * Greenwich will fail (due to the year 0 being out of range).
2039 * You should release the return value by calling g_date_time_unref()
2040 * when you are done with it.
2042 * Returns: a new #GDateTime, or %NULL
2044 * Since: 2.26
2046 GDateTime *
2047 g_date_time_to_timezone (GDateTime *datetime,
2048 GTimeZone *tz)
2050 return g_date_time_from_instant (tz, g_date_time_to_instant (datetime));
2054 * g_date_time_to_local:
2055 * @datetime: a #GDateTime
2057 * Creates a new #GDateTime corresponding to the same instant in time as
2058 * @datetime, but in the local time zone.
2060 * This call is equivalent to calling g_date_time_to_timezone() with the
2061 * time zone returned by g_time_zone_new_local().
2063 * Returns: the newly created #GDateTime
2065 * Since: 2.26
2067 GDateTime *
2068 g_date_time_to_local (GDateTime *datetime)
2070 GDateTime *new;
2071 GTimeZone *local;
2073 local = g_time_zone_new_local ();
2074 new = g_date_time_to_timezone (datetime, local);
2075 g_time_zone_unref (local);
2077 return new;
2081 * g_date_time_to_utc:
2082 * @datetime: a #GDateTime
2084 * Creates a new #GDateTime corresponding to the same instant in time as
2085 * @datetime, but in UTC.
2087 * This call is equivalent to calling g_date_time_to_timezone() with the
2088 * time zone returned by g_time_zone_new_utc().
2090 * Returns: the newly created #GDateTime
2092 * Since: 2.26
2094 GDateTime *
2095 g_date_time_to_utc (GDateTime *datetime)
2097 GDateTime *new;
2098 GTimeZone *utc;
2100 utc = g_time_zone_new_utc ();
2101 new = g_date_time_to_timezone (datetime, utc);
2102 g_time_zone_unref (utc);
2104 return new;
2107 /* Format {{{1 */
2109 static gboolean
2110 format_z (GString *outstr,
2111 gint offset,
2112 guint colons)
2114 gint hours;
2115 gint minutes;
2116 gint seconds;
2118 hours = offset / 3600;
2119 minutes = ABS (offset) / 60 % 60;
2120 seconds = ABS (offset) % 60;
2122 switch (colons)
2124 case 0:
2125 g_string_append_printf (outstr, "%+03d%02d",
2126 hours,
2127 minutes);
2128 break;
2130 case 1:
2131 g_string_append_printf (outstr, "%+03d:%02d",
2132 hours,
2133 minutes);
2134 break;
2136 case 2:
2137 g_string_append_printf (outstr, "%+03d:%02d:%02d",
2138 hours,
2139 minutes,
2140 seconds);
2141 break;
2143 case 3:
2144 g_string_append_printf (outstr, "%+03d", hours);
2146 if (minutes != 0 || seconds != 0)
2148 g_string_append_printf (outstr, ":%02d", minutes);
2150 if (seconds != 0)
2151 g_string_append_printf (outstr, ":%02d", seconds);
2153 break;
2155 default:
2156 return FALSE;
2159 return TRUE;
2162 static void
2163 format_number (GString *str,
2164 gboolean use_alt_digits,
2165 gchar *pad,
2166 gint width,
2167 guint32 number)
2169 const gchar *ascii_digits[10] = {
2170 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
2172 const gchar **digits = ascii_digits;
2173 const gchar *tmp[10];
2174 gint i = 0;
2176 g_return_if_fail (width <= 10);
2178 #ifdef HAVE_LANGINFO_OUTDIGIT
2179 if (use_alt_digits)
2181 static const gchar *alt_digits[10];
2182 static gsize initialised;
2183 /* 2^32 has 10 digits */
2185 if G_UNLIKELY (g_once_init_enter (&initialised))
2187 #define DO_DIGIT(n) \
2188 alt_digits[n] = nl_langinfo (_NL_CTYPE_OUTDIGIT## n ##_MB)
2189 DO_DIGIT(0); DO_DIGIT(1); DO_DIGIT(2); DO_DIGIT(3); DO_DIGIT(4);
2190 DO_DIGIT(5); DO_DIGIT(6); DO_DIGIT(7); DO_DIGIT(8); DO_DIGIT(9);
2191 #undef DO_DIGIT
2192 g_once_init_leave (&initialised, TRUE);
2195 digits = alt_digits;
2197 #endif /* HAVE_LANGINFO_OUTDIGIT */
2201 tmp[i++] = digits[number % 10];
2202 number /= 10;
2204 while (number);
2206 while (pad && i < width)
2207 tmp[i++] = *pad == '0' ? digits[0] : pad;
2209 /* should really be impossible */
2210 g_assert (i <= 10);
2212 while (i)
2213 g_string_append (str, tmp[--i]);
2216 static gboolean
2217 format_ampm (GDateTime *datetime,
2218 GString *outstr,
2219 gboolean locale_is_utf8,
2220 gboolean uppercase)
2222 const gchar *ampm;
2223 gchar *tmp = NULL, *ampm_dup;
2224 gsize len;
2226 ampm = GET_AMPM (datetime);
2228 if (!ampm || ampm[0] == '\0')
2229 ampm = get_fallback_ampm (g_date_time_get_hour (datetime));
2231 #if defined (HAVE_LANGINFO_TIME)
2232 if (!locale_is_utf8)
2234 /* This assumes that locale encoding can't have embedded NULs */
2235 ampm = tmp = g_locale_to_utf8 (ampm, -1, NULL, NULL, NULL);
2236 if (!tmp)
2237 return FALSE;
2239 #endif
2240 if (uppercase)
2241 ampm_dup = g_utf8_strup (ampm, -1);
2242 else
2243 ampm_dup = g_utf8_strdown (ampm, -1);
2244 len = strlen (ampm_dup);
2245 if (!locale_is_utf8)
2247 #if defined (HAVE_LANGINFO_TIME)
2248 g_free (tmp);
2249 #endif
2250 tmp = g_locale_from_utf8 (ampm_dup, -1, NULL, &len, NULL);
2251 g_free (ampm_dup);
2252 if (!tmp)
2253 return FALSE;
2254 ampm_dup = tmp;
2256 g_string_append_len (outstr, ampm_dup, len);
2257 g_free (ampm_dup);
2259 return TRUE;
2262 static gboolean g_date_time_format_locale (GDateTime *datetime,
2263 const gchar *format,
2264 GString *outstr,
2265 gboolean locale_is_utf8);
2267 /* g_date_time_format() subroutine that takes a locale-encoded format
2268 * string and produces a locale-encoded date/time string.
2270 static gboolean
2271 g_date_time_locale_format_locale (GDateTime *datetime,
2272 const gchar *format,
2273 GString *outstr,
2274 gboolean locale_is_utf8)
2276 gchar *utf8_format;
2277 gboolean success;
2279 if (locale_is_utf8)
2280 return g_date_time_format_locale (datetime, format, outstr,
2281 locale_is_utf8);
2283 utf8_format = g_locale_to_utf8 (format, -1, NULL, NULL, NULL);
2284 if (!utf8_format)
2285 return FALSE;
2287 success = g_date_time_format_locale (datetime, utf8_format, outstr,
2288 locale_is_utf8);
2289 g_free (utf8_format);
2290 return success;
2293 /* g_date_time_format() subroutine that takes a UTF-8 format
2294 * string and produces a locale-encoded date/time string.
2296 static gboolean
2297 g_date_time_format_locale (GDateTime *datetime,
2298 const gchar *format,
2299 GString *outstr,
2300 gboolean locale_is_utf8)
2302 guint len;
2303 guint colons;
2304 gchar *tmp;
2305 gsize tmp_len;
2306 gunichar c;
2307 gboolean alt_digits = FALSE;
2308 gboolean pad_set = FALSE;
2309 gchar *pad = "";
2310 const gchar *name;
2311 const gchar *tz;
2313 while (*format)
2315 len = strcspn (format, "%");
2316 if (len)
2318 if (locale_is_utf8)
2319 g_string_append_len (outstr, format, len);
2320 else
2322 tmp = g_locale_from_utf8 (format, len, NULL, &tmp_len, NULL);
2323 if (!tmp)
2324 return FALSE;
2325 g_string_append_len (outstr, tmp, tmp_len);
2326 g_free (tmp);
2330 format += len;
2331 if (!*format)
2332 break;
2334 g_assert (*format == '%');
2335 format++;
2336 if (!*format)
2337 break;
2339 colons = 0;
2340 alt_digits = FALSE;
2341 pad_set = FALSE;
2343 next_mod:
2344 c = g_utf8_get_char (format);
2345 format = g_utf8_next_char (format);
2346 switch (c)
2348 case 'a':
2349 name = WEEKDAY_ABBR (datetime);
2350 #if !defined (HAVE_LANGINFO_TIME)
2351 if (!locale_is_utf8)
2353 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2354 if (!tmp)
2355 return FALSE;
2356 g_string_append_len (outstr, tmp, tmp_len);
2357 g_free (tmp);
2359 else
2360 #endif
2362 g_string_append (outstr, name);
2364 break;
2365 case 'A':
2366 name = WEEKDAY_FULL (datetime);
2367 #if !defined (HAVE_LANGINFO_TIME)
2368 if (!locale_is_utf8)
2370 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2371 if (!tmp)
2372 return FALSE;
2373 g_string_append_len (outstr, tmp, tmp_len);
2374 g_free (tmp);
2376 else
2377 #endif
2379 g_string_append (outstr, name);
2381 break;
2382 case 'b':
2383 name = MONTH_ABBR (datetime);
2384 #if !defined (HAVE_LANGINFO_TIME)
2385 if (!locale_is_utf8)
2387 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2388 if (!tmp)
2389 return FALSE;
2390 g_string_append_len (outstr, tmp, tmp_len);
2391 g_free (tmp);
2393 else
2394 #endif
2396 g_string_append (outstr, name);
2398 break;
2399 case 'B':
2400 name = MONTH_FULL (datetime);
2401 #if !defined (HAVE_LANGINFO_TIME)
2402 if (!locale_is_utf8)
2404 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2405 if (!tmp)
2406 return FALSE;
2407 g_string_append_len (outstr, tmp, tmp_len);
2408 g_free (tmp);
2410 else
2411 #endif
2413 g_string_append (outstr, name);
2415 break;
2416 case 'c':
2418 if (!g_date_time_locale_format_locale (datetime, PREFERRED_DATE_TIME_FMT,
2419 outstr, locale_is_utf8))
2420 return FALSE;
2422 break;
2423 case 'C':
2424 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2425 g_date_time_get_year (datetime) / 100);
2426 break;
2427 case 'd':
2428 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2429 g_date_time_get_day_of_month (datetime));
2430 break;
2431 case 'e':
2432 format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
2433 g_date_time_get_day_of_month (datetime));
2434 break;
2435 case 'F':
2436 g_string_append_printf (outstr, "%d-%02d-%02d",
2437 g_date_time_get_year (datetime),
2438 g_date_time_get_month (datetime),
2439 g_date_time_get_day_of_month (datetime));
2440 break;
2441 case 'g':
2442 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2443 g_date_time_get_week_numbering_year (datetime) % 100);
2444 break;
2445 case 'G':
2446 format_number (outstr, alt_digits, pad_set ? pad : 0, 0,
2447 g_date_time_get_week_numbering_year (datetime));
2448 break;
2449 case 'h':
2450 name = MONTH_ABBR (datetime);
2451 #if !defined (HAVE_LANGINFO_TIME)
2452 if (!locale_is_utf8)
2454 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2455 if (!tmp)
2456 return FALSE;
2457 g_string_append_len (outstr, tmp, tmp_len);
2458 g_free (tmp);
2460 else
2461 #endif
2463 g_string_append (outstr, name);
2465 break;
2466 case 'H':
2467 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2468 g_date_time_get_hour (datetime));
2469 break;
2470 case 'I':
2471 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2472 (g_date_time_get_hour (datetime) + 11) % 12 + 1);
2473 break;
2474 case 'j':
2475 format_number (outstr, alt_digits, pad_set ? pad : "0", 3,
2476 g_date_time_get_day_of_year (datetime));
2477 break;
2478 case 'k':
2479 format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
2480 g_date_time_get_hour (datetime));
2481 break;
2482 case 'l':
2483 format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
2484 (g_date_time_get_hour (datetime) + 11) % 12 + 1);
2485 break;
2486 case 'n':
2487 g_string_append_c (outstr, '\n');
2488 break;
2489 case 'm':
2490 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2491 g_date_time_get_month (datetime));
2492 break;
2493 case 'M':
2494 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2495 g_date_time_get_minute (datetime));
2496 break;
2497 case 'O':
2498 alt_digits = TRUE;
2499 goto next_mod;
2500 case 'p':
2501 if (!format_ampm (datetime, outstr, locale_is_utf8, TRUE))
2502 return FALSE;
2503 break;
2504 case 'P':
2505 if (!format_ampm (datetime, outstr, locale_is_utf8, FALSE))
2506 return FALSE;
2507 break;
2508 case 'r':
2510 if (!g_date_time_locale_format_locale (datetime, PREFERRED_12HR_TIME_FMT,
2511 outstr, locale_is_utf8))
2512 return FALSE;
2514 break;
2515 case 'R':
2516 g_string_append_printf (outstr, "%02d:%02d",
2517 g_date_time_get_hour (datetime),
2518 g_date_time_get_minute (datetime));
2519 break;
2520 case 's':
2521 g_string_append_printf (outstr, "%" G_GINT64_FORMAT, g_date_time_to_unix (datetime));
2522 break;
2523 case 'S':
2524 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2525 g_date_time_get_second (datetime));
2526 break;
2527 case 't':
2528 g_string_append_c (outstr, '\t');
2529 break;
2530 case 'T':
2531 g_string_append_printf (outstr, "%02d:%02d:%02d",
2532 g_date_time_get_hour (datetime),
2533 g_date_time_get_minute (datetime),
2534 g_date_time_get_second (datetime));
2535 break;
2536 case 'u':
2537 format_number (outstr, alt_digits, 0, 0,
2538 g_date_time_get_day_of_week (datetime));
2539 break;
2540 case 'V':
2541 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2542 g_date_time_get_week_of_year (datetime));
2543 break;
2544 case 'w':
2545 format_number (outstr, alt_digits, 0, 0,
2546 g_date_time_get_day_of_week (datetime) % 7);
2547 break;
2548 case 'x':
2550 if (!g_date_time_locale_format_locale (datetime, PREFERRED_DATE_FMT,
2551 outstr, locale_is_utf8))
2552 return FALSE;
2554 break;
2555 case 'X':
2557 if (!g_date_time_locale_format_locale (datetime, PREFERRED_TIME_FMT,
2558 outstr, locale_is_utf8))
2559 return FALSE;
2561 break;
2562 case 'y':
2563 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2564 g_date_time_get_year (datetime) % 100);
2565 break;
2566 case 'Y':
2567 format_number (outstr, alt_digits, 0, 0,
2568 g_date_time_get_year (datetime));
2569 break;
2570 case 'z':
2572 gint64 offset;
2573 offset = g_date_time_get_utc_offset (datetime) / USEC_PER_SECOND;
2574 if (!format_z (outstr, (int) offset, colons))
2575 return FALSE;
2577 break;
2578 case 'Z':
2579 tz = g_date_time_get_timezone_abbreviation (datetime);
2580 tmp_len = strlen (tz);
2581 if (!locale_is_utf8)
2583 tz = tmp = g_locale_from_utf8 (tz, -1, NULL, &tmp_len, NULL);
2584 if (!tmp)
2585 return FALSE;
2587 g_string_append_len (outstr, tz, tmp_len);
2588 if (!locale_is_utf8)
2589 g_free (tmp);
2590 break;
2591 case '%':
2592 g_string_append_c (outstr, '%');
2593 break;
2594 case '-':
2595 pad_set = TRUE;
2596 pad = "";
2597 goto next_mod;
2598 case '_':
2599 pad_set = TRUE;
2600 pad = " ";
2601 goto next_mod;
2602 case '0':
2603 pad_set = TRUE;
2604 pad = "0";
2605 goto next_mod;
2606 case ':':
2607 /* Colons are only allowed before 'z' */
2608 if (*format && *format != 'z' && *format != ':')
2609 return FALSE;
2610 colons++;
2611 goto next_mod;
2612 default:
2613 return FALSE;
2617 return TRUE;
2621 * g_date_time_format:
2622 * @datetime: A #GDateTime
2623 * @format: a valid UTF-8 string, containing the format for the
2624 * #GDateTime
2626 * Creates a newly allocated string representing the requested @format.
2628 * The format strings understood by this function are a subset of the
2629 * strftime() format language as specified by C99. The \%D, \%U and \%W
2630 * conversions are not supported, nor is the 'E' modifier. The GNU
2631 * extensions \%k, \%l, \%s and \%P are supported, however, as are the
2632 * '0', '_' and '-' modifiers.
2634 * In contrast to strftime(), this function always produces a UTF-8
2635 * string, regardless of the current locale. Note that the rendering of
2636 * many formats is locale-dependent and may not match the strftime()
2637 * output exactly.
2639 * The following format specifiers are supported:
2641 * - \%a: the abbreviated weekday name according to the current locale
2642 * - \%A: the full weekday name according to the current locale
2643 * - \%b: the abbreviated month name according to the current locale
2644 * - \%B: the full month name according to the current locale
2645 * - \%c: the preferred date and time representation for the current locale
2646 * - \%C: the century number (year/100) as a 2-digit integer (00-99)
2647 * - \%d: the day of the month as a decimal number (range 01 to 31)
2648 * - \%e: the day of the month as a decimal number (range 1 to 31)
2649 * - \%F: equivalent to `%Y-%m-%d` (the ISO 8601 date format)
2650 * - \%g: the last two digits of the ISO 8601 week-based year as a
2651 * decimal number (00-99). This works well with \%V and \%u.
2652 * - \%G: the ISO 8601 week-based year as a decimal number. This works
2653 * well with \%V and \%u.
2654 * - \%h: equivalent to \%b
2655 * - \%H: the hour as a decimal number using a 24-hour clock (range 00 to 23)
2656 * - \%I: the hour as a decimal number using a 12-hour clock (range 01 to 12)
2657 * - \%j: the day of the year as a decimal number (range 001 to 366)
2658 * - \%k: the hour (24-hour clock) as a decimal number (range 0 to 23);
2659 * single digits are preceded by a blank
2660 * - \%l: the hour (12-hour clock) as a decimal number (range 1 to 12);
2661 * single digits are preceded by a blank
2662 * - \%m: the month as a decimal number (range 01 to 12)
2663 * - \%M: the minute as a decimal number (range 00 to 59)
2664 * - \%p: either "AM" or "PM" according to the given time value, or the
2665 * corresponding strings for the current locale. Noon is treated as
2666 * "PM" and midnight as "AM".
2667 * - \%P: like \%p but lowercase: "am" or "pm" or a corresponding string for
2668 * the current locale
2669 * - \%r: the time in a.m. or p.m. notation
2670 * - \%R: the time in 24-hour notation (\%H:\%M)
2671 * - \%s: the number of seconds since the Epoch, that is, since 1970-01-01
2672 * 00:00:00 UTC
2673 * - \%S: the second as a decimal number (range 00 to 60)
2674 * - \%t: a tab character
2675 * - \%T: the time in 24-hour notation with seconds (\%H:\%M:\%S)
2676 * - \%u: the ISO 8601 standard day of the week as a decimal, range 1 to 7,
2677 * Monday being 1. This works well with \%G and \%V.
2678 * - \%V: the ISO 8601 standard week number of the current year as a decimal
2679 * number, range 01 to 53, where week 1 is the first week that has at
2680 * least 4 days in the new year. See g_date_time_get_week_of_year().
2681 * This works well with \%G and \%u.
2682 * - \%w: the day of the week as a decimal, range 0 to 6, Sunday being 0.
2683 * This is not the ISO 8601 standard format -- use \%u instead.
2684 * - \%x: the preferred date representation for the current locale without
2685 * the time
2686 * - \%X: the preferred time representation for the current locale without
2687 * the date
2688 * - \%y: the year as a decimal number without the century
2689 * - \%Y: the year as a decimal number including the century
2690 * - \%z: the time zone as an offset from UTC (+hhmm)
2691 * - \%:z: the time zone as an offset from UTC (+hh:mm).
2692 * This is a gnulib strftime() extension. Since: 2.38
2693 * - \%::z: the time zone as an offset from UTC (+hh:mm:ss). This is a
2694 * gnulib strftime() extension. Since: 2.38
2695 * - \%:::z: the time zone as an offset from UTC, with : to necessary
2696 * precision (e.g., -04, +05:30). This is a gnulib strftime() extension. Since: 2.38
2697 * - \%Z: the time zone or name or abbreviation
2698 * - \%\%: a literal \% character
2700 * Some conversion specifications can be modified by preceding the
2701 * conversion specifier by one or more modifier characters. The
2702 * following modifiers are supported for many of the numeric
2703 * conversions:
2705 * - O: Use alternative numeric symbols, if the current locale supports those.
2706 * - _: Pad a numeric result with spaces. This overrides the default padding
2707 * for the specifier.
2708 * - -: Do not pad a numeric result. This overrides the default padding
2709 * for the specifier.
2710 * - 0: Pad a numeric result with zeros. This overrides the default padding
2711 * for the specifier.
2713 * Returns: a newly allocated string formatted to the requested format
2714 * or %NULL in the case that there was an error. The string
2715 * should be freed with g_free().
2717 * Since: 2.26
2719 gchar *
2720 g_date_time_format (GDateTime *datetime,
2721 const gchar *format)
2723 GString *outstr;
2724 gchar *utf8;
2725 gboolean locale_is_utf8 = g_get_charset (NULL);
2727 g_return_val_if_fail (datetime != NULL, NULL);
2728 g_return_val_if_fail (format != NULL, NULL);
2729 g_return_val_if_fail (g_utf8_validate (format, -1, NULL), NULL);
2731 outstr = g_string_sized_new (strlen (format) * 2);
2733 if (!g_date_time_format_locale (datetime, format, outstr, locale_is_utf8))
2735 g_string_free (outstr, TRUE);
2736 return NULL;
2739 if (locale_is_utf8)
2740 return g_string_free (outstr, FALSE);
2742 utf8 = g_locale_to_utf8 (outstr->str, outstr->len, NULL, NULL, NULL);
2743 g_string_free (outstr, TRUE);
2744 return utf8;
2748 /* Epilogue {{{1 */
2749 /* vim:set foldmethod=marker: */