Update German translation
[glib.git] / glib / gdatetime.c
blob7e1da4d7db65160c8b80f43103528350c9dd9d49
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 (((unix) + UNIX_EPOCH_START * SEC_PER_DAY) * USEC_PER_SECOND)
133 #define DAYS_IN_4YEARS 1461 /* days in 4 years */
134 #define DAYS_IN_100YEARS 36524 /* days in 100 years */
135 #define DAYS_IN_400YEARS 146097 /* days in 400 years */
137 #define USEC_PER_SECOND (G_GINT64_CONSTANT (1000000))
138 #define USEC_PER_MINUTE (G_GINT64_CONSTANT (60000000))
139 #define USEC_PER_HOUR (G_GINT64_CONSTANT (3600000000))
140 #define USEC_PER_MILLISECOND (G_GINT64_CONSTANT (1000))
141 #define USEC_PER_DAY (G_GINT64_CONSTANT (86400000000))
142 #define SEC_PER_DAY (G_GINT64_CONSTANT (86400))
144 #define SECS_PER_MINUTE (60)
145 #define SECS_PER_HOUR (60 * SECS_PER_MINUTE)
146 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
147 #define SECS_PER_YEAR (365 * SECS_PER_DAY)
148 #define SECS_PER_JULIAN (DAYS_PER_PERIOD * SECS_PER_DAY)
150 #define GREGORIAN_LEAP(y) ((((y) % 4) == 0) && (!((((y) % 100) == 0) && (((y) % 400) != 0))))
151 #define JULIAN_YEAR(d) ((d)->julian / 365.25)
152 #define DAYS_PER_PERIOD (G_GINT64_CONSTANT (2914695))
154 static const guint16 days_in_months[2][13] =
156 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
157 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
160 static const guint16 days_in_year[2][13] =
162 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
163 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
166 #ifdef HAVE_LANGINFO_TIME
168 #define GET_AMPM(d) ((g_date_time_get_hour (d) < 12) ? \
169 nl_langinfo (AM_STR) : \
170 nl_langinfo (PM_STR))
172 #define PREFERRED_DATE_TIME_FMT nl_langinfo (D_T_FMT)
173 #define PREFERRED_DATE_FMT nl_langinfo (D_FMT)
174 #define PREFERRED_TIME_FMT nl_langinfo (T_FMT)
175 #define PREFERRED_TIME_FMT nl_langinfo (T_FMT)
176 #define PREFERRED_12HR_TIME_FMT nl_langinfo (T_FMT_AMPM)
178 static const gint weekday_item[2][7] =
180 { ABDAY_2, ABDAY_3, ABDAY_4, ABDAY_5, ABDAY_6, ABDAY_7, ABDAY_1 },
181 { DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7, DAY_1 }
184 static const gint month_item[2][12] =
186 { ABMON_1, ABMON_2, ABMON_3, ABMON_4, ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10, ABMON_11, ABMON_12 },
187 { MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7, MON_8, MON_9, MON_10, MON_11, MON_12 },
190 #define WEEKDAY_ABBR(d) nl_langinfo (weekday_item[0][g_date_time_get_day_of_week (d) - 1])
191 #define WEEKDAY_FULL(d) nl_langinfo (weekday_item[1][g_date_time_get_day_of_week (d) - 1])
192 #define MONTH_ABBR(d) nl_langinfo (month_item[0][g_date_time_get_month (d) - 1])
193 #define MONTH_FULL(d) nl_langinfo (month_item[1][g_date_time_get_month (d) - 1])
195 #else
197 #define GET_AMPM(d) ((g_date_time_get_hour (d) < 12) \
198 /* Translators: 'before midday' indicator */ \
199 ? C_("GDateTime", "AM") \
200 /* Translators: 'after midday' indicator */ \
201 : C_("GDateTime", "PM"))
203 /* Translators: this is the preferred format for expressing the date and the time */
204 #define PREFERRED_DATE_TIME_FMT C_("GDateTime", "%a %b %e %H:%M:%S %Y")
206 /* Translators: this is the preferred format for expressing the date */
207 #define PREFERRED_DATE_FMT C_("GDateTime", "%m/%d/%y")
209 /* Translators: this is the preferred format for expressing the time */
210 #define PREFERRED_TIME_FMT C_("GDateTime", "%H:%M:%S")
212 /* Translators: this is the preferred format for expressing 12 hour time */
213 #define PREFERRED_12HR_TIME_FMT C_("GDateTime", "%I:%M:%S %p")
215 #define WEEKDAY_ABBR(d) (get_weekday_name_abbr (g_date_time_get_day_of_week (d)))
216 #define WEEKDAY_FULL(d) (get_weekday_name (g_date_time_get_day_of_week (d)))
217 #define MONTH_ABBR(d) (get_month_name_abbr (g_date_time_get_month (d)))
218 #define MONTH_FULL(d) (get_month_name (g_date_time_get_month (d)))
220 static const gchar *
221 get_month_name (gint month)
223 switch (month)
225 case 1:
226 return C_("full month name", "January");
227 case 2:
228 return C_("full month name", "February");
229 case 3:
230 return C_("full month name", "March");
231 case 4:
232 return C_("full month name", "April");
233 case 5:
234 return C_("full month name", "May");
235 case 6:
236 return C_("full month name", "June");
237 case 7:
238 return C_("full month name", "July");
239 case 8:
240 return C_("full month name", "August");
241 case 9:
242 return C_("full month name", "September");
243 case 10:
244 return C_("full month name", "October");
245 case 11:
246 return C_("full month name", "November");
247 case 12:
248 return C_("full month name", "December");
250 default:
251 g_warning ("Invalid month number %d", month);
254 return NULL;
257 static const gchar *
258 get_month_name_abbr (gint month)
260 switch (month)
262 case 1:
263 return C_("abbreviated month name", "Jan");
264 case 2:
265 return C_("abbreviated month name", "Feb");
266 case 3:
267 return C_("abbreviated month name", "Mar");
268 case 4:
269 return C_("abbreviated month name", "Apr");
270 case 5:
271 return C_("abbreviated month name", "May");
272 case 6:
273 return C_("abbreviated month name", "Jun");
274 case 7:
275 return C_("abbreviated month name", "Jul");
276 case 8:
277 return C_("abbreviated month name", "Aug");
278 case 9:
279 return C_("abbreviated month name", "Sep");
280 case 10:
281 return C_("abbreviated month name", "Oct");
282 case 11:
283 return C_("abbreviated month name", "Nov");
284 case 12:
285 return C_("abbreviated month name", "Dec");
287 default:
288 g_warning ("Invalid month number %d", month);
291 return NULL;
294 static const gchar *
295 get_weekday_name (gint day)
297 switch (day)
299 case 1:
300 return C_("full weekday name", "Monday");
301 case 2:
302 return C_("full weekday name", "Tuesday");
303 case 3:
304 return C_("full weekday name", "Wednesday");
305 case 4:
306 return C_("full weekday name", "Thursday");
307 case 5:
308 return C_("full weekday name", "Friday");
309 case 6:
310 return C_("full weekday name", "Saturday");
311 case 7:
312 return C_("full weekday name", "Sunday");
314 default:
315 g_warning ("Invalid week day number %d", day);
318 return NULL;
321 static const gchar *
322 get_weekday_name_abbr (gint day)
324 switch (day)
326 case 1:
327 return C_("abbreviated weekday name", "Mon");
328 case 2:
329 return C_("abbreviated weekday name", "Tue");
330 case 3:
331 return C_("abbreviated weekday name", "Wed");
332 case 4:
333 return C_("abbreviated weekday name", "Thu");
334 case 5:
335 return C_("abbreviated weekday name", "Fri");
336 case 6:
337 return C_("abbreviated weekday name", "Sat");
338 case 7:
339 return C_("abbreviated weekday name", "Sun");
341 default:
342 g_warning ("Invalid week day number %d", day);
345 return NULL;
348 #endif /* HAVE_LANGINFO_TIME */
350 static inline gint
351 ymd_to_days (gint year,
352 gint month,
353 gint day)
355 gint64 days;
357 days = (year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100)
358 + ((year - 1) / 400);
360 days += days_in_year[0][month - 1];
361 if (GREGORIAN_LEAP (year) && month > 2)
362 day++;
364 days += day;
366 return days;
369 static void
370 g_date_time_get_week_number (GDateTime *datetime,
371 gint *week_number,
372 gint *day_of_week,
373 gint *day_of_year)
375 gint a, b, c, d, e, f, g, n, s, month, day, year;
377 g_date_time_get_ymd (datetime, &year, &month, &day);
379 if (month <= 2)
381 a = g_date_time_get_year (datetime) - 1;
382 b = (a / 4) - (a / 100) + (a / 400);
383 c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
384 s = b - c;
385 e = 0;
386 f = day - 1 + (31 * (month - 1));
388 else
390 a = year;
391 b = (a / 4) - (a / 100) + (a / 400);
392 c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
393 s = b - c;
394 e = s + 1;
395 f = day + (((153 * (month - 3)) + 2) / 5) + 58 + s;
398 g = (a + b) % 7;
399 d = (f + g - e) % 7;
400 n = f + 3 - d;
402 if (week_number)
404 if (n < 0)
405 *week_number = 53 - ((g - s) / 5);
406 else if (n > 364 + s)
407 *week_number = 1;
408 else
409 *week_number = (n / 7) + 1;
412 if (day_of_week)
413 *day_of_week = d + 1;
415 if (day_of_year)
416 *day_of_year = f + 1;
419 /* Lifecycle {{{1 */
421 static GDateTime *
422 g_date_time_alloc (GTimeZone *tz)
424 GDateTime *datetime;
426 datetime = g_slice_new0 (GDateTime);
427 datetime->tz = g_time_zone_ref (tz);
428 datetime->ref_count = 1;
430 return datetime;
434 * g_date_time_ref:
435 * @datetime: a #GDateTime
437 * Atomically increments the reference count of @datetime by one.
439 * Returns: the #GDateTime with the reference count increased
441 * Since: 2.26
443 GDateTime *
444 g_date_time_ref (GDateTime *datetime)
446 g_return_val_if_fail (datetime != NULL, NULL);
447 g_return_val_if_fail (datetime->ref_count > 0, NULL);
449 g_atomic_int_inc (&datetime->ref_count);
451 return datetime;
455 * g_date_time_unref:
456 * @datetime: a #GDateTime
458 * Atomically decrements the reference count of @datetime by one.
460 * When the reference count reaches zero, the resources allocated by
461 * @datetime are freed
463 * Since: 2.26
465 void
466 g_date_time_unref (GDateTime *datetime)
468 g_return_if_fail (datetime != NULL);
469 g_return_if_fail (datetime->ref_count > 0);
471 if (g_atomic_int_dec_and_test (&datetime->ref_count))
473 g_time_zone_unref (datetime->tz);
474 g_slice_free (GDateTime, datetime);
478 /* Internal state transformers {{{1 */
479 /*< internal >
480 * g_date_time_to_instant:
481 * @datetime: a #GDateTime
483 * Convert a @datetime into an instant.
485 * An instant is a number that uniquely describes a particular
486 * microsecond in time, taking time zone considerations into account.
487 * (ie: "03:00 -0400" is the same instant as "02:00 -0500").
489 * An instant is always positive but we use a signed return value to
490 * avoid troubles with C.
492 static gint64
493 g_date_time_to_instant (GDateTime *datetime)
495 gint64 offset;
497 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
498 offset *= USEC_PER_SECOND;
500 return datetime->days * USEC_PER_DAY + datetime->usec - offset;
503 /*< internal >
504 * g_date_time_from_instant:
505 * @tz: a #GTimeZone
506 * @instant: a instant in time
508 * Creates a #GDateTime from a time zone and an instant.
510 * This might fail if the time ends up being out of range.
512 static GDateTime *
513 g_date_time_from_instant (GTimeZone *tz,
514 gint64 instant)
516 GDateTime *datetime;
517 gint64 offset;
519 if (instant < 0 || instant > G_GINT64_CONSTANT (1000000000000000000))
520 return NULL;
522 datetime = g_date_time_alloc (tz);
523 datetime->interval = g_time_zone_find_interval (tz,
524 G_TIME_TYPE_UNIVERSAL,
525 INSTANT_TO_UNIX (instant));
526 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
527 offset *= USEC_PER_SECOND;
529 instant += offset;
531 datetime->days = instant / USEC_PER_DAY;
532 datetime->usec = instant % USEC_PER_DAY;
534 if (datetime->days < 1 || 3652059 < datetime->days)
536 g_date_time_unref (datetime);
537 datetime = NULL;
540 return datetime;
544 /*< internal >
545 * g_date_time_deal_with_date_change:
546 * @datetime: a #GDateTime
548 * This function should be called whenever the date changes by adding
549 * days, months or years. It does three things.
551 * First, we ensure that the date falls between 0001-01-01 and
552 * 9999-12-31 and return %FALSE if it does not.
554 * Next we update the ->interval field.
556 * Finally, we ensure that the resulting date and time pair exists (by
557 * ensuring that our time zone has an interval containing it) and
558 * adjusting as required. For example, if we have the time 02:30:00 on
559 * March 13 2010 in Toronto and we add 1 day to it, we would end up with
560 * 2:30am on March 14th, which doesn't exist. In that case, we bump the
561 * time up to 3:00am.
563 static gboolean
564 g_date_time_deal_with_date_change (GDateTime *datetime)
566 GTimeType was_dst;
567 gint64 full_time;
568 gint64 usec;
570 if (datetime->days < 1 || datetime->days > 3652059)
571 return FALSE;
573 was_dst = g_time_zone_is_dst (datetime->tz, datetime->interval);
575 full_time = datetime->days * USEC_PER_DAY + datetime->usec;
578 usec = full_time % USEC_PER_SECOND;
579 full_time /= USEC_PER_SECOND;
580 full_time -= UNIX_EPOCH_START * SEC_PER_DAY;
582 datetime->interval = g_time_zone_adjust_time (datetime->tz,
583 was_dst,
584 &full_time);
585 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
586 full_time *= USEC_PER_SECOND;
587 full_time += usec;
589 datetime->days = full_time / USEC_PER_DAY;
590 datetime->usec = full_time % USEC_PER_DAY;
592 /* maybe daylight time caused us to shift to a different day,
593 * but it definitely didn't push us into a different year */
594 return TRUE;
597 static GDateTime *
598 g_date_time_replace_days (GDateTime *datetime,
599 gint days)
601 GDateTime *new;
603 new = g_date_time_alloc (datetime->tz);
604 new->interval = datetime->interval;
605 new->usec = datetime->usec;
606 new->days = days;
608 if (!g_date_time_deal_with_date_change (new))
610 g_date_time_unref (new);
611 new = NULL;
614 return new;
617 /* now/unix/timeval Constructors {{{1 */
619 /*< internal >
620 * g_date_time_new_from_timeval:
621 * @tz: a #GTimeZone
622 * @tv: a #GTimeVal
624 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
625 * given time zone @tz.
627 * The time contained in a #GTimeVal is always stored in the form of
628 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
629 * given time zone.
631 * This call can fail (returning %NULL) if @tv represents a time outside
632 * of the supported range of #GDateTime.
634 * You should release the return value by calling g_date_time_unref()
635 * when you are done with it.
637 * Returns: a new #GDateTime, or %NULL
639 * Since: 2.26
641 static GDateTime *
642 g_date_time_new_from_timeval (GTimeZone *tz,
643 const GTimeVal *tv)
645 return g_date_time_from_instant (tz, tv->tv_usec +
646 UNIX_TO_INSTANT (tv->tv_sec));
649 /*< internal >
650 * g_date_time_new_from_unix:
651 * @tz: a #GTimeZone
652 * @t: the Unix time
654 * Creates a #GDateTime corresponding to the given Unix time @t in the
655 * given time zone @tz.
657 * Unix time is the number of seconds that have elapsed since 1970-01-01
658 * 00:00:00 UTC, regardless of the time zone given.
660 * This call can fail (returning %NULL) if @t represents a time outside
661 * of the supported range of #GDateTime.
663 * You should release the return value by calling g_date_time_unref()
664 * when you are done with it.
666 * Returns: a new #GDateTime, or %NULL
668 * Since: 2.26
670 static GDateTime *
671 g_date_time_new_from_unix (GTimeZone *tz,
672 gint64 secs)
674 return g_date_time_from_instant (tz, UNIX_TO_INSTANT (secs));
678 * g_date_time_new_now:
679 * @tz: a #GTimeZone
681 * Creates a #GDateTime corresponding to this exact instant in the given
682 * time zone @tz. The time is as accurate as the system allows, to a
683 * maximum accuracy of 1 microsecond.
685 * This function will always succeed unless the system clock is set to
686 * truly insane values (or unless GLib is still being used after the
687 * year 9999).
689 * You should release the return value by calling g_date_time_unref()
690 * when you are done with it.
692 * Returns: a new #GDateTime, or %NULL
694 * Since: 2.26
696 GDateTime *
697 g_date_time_new_now (GTimeZone *tz)
699 GTimeVal tv;
701 g_get_current_time (&tv);
703 return g_date_time_new_from_timeval (tz, &tv);
707 * g_date_time_new_now_local:
709 * Creates a #GDateTime corresponding to this exact instant in the local
710 * time zone.
712 * This is equivalent to calling g_date_time_new_now() with the time
713 * zone returned by g_time_zone_new_local().
715 * Returns: a new #GDateTime, or %NULL
717 * Since: 2.26
719 GDateTime *
720 g_date_time_new_now_local (void)
722 GDateTime *datetime;
723 GTimeZone *local;
725 local = g_time_zone_new_local ();
726 datetime = g_date_time_new_now (local);
727 g_time_zone_unref (local);
729 return datetime;
733 * g_date_time_new_now_utc:
735 * Creates a #GDateTime corresponding to this exact instant in UTC.
737 * This is equivalent to calling g_date_time_new_now() with the time
738 * zone returned by g_time_zone_new_utc().
740 * Returns: a new #GDateTime, or %NULL
742 * Since: 2.26
744 GDateTime *
745 g_date_time_new_now_utc (void)
747 GDateTime *datetime;
748 GTimeZone *utc;
750 utc = g_time_zone_new_utc ();
751 datetime = g_date_time_new_now (utc);
752 g_time_zone_unref (utc);
754 return datetime;
758 * g_date_time_new_from_unix_local:
759 * @t: the Unix time
761 * Creates a #GDateTime corresponding to the given Unix time @t in the
762 * local time zone.
764 * Unix time is the number of seconds that have elapsed since 1970-01-01
765 * 00:00:00 UTC, regardless of the local time offset.
767 * This call can fail (returning %NULL) if @t represents a time outside
768 * of the supported range of #GDateTime.
770 * You should release the return value by calling g_date_time_unref()
771 * when you are done with it.
773 * Returns: a new #GDateTime, or %NULL
775 * Since: 2.26
777 GDateTime *
778 g_date_time_new_from_unix_local (gint64 t)
780 GDateTime *datetime;
781 GTimeZone *local;
783 local = g_time_zone_new_local ();
784 datetime = g_date_time_new_from_unix (local, t);
785 g_time_zone_unref (local);
787 return datetime;
791 * g_date_time_new_from_unix_utc:
792 * @t: the Unix time
794 * Creates a #GDateTime corresponding to the given Unix time @t in UTC.
796 * Unix time is the number of seconds that have elapsed since 1970-01-01
797 * 00:00:00 UTC.
799 * This call can fail (returning %NULL) if @t represents a time outside
800 * of the supported range of #GDateTime.
802 * You should release the return value by calling g_date_time_unref()
803 * when you are done with it.
805 * Returns: a new #GDateTime, or %NULL
807 * Since: 2.26
809 GDateTime *
810 g_date_time_new_from_unix_utc (gint64 t)
812 GDateTime *datetime;
813 GTimeZone *utc;
815 utc = g_time_zone_new_utc ();
816 datetime = g_date_time_new_from_unix (utc, t);
817 g_time_zone_unref (utc);
819 return datetime;
823 * g_date_time_new_from_timeval_local:
824 * @tv: a #GTimeVal
826 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
827 * local time zone.
829 * The time contained in a #GTimeVal is always stored in the form of
830 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
831 * local time offset.
833 * This call can fail (returning %NULL) if @tv represents a time outside
834 * of the supported range of #GDateTime.
836 * You should release the return value by calling g_date_time_unref()
837 * when you are done with it.
839 * Returns: a new #GDateTime, or %NULL
841 * Since: 2.26
843 GDateTime *
844 g_date_time_new_from_timeval_local (const GTimeVal *tv)
846 GDateTime *datetime;
847 GTimeZone *local;
849 local = g_time_zone_new_local ();
850 datetime = g_date_time_new_from_timeval (local, tv);
851 g_time_zone_unref (local);
853 return datetime;
857 * g_date_time_new_from_timeval_utc:
858 * @tv: a #GTimeVal
860 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in UTC.
862 * The time contained in a #GTimeVal is always stored in the form of
863 * seconds elapsed since 1970-01-01 00:00:00 UTC.
865 * This call can fail (returning %NULL) if @tv represents a time outside
866 * of the supported range of #GDateTime.
868 * You should release the return value by calling g_date_time_unref()
869 * when you are done with it.
871 * Returns: a new #GDateTime, or %NULL
873 * Since: 2.26
875 GDateTime *
876 g_date_time_new_from_timeval_utc (const GTimeVal *tv)
878 GDateTime *datetime;
879 GTimeZone *utc;
881 utc = g_time_zone_new_utc ();
882 datetime = g_date_time_new_from_timeval (utc, tv);
883 g_time_zone_unref (utc);
885 return datetime;
888 /* full new functions {{{1 */
891 * g_date_time_new:
892 * @tz: a #GTimeZone
893 * @year: the year component of the date
894 * @month: the month component of the date
895 * @day: the day component of the date
896 * @hour: the hour component of the date
897 * @minute: the minute component of the date
898 * @seconds: the number of seconds past the minute
900 * Creates a new #GDateTime corresponding to the given date and time in
901 * the time zone @tz.
903 * The @year must be between 1 and 9999, @month between 1 and 12 and @day
904 * between 1 and 28, 29, 30 or 31 depending on the month and the year.
906 * @hour must be between 0 and 23 and @minute must be between 0 and 59.
908 * @seconds must be at least 0.0 and must be strictly less than 60.0.
909 * It will be rounded down to the nearest microsecond.
911 * If the given time is not representable in the given time zone (for
912 * example, 02:30 on March 14th 2010 in Toronto, due to daylight savings
913 * time) then the time will be rounded up to the nearest existing time
914 * (in this case, 03:00). If this matters to you then you should verify
915 * the return value for containing the same as the numbers you gave.
917 * In the case that the given time is ambiguous in the given time zone
918 * (for example, 01:30 on November 7th 2010 in Toronto, due to daylight
919 * savings time) then the time falling within standard (ie:
920 * non-daylight) time is taken.
922 * It not considered a programmer error for the values to this function
923 * to be out of range, but in the case that they are, the function will
924 * return %NULL.
926 * You should release the return value by calling g_date_time_unref()
927 * when you are done with it.
929 * Returns: a new #GDateTime, or %NULL
931 * Since: 2.26
933 GDateTime *
934 g_date_time_new (GTimeZone *tz,
935 gint year,
936 gint month,
937 gint day,
938 gint hour,
939 gint minute,
940 gdouble seconds)
942 GDateTime *datetime;
943 gint64 full_time;
945 g_return_val_if_fail (tz != NULL, NULL);
947 if (year < 1 || year > 9999 ||
948 month < 1 || month > 12 ||
949 day < 1 || day > 31 ||
950 hour < 0 || hour > 23 ||
951 minute < 0 || minute > 59 ||
952 seconds < 0.0 || seconds >= 60.0)
953 return NULL;
955 datetime = g_date_time_alloc (tz);
956 datetime->days = ymd_to_days (year, month, day);
957 datetime->usec = (hour * USEC_PER_HOUR)
958 + (minute * USEC_PER_MINUTE)
959 + (gint64) (seconds * USEC_PER_SECOND);
961 full_time = SEC_PER_DAY *
962 (ymd_to_days (year, month, day) - UNIX_EPOCH_START) +
963 SECS_PER_HOUR * hour +
964 SECS_PER_MINUTE * minute +
965 (int) seconds;
967 datetime->interval = g_time_zone_adjust_time (datetime->tz,
968 G_TIME_TYPE_STANDARD,
969 &full_time);
971 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
972 datetime->days = full_time / SEC_PER_DAY;
973 datetime->usec = (full_time % SEC_PER_DAY) * USEC_PER_SECOND;
974 datetime->usec += ((int) (seconds * USEC_PER_SECOND)) % USEC_PER_SECOND;
976 return datetime;
980 * g_date_time_new_local:
981 * @year: the year component of the date
982 * @month: the month component of the date
983 * @day: the day component of the date
984 * @hour: the hour component of the date
985 * @minute: the minute component of the date
986 * @seconds: the number of seconds past the minute
988 * Creates a new #GDateTime corresponding to the given date and time in
989 * the local time zone.
991 * This call is equivalent to calling g_date_time_new() with the time
992 * zone returned by g_time_zone_new_local().
994 * Returns: a #GDateTime, or %NULL
996 * Since: 2.26
998 GDateTime *
999 g_date_time_new_local (gint year,
1000 gint month,
1001 gint day,
1002 gint hour,
1003 gint minute,
1004 gdouble seconds)
1006 GDateTime *datetime;
1007 GTimeZone *local;
1009 local = g_time_zone_new_local ();
1010 datetime = g_date_time_new (local, year, month, day, hour, minute, seconds);
1011 g_time_zone_unref (local);
1013 return datetime;
1017 * g_date_time_new_utc:
1018 * @year: the year component of the date
1019 * @month: the month component of the date
1020 * @day: the day component of the date
1021 * @hour: the hour component of the date
1022 * @minute: the minute component of the date
1023 * @seconds: the number of seconds past the minute
1025 * Creates a new #GDateTime corresponding to the given date and time in
1026 * UTC.
1028 * This call is equivalent to calling g_date_time_new() with the time
1029 * zone returned by g_time_zone_new_utc().
1031 * Returns: a #GDateTime, or %NULL
1033 * Since: 2.26
1035 GDateTime *
1036 g_date_time_new_utc (gint year,
1037 gint month,
1038 gint day,
1039 gint hour,
1040 gint minute,
1041 gdouble seconds)
1043 GDateTime *datetime;
1044 GTimeZone *utc;
1046 utc = g_time_zone_new_utc ();
1047 datetime = g_date_time_new (utc, year, month, day, hour, minute, seconds);
1048 g_time_zone_unref (utc);
1050 return datetime;
1053 /* Adders {{{1 */
1056 * g_date_time_add:
1057 * @datetime: a #GDateTime
1058 * @timespan: a #GTimeSpan
1060 * Creates a copy of @datetime and adds the specified timespan to the copy.
1062 * Returns: the newly created #GDateTime which should be freed with
1063 * g_date_time_unref().
1065 * Since: 2.26
1067 GDateTime*
1068 g_date_time_add (GDateTime *datetime,
1069 GTimeSpan timespan)
1071 return g_date_time_from_instant (datetime->tz, timespan +
1072 g_date_time_to_instant (datetime));
1076 * g_date_time_add_years:
1077 * @datetime: a #GDateTime
1078 * @years: the number of years
1080 * Creates a copy of @datetime and adds the specified number of years to the
1081 * copy. Add negative values to subtract years.
1083 * Returns: the newly created #GDateTime which should be freed with
1084 * g_date_time_unref().
1086 * Since: 2.26
1088 GDateTime *
1089 g_date_time_add_years (GDateTime *datetime,
1090 gint years)
1092 gint year, month, day;
1094 g_return_val_if_fail (datetime != NULL, NULL);
1096 if (years < -10000 || years > 10000)
1097 return NULL;
1099 g_date_time_get_ymd (datetime, &year, &month, &day);
1100 year += years;
1102 /* only possible issue is if we've entered a year with no February 29
1104 if (month == 2 && day == 29 && !GREGORIAN_LEAP (year))
1105 day = 28;
1107 return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
1111 * g_date_time_add_months:
1112 * @datetime: a #GDateTime
1113 * @months: the number of months
1115 * Creates a copy of @datetime and adds the specified number of months to the
1116 * copy. Add negative values to subtract months.
1118 * Returns: the newly created #GDateTime which should be freed with
1119 * g_date_time_unref().
1121 * Since: 2.26
1123 GDateTime*
1124 g_date_time_add_months (GDateTime *datetime,
1125 gint months)
1127 gint year, month, day;
1129 g_return_val_if_fail (datetime != NULL, NULL);
1130 g_date_time_get_ymd (datetime, &year, &month, &day);
1132 if (months < -120000 || months > 120000)
1133 return NULL;
1135 year += months / 12;
1136 month += months % 12;
1137 if (month < 1)
1139 month += 12;
1140 year--;
1142 else if (month > 12)
1144 month -= 12;
1145 year++;
1148 day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
1150 return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
1154 * g_date_time_add_weeks:
1155 * @datetime: a #GDateTime
1156 * @weeks: the number of weeks
1158 * Creates a copy of @datetime and adds the specified number of weeks to the
1159 * copy. Add negative values to subtract weeks.
1161 * Returns: the newly created #GDateTime which should be freed with
1162 * g_date_time_unref().
1164 * Since: 2.26
1166 GDateTime*
1167 g_date_time_add_weeks (GDateTime *datetime,
1168 gint weeks)
1170 g_return_val_if_fail (datetime != NULL, NULL);
1172 return g_date_time_add_days (datetime, weeks * 7);
1176 * g_date_time_add_days:
1177 * @datetime: a #GDateTime
1178 * @days: the number of days
1180 * Creates a copy of @datetime and adds the specified number of days to the
1181 * copy. Add negative values to subtract days.
1183 * Returns: the newly created #GDateTime which should be freed with
1184 * g_date_time_unref().
1186 * Since: 2.26
1188 GDateTime*
1189 g_date_time_add_days (GDateTime *datetime,
1190 gint days)
1192 g_return_val_if_fail (datetime != NULL, NULL);
1194 if (days < -3660000 || days > 3660000)
1195 return NULL;
1197 return g_date_time_replace_days (datetime, datetime->days + days);
1201 * g_date_time_add_hours:
1202 * @datetime: a #GDateTime
1203 * @hours: the number of hours to add
1205 * Creates a copy of @datetime and adds the specified number of hours.
1206 * Add negative values to subtract hours.
1208 * Returns: the newly created #GDateTime which should be freed with
1209 * g_date_time_unref().
1211 * Since: 2.26
1213 GDateTime*
1214 g_date_time_add_hours (GDateTime *datetime,
1215 gint hours)
1217 return g_date_time_add (datetime, hours * USEC_PER_HOUR);
1221 * g_date_time_add_minutes:
1222 * @datetime: a #GDateTime
1223 * @minutes: the number of minutes to add
1225 * Creates a copy of @datetime adding the specified number of minutes.
1226 * Add negative values to subtract minutes.
1228 * Returns: the newly created #GDateTime which should be freed with
1229 * g_date_time_unref().
1231 * Since: 2.26
1233 GDateTime*
1234 g_date_time_add_minutes (GDateTime *datetime,
1235 gint minutes)
1237 return g_date_time_add (datetime, minutes * USEC_PER_MINUTE);
1242 * g_date_time_add_seconds:
1243 * @datetime: a #GDateTime
1244 * @seconds: the number of seconds to add
1246 * Creates a copy of @datetime and adds the specified number of seconds.
1247 * Add negative values to subtract seconds.
1249 * Returns: the newly created #GDateTime which should be freed with
1250 * g_date_time_unref().
1252 * Since: 2.26
1254 GDateTime*
1255 g_date_time_add_seconds (GDateTime *datetime,
1256 gdouble seconds)
1258 return g_date_time_add (datetime, seconds * USEC_PER_SECOND);
1262 * g_date_time_add_full:
1263 * @datetime: a #GDateTime
1264 * @years: the number of years to add
1265 * @months: the number of months to add
1266 * @days: the number of days to add
1267 * @hours: the number of hours to add
1268 * @minutes: the number of minutes to add
1269 * @seconds: the number of seconds to add
1271 * Creates a new #GDateTime adding the specified values to the current date and
1272 * time in @datetime. Add negative values to subtract.
1274 * Returns: the newly created #GDateTime that should be freed with
1275 * g_date_time_unref().
1277 * Since: 2.26
1279 GDateTime *
1280 g_date_time_add_full (GDateTime *datetime,
1281 gint years,
1282 gint months,
1283 gint days,
1284 gint hours,
1285 gint minutes,
1286 gdouble seconds)
1288 gint year, month, day;
1289 gint64 full_time;
1290 GDateTime *new;
1291 gint interval;
1293 g_return_val_if_fail (datetime != NULL, NULL);
1294 g_date_time_get_ymd (datetime, &year, &month, &day);
1296 months += years * 12;
1298 if (months < -120000 || months > 120000)
1299 return NULL;
1301 if (days < -3660000 || days > 3660000)
1302 return NULL;
1304 year += months / 12;
1305 month += months % 12;
1306 if (month < 1)
1308 month += 12;
1309 year--;
1311 else if (month > 12)
1313 month -= 12;
1314 year++;
1317 day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
1319 /* full_time is now in unix (local) time */
1320 full_time = datetime->usec / USEC_PER_SECOND + SEC_PER_DAY *
1321 (ymd_to_days (year, month, day) + days - UNIX_EPOCH_START);
1323 interval = g_time_zone_adjust_time (datetime->tz,
1324 g_time_zone_is_dst (datetime->tz,
1325 datetime->interval),
1326 &full_time);
1328 /* move to UTC unix time */
1329 full_time -= g_time_zone_get_offset (datetime->tz, interval);
1331 /* convert back to an instant, add back fractional seconds */
1332 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
1333 full_time = full_time * USEC_PER_SECOND +
1334 datetime->usec % USEC_PER_SECOND;
1336 /* do the actual addition now */
1337 full_time += (hours * USEC_PER_HOUR) +
1338 (minutes * USEC_PER_MINUTE) +
1339 (gint64) (seconds * USEC_PER_SECOND);
1341 /* find the new interval */
1342 interval = g_time_zone_find_interval (datetime->tz,
1343 G_TIME_TYPE_UNIVERSAL,
1344 INSTANT_TO_UNIX (full_time));
1346 /* convert back into local time */
1347 full_time += USEC_PER_SECOND *
1348 g_time_zone_get_offset (datetime->tz, interval);
1350 /* split into days and usec of a new datetime */
1351 new = g_date_time_alloc (datetime->tz);
1352 new->interval = interval;
1353 new->days = full_time / USEC_PER_DAY;
1354 new->usec = full_time % USEC_PER_DAY;
1356 /* XXX validate */
1358 return new;
1361 /* Compare, difference, hash, equal {{{1 */
1363 * g_date_time_compare:
1364 * @dt1: (not nullable): first #GDateTime to compare
1365 * @dt2: (not nullable): second #GDateTime to compare
1367 * A comparison function for #GDateTimes that is suitable
1368 * as a #GCompareFunc. Both #GDateTimes must be non-%NULL.
1370 * Returns: -1, 0 or 1 if @dt1 is less than, equal to or greater
1371 * than @dt2.
1373 * Since: 2.26
1375 gint
1376 g_date_time_compare (gconstpointer dt1,
1377 gconstpointer dt2)
1379 gint64 difference;
1381 difference = g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2);
1383 if (difference < 0)
1384 return -1;
1386 else if (difference > 0)
1387 return 1;
1389 else
1390 return 0;
1394 * g_date_time_difference:
1395 * @end: a #GDateTime
1396 * @begin: a #GDateTime
1398 * Calculates the difference in time between @end and @begin. The
1399 * #GTimeSpan that is returned is effectively @end - @begin (ie:
1400 * positive if the first parameter is larger).
1402 * Returns: the difference between the two #GDateTime, as a time
1403 * span expressed in microseconds.
1405 * Since: 2.26
1407 GTimeSpan
1408 g_date_time_difference (GDateTime *end,
1409 GDateTime *begin)
1411 g_return_val_if_fail (begin != NULL, 0);
1412 g_return_val_if_fail (end != NULL, 0);
1414 return g_date_time_to_instant (end) -
1415 g_date_time_to_instant (begin);
1419 * g_date_time_hash:
1420 * @datetime: (not nullable): a #GDateTime
1422 * Hashes @datetime into a #guint, suitable for use within #GHashTable.
1424 * Returns: a #guint containing the hash
1426 * Since: 2.26
1428 guint
1429 g_date_time_hash (gconstpointer datetime)
1431 return g_date_time_to_instant ((GDateTime *) datetime);
1435 * g_date_time_equal:
1436 * @dt1: (not nullable): a #GDateTime
1437 * @dt2: (not nullable): a #GDateTime
1439 * Checks to see if @dt1 and @dt2 are equal.
1441 * Equal here means that they represent the same moment after converting
1442 * them to the same time zone.
1444 * Returns: %TRUE if @dt1 and @dt2 are equal
1446 * Since: 2.26
1448 gboolean
1449 g_date_time_equal (gconstpointer dt1,
1450 gconstpointer dt2)
1452 return g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2) == 0;
1455 /* Year, Month, Day Getters {{{1 */
1457 * g_date_time_get_ymd:
1458 * @datetime: a #GDateTime.
1459 * @year: (out) (optional): the return location for the gregorian year, or %NULL.
1460 * @month: (out) (optional): the return location for the month of the year, or %NULL.
1461 * @day: (out) (optional): the return location for the day of the month, or %NULL.
1463 * Retrieves the Gregorian day, month, and year of a given #GDateTime.
1465 * Since: 2.26
1467 void
1468 g_date_time_get_ymd (GDateTime *datetime,
1469 gint *year,
1470 gint *month,
1471 gint *day)
1473 gint the_year;
1474 gint the_month;
1475 gint the_day;
1476 gint remaining_days;
1477 gint y100_cycles;
1478 gint y4_cycles;
1479 gint y1_cycles;
1480 gint preceding;
1481 gboolean leap;
1483 g_return_if_fail (datetime != NULL);
1485 remaining_days = datetime->days;
1488 * We need to convert an offset in days to its year/month/day representation.
1489 * Leap years makes this a little trickier than it should be, so we use
1490 * 400, 100 and 4 years cycles here to get to the correct year.
1493 /* Our days offset starts sets 0001-01-01 as day 1, if it was day 0 our
1494 * math would be simpler, so let's do it */
1495 remaining_days--;
1497 the_year = (remaining_days / DAYS_IN_400YEARS) * 400 + 1;
1498 remaining_days = remaining_days % DAYS_IN_400YEARS;
1500 y100_cycles = remaining_days / DAYS_IN_100YEARS;
1501 remaining_days = remaining_days % DAYS_IN_100YEARS;
1502 the_year += y100_cycles * 100;
1504 y4_cycles = remaining_days / DAYS_IN_4YEARS;
1505 remaining_days = remaining_days % DAYS_IN_4YEARS;
1506 the_year += y4_cycles * 4;
1508 y1_cycles = remaining_days / 365;
1509 the_year += y1_cycles;
1510 remaining_days = remaining_days % 365;
1512 if (y1_cycles == 4 || y100_cycles == 4) {
1513 g_assert (remaining_days == 0);
1515 /* special case that indicates that the date is actually one year before,
1516 * in the 31th of December */
1517 the_year--;
1518 the_month = 12;
1519 the_day = 31;
1520 goto end;
1523 /* now get the month and the day */
1524 leap = y1_cycles == 3 && (y4_cycles != 24 || y100_cycles == 3);
1526 g_assert (leap == GREGORIAN_LEAP(the_year));
1528 the_month = (remaining_days + 50) >> 5;
1529 preceding = (days_in_year[0][the_month - 1] + (the_month > 2 && leap));
1530 if (preceding > remaining_days)
1532 /* estimate is too large */
1533 the_month -= 1;
1534 preceding -= leap ? days_in_months[1][the_month]
1535 : days_in_months[0][the_month];
1538 remaining_days -= preceding;
1539 g_assert(0 <= remaining_days);
1541 the_day = remaining_days + 1;
1543 end:
1544 if (year)
1545 *year = the_year;
1546 if (month)
1547 *month = the_month;
1548 if (day)
1549 *day = the_day;
1553 * g_date_time_get_year:
1554 * @datetime: A #GDateTime
1556 * Retrieves the year represented by @datetime in the Gregorian calendar.
1558 * Returns: the year represented by @datetime
1560 * Since: 2.26
1562 gint
1563 g_date_time_get_year (GDateTime *datetime)
1565 gint year;
1567 g_return_val_if_fail (datetime != NULL, 0);
1569 g_date_time_get_ymd (datetime, &year, NULL, NULL);
1571 return year;
1575 * g_date_time_get_month:
1576 * @datetime: a #GDateTime
1578 * Retrieves the month of the year represented by @datetime in the Gregorian
1579 * calendar.
1581 * Returns: the month represented by @datetime
1583 * Since: 2.26
1585 gint
1586 g_date_time_get_month (GDateTime *datetime)
1588 gint month;
1590 g_return_val_if_fail (datetime != NULL, 0);
1592 g_date_time_get_ymd (datetime, NULL, &month, NULL);
1594 return month;
1598 * g_date_time_get_day_of_month:
1599 * @datetime: a #GDateTime
1601 * Retrieves the day of the month represented by @datetime in the gregorian
1602 * calendar.
1604 * Returns: the day of the month
1606 * Since: 2.26
1608 gint
1609 g_date_time_get_day_of_month (GDateTime *datetime)
1611 gint day_of_year,
1613 const guint16 *days;
1614 guint16 last = 0;
1616 g_return_val_if_fail (datetime != NULL, 0);
1618 days = days_in_year[GREGORIAN_LEAP (g_date_time_get_year (datetime)) ? 1 : 0];
1619 g_date_time_get_week_number (datetime, NULL, NULL, &day_of_year);
1621 for (i = 1; i <= 12; i++)
1623 if (days [i] >= day_of_year)
1624 return day_of_year - last;
1625 last = days [i];
1628 g_warn_if_reached ();
1629 return 0;
1632 /* Week of year / day of week getters {{{1 */
1634 * g_date_time_get_week_numbering_year:
1635 * @datetime: a #GDateTime
1637 * Returns the ISO 8601 week-numbering year in which the week containing
1638 * @datetime falls.
1640 * This function, taken together with g_date_time_get_week_of_year() and
1641 * g_date_time_get_day_of_week() can be used to determine the full ISO
1642 * week date on which @datetime falls.
1644 * This is usually equal to the normal Gregorian year (as returned by
1645 * g_date_time_get_year()), except as detailed below:
1647 * For Thursday, the week-numbering year is always equal to the usual
1648 * calendar year. For other days, the number is such that every day
1649 * within a complete week (Monday to Sunday) is contained within the
1650 * same week-numbering year.
1652 * For Monday, Tuesday and Wednesday occurring near the end of the year,
1653 * this may mean that the week-numbering year is one greater than the
1654 * calendar year (so that these days have the same week-numbering year
1655 * as the Thursday occurring early in the next year).
1657 * For Friday, Saturday and Sunday occurring near the start of the year,
1658 * this may mean that the week-numbering year is one less than the
1659 * calendar year (so that these days have the same week-numbering year
1660 * as the Thursday occurring late in the previous year).
1662 * An equivalent description is that the week-numbering year is equal to
1663 * the calendar year containing the majority of the days in the current
1664 * week (Monday to Sunday).
1666 * Note that January 1 0001 in the proleptic Gregorian calendar is a
1667 * Monday, so this function never returns 0.
1669 * Returns: the ISO 8601 week-numbering year for @datetime
1671 * Since: 2.26
1673 gint
1674 g_date_time_get_week_numbering_year (GDateTime *datetime)
1676 gint year, month, day, weekday;
1678 g_date_time_get_ymd (datetime, &year, &month, &day);
1679 weekday = g_date_time_get_day_of_week (datetime);
1681 /* January 1, 2, 3 might be in the previous year if they occur after
1682 * Thursday.
1684 * Jan 1: Friday, Saturday, Sunday => day 1: weekday 5, 6, 7
1685 * Jan 2: Saturday, Sunday => day 2: weekday 6, 7
1686 * Jan 3: Sunday => day 3: weekday 7
1688 * So we have a special case if (day - weekday) <= -4
1690 if (month == 1 && (day - weekday) <= -4)
1691 return year - 1;
1693 /* December 29, 30, 31 might be in the next year if they occur before
1694 * Thursday.
1696 * Dec 31: Monday, Tuesday, Wednesday => day 31: weekday 1, 2, 3
1697 * Dec 30: Monday, Tuesday => day 30: weekday 1, 2
1698 * Dec 29: Monday => day 29: weekday 1
1700 * So we have a special case if (day - weekday) >= 28
1702 else if (month == 12 && (day - weekday) >= 28)
1703 return year + 1;
1705 else
1706 return year;
1710 * g_date_time_get_week_of_year:
1711 * @datetime: a #GDateTime
1713 * Returns the ISO 8601 week number for the week containing @datetime.
1714 * The ISO 8601 week number is the same for every day of the week (from
1715 * Moday through Sunday). That can produce some unusual results
1716 * (described below).
1718 * The first week of the year is week 1. This is the week that contains
1719 * the first Thursday of the year. Equivalently, this is the first week
1720 * that has more than 4 of its days falling within the calendar year.
1722 * The value 0 is never returned by this function. Days contained
1723 * within a year but occurring before the first ISO 8601 week of that
1724 * year are considered as being contained in the last week of the
1725 * previous year. Similarly, the final days of a calendar year may be
1726 * considered as being part of the first ISO 8601 week of the next year
1727 * if 4 or more days of that week are contained within the new year.
1729 * Returns: the ISO 8601 week number for @datetime.
1731 * Since: 2.26
1733 gint
1734 g_date_time_get_week_of_year (GDateTime *datetime)
1736 gint weeknum;
1738 g_return_val_if_fail (datetime != NULL, 0);
1740 g_date_time_get_week_number (datetime, &weeknum, NULL, NULL);
1742 return weeknum;
1746 * g_date_time_get_day_of_week:
1747 * @datetime: a #GDateTime
1749 * Retrieves the ISO 8601 day of the week on which @datetime falls (1 is
1750 * Monday, 2 is Tuesday... 7 is Sunday).
1752 * Returns: the day of the week
1754 * Since: 2.26
1756 gint
1757 g_date_time_get_day_of_week (GDateTime *datetime)
1759 g_return_val_if_fail (datetime != NULL, 0);
1761 return (datetime->days - 1) % 7 + 1;
1764 /* Day of year getter {{{1 */
1766 * g_date_time_get_day_of_year:
1767 * @datetime: a #GDateTime
1769 * Retrieves the day of the year represented by @datetime in the Gregorian
1770 * calendar.
1772 * Returns: the day of the year
1774 * Since: 2.26
1776 gint
1777 g_date_time_get_day_of_year (GDateTime *datetime)
1779 gint doy = 0;
1781 g_return_val_if_fail (datetime != NULL, 0);
1783 g_date_time_get_week_number (datetime, NULL, NULL, &doy);
1784 return doy;
1787 /* Time component getters {{{1 */
1790 * g_date_time_get_hour:
1791 * @datetime: a #GDateTime
1793 * Retrieves the hour of the day represented by @datetime
1795 * Returns: the hour of the day
1797 * Since: 2.26
1799 gint
1800 g_date_time_get_hour (GDateTime *datetime)
1802 g_return_val_if_fail (datetime != NULL, 0);
1804 return (datetime->usec / USEC_PER_HOUR);
1808 * g_date_time_get_minute:
1809 * @datetime: a #GDateTime
1811 * Retrieves the minute of the hour represented by @datetime
1813 * Returns: the minute of the hour
1815 * Since: 2.26
1817 gint
1818 g_date_time_get_minute (GDateTime *datetime)
1820 g_return_val_if_fail (datetime != NULL, 0);
1822 return (datetime->usec % USEC_PER_HOUR) / USEC_PER_MINUTE;
1826 * g_date_time_get_second:
1827 * @datetime: a #GDateTime
1829 * Retrieves the second of the minute represented by @datetime
1831 * Returns: the second represented by @datetime
1833 * Since: 2.26
1835 gint
1836 g_date_time_get_second (GDateTime *datetime)
1838 g_return_val_if_fail (datetime != NULL, 0);
1840 return (datetime->usec % USEC_PER_MINUTE) / USEC_PER_SECOND;
1844 * g_date_time_get_microsecond:
1845 * @datetime: a #GDateTime
1847 * Retrieves the microsecond of the date represented by @datetime
1849 * Returns: the microsecond of the second
1851 * Since: 2.26
1853 gint
1854 g_date_time_get_microsecond (GDateTime *datetime)
1856 g_return_val_if_fail (datetime != NULL, 0);
1858 return (datetime->usec % USEC_PER_SECOND);
1862 * g_date_time_get_seconds:
1863 * @datetime: a #GDateTime
1865 * Retrieves the number of seconds since the start of the last minute,
1866 * including the fractional part.
1868 * Returns: the number of seconds
1870 * Since: 2.26
1872 gdouble
1873 g_date_time_get_seconds (GDateTime *datetime)
1875 g_return_val_if_fail (datetime != NULL, 0);
1877 return (datetime->usec % USEC_PER_MINUTE) / 1000000.0;
1880 /* Exporters {{{1 */
1882 * g_date_time_to_unix:
1883 * @datetime: a #GDateTime
1885 * Gives the Unix time corresponding to @datetime, rounding down to the
1886 * nearest second.
1888 * Unix time is the number of seconds that have elapsed since 1970-01-01
1889 * 00:00:00 UTC, regardless of the time zone associated with @datetime.
1891 * Returns: the Unix time corresponding to @datetime
1893 * Since: 2.26
1895 gint64
1896 g_date_time_to_unix (GDateTime *datetime)
1898 return INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
1902 * g_date_time_to_timeval:
1903 * @datetime: a #GDateTime
1904 * @tv: a #GTimeVal to modify
1906 * Stores the instant in time that @datetime represents into @tv.
1908 * The time contained in a #GTimeVal is always stored in the form of
1909 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the time
1910 * zone associated with @datetime.
1912 * On systems where 'long' is 32bit (ie: all 32bit systems and all
1913 * Windows systems), a #GTimeVal is incapable of storing the entire
1914 * range of values that #GDateTime is capable of expressing. On those
1915 * systems, this function returns %FALSE to indicate that the time is
1916 * out of range.
1918 * On systems where 'long' is 64bit, this function never fails.
1920 * Returns: %TRUE if successful, else %FALSE
1922 * Since: 2.26
1924 gboolean
1925 g_date_time_to_timeval (GDateTime *datetime,
1926 GTimeVal *tv)
1928 tv->tv_sec = INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
1929 tv->tv_usec = datetime->usec % USEC_PER_SECOND;
1931 return TRUE;
1934 /* Timezone queries {{{1 */
1936 * g_date_time_get_utc_offset:
1937 * @datetime: a #GDateTime
1939 * Determines the offset to UTC in effect at the time and in the time
1940 * zone of @datetime.
1942 * The offset is the number of microseconds that you add to UTC time to
1943 * arrive at local time for the time zone (ie: negative numbers for time
1944 * zones west of GMT, positive numbers for east).
1946 * If @datetime represents UTC time, then the offset is always zero.
1948 * Returns: the number of microseconds that should be added to UTC to
1949 * get the local time
1951 * Since: 2.26
1953 GTimeSpan
1954 g_date_time_get_utc_offset (GDateTime *datetime)
1956 gint offset;
1958 g_return_val_if_fail (datetime != NULL, 0);
1960 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
1962 return (gint64) offset * USEC_PER_SECOND;
1966 * g_date_time_get_timezone_abbreviation:
1967 * @datetime: a #GDateTime
1969 * Determines the time zone abbreviation to be used at the time and in
1970 * the time zone of @datetime.
1972 * For example, in Toronto this is currently "EST" during the winter
1973 * months and "EDT" during the summer months when daylight savings
1974 * time is in effect.
1976 * Returns: (transfer none): the time zone abbreviation. The returned
1977 * string is owned by the #GDateTime and it should not be
1978 * modified or freed
1980 * Since: 2.26
1982 const gchar *
1983 g_date_time_get_timezone_abbreviation (GDateTime *datetime)
1985 g_return_val_if_fail (datetime != NULL, NULL);
1987 return g_time_zone_get_abbreviation (datetime->tz, datetime->interval);
1991 * g_date_time_is_daylight_savings:
1992 * @datetime: a #GDateTime
1994 * Determines if daylight savings time is in effect at the time and in
1995 * the time zone of @datetime.
1997 * Returns: %TRUE if daylight savings time is in effect
1999 * Since: 2.26
2001 gboolean
2002 g_date_time_is_daylight_savings (GDateTime *datetime)
2004 g_return_val_if_fail (datetime != NULL, FALSE);
2006 return g_time_zone_is_dst (datetime->tz, datetime->interval);
2009 /* Timezone convert {{{1 */
2011 * g_date_time_to_timezone:
2012 * @datetime: a #GDateTime
2013 * @tz: the new #GTimeZone
2015 * Create a new #GDateTime corresponding to the same instant in time as
2016 * @datetime, but in the time zone @tz.
2018 * This call can fail in the case that the time goes out of bounds. For
2019 * example, converting 0001-01-01 00:00:00 UTC to a time zone west of
2020 * Greenwich will fail (due to the year 0 being out of range).
2022 * You should release the return value by calling g_date_time_unref()
2023 * when you are done with it.
2025 * Returns: a new #GDateTime, or %NULL
2027 * Since: 2.26
2029 GDateTime *
2030 g_date_time_to_timezone (GDateTime *datetime,
2031 GTimeZone *tz)
2033 return g_date_time_from_instant (tz, g_date_time_to_instant (datetime));
2037 * g_date_time_to_local:
2038 * @datetime: a #GDateTime
2040 * Creates a new #GDateTime corresponding to the same instant in time as
2041 * @datetime, but in the local time zone.
2043 * This call is equivalent to calling g_date_time_to_timezone() with the
2044 * time zone returned by g_time_zone_new_local().
2046 * Returns: the newly created #GDateTime
2048 * Since: 2.26
2050 GDateTime *
2051 g_date_time_to_local (GDateTime *datetime)
2053 GDateTime *new;
2054 GTimeZone *local;
2056 local = g_time_zone_new_local ();
2057 new = g_date_time_to_timezone (datetime, local);
2058 g_time_zone_unref (local);
2060 return new;
2064 * g_date_time_to_utc:
2065 * @datetime: a #GDateTime
2067 * Creates a new #GDateTime corresponding to the same instant in time as
2068 * @datetime, but in UTC.
2070 * This call is equivalent to calling g_date_time_to_timezone() with the
2071 * time zone returned by g_time_zone_new_utc().
2073 * Returns: the newly created #GDateTime
2075 * Since: 2.26
2077 GDateTime *
2078 g_date_time_to_utc (GDateTime *datetime)
2080 GDateTime *new;
2081 GTimeZone *utc;
2083 utc = g_time_zone_new_utc ();
2084 new = g_date_time_to_timezone (datetime, utc);
2085 g_time_zone_unref (utc);
2087 return new;
2090 /* Format {{{1 */
2092 static gboolean
2093 format_z (GString *outstr,
2094 gint offset,
2095 guint colons)
2097 gint hours;
2098 gint minutes;
2099 gint seconds;
2101 hours = offset / 3600;
2102 minutes = ABS (offset) / 60 % 60;
2103 seconds = ABS (offset) % 60;
2105 switch (colons)
2107 case 0:
2108 g_string_append_printf (outstr, "%+03d%02d",
2109 hours,
2110 minutes);
2111 break;
2113 case 1:
2114 g_string_append_printf (outstr, "%+03d:%02d",
2115 hours,
2116 minutes);
2117 break;
2119 case 2:
2120 g_string_append_printf (outstr, "%+03d:%02d:%02d",
2121 hours,
2122 minutes,
2123 seconds);
2124 break;
2126 case 3:
2127 g_string_append_printf (outstr, "%+03d", hours);
2129 if (minutes != 0 || seconds != 0)
2131 g_string_append_printf (outstr, ":%02d", minutes);
2133 if (seconds != 0)
2134 g_string_append_printf (outstr, ":%02d", seconds);
2136 break;
2138 default:
2139 return FALSE;
2142 return TRUE;
2145 static void
2146 format_number (GString *str,
2147 gboolean use_alt_digits,
2148 gchar *pad,
2149 gint width,
2150 guint32 number)
2152 const gchar *ascii_digits[10] = {
2153 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
2155 const gchar **digits = ascii_digits;
2156 const gchar *tmp[10];
2157 gint i = 0;
2159 g_return_if_fail (width <= 10);
2161 #ifdef HAVE_LANGINFO_OUTDIGIT
2162 if (use_alt_digits)
2164 static const gchar *alt_digits[10];
2165 static gsize initialised;
2166 /* 2^32 has 10 digits */
2168 if G_UNLIKELY (g_once_init_enter (&initialised))
2170 #define DO_DIGIT(n) \
2171 alt_digits[n] = nl_langinfo (_NL_CTYPE_OUTDIGIT## n ##_MB)
2172 DO_DIGIT(0); DO_DIGIT(1); DO_DIGIT(2); DO_DIGIT(3); DO_DIGIT(4);
2173 DO_DIGIT(5); DO_DIGIT(6); DO_DIGIT(7); DO_DIGIT(8); DO_DIGIT(9);
2174 #undef DO_DIGIT
2175 g_once_init_leave (&initialised, TRUE);
2178 digits = alt_digits;
2180 #endif /* HAVE_LANGINFO_OUTDIGIT */
2184 tmp[i++] = digits[number % 10];
2185 number /= 10;
2187 while (number);
2189 while (pad && i < width)
2190 tmp[i++] = *pad == '0' ? digits[0] : pad;
2192 /* should really be impossible */
2193 g_assert (i <= 10);
2195 while (i)
2196 g_string_append (str, tmp[--i]);
2199 static gboolean g_date_time_format_locale (GDateTime *datetime,
2200 const gchar *format,
2201 GString *outstr,
2202 gboolean locale_is_utf8);
2204 /* g_date_time_format() subroutine that takes a locale-encoded format
2205 * string and produces a locale-encoded date/time string.
2207 static gboolean
2208 g_date_time_locale_format_locale (GDateTime *datetime,
2209 const gchar *format,
2210 GString *outstr,
2211 gboolean locale_is_utf8)
2213 gchar *utf8_format;
2214 gboolean success;
2216 if (locale_is_utf8)
2217 return g_date_time_format_locale (datetime, format, outstr,
2218 locale_is_utf8);
2220 utf8_format = g_locale_to_utf8 (format, -1, NULL, NULL, NULL);
2221 if (!utf8_format)
2222 return FALSE;
2224 success = g_date_time_format_locale (datetime, utf8_format, outstr,
2225 locale_is_utf8);
2226 g_free (utf8_format);
2227 return success;
2230 /* g_date_time_format() subroutine that takes a UTF-8 format
2231 * string and produces a locale-encoded date/time string.
2233 static gboolean
2234 g_date_time_format_locale (GDateTime *datetime,
2235 const gchar *format,
2236 GString *outstr,
2237 gboolean locale_is_utf8)
2239 guint len;
2240 guint colons;
2241 gchar *tmp;
2242 gsize tmp_len;
2243 gunichar c;
2244 gboolean alt_digits = FALSE;
2245 gboolean pad_set = FALSE;
2246 gchar *pad = "";
2247 gchar *ampm;
2248 const gchar *name;
2249 const gchar *tz;
2251 while (*format)
2253 len = strcspn (format, "%");
2254 if (len)
2256 if (locale_is_utf8)
2257 g_string_append_len (outstr, format, len);
2258 else
2260 tmp = g_locale_from_utf8 (format, len, NULL, &tmp_len, NULL);
2261 if (!tmp)
2262 return FALSE;
2263 g_string_append_len (outstr, tmp, tmp_len);
2264 g_free (tmp);
2268 format += len;
2269 if (!*format)
2270 break;
2272 g_assert (*format == '%');
2273 format++;
2274 if (!*format)
2275 break;
2277 colons = 0;
2278 alt_digits = FALSE;
2279 pad_set = FALSE;
2281 next_mod:
2282 c = g_utf8_get_char (format);
2283 format = g_utf8_next_char (format);
2284 switch (c)
2286 case 'a':
2287 name = WEEKDAY_ABBR (datetime);
2288 #if !defined (HAVE_LANGINFO_TIME)
2289 if (!locale_is_utf8)
2291 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2292 if (!tmp)
2293 return FALSE;
2294 g_string_append_len (outstr, tmp, tmp_len);
2295 g_free (tmp);
2297 else
2298 #endif
2300 g_string_append (outstr, name);
2302 break;
2303 case 'A':
2304 name = WEEKDAY_FULL (datetime);
2305 #if !defined (HAVE_LANGINFO_TIME)
2306 if (!locale_is_utf8)
2308 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2309 if (!tmp)
2310 return FALSE;
2311 g_string_append_len (outstr, tmp, tmp_len);
2312 g_free (tmp);
2314 else
2315 #endif
2317 g_string_append (outstr, name);
2319 break;
2320 case 'b':
2321 name = MONTH_ABBR (datetime);
2322 #if !defined (HAVE_LANGINFO_TIME)
2323 if (!locale_is_utf8)
2325 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2326 if (!tmp)
2327 return FALSE;
2328 g_string_append_len (outstr, tmp, tmp_len);
2329 g_free (tmp);
2331 else
2332 #endif
2334 g_string_append (outstr, name);
2336 break;
2337 case 'B':
2338 name = MONTH_FULL (datetime);
2339 #if !defined (HAVE_LANGINFO_TIME)
2340 if (!locale_is_utf8)
2342 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2343 if (!tmp)
2344 return FALSE;
2345 g_string_append_len (outstr, tmp, tmp_len);
2346 g_free (tmp);
2348 else
2349 #endif
2351 g_string_append (outstr, name);
2353 break;
2354 case 'c':
2356 if (!g_date_time_locale_format_locale (datetime, PREFERRED_DATE_TIME_FMT,
2357 outstr, locale_is_utf8))
2358 return FALSE;
2360 break;
2361 case 'C':
2362 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2363 g_date_time_get_year (datetime) / 100);
2364 break;
2365 case 'd':
2366 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2367 g_date_time_get_day_of_month (datetime));
2368 break;
2369 case 'e':
2370 format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
2371 g_date_time_get_day_of_month (datetime));
2372 break;
2373 case 'F':
2374 g_string_append_printf (outstr, "%d-%02d-%02d",
2375 g_date_time_get_year (datetime),
2376 g_date_time_get_month (datetime),
2377 g_date_time_get_day_of_month (datetime));
2378 break;
2379 case 'g':
2380 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2381 g_date_time_get_week_numbering_year (datetime) % 100);
2382 break;
2383 case 'G':
2384 format_number (outstr, alt_digits, pad_set ? pad : 0, 0,
2385 g_date_time_get_week_numbering_year (datetime));
2386 break;
2387 case 'h':
2388 name = MONTH_ABBR (datetime);
2389 #if !defined (HAVE_LANGINFO_TIME)
2390 if (!locale_is_utf8)
2392 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2393 if (!tmp)
2394 return FALSE;
2395 g_string_append_len (outstr, tmp, tmp_len);
2396 g_free (tmp);
2398 else
2399 #endif
2401 g_string_append (outstr, name);
2403 break;
2404 case 'H':
2405 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2406 g_date_time_get_hour (datetime));
2407 break;
2408 case 'I':
2409 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2410 (g_date_time_get_hour (datetime) + 11) % 12 + 1);
2411 break;
2412 case 'j':
2413 format_number (outstr, alt_digits, pad_set ? pad : "0", 3,
2414 g_date_time_get_day_of_year (datetime));
2415 break;
2416 case 'k':
2417 format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
2418 g_date_time_get_hour (datetime));
2419 break;
2420 case 'l':
2421 format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
2422 (g_date_time_get_hour (datetime) + 11) % 12 + 1);
2423 break;
2424 case 'n':
2425 g_string_append_c (outstr, '\n');
2426 break;
2427 case 'm':
2428 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2429 g_date_time_get_month (datetime));
2430 break;
2431 case 'M':
2432 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2433 g_date_time_get_minute (datetime));
2434 break;
2435 case 'O':
2436 alt_digits = TRUE;
2437 goto next_mod;
2438 case 'p':
2439 ampm = (gchar *) GET_AMPM (datetime);
2440 #if defined (HAVE_LANGINFO_TIME)
2441 if (!locale_is_utf8)
2443 /* This assumes that locale encoding can't have embedded NULs */
2444 ampm = tmp = g_locale_to_utf8 (ampm, -1, NULL, NULL, NULL);
2445 if (!tmp)
2446 return FALSE;
2448 #endif
2449 ampm = g_utf8_strup (ampm, -1);
2450 tmp_len = strlen (ampm);
2451 if (!locale_is_utf8)
2453 #if defined (HAVE_LANGINFO_TIME)
2454 g_free (tmp);
2455 #endif
2456 tmp = g_locale_from_utf8 (ampm, -1, NULL, &tmp_len, NULL);
2457 g_free (ampm);
2458 if (!tmp)
2459 return FALSE;
2460 ampm = tmp;
2462 g_string_append_len (outstr, ampm, tmp_len);
2463 g_free (ampm);
2464 break;
2465 case 'P':
2466 ampm = (gchar *) GET_AMPM (datetime);
2467 #if defined (HAVE_LANGINFO_TIME)
2468 if (!locale_is_utf8)
2470 /* This assumes that locale encoding can't have embedded NULs */
2471 ampm = tmp = g_locale_to_utf8 (ampm, -1, NULL, NULL, NULL);
2472 if (!tmp)
2473 return FALSE;
2475 #endif
2476 ampm = g_utf8_strdown (ampm, -1);
2477 tmp_len = strlen (ampm);
2478 if (!locale_is_utf8)
2480 #if defined (HAVE_LANGINFO_TIME)
2481 g_free (tmp);
2482 #endif
2483 tmp = g_locale_from_utf8 (ampm, -1, NULL, &tmp_len, NULL);
2484 g_free (ampm);
2485 if (!tmp)
2486 return FALSE;
2487 ampm = tmp;
2489 g_string_append_len (outstr, ampm, tmp_len);
2490 g_free (ampm);
2491 break;
2492 case 'r':
2494 if (!g_date_time_locale_format_locale (datetime, PREFERRED_12HR_TIME_FMT,
2495 outstr, locale_is_utf8))
2496 return FALSE;
2498 break;
2499 case 'R':
2500 g_string_append_printf (outstr, "%02d:%02d",
2501 g_date_time_get_hour (datetime),
2502 g_date_time_get_minute (datetime));
2503 break;
2504 case 's':
2505 g_string_append_printf (outstr, "%" G_GINT64_FORMAT, g_date_time_to_unix (datetime));
2506 break;
2507 case 'S':
2508 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2509 g_date_time_get_second (datetime));
2510 break;
2511 case 't':
2512 g_string_append_c (outstr, '\t');
2513 break;
2514 case 'T':
2515 g_string_append_printf (outstr, "%02d:%02d:%02d",
2516 g_date_time_get_hour (datetime),
2517 g_date_time_get_minute (datetime),
2518 g_date_time_get_second (datetime));
2519 break;
2520 case 'u':
2521 format_number (outstr, alt_digits, 0, 0,
2522 g_date_time_get_day_of_week (datetime));
2523 break;
2524 case 'V':
2525 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2526 g_date_time_get_week_of_year (datetime));
2527 break;
2528 case 'w':
2529 format_number (outstr, alt_digits, 0, 0,
2530 g_date_time_get_day_of_week (datetime) % 7);
2531 break;
2532 case 'x':
2534 if (!g_date_time_locale_format_locale (datetime, PREFERRED_DATE_FMT,
2535 outstr, locale_is_utf8))
2536 return FALSE;
2538 break;
2539 case 'X':
2541 if (!g_date_time_locale_format_locale (datetime, PREFERRED_TIME_FMT,
2542 outstr, locale_is_utf8))
2543 return FALSE;
2545 break;
2546 case 'y':
2547 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
2548 g_date_time_get_year (datetime) % 100);
2549 break;
2550 case 'Y':
2551 format_number (outstr, alt_digits, 0, 0,
2552 g_date_time_get_year (datetime));
2553 break;
2554 case 'z':
2556 gint64 offset;
2557 if (datetime->tz != NULL)
2558 offset = g_date_time_get_utc_offset (datetime) / USEC_PER_SECOND;
2559 else
2560 offset = 0;
2561 if (!format_z (outstr, (int) offset, colons))
2562 return FALSE;
2564 break;
2565 case 'Z':
2566 tz = g_date_time_get_timezone_abbreviation (datetime);
2567 tmp_len = strlen (tz);
2568 if (!locale_is_utf8)
2570 tz = tmp = g_locale_from_utf8 (tz, -1, NULL, &tmp_len, NULL);
2571 if (!tmp)
2572 return FALSE;
2574 g_string_append_len (outstr, tz, tmp_len);
2575 if (!locale_is_utf8)
2576 g_free (tmp);
2577 break;
2578 case '%':
2579 g_string_append_c (outstr, '%');
2580 break;
2581 case '-':
2582 pad_set = TRUE;
2583 pad = "";
2584 goto next_mod;
2585 case '_':
2586 pad_set = TRUE;
2587 pad = " ";
2588 goto next_mod;
2589 case '0':
2590 pad_set = TRUE;
2591 pad = "0";
2592 goto next_mod;
2593 case ':':
2594 /* Colons are only allowed before 'z' */
2595 if (*format && *format != 'z' && *format != ':')
2596 return FALSE;
2597 colons++;
2598 goto next_mod;
2599 default:
2600 return FALSE;
2604 return TRUE;
2608 * g_date_time_format:
2609 * @datetime: A #GDateTime
2610 * @format: a valid UTF-8 string, containing the format for the
2611 * #GDateTime
2613 * Creates a newly allocated string representing the requested @format.
2615 * The format strings understood by this function are a subset of the
2616 * strftime() format language as specified by C99. The \%D, \%U and \%W
2617 * conversions are not supported, nor is the 'E' modifier. The GNU
2618 * extensions \%k, \%l, \%s and \%P are supported, however, as are the
2619 * '0', '_' and '-' modifiers.
2621 * In contrast to strftime(), this function always produces a UTF-8
2622 * string, regardless of the current locale. Note that the rendering of
2623 * many formats is locale-dependent and may not match the strftime()
2624 * output exactly.
2626 * The following format specifiers are supported:
2628 * - \%a: the abbreviated weekday name according to the current locale
2629 * - \%A: the full weekday name according to the current locale
2630 * - \%b: the abbreviated month name according to the current locale
2631 * - \%B: the full month name according to the current locale
2632 * - \%c: the preferred date and time representation for the current locale
2633 * - \%C: the century number (year/100) as a 2-digit integer (00-99)
2634 * - \%d: the day of the month as a decimal number (range 01 to 31)
2635 * - \%e: the day of the month as a decimal number (range 1 to 31)
2636 * - \%F: equivalent to `%Y-%m-%d` (the ISO 8601 date format)
2637 * - \%g: the last two digits of the ISO 8601 week-based year as a
2638 * decimal number (00-99). This works well with \%V and \%u.
2639 * - \%G: the ISO 8601 week-based year as a decimal number. This works
2640 * well with \%V and \%u.
2641 * - \%h: equivalent to \%b
2642 * - \%H: the hour as a decimal number using a 24-hour clock (range 00 to 23)
2643 * - \%I: the hour as a decimal number using a 12-hour clock (range 01 to 12)
2644 * - \%j: the day of the year as a decimal number (range 001 to 366)
2645 * - \%k: the hour (24-hour clock) as a decimal number (range 0 to 23);
2646 * single digits are preceded by a blank
2647 * - \%l: the hour (12-hour clock) as a decimal number (range 1 to 12);
2648 * single digits are preceded by a blank
2649 * - \%m: the month as a decimal number (range 01 to 12)
2650 * - \%M: the minute as a decimal number (range 00 to 59)
2651 * - \%p: either "AM" or "PM" according to the given time value, or the
2652 * corresponding strings for the current locale. Noon is treated as
2653 * "PM" and midnight as "AM".
2654 * - \%P: like \%p but lowercase: "am" or "pm" or a corresponding string for
2655 * the current locale
2656 * - \%r: the time in a.m. or p.m. notation
2657 * - \%R: the time in 24-hour notation (\%H:\%M)
2658 * - \%s: the number of seconds since the Epoch, that is, since 1970-01-01
2659 * 00:00:00 UTC
2660 * - \%S: the second as a decimal number (range 00 to 60)
2661 * - \%t: a tab character
2662 * - \%T: the time in 24-hour notation with seconds (\%H:\%M:\%S)
2663 * - \%u: the ISO 8601 standard day of the week as a decimal, range 1 to 7,
2664 * Monday being 1. This works well with \%G and \%V.
2665 * - \%V: the ISO 8601 standard week number of the current year as a decimal
2666 * number, range 01 to 53, where week 1 is the first week that has at
2667 * least 4 days in the new year. See g_date_time_get_week_of_year().
2668 * This works well with \%G and \%u.
2669 * - \%w: the day of the week as a decimal, range 0 to 6, Sunday being 0.
2670 * This is not the ISO 8601 standard format -- use \%u instead.
2671 * - \%x: the preferred date representation for the current locale without
2672 * the time
2673 * - \%X: the preferred time representation for the current locale without
2674 * the date
2675 * - \%y: the year as a decimal number without the century
2676 * - \%Y: the year as a decimal number including the century
2677 * - \%z: the time zone as an offset from UTC (+hhmm)
2678 * - \%:z: the time zone as an offset from UTC (+hh:mm).
2679 * This is a gnulib strftime() extension. Since: 2.38
2680 * - \%::z: the time zone as an offset from UTC (+hh:mm:ss). This is a
2681 * gnulib strftime() extension. Since: 2.38
2682 * - \%:::z: the time zone as an offset from UTC, with : to necessary
2683 * precision (e.g., -04, +05:30). This is a gnulib strftime() extension. Since: 2.38
2684 * - \%Z: the time zone or name or abbreviation
2685 * - \%\%: a literal \% character
2687 * Some conversion specifications can be modified by preceding the
2688 * conversion specifier by one or more modifier characters. The
2689 * following modifiers are supported for many of the numeric
2690 * conversions:
2692 * - O: Use alternative numeric symbols, if the current locale supports those.
2693 * - _: Pad a numeric result with spaces. This overrides the default padding
2694 * for the specifier.
2695 * - -: Do not pad a numeric result. This overrides the default padding
2696 * for the specifier.
2697 * - 0: Pad a numeric result with zeros. This overrides the default padding
2698 * for the specifier.
2700 * Returns: a newly allocated string formatted to the requested format
2701 * or %NULL in the case that there was an error. The string
2702 * should be freed with g_free().
2704 * Since: 2.26
2706 gchar *
2707 g_date_time_format (GDateTime *datetime,
2708 const gchar *format)
2710 GString *outstr;
2711 gchar *utf8;
2712 gboolean locale_is_utf8 = g_get_charset (NULL);
2714 g_return_val_if_fail (datetime != NULL, NULL);
2715 g_return_val_if_fail (format != NULL, NULL);
2716 g_return_val_if_fail (g_utf8_validate (format, -1, NULL), NULL);
2718 outstr = g_string_sized_new (strlen (format) * 2);
2720 if (!g_date_time_format_locale (datetime, format, outstr, locale_is_utf8))
2722 g_string_free (outstr, TRUE);
2723 return NULL;
2726 if (locale_is_utf8)
2727 return g_string_free (outstr, FALSE);
2729 utf8 = g_locale_to_utf8 (outstr->str, outstr->len, NULL, NULL, NULL);
2730 g_string_free (outstr, TRUE);
2731 return utf8;
2735 /* Epilogue {{{1 */
2736 /* vim:set foldmethod=marker: */