Merge branch 'source-get-id-docs' into 'master'
[glib.git] / glib / gdatetime.c
blob1bd916587c28f67ab30344806dc23e7caec76922
1 /* gdatetime.c
3 * Copyright (C) 2009-2010 Christian Hergert <chris@dronelabs.com>
4 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
5 * Copyright (C) 2010 Emmanuele Bassi <ebassi@linux.intel.com>
6 * Copyright © 2010 Codethink Limited
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * licence, or (at your option) any later version.
13 * This is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 * License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Authors: Christian Hergert <chris@dronelabs.com>
22 * Thiago Santos <thiago.sousa.santos@collabora.co.uk>
23 * Emmanuele Bassi <ebassi@linux.intel.com>
24 * Ryan Lortie <desrt@desrt.ca>
25 * Robert Ancell <robert.ancell@canonical.com>
28 /* Algorithms within this file are based on the Calendar FAQ by
29 * Claus Tondering. It can be found at
30 * http://www.tondering.dk/claus/cal/calendar29.txt
32 * Copyright and disclaimer
33 * ------------------------
34 * This document is Copyright (C) 2008 by Claus Tondering.
35 * E-mail: claus@tondering.dk. (Please include the word
36 * "calendar" in the subject line.)
37 * The document may be freely distributed, provided this
38 * copyright notice is included and no money is charged for
39 * the document.
41 * This document is provided "as is". No warranties are made as
42 * to its correctness.
45 /* Prologue {{{1 */
47 #include "config.h"
49 /* langinfo.h in glibc 2.27 defines ALTMON_* only if _GNU_SOURCE is defined. */
50 #ifndef _GNU_SOURCE
51 #define _GNU_SOURCE 1
52 #endif
54 #include <stdlib.h>
55 #include <string.h>
57 #ifdef HAVE_LANGINFO_TIME
58 #include <langinfo.h>
59 #endif
61 #include "gdatetime.h"
63 #include "gslice.h"
64 #include "gatomic.h"
65 #include "gcharset.h"
66 #include "gconvert.h"
67 #include "gfileutils.h"
68 #include "ghash.h"
69 #include "gmain.h"
70 #include "gmappedfile.h"
71 #include "gstrfuncs.h"
72 #include "gtestutils.h"
73 #include "gthread.h"
74 #include "gtimezone.h"
76 #include "glibintl.h"
78 #ifndef G_OS_WIN32
79 #include <sys/time.h>
80 #include <time.h>
81 #endif /* !G_OS_WIN32 */
83 /**
84 * SECTION:date-time
85 * @title: GDateTime
86 * @short_description: a structure representing Date and Time
87 * @see_also: #GTimeZone
89 * #GDateTime is a structure that combines a Gregorian date and time
90 * into a single structure. It provides many conversion and methods to
91 * manipulate dates and times. Time precision is provided down to
92 * microseconds and the time can range (proleptically) from 0001-01-01
93 * 00:00:00 to 9999-12-31 23:59:59.999999. #GDateTime follows POSIX
94 * time in the sense that it is oblivious to leap seconds.
96 * #GDateTime is an immutable object; once it has been created it cannot
97 * be modified further. All modifiers will create a new #GDateTime.
98 * Nearly all such functions can fail due to the date or time going out
99 * of range, in which case %NULL will be returned.
101 * #GDateTime is reference counted: the reference count is increased by calling
102 * g_date_time_ref() and decreased by calling g_date_time_unref(). When the
103 * reference count drops to 0, the resources allocated by the #GDateTime
104 * structure are released.
106 * Many parts of the API may produce non-obvious results. As an
107 * example, adding two months to January 31st will yield March 31st
108 * whereas adding one month and then one month again will yield either
109 * March 28th or March 29th. Also note that adding 24 hours is not
110 * always the same as adding one day (since days containing daylight
111 * savings time transitions are either 23 or 25 hours in length).
113 * #GDateTime is available since GLib 2.26.
116 struct _GDateTime
118 /* Microsecond timekeeping within Day */
119 guint64 usec;
121 /* TimeZone information */
122 GTimeZone *tz;
123 gint interval;
125 /* 1 is 0001-01-01 in Proleptic Gregorian */
126 gint32 days;
128 volatile gint ref_count;
131 /* Time conversion {{{1 */
133 #define UNIX_EPOCH_START 719163
134 #define INSTANT_TO_UNIX(instant) \
135 ((instant)/USEC_PER_SECOND - UNIX_EPOCH_START * SEC_PER_DAY)
136 #define UNIX_TO_INSTANT(unix) \
137 (((gint64) (unix) + UNIX_EPOCH_START * SEC_PER_DAY) * USEC_PER_SECOND)
138 #define UNIX_TO_INSTANT_IS_VALID(unix) \
139 ((gint64) (unix) <= INSTANT_TO_UNIX (G_MAXINT64))
141 #define DAYS_IN_4YEARS 1461 /* days in 4 years */
142 #define DAYS_IN_100YEARS 36524 /* days in 100 years */
143 #define DAYS_IN_400YEARS 146097 /* days in 400 years */
145 #define USEC_PER_SECOND (G_GINT64_CONSTANT (1000000))
146 #define USEC_PER_MINUTE (G_GINT64_CONSTANT (60000000))
147 #define USEC_PER_HOUR (G_GINT64_CONSTANT (3600000000))
148 #define USEC_PER_MILLISECOND (G_GINT64_CONSTANT (1000))
149 #define USEC_PER_DAY (G_GINT64_CONSTANT (86400000000))
150 #define SEC_PER_DAY (G_GINT64_CONSTANT (86400))
152 #define SECS_PER_MINUTE (60)
153 #define SECS_PER_HOUR (60 * SECS_PER_MINUTE)
154 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
155 #define SECS_PER_YEAR (365 * SECS_PER_DAY)
156 #define SECS_PER_JULIAN (DAYS_PER_PERIOD * SECS_PER_DAY)
158 #define GREGORIAN_LEAP(y) ((((y) % 4) == 0) && (!((((y) % 100) == 0) && (((y) % 400) != 0))))
159 #define JULIAN_YEAR(d) ((d)->julian / 365.25)
160 #define DAYS_PER_PERIOD (G_GINT64_CONSTANT (2914695))
162 static const guint16 days_in_months[2][13] =
164 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
165 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
168 static const guint16 days_in_year[2][13] =
170 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
171 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
174 #ifdef HAVE_LANGINFO_TIME
176 #define GET_AMPM(d) ((g_date_time_get_hour (d) < 12) ? \
177 nl_langinfo (AM_STR) : \
178 nl_langinfo (PM_STR))
179 #define GET_AMPM_IS_LOCALE TRUE
181 #define PREFERRED_DATE_TIME_FMT nl_langinfo (D_T_FMT)
182 #define PREFERRED_DATE_FMT nl_langinfo (D_FMT)
183 #define PREFERRED_TIME_FMT nl_langinfo (T_FMT)
184 #define PREFERRED_12HR_TIME_FMT nl_langinfo (T_FMT_AMPM)
186 static const gint weekday_item[2][7] =
188 { ABDAY_2, ABDAY_3, ABDAY_4, ABDAY_5, ABDAY_6, ABDAY_7, ABDAY_1 },
189 { DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7, DAY_1 }
192 static const gint month_item[2][12] =
194 { ABMON_1, ABMON_2, ABMON_3, ABMON_4, ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10, ABMON_11, ABMON_12 },
195 { MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7, MON_8, MON_9, MON_10, MON_11, MON_12 },
198 #define WEEKDAY_ABBR(d) nl_langinfo (weekday_item[0][g_date_time_get_day_of_week (d) - 1])
199 #define WEEKDAY_ABBR_IS_LOCALE TRUE
200 #define WEEKDAY_FULL(d) nl_langinfo (weekday_item[1][g_date_time_get_day_of_week (d) - 1])
201 #define WEEKDAY_FULL_IS_LOCALE TRUE
202 #define MONTH_ABBR(d) nl_langinfo (month_item[0][g_date_time_get_month (d) - 1])
203 #define MONTH_ABBR_IS_LOCALE TRUE
204 #define MONTH_FULL(d) nl_langinfo (month_item[1][g_date_time_get_month (d) - 1])
205 #define MONTH_FULL_IS_LOCALE TRUE
207 #else
209 #define GET_AMPM(d) (get_fallback_ampm (g_date_time_get_hour (d)))
210 #define GET_AMPM_IS_LOCALE FALSE
212 /* Translators: this is the preferred format for expressing the date and the time */
213 #define PREFERRED_DATE_TIME_FMT C_("GDateTime", "%a %b %e %H:%M:%S %Y")
215 /* Translators: this is the preferred format for expressing the date */
216 #define PREFERRED_DATE_FMT C_("GDateTime", "%m/%d/%y")
218 /* Translators: this is the preferred format for expressing the time */
219 #define PREFERRED_TIME_FMT C_("GDateTime", "%H:%M:%S")
221 /* Translators: this is the preferred format for expressing 12 hour time */
222 #define PREFERRED_12HR_TIME_FMT C_("GDateTime", "%I:%M:%S %p")
224 #define WEEKDAY_ABBR(d) (get_weekday_name_abbr (g_date_time_get_day_of_week (d)))
225 #define WEEKDAY_ABBR_IS_LOCALE FALSE
226 #define WEEKDAY_FULL(d) (get_weekday_name (g_date_time_get_day_of_week (d)))
227 #define WEEKDAY_FULL_IS_LOCALE FALSE
228 /* We don't yet know if nl_langinfo (MON_n) returns standalone or complete-date
229 * format forms but if nl_langinfo (ALTMON_n) is not supported then we will
230 * have to use MONTH_FULL as standalone. The same if nl_langinfo () does not
231 * exist at all. MONTH_ABBR is similar: if nl_langinfo (_NL_ABALTMON_n) is not
232 * supported then we will use MONTH_ABBR as standalone.
234 #define MONTH_ABBR(d) (get_month_name_abbr_standalone (g_date_time_get_month (d)))
235 #define MONTH_ABBR_IS_LOCALE FALSE
236 #define MONTH_FULL(d) (get_month_name_standalone (g_date_time_get_month (d)))
237 #define MONTH_FULL_IS_LOCALE FALSE
239 static const gchar *
240 get_month_name_standalone (gint month)
242 switch (month)
244 case 1:
245 /* Translators: Some languages (Baltic, Slavic, Greek, and some more)
246 * need different grammatical forms of month names depending on whether
247 * they are standalone or in a complete date context, with the day
248 * number. Some other languages may prefer starting with uppercase when
249 * they are standalone and with lowercase when they are in a complete
250 * date context. Here are full month names in a form appropriate when
251 * they are used standalone. If your system is Linux with the glibc
252 * version 2.27 (released Feb 1, 2018) or newer or if it is from the BSD
253 * family (which includes OS X) then you can refer to the date command
254 * line utility and see what the command `date +%OB' produces. Also in
255 * the latest Linux the command `locale alt_mon' in your native locale
256 * produces a complete list of month names almost ready to copy and
257 * paste here. Note that in most of the languages (western European,
258 * non-European) there is no difference between the standalone and
259 * complete date form.
261 return C_("full month name", "January");
262 case 2:
263 return C_("full month name", "February");
264 case 3:
265 return C_("full month name", "March");
266 case 4:
267 return C_("full month name", "April");
268 case 5:
269 return C_("full month name", "May");
270 case 6:
271 return C_("full month name", "June");
272 case 7:
273 return C_("full month name", "July");
274 case 8:
275 return C_("full month name", "August");
276 case 9:
277 return C_("full month name", "September");
278 case 10:
279 return C_("full month name", "October");
280 case 11:
281 return C_("full month name", "November");
282 case 12:
283 return C_("full month name", "December");
285 default:
286 g_warning ("Invalid month number %d", month);
289 return NULL;
292 static const gchar *
293 get_month_name_abbr_standalone (gint month)
295 switch (month)
297 case 1:
298 /* Translators: Some languages need different grammatical forms of
299 * month names depending on whether they are standalone or in a complete
300 * date context, with the day number. Some may prefer starting with
301 * uppercase when they are standalone and with lowercase when they are
302 * in a full date context. However, as these names are abbreviated
303 * the grammatical difference is visible probably only in Belarusian
304 * and Russian. In other languages there is no difference between
305 * the standalone and complete date form when they are abbreviated.
306 * If your system is Linux with the glibc version 2.27 (released
307 * Feb 1, 2018) or newer then you can refer to the date command line
308 * utility and see what the command `date +%Ob' produces. Also in
309 * the latest Linux the command `locale ab_alt_mon' in your native
310 * locale produces a complete list of month names almost ready to copy
311 * and paste here. Note that this feature is not yet supported by any
312 * other platform. Here are abbreviated month names in a form
313 * appropriate when they are used standalone.
315 return C_("abbreviated month name", "Jan");
316 case 2:
317 return C_("abbreviated month name", "Feb");
318 case 3:
319 return C_("abbreviated month name", "Mar");
320 case 4:
321 return C_("abbreviated month name", "Apr");
322 case 5:
323 return C_("abbreviated month name", "May");
324 case 6:
325 return C_("abbreviated month name", "Jun");
326 case 7:
327 return C_("abbreviated month name", "Jul");
328 case 8:
329 return C_("abbreviated month name", "Aug");
330 case 9:
331 return C_("abbreviated month name", "Sep");
332 case 10:
333 return C_("abbreviated month name", "Oct");
334 case 11:
335 return C_("abbreviated month name", "Nov");
336 case 12:
337 return C_("abbreviated month name", "Dec");
339 default:
340 g_warning ("Invalid month number %d", month);
343 return NULL;
346 static const gchar *
347 get_weekday_name (gint day)
349 switch (day)
351 case 1:
352 return C_("full weekday name", "Monday");
353 case 2:
354 return C_("full weekday name", "Tuesday");
355 case 3:
356 return C_("full weekday name", "Wednesday");
357 case 4:
358 return C_("full weekday name", "Thursday");
359 case 5:
360 return C_("full weekday name", "Friday");
361 case 6:
362 return C_("full weekday name", "Saturday");
363 case 7:
364 return C_("full weekday name", "Sunday");
366 default:
367 g_warning ("Invalid week day number %d", day);
370 return NULL;
373 static const gchar *
374 get_weekday_name_abbr (gint day)
376 switch (day)
378 case 1:
379 return C_("abbreviated weekday name", "Mon");
380 case 2:
381 return C_("abbreviated weekday name", "Tue");
382 case 3:
383 return C_("abbreviated weekday name", "Wed");
384 case 4:
385 return C_("abbreviated weekday name", "Thu");
386 case 5:
387 return C_("abbreviated weekday name", "Fri");
388 case 6:
389 return C_("abbreviated weekday name", "Sat");
390 case 7:
391 return C_("abbreviated weekday name", "Sun");
393 default:
394 g_warning ("Invalid week day number %d", day);
397 return NULL;
400 #endif /* HAVE_LANGINFO_TIME */
402 #ifdef HAVE_LANGINFO_ALTMON
404 /* If nl_langinfo () supports ALTMON_n then MON_n returns full date format
405 * forms and ALTMON_n returns standalone forms.
408 #define MONTH_FULL_WITH_DAY(d) MONTH_FULL(d)
409 #define MONTH_FULL_WITH_DAY_IS_LOCALE MONTH_FULL_IS_LOCALE
411 static const gint alt_month_item[12] =
413 ALTMON_1, ALTMON_2, ALTMON_3, ALTMON_4, ALTMON_5, ALTMON_6,
414 ALTMON_7, ALTMON_8, ALTMON_9, ALTMON_10, ALTMON_11, ALTMON_12
417 #define MONTH_FULL_STANDALONE(d) nl_langinfo (alt_month_item[g_date_time_get_month (d) - 1])
418 #define MONTH_FULL_STANDALONE_IS_LOCALE TRUE
420 #else
422 /* If nl_langinfo () does not support ALTMON_n then either MON_n returns
423 * standalone forms or nl_langinfo (MON_n) does not work so we have defined
424 * it as standalone form.
427 #define MONTH_FULL_STANDALONE(d) MONTH_FULL(d)
428 #define MONTH_FULL_STANDALONE_IS_LOCALE MONTH_FULL_IS_LOCALE
429 #define MONTH_FULL_WITH_DAY(d) (get_month_name_with_day (g_date_time_get_month (d)))
430 #define MONTH_FULL_WITH_DAY_IS_LOCALE FALSE
432 static const gchar *
433 get_month_name_with_day (gint month)
435 switch (month)
437 case 1:
438 /* Translators: Some languages need different grammatical forms of
439 * month names depending on whether they are standalone or in a full
440 * date context, with the day number. Some may prefer starting with
441 * uppercase when they are standalone and with lowercase when they are
442 * in a full date context. Here are full month names in a form
443 * appropriate when they are used in a full date context, with the
444 * day number. If your system is Linux with the glibc version 2.27
445 * (released Feb 1, 2018) or newer or if it is from the BSD family
446 * (which includes OS X) then you can refer to the date command line
447 * utility and see what the command `date +%B' produces. Also in
448 * the latest Linux the command `locale mon' in your native locale
449 * produces a complete list of month names almost ready to copy and
450 * paste here. In older Linux systems due to a bug the result is
451 * incorrect in some languages. Note that in most of the languages
452 * (western European, non-European) there is no difference between the
453 * standalone and complete date form.
455 return C_("full month name with day", "January");
456 case 2:
457 return C_("full month name with day", "February");
458 case 3:
459 return C_("full month name with day", "March");
460 case 4:
461 return C_("full month name with day", "April");
462 case 5:
463 return C_("full month name with day", "May");
464 case 6:
465 return C_("full month name with day", "June");
466 case 7:
467 return C_("full month name with day", "July");
468 case 8:
469 return C_("full month name with day", "August");
470 case 9:
471 return C_("full month name with day", "September");
472 case 10:
473 return C_("full month name with day", "October");
474 case 11:
475 return C_("full month name with day", "November");
476 case 12:
477 return C_("full month name with day", "December");
479 default:
480 g_warning ("Invalid month number %d", month);
483 return NULL;
486 #endif /* HAVE_LANGINFO_ALTMON */
488 #ifdef HAVE_LANGINFO_ABALTMON
490 /* If nl_langinfo () supports _NL_ABALTMON_n then ABMON_n returns full
491 * date format forms and _NL_ABALTMON_n returns standalone forms.
494 #define MONTH_ABBR_WITH_DAY(d) MONTH_ABBR(d)
495 #define MONTH_ABBR_WITH_DAY_IS_LOCALE MONTH_ABBR_IS_LOCALE
497 static const gint ab_alt_month_item[12] =
499 _NL_ABALTMON_1, _NL_ABALTMON_2, _NL_ABALTMON_3, _NL_ABALTMON_4,
500 _NL_ABALTMON_5, _NL_ABALTMON_6, _NL_ABALTMON_7, _NL_ABALTMON_8,
501 _NL_ABALTMON_9, _NL_ABALTMON_10, _NL_ABALTMON_11, _NL_ABALTMON_12
504 #define MONTH_ABBR_STANDALONE(d) nl_langinfo (ab_alt_month_item[g_date_time_get_month (d) - 1])
505 #define MONTH_ABBR_STANDALONE_IS_LOCALE TRUE
507 #else
509 /* If nl_langinfo () does not support _NL_ABALTMON_n then either ABMON_n
510 * returns standalone forms or nl_langinfo (ABMON_n) does not work so we
511 * have defined it as standalone form. Now it's time to swap.
514 #define MONTH_ABBR_STANDALONE(d) MONTH_ABBR(d)
515 #define MONTH_ABBR_STANDALONE_IS_LOCALE MONTH_ABBR_IS_LOCALE
516 #define MONTH_ABBR_WITH_DAY(d) (get_month_name_abbr_with_day (g_date_time_get_month (d)))
517 #define MONTH_ABBR_WITH_DAY_IS_LOCALE FALSE
519 static const gchar *
520 get_month_name_abbr_with_day (gint month)
522 switch (month)
524 case 1:
525 /* Translators: Some languages need different grammatical forms of
526 * month names depending on whether they are standalone or in a full
527 * date context, with the day number. Some may prefer starting with
528 * uppercase when they are standalone and with lowercase when they are
529 * in a full date context. Here are abbreviated month names in a form
530 * appropriate when they are used in a full date context, with the
531 * day number. However, as these names are abbreviated the grammatical
532 * difference is visible probably only in Belarusian and Russian.
533 * In other languages there is no difference between the standalone
534 * and complete date form when they are abbreviated. If your system
535 * is Linux with the glibc version 2.27 (released Feb 1, 2018) or newer
536 * then you can refer to the date command line utility and see what the
537 * command `date +%b' produces. Also in the latest Linux the command
538 * `locale abmon' in your native locale produces a complete list of
539 * month names almost ready to copy and paste here. In other systems
540 * due to a bug the result is incorrect in some languages.
542 return C_("abbreviated month name with day", "Jan");
543 case 2:
544 return C_("abbreviated month name with day", "Feb");
545 case 3:
546 return C_("abbreviated month name with day", "Mar");
547 case 4:
548 return C_("abbreviated month name with day", "Apr");
549 case 5:
550 return C_("abbreviated month name with day", "May");
551 case 6:
552 return C_("abbreviated month name with day", "Jun");
553 case 7:
554 return C_("abbreviated month name with day", "Jul");
555 case 8:
556 return C_("abbreviated month name with day", "Aug");
557 case 9:
558 return C_("abbreviated month name with day", "Sep");
559 case 10:
560 return C_("abbreviated month name with day", "Oct");
561 case 11:
562 return C_("abbreviated month name with day", "Nov");
563 case 12:
564 return C_("abbreviated month name with day", "Dec");
566 default:
567 g_warning ("Invalid month number %d", month);
570 return NULL;
573 #endif /* HAVE_LANGINFO_ABALTMON */
575 /* Format AM/PM indicator if the locale does not have a localized version. */
576 static const gchar *
577 get_fallback_ampm (gint hour)
579 if (hour < 12)
580 /* Translators: 'before midday' indicator */
581 return C_("GDateTime", "AM");
582 else
583 /* Translators: 'after midday' indicator */
584 return C_("GDateTime", "PM");
587 static inline gint
588 ymd_to_days (gint year,
589 gint month,
590 gint day)
592 gint64 days;
594 days = (year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100)
595 + ((year - 1) / 400);
597 days += days_in_year[0][month - 1];
598 if (GREGORIAN_LEAP (year) && month > 2)
599 day++;
601 days += day;
603 return days;
606 static void
607 g_date_time_get_week_number (GDateTime *datetime,
608 gint *week_number,
609 gint *day_of_week,
610 gint *day_of_year)
612 gint a, b, c, d, e, f, g, n, s, month, day, year;
614 g_date_time_get_ymd (datetime, &year, &month, &day);
616 if (month <= 2)
618 a = g_date_time_get_year (datetime) - 1;
619 b = (a / 4) - (a / 100) + (a / 400);
620 c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
621 s = b - c;
622 e = 0;
623 f = day - 1 + (31 * (month - 1));
625 else
627 a = year;
628 b = (a / 4) - (a / 100) + (a / 400);
629 c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400);
630 s = b - c;
631 e = s + 1;
632 f = day + (((153 * (month - 3)) + 2) / 5) + 58 + s;
635 g = (a + b) % 7;
636 d = (f + g - e) % 7;
637 n = f + 3 - d;
639 if (week_number)
641 if (n < 0)
642 *week_number = 53 - ((g - s) / 5);
643 else if (n > 364 + s)
644 *week_number = 1;
645 else
646 *week_number = (n / 7) + 1;
649 if (day_of_week)
650 *day_of_week = d + 1;
652 if (day_of_year)
653 *day_of_year = f + 1;
656 /* Lifecycle {{{1 */
658 static GDateTime *
659 g_date_time_alloc (GTimeZone *tz)
661 GDateTime *datetime;
663 datetime = g_slice_new0 (GDateTime);
664 datetime->tz = g_time_zone_ref (tz);
665 datetime->ref_count = 1;
667 return datetime;
671 * g_date_time_ref:
672 * @datetime: a #GDateTime
674 * Atomically increments the reference count of @datetime by one.
676 * Returns: the #GDateTime with the reference count increased
678 * Since: 2.26
680 GDateTime *
681 g_date_time_ref (GDateTime *datetime)
683 g_return_val_if_fail (datetime != NULL, NULL);
684 g_return_val_if_fail (datetime->ref_count > 0, NULL);
686 g_atomic_int_inc (&datetime->ref_count);
688 return datetime;
692 * g_date_time_unref:
693 * @datetime: a #GDateTime
695 * Atomically decrements the reference count of @datetime by one.
697 * When the reference count reaches zero, the resources allocated by
698 * @datetime are freed
700 * Since: 2.26
702 void
703 g_date_time_unref (GDateTime *datetime)
705 g_return_if_fail (datetime != NULL);
706 g_return_if_fail (datetime->ref_count > 0);
708 if (g_atomic_int_dec_and_test (&datetime->ref_count))
710 g_time_zone_unref (datetime->tz);
711 g_slice_free (GDateTime, datetime);
715 /* Internal state transformers {{{1 */
716 /*< internal >
717 * g_date_time_to_instant:
718 * @datetime: a #GDateTime
720 * Convert a @datetime into an instant.
722 * An instant is a number that uniquely describes a particular
723 * microsecond in time, taking time zone considerations into account.
724 * (ie: "03:00 -0400" is the same instant as "02:00 -0500").
726 * An instant is always positive but we use a signed return value to
727 * avoid troubles with C.
729 static gint64
730 g_date_time_to_instant (GDateTime *datetime)
732 gint64 offset;
734 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
735 offset *= USEC_PER_SECOND;
737 return datetime->days * USEC_PER_DAY + datetime->usec - offset;
740 /*< internal >
741 * g_date_time_from_instant:
742 * @tz: a #GTimeZone
743 * @instant: a instant in time
745 * Creates a #GDateTime from a time zone and an instant.
747 * This might fail if the time ends up being out of range.
749 static GDateTime *
750 g_date_time_from_instant (GTimeZone *tz,
751 gint64 instant)
753 GDateTime *datetime;
754 gint64 offset;
756 if (instant < 0 || instant > G_GINT64_CONSTANT (1000000000000000000))
757 return NULL;
759 datetime = g_date_time_alloc (tz);
760 datetime->interval = g_time_zone_find_interval (tz,
761 G_TIME_TYPE_UNIVERSAL,
762 INSTANT_TO_UNIX (instant));
763 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
764 offset *= USEC_PER_SECOND;
766 instant += offset;
768 datetime->days = instant / USEC_PER_DAY;
769 datetime->usec = instant % USEC_PER_DAY;
771 if (datetime->days < 1 || 3652059 < datetime->days)
773 g_date_time_unref (datetime);
774 datetime = NULL;
777 return datetime;
781 /*< internal >
782 * g_date_time_deal_with_date_change:
783 * @datetime: a #GDateTime
785 * This function should be called whenever the date changes by adding
786 * days, months or years. It does three things.
788 * First, we ensure that the date falls between 0001-01-01 and
789 * 9999-12-31 and return %FALSE if it does not.
791 * Next we update the ->interval field.
793 * Finally, we ensure that the resulting date and time pair exists (by
794 * ensuring that our time zone has an interval containing it) and
795 * adjusting as required. For example, if we have the time 02:30:00 on
796 * March 13 2010 in Toronto and we add 1 day to it, we would end up with
797 * 2:30am on March 14th, which doesn't exist. In that case, we bump the
798 * time up to 3:00am.
800 static gboolean
801 g_date_time_deal_with_date_change (GDateTime *datetime)
803 GTimeType was_dst;
804 gint64 full_time;
805 gint64 usec;
807 if (datetime->days < 1 || datetime->days > 3652059)
808 return FALSE;
810 was_dst = g_time_zone_is_dst (datetime->tz, datetime->interval);
812 full_time = datetime->days * USEC_PER_DAY + datetime->usec;
815 usec = full_time % USEC_PER_SECOND;
816 full_time /= USEC_PER_SECOND;
817 full_time -= UNIX_EPOCH_START * SEC_PER_DAY;
819 datetime->interval = g_time_zone_adjust_time (datetime->tz,
820 was_dst,
821 &full_time);
822 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
823 full_time *= USEC_PER_SECOND;
824 full_time += usec;
826 datetime->days = full_time / USEC_PER_DAY;
827 datetime->usec = full_time % USEC_PER_DAY;
829 /* maybe daylight time caused us to shift to a different day,
830 * but it definitely didn't push us into a different year */
831 return TRUE;
834 static GDateTime *
835 g_date_time_replace_days (GDateTime *datetime,
836 gint days)
838 GDateTime *new;
840 new = g_date_time_alloc (datetime->tz);
841 new->interval = datetime->interval;
842 new->usec = datetime->usec;
843 new->days = days;
845 if (!g_date_time_deal_with_date_change (new))
847 g_date_time_unref (new);
848 new = NULL;
851 return new;
854 /* now/unix/timeval Constructors {{{1 */
856 /*< internal >
857 * g_date_time_new_from_timeval:
858 * @tz: a #GTimeZone
859 * @tv: a #GTimeVal
861 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
862 * given time zone @tz.
864 * The time contained in a #GTimeVal is always stored in the form of
865 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
866 * given time zone.
868 * This call can fail (returning %NULL) if @tv represents a time outside
869 * of the supported range of #GDateTime.
871 * You should release the return value by calling g_date_time_unref()
872 * when you are done with it.
874 * Returns: a new #GDateTime, or %NULL
876 * Since: 2.26
878 static GDateTime *
879 g_date_time_new_from_timeval (GTimeZone *tz,
880 const GTimeVal *tv)
882 if ((gint64) tv->tv_sec > G_MAXINT64 - 1 ||
883 !UNIX_TO_INSTANT_IS_VALID ((gint64) tv->tv_sec + 1))
884 return NULL;
886 return g_date_time_from_instant (tz, tv->tv_usec +
887 UNIX_TO_INSTANT (tv->tv_sec));
890 /*< internal >
891 * g_date_time_new_from_unix:
892 * @tz: a #GTimeZone
893 * @t: the Unix time
895 * Creates a #GDateTime corresponding to the given Unix time @t in the
896 * given time zone @tz.
898 * Unix time is the number of seconds that have elapsed since 1970-01-01
899 * 00:00:00 UTC, regardless of the time zone given.
901 * This call can fail (returning %NULL) if @t represents a time outside
902 * of the supported range of #GDateTime.
904 * You should release the return value by calling g_date_time_unref()
905 * when you are done with it.
907 * Returns: a new #GDateTime, or %NULL
909 * Since: 2.26
911 static GDateTime *
912 g_date_time_new_from_unix (GTimeZone *tz,
913 gint64 secs)
915 if (!UNIX_TO_INSTANT_IS_VALID (secs))
916 return NULL;
918 return g_date_time_from_instant (tz, UNIX_TO_INSTANT (secs));
922 * g_date_time_new_now:
923 * @tz: a #GTimeZone
925 * Creates a #GDateTime corresponding to this exact instant in the given
926 * time zone @tz. The time is as accurate as the system allows, to a
927 * maximum accuracy of 1 microsecond.
929 * This function will always succeed unless the system clock is set to
930 * truly insane values (or unless GLib is still being used after the
931 * year 9999).
933 * You should release the return value by calling g_date_time_unref()
934 * when you are done with it.
936 * Returns: a new #GDateTime, or %NULL
938 * Since: 2.26
940 GDateTime *
941 g_date_time_new_now (GTimeZone *tz)
943 GTimeVal tv;
945 g_get_current_time (&tv);
947 return g_date_time_new_from_timeval (tz, &tv);
951 * g_date_time_new_now_local:
953 * Creates a #GDateTime corresponding to this exact instant in the local
954 * time zone.
956 * This is equivalent to calling g_date_time_new_now() with the time
957 * zone returned by g_time_zone_new_local().
959 * Returns: a new #GDateTime, or %NULL
961 * Since: 2.26
963 GDateTime *
964 g_date_time_new_now_local (void)
966 GDateTime *datetime;
967 GTimeZone *local;
969 local = g_time_zone_new_local ();
970 datetime = g_date_time_new_now (local);
971 g_time_zone_unref (local);
973 return datetime;
977 * g_date_time_new_now_utc:
979 * Creates a #GDateTime corresponding to this exact instant in UTC.
981 * This is equivalent to calling g_date_time_new_now() with the time
982 * zone returned by g_time_zone_new_utc().
984 * Returns: a new #GDateTime, or %NULL
986 * Since: 2.26
988 GDateTime *
989 g_date_time_new_now_utc (void)
991 GDateTime *datetime;
992 GTimeZone *utc;
994 utc = g_time_zone_new_utc ();
995 datetime = g_date_time_new_now (utc);
996 g_time_zone_unref (utc);
998 return datetime;
1002 * g_date_time_new_from_unix_local:
1003 * @t: the Unix time
1005 * Creates a #GDateTime corresponding to the given Unix time @t in the
1006 * local time zone.
1008 * Unix time is the number of seconds that have elapsed since 1970-01-01
1009 * 00:00:00 UTC, regardless of the local time offset.
1011 * This call can fail (returning %NULL) if @t represents a time outside
1012 * of the supported range of #GDateTime.
1014 * You should release the return value by calling g_date_time_unref()
1015 * when you are done with it.
1017 * Returns: a new #GDateTime, or %NULL
1019 * Since: 2.26
1021 GDateTime *
1022 g_date_time_new_from_unix_local (gint64 t)
1024 GDateTime *datetime;
1025 GTimeZone *local;
1027 local = g_time_zone_new_local ();
1028 datetime = g_date_time_new_from_unix (local, t);
1029 g_time_zone_unref (local);
1031 return datetime;
1035 * g_date_time_new_from_unix_utc:
1036 * @t: the Unix time
1038 * Creates a #GDateTime corresponding to the given Unix time @t in UTC.
1040 * Unix time is the number of seconds that have elapsed since 1970-01-01
1041 * 00:00:00 UTC.
1043 * This call can fail (returning %NULL) if @t represents a time outside
1044 * of the supported range of #GDateTime.
1046 * You should release the return value by calling g_date_time_unref()
1047 * when you are done with it.
1049 * Returns: a new #GDateTime, or %NULL
1051 * Since: 2.26
1053 GDateTime *
1054 g_date_time_new_from_unix_utc (gint64 t)
1056 GDateTime *datetime;
1057 GTimeZone *utc;
1059 utc = g_time_zone_new_utc ();
1060 datetime = g_date_time_new_from_unix (utc, t);
1061 g_time_zone_unref (utc);
1063 return datetime;
1067 * g_date_time_new_from_timeval_local:
1068 * @tv: a #GTimeVal
1070 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
1071 * local time zone.
1073 * The time contained in a #GTimeVal is always stored in the form of
1074 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
1075 * local time offset.
1077 * This call can fail (returning %NULL) if @tv represents a time outside
1078 * of the supported range of #GDateTime.
1080 * You should release the return value by calling g_date_time_unref()
1081 * when you are done with it.
1083 * Returns: a new #GDateTime, or %NULL
1085 * Since: 2.26
1087 GDateTime *
1088 g_date_time_new_from_timeval_local (const GTimeVal *tv)
1090 GDateTime *datetime;
1091 GTimeZone *local;
1093 local = g_time_zone_new_local ();
1094 datetime = g_date_time_new_from_timeval (local, tv);
1095 g_time_zone_unref (local);
1097 return datetime;
1101 * g_date_time_new_from_timeval_utc:
1102 * @tv: a #GTimeVal
1104 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in UTC.
1106 * The time contained in a #GTimeVal is always stored in the form of
1107 * seconds elapsed since 1970-01-01 00:00:00 UTC.
1109 * This call can fail (returning %NULL) if @tv represents a time outside
1110 * of the supported range of #GDateTime.
1112 * You should release the return value by calling g_date_time_unref()
1113 * when you are done with it.
1115 * Returns: a new #GDateTime, or %NULL
1117 * Since: 2.26
1119 GDateTime *
1120 g_date_time_new_from_timeval_utc (const GTimeVal *tv)
1122 GDateTime *datetime;
1123 GTimeZone *utc;
1125 utc = g_time_zone_new_utc ();
1126 datetime = g_date_time_new_from_timeval (utc, tv);
1127 g_time_zone_unref (utc);
1129 return datetime;
1132 /* Parse integers in the form d (week days), dd (hours etc), ddd (ordinal days) or dddd (years) */
1133 static gboolean
1134 get_iso8601_int (const gchar *text, gsize length, gint *value)
1136 gint i, v = 0;
1138 if (length < 1 || length > 4)
1139 return FALSE;
1141 for (i = 0; i < length; i++)
1143 const gchar c = text[i];
1144 if (c < '0' || c > '9')
1145 return FALSE;
1146 v = v * 10 + (c - '0');
1149 *value = v;
1150 return TRUE;
1153 /* Parse seconds in the form ss or ss.sss (variable length decimal) */
1154 static gboolean
1155 get_iso8601_seconds (const gchar *text, gsize length, gdouble *value)
1157 gint i;
1158 gdouble divisor = 1, v = 0;
1160 if (length < 2)
1161 return FALSE;
1163 for (i = 0; i < 2; i++)
1165 const gchar c = text[i];
1166 if (c < '0' || c > '9')
1167 return FALSE;
1168 v = v * 10 + (c - '0');
1171 if (length > 2 && !(text[i] == '.' || text[i] == ','))
1172 return FALSE;
1173 i++;
1174 if (i == length)
1175 return FALSE;
1177 for (; i < length; i++)
1179 const gchar c = text[i];
1180 if (c < '0' || c > '9')
1181 return FALSE;
1182 v = v * 10 + (c - '0');
1183 divisor *= 10;
1186 *value = v / divisor;
1187 return TRUE;
1190 static GDateTime *
1191 g_date_time_new_ordinal (GTimeZone *tz, gint year, gint ordinal_day, gint hour, gint minute, gdouble seconds)
1193 GDateTime *dt;
1195 if (ordinal_day < 1 || ordinal_day > (GREGORIAN_LEAP (year) ? 366 : 365))
1196 return NULL;
1198 dt = g_date_time_new (tz, year, 1, 1, hour, minute, seconds);
1199 dt->days += ordinal_day - 1;
1201 return dt;
1204 static GDateTime *
1205 g_date_time_new_week (GTimeZone *tz, gint year, gint week, gint week_day, gint hour, gint minute, gdouble seconds)
1207 gint64 p;
1208 gint max_week, jan4_week_day, ordinal_day;
1209 GDateTime *dt;
1211 p = (year * 365 + (year / 4) - (year / 100) + (year / 400)) % 7;
1212 max_week = p == 4 ? 53 : 52;
1214 if (week < 1 || week > max_week || week_day < 1 || week_day > 7)
1215 return NULL;
1217 dt = g_date_time_new (tz, year, 1, 4, 0, 0, 0);
1218 g_date_time_get_week_number (dt, NULL, &jan4_week_day, NULL);
1219 g_date_time_unref (dt);
1221 ordinal_day = (week * 7) + week_day - (jan4_week_day + 3);
1222 if (ordinal_day < 0)
1224 year--;
1225 ordinal_day += GREGORIAN_LEAP (year) ? 366 : 365;
1227 else if (ordinal_day > (GREGORIAN_LEAP (year) ? 366 : 365))
1229 ordinal_day -= (GREGORIAN_LEAP (year) ? 366 : 365);
1230 year++;
1233 return g_date_time_new_ordinal (tz, year, ordinal_day, hour, minute, seconds);
1236 static GDateTime *
1237 parse_iso8601_date (const gchar *text, gsize length,
1238 gint hour, gint minute, gdouble seconds, GTimeZone *tz)
1240 /* YYYY-MM-DD */
1241 if (length == 10 && text[4] == '-' && text[7] == '-')
1243 int year, month, day;
1244 if (!get_iso8601_int (text, 4, &year) ||
1245 !get_iso8601_int (text + 5, 2, &month) ||
1246 !get_iso8601_int (text + 8, 2, &day))
1247 return NULL;
1248 return g_date_time_new (tz, year, month, day, hour, minute, seconds);
1250 /* YYYY-DDD */
1251 else if (length == 8 && text[4] == '-')
1253 gint year, ordinal_day;
1254 if (!get_iso8601_int (text, 4, &year) ||
1255 !get_iso8601_int (text + 5, 3, &ordinal_day))
1256 return NULL;
1257 return g_date_time_new_ordinal (tz, year, ordinal_day, hour, minute, seconds);
1259 /* YYYY-Www-D */
1260 else if (length == 10 && text[4] == '-' && text[5] == 'W' && text[8] == '-')
1262 gint year, week, week_day;
1263 if (!get_iso8601_int (text, 4, &year) ||
1264 !get_iso8601_int (text + 6, 2, &week) ||
1265 !get_iso8601_int (text + 9, 1, &week_day))
1266 return NULL;
1267 return g_date_time_new_week (tz, year, week, week_day, hour, minute, seconds);
1269 /* YYYYWwwD */
1270 else if (length == 8 && text[4] == 'W')
1272 gint year, week, week_day;
1273 if (!get_iso8601_int (text, 4, &year) ||
1274 !get_iso8601_int (text + 5, 2, &week) ||
1275 !get_iso8601_int (text + 7, 1, &week_day))
1276 return NULL;
1277 return g_date_time_new_week (tz, year, week, week_day, hour, minute, seconds);
1279 /* YYYYMMDD */
1280 else if (length == 8)
1282 int year, month, day;
1283 if (!get_iso8601_int (text, 4, &year) ||
1284 !get_iso8601_int (text + 4, 2, &month) ||
1285 !get_iso8601_int (text + 6, 2, &day))
1286 return NULL;
1287 return g_date_time_new (tz, year, month, day, hour, minute, seconds);
1289 /* YYYYDDD */
1290 else if (length == 7)
1292 gint year, ordinal_day;
1293 if (!get_iso8601_int (text, 4, &year) ||
1294 !get_iso8601_int (text + 4, 3, &ordinal_day))
1295 return NULL;
1296 return g_date_time_new_ordinal (tz, year, ordinal_day, hour, minute, seconds);
1298 else
1299 return FALSE;
1302 static GTimeZone *
1303 parse_iso8601_timezone (const gchar *text, gsize length, gssize *tz_offset)
1305 gint i, tz_length, offset_sign = 1, offset_hours, offset_minutes;
1306 GTimeZone *tz;
1308 /* UTC uses Z suffix */
1309 if (length > 0 && text[length - 1] == 'Z')
1311 *tz_offset = length - 1;
1312 return g_time_zone_new_utc ();
1315 /* Look for '+' or '-' of offset */
1316 for (i = length - 1; i >= 0; i--)
1317 if (text[i] == '+' || text[i] == '-')
1319 offset_sign = text[i] == '-' ? -1 : 1;
1320 break;
1322 if (i < 0)
1323 return NULL;
1324 tz_length = length - i;
1326 /* +hh:mm or -hh:mm */
1327 if (tz_length == 6 && text[i+3] == ':')
1329 if (!get_iso8601_int (text + i + 1, 2, &offset_hours) ||
1330 !get_iso8601_int (text + i + 4, 2, &offset_minutes))
1331 return NULL;
1333 /* +hhmm or -hhmm */
1334 else if (tz_length == 5)
1336 if (!get_iso8601_int (text + i + 1, 2, &offset_hours) ||
1337 !get_iso8601_int (text + i + 3, 2, &offset_minutes))
1338 return NULL;
1340 /* +hh or -hh */
1341 else if (tz_length == 3)
1343 if (!get_iso8601_int (text + i + 1, 2, &offset_hours))
1344 return NULL;
1345 offset_minutes = 0;
1347 else
1348 return NULL;
1350 *tz_offset = i;
1351 tz = g_time_zone_new (text + i);
1353 /* Double-check that the GTimeZone matches our interpretation of the timezone.
1354 * Failure would indicate a bug either here of in the GTimeZone code. */
1355 g_assert (g_time_zone_get_offset (tz, 0) == offset_sign * (offset_hours * 3600 + offset_minutes * 60));
1357 return tz;
1360 static gboolean
1361 parse_iso8601_time (const gchar *text, gsize length,
1362 gint *hour, gint *minute, gdouble *seconds, GTimeZone **tz)
1364 gssize tz_offset = -1;
1366 /* Check for timezone suffix */
1367 *tz = parse_iso8601_timezone (text, length, &tz_offset);
1368 if (tz_offset >= 0)
1369 length = tz_offset;
1371 /* hh:mm:ss(.sss) */
1372 if (length >= 8 && text[2] == ':' && text[5] == ':')
1374 return get_iso8601_int (text, 2, hour) &&
1375 get_iso8601_int (text + 3, 2, minute) &&
1376 get_iso8601_seconds (text + 6, length - 6, seconds);
1378 /* hhmmss(.sss) */
1379 else if (length >= 6)
1381 return get_iso8601_int (text, 2, hour) &&
1382 get_iso8601_int (text + 2, 2, minute) &&
1383 get_iso8601_seconds (text + 4, length - 4, seconds);
1385 else
1386 return FALSE;
1390 * g_date_time_new_from_iso8601:
1391 * @text: an ISO 8601 formatted time string.
1392 * @default_tz: (nullable): a #GTimeZone to use if the text doesn't contain a
1393 * timezone, or %NULL.
1395 * Creates a #GDateTime corresponding to the given
1396 * [ISO 8601 formatted string](https://en.wikipedia.org/wiki/ISO_8601)
1397 * @text. ISO 8601 strings of the form <date><sep><time><tz> are supported.
1399 * <sep> is the separator and can be either 'T', 't' or ' '.
1401 * <date> is in the form:
1403 * - `YYYY-MM-DD` - Year/month/day, e.g. 2016-08-24.
1404 * - `YYYYMMDD` - Same as above without dividers.
1405 * - `YYYY-DDD` - Ordinal day where DDD is from 001 to 366, e.g. 2016-237.
1406 * - `YYYYDDD` - Same as above without dividers.
1407 * - `YYYY-Www-D` - Week day where ww is from 01 to 52 and D from 1-7,
1408 * e.g. 2016-W34-3.
1409 * - `YYYYWwwD` - Same as above without dividers.
1411 * <time> is in the form:
1413 * - `hh:mm:ss(.sss)` - Hours, minutes, seconds (subseconds), e.g. 22:10:42.123.
1414 * - `hhmmss(.sss)` - Same as above without dividers.
1416 * <tz> is an optional timezone suffix of the form:
1418 * - `Z` - UTC.
1419 * - `+hh:mm` or `-hh:mm` - Offset from UTC in hours and minutes, e.g. +12:00.
1420 * - `+hh` or `-hh` - Offset from UTC in hours, e.g. +12.
1422 * If the timezone is not provided in @text it must be provided in @default_tz
1423 * (this field is otherwise ignored).
1425 * This call can fail (returning %NULL) if @text is not a valid ISO 8601
1426 * formatted string.
1428 * You should release the return value by calling g_date_time_unref()
1429 * when you are done with it.
1431 * Returns: (transfer full) (nullable): a new #GDateTime, or %NULL
1433 * Since: 2.56
1435 GDateTime *
1436 g_date_time_new_from_iso8601 (const gchar *text, GTimeZone *default_tz)
1438 gint length, date_length = -1;
1439 gint hour = 0, minute = 0;
1440 gdouble seconds = 0.0;
1441 GTimeZone *tz = NULL;
1442 GDateTime *datetime = NULL;
1444 g_return_val_if_fail (text != NULL, NULL);
1446 /* Count length of string and find date / time separator ('T', 't', or ' ') */
1447 for (length = 0; text[length] != '\0'; length++)
1449 if (date_length < 0 && (text[length] == 'T' || text[length] == 't' || text[length] == ' '))
1450 date_length = length;
1453 if (date_length < 0)
1454 return NULL;
1456 if (!parse_iso8601_time (text + date_length + 1, length - (date_length + 1),
1457 &hour, &minute, &seconds, &tz))
1458 goto out;
1459 if (tz == NULL && default_tz == NULL)
1460 return NULL;
1462 datetime = parse_iso8601_date (text, date_length, hour, minute, seconds, tz ? tz : default_tz);
1464 out:
1465 if (tz != NULL)
1466 g_time_zone_unref (tz);
1467 return datetime;
1470 /* full new functions {{{1 */
1473 * g_date_time_new:
1474 * @tz: a #GTimeZone
1475 * @year: the year component of the date
1476 * @month: the month component of the date
1477 * @day: the day component of the date
1478 * @hour: the hour component of the date
1479 * @minute: the minute component of the date
1480 * @seconds: the number of seconds past the minute
1482 * Creates a new #GDateTime corresponding to the given date and time in
1483 * the time zone @tz.
1485 * The @year must be between 1 and 9999, @month between 1 and 12 and @day
1486 * between 1 and 28, 29, 30 or 31 depending on the month and the year.
1488 * @hour must be between 0 and 23 and @minute must be between 0 and 59.
1490 * @seconds must be at least 0.0 and must be strictly less than 60.0.
1491 * It will be rounded down to the nearest microsecond.
1493 * If the given time is not representable in the given time zone (for
1494 * example, 02:30 on March 14th 2010 in Toronto, due to daylight savings
1495 * time) then the time will be rounded up to the nearest existing time
1496 * (in this case, 03:00). If this matters to you then you should verify
1497 * the return value for containing the same as the numbers you gave.
1499 * In the case that the given time is ambiguous in the given time zone
1500 * (for example, 01:30 on November 7th 2010 in Toronto, due to daylight
1501 * savings time) then the time falling within standard (ie:
1502 * non-daylight) time is taken.
1504 * It not considered a programmer error for the values to this function
1505 * to be out of range, but in the case that they are, the function will
1506 * return %NULL.
1508 * You should release the return value by calling g_date_time_unref()
1509 * when you are done with it.
1511 * Returns: a new #GDateTime, or %NULL
1513 * Since: 2.26
1515 GDateTime *
1516 g_date_time_new (GTimeZone *tz,
1517 gint year,
1518 gint month,
1519 gint day,
1520 gint hour,
1521 gint minute,
1522 gdouble seconds)
1524 GDateTime *datetime;
1525 gint64 full_time;
1526 /* keep these variables as volatile. We do not want them ending up in
1527 * registers - them doing so may cause us to hit precision problems on i386.
1528 * See: https://bugzilla.gnome.org/show_bug.cgi?id=792410 */
1529 volatile gint64 usec;
1530 volatile gdouble usecd;
1532 g_return_val_if_fail (tz != NULL, NULL);
1534 if (year < 1 || year > 9999 ||
1535 month < 1 || month > 12 ||
1536 day < 1 || day > days_in_months[GREGORIAN_LEAP (year)][month] ||
1537 hour < 0 || hour > 23 ||
1538 minute < 0 || minute > 59 ||
1539 seconds < 0.0 || seconds >= 60.0)
1540 return NULL;
1542 datetime = g_date_time_alloc (tz);
1543 datetime->days = ymd_to_days (year, month, day);
1544 datetime->usec = (hour * USEC_PER_HOUR)
1545 + (minute * USEC_PER_MINUTE)
1546 + (gint64) (seconds * USEC_PER_SECOND);
1548 full_time = SEC_PER_DAY *
1549 (ymd_to_days (year, month, day) - UNIX_EPOCH_START) +
1550 SECS_PER_HOUR * hour +
1551 SECS_PER_MINUTE * minute +
1552 (int) seconds;
1554 datetime->interval = g_time_zone_adjust_time (datetime->tz,
1555 G_TIME_TYPE_STANDARD,
1556 &full_time);
1558 /* This is the correct way to convert a scaled FP value to integer.
1559 * If this surprises you, please observe that (int)(1.000001 * 1e6)
1560 * is 1000000. This is not a problem with precision, it's just how
1561 * FP numbers work.
1562 * See https://bugzilla.gnome.org/show_bug.cgi?id=697715. */
1563 usec = seconds * USEC_PER_SECOND;
1564 usecd = (usec + 1) * 1e-6;
1565 if (usecd <= seconds) {
1566 usec++;
1569 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
1570 datetime->days = full_time / SEC_PER_DAY;
1571 datetime->usec = (full_time % SEC_PER_DAY) * USEC_PER_SECOND;
1572 datetime->usec += usec % USEC_PER_SECOND;
1574 return datetime;
1578 * g_date_time_new_local:
1579 * @year: the year component of the date
1580 * @month: the month component of the date
1581 * @day: the day component of the date
1582 * @hour: the hour component of the date
1583 * @minute: the minute component of the date
1584 * @seconds: the number of seconds past the minute
1586 * Creates a new #GDateTime corresponding to the given date and time in
1587 * the local time zone.
1589 * This call is equivalent to calling g_date_time_new() with the time
1590 * zone returned by g_time_zone_new_local().
1592 * Returns: a #GDateTime, or %NULL
1594 * Since: 2.26
1596 GDateTime *
1597 g_date_time_new_local (gint year,
1598 gint month,
1599 gint day,
1600 gint hour,
1601 gint minute,
1602 gdouble seconds)
1604 GDateTime *datetime;
1605 GTimeZone *local;
1607 local = g_time_zone_new_local ();
1608 datetime = g_date_time_new (local, year, month, day, hour, minute, seconds);
1609 g_time_zone_unref (local);
1611 return datetime;
1615 * g_date_time_new_utc:
1616 * @year: the year component of the date
1617 * @month: the month component of the date
1618 * @day: the day component of the date
1619 * @hour: the hour component of the date
1620 * @minute: the minute component of the date
1621 * @seconds: the number of seconds past the minute
1623 * Creates a new #GDateTime corresponding to the given date and time in
1624 * UTC.
1626 * This call is equivalent to calling g_date_time_new() with the time
1627 * zone returned by g_time_zone_new_utc().
1629 * Returns: a #GDateTime, or %NULL
1631 * Since: 2.26
1633 GDateTime *
1634 g_date_time_new_utc (gint year,
1635 gint month,
1636 gint day,
1637 gint hour,
1638 gint minute,
1639 gdouble seconds)
1641 GDateTime *datetime;
1642 GTimeZone *utc;
1644 utc = g_time_zone_new_utc ();
1645 datetime = g_date_time_new (utc, year, month, day, hour, minute, seconds);
1646 g_time_zone_unref (utc);
1648 return datetime;
1651 /* Adders {{{1 */
1654 * g_date_time_add:
1655 * @datetime: a #GDateTime
1656 * @timespan: a #GTimeSpan
1658 * Creates a copy of @datetime and adds the specified timespan to the copy.
1660 * Returns: the newly created #GDateTime which should be freed with
1661 * g_date_time_unref().
1663 * Since: 2.26
1665 GDateTime*
1666 g_date_time_add (GDateTime *datetime,
1667 GTimeSpan timespan)
1669 return g_date_time_from_instant (datetime->tz, timespan +
1670 g_date_time_to_instant (datetime));
1674 * g_date_time_add_years:
1675 * @datetime: a #GDateTime
1676 * @years: the number of years
1678 * Creates a copy of @datetime and adds the specified number of years to the
1679 * copy. Add negative values to subtract years.
1681 * As with g_date_time_add_months(), if the resulting date would be 29th
1682 * February on a non-leap year, the day will be clamped to 28th February.
1684 * Returns: the newly created #GDateTime which should be freed with
1685 * g_date_time_unref().
1687 * Since: 2.26
1689 GDateTime *
1690 g_date_time_add_years (GDateTime *datetime,
1691 gint years)
1693 gint year, month, day;
1695 g_return_val_if_fail (datetime != NULL, NULL);
1697 if (years < -10000 || years > 10000)
1698 return NULL;
1700 g_date_time_get_ymd (datetime, &year, &month, &day);
1701 year += years;
1703 /* only possible issue is if we've entered a year with no February 29
1705 if (month == 2 && day == 29 && !GREGORIAN_LEAP (year))
1706 day = 28;
1708 return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
1712 * g_date_time_add_months:
1713 * @datetime: a #GDateTime
1714 * @months: the number of months
1716 * Creates a copy of @datetime and adds the specified number of months to the
1717 * copy. Add negative values to subtract months.
1719 * The day of the month of the resulting #GDateTime is clamped to the number
1720 * of days in the updated calendar month. For example, if adding 1 month to
1721 * 31st January 2018, the result would be 28th February 2018. In 2020 (a leap
1722 * year), the result would be 29th February.
1724 * Returns: the newly created #GDateTime which should be freed with
1725 * g_date_time_unref().
1727 * Since: 2.26
1729 GDateTime*
1730 g_date_time_add_months (GDateTime *datetime,
1731 gint months)
1733 gint year, month, day;
1735 g_return_val_if_fail (datetime != NULL, NULL);
1736 g_date_time_get_ymd (datetime, &year, &month, &day);
1738 if (months < -120000 || months > 120000)
1739 return NULL;
1741 year += months / 12;
1742 month += months % 12;
1743 if (month < 1)
1745 month += 12;
1746 year--;
1748 else if (month > 12)
1750 month -= 12;
1751 year++;
1754 day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
1756 return g_date_time_replace_days (datetime, ymd_to_days (year, month, day));
1760 * g_date_time_add_weeks:
1761 * @datetime: a #GDateTime
1762 * @weeks: the number of weeks
1764 * Creates a copy of @datetime and adds the specified number of weeks to the
1765 * copy. Add negative values to subtract weeks.
1767 * Returns: the newly created #GDateTime which should be freed with
1768 * g_date_time_unref().
1770 * Since: 2.26
1772 GDateTime*
1773 g_date_time_add_weeks (GDateTime *datetime,
1774 gint weeks)
1776 g_return_val_if_fail (datetime != NULL, NULL);
1778 return g_date_time_add_days (datetime, weeks * 7);
1782 * g_date_time_add_days:
1783 * @datetime: a #GDateTime
1784 * @days: the number of days
1786 * Creates a copy of @datetime and adds the specified number of days to the
1787 * copy. Add negative values to subtract days.
1789 * Returns: the newly created #GDateTime which should be freed with
1790 * g_date_time_unref().
1792 * Since: 2.26
1794 GDateTime*
1795 g_date_time_add_days (GDateTime *datetime,
1796 gint days)
1798 g_return_val_if_fail (datetime != NULL, NULL);
1800 if (days < -3660000 || days > 3660000)
1801 return NULL;
1803 return g_date_time_replace_days (datetime, datetime->days + days);
1807 * g_date_time_add_hours:
1808 * @datetime: a #GDateTime
1809 * @hours: the number of hours to add
1811 * Creates a copy of @datetime and adds the specified number of hours.
1812 * Add negative values to subtract hours.
1814 * Returns: the newly created #GDateTime which should be freed with
1815 * g_date_time_unref().
1817 * Since: 2.26
1819 GDateTime*
1820 g_date_time_add_hours (GDateTime *datetime,
1821 gint hours)
1823 return g_date_time_add (datetime, hours * USEC_PER_HOUR);
1827 * g_date_time_add_minutes:
1828 * @datetime: a #GDateTime
1829 * @minutes: the number of minutes to add
1831 * Creates a copy of @datetime adding the specified number of minutes.
1832 * Add negative values to subtract minutes.
1834 * Returns: the newly created #GDateTime which should be freed with
1835 * g_date_time_unref().
1837 * Since: 2.26
1839 GDateTime*
1840 g_date_time_add_minutes (GDateTime *datetime,
1841 gint minutes)
1843 return g_date_time_add (datetime, minutes * USEC_PER_MINUTE);
1848 * g_date_time_add_seconds:
1849 * @datetime: a #GDateTime
1850 * @seconds: the number of seconds to add
1852 * Creates a copy of @datetime and adds the specified number of seconds.
1853 * Add negative values to subtract seconds.
1855 * Returns: the newly created #GDateTime which should be freed with
1856 * g_date_time_unref().
1858 * Since: 2.26
1860 GDateTime*
1861 g_date_time_add_seconds (GDateTime *datetime,
1862 gdouble seconds)
1864 return g_date_time_add (datetime, seconds * USEC_PER_SECOND);
1868 * g_date_time_add_full:
1869 * @datetime: a #GDateTime
1870 * @years: the number of years to add
1871 * @months: the number of months to add
1872 * @days: the number of days to add
1873 * @hours: the number of hours to add
1874 * @minutes: the number of minutes to add
1875 * @seconds: the number of seconds to add
1877 * Creates a new #GDateTime adding the specified values to the current date and
1878 * time in @datetime. Add negative values to subtract.
1880 * Returns: the newly created #GDateTime that should be freed with
1881 * g_date_time_unref().
1883 * Since: 2.26
1885 GDateTime *
1886 g_date_time_add_full (GDateTime *datetime,
1887 gint years,
1888 gint months,
1889 gint days,
1890 gint hours,
1891 gint minutes,
1892 gdouble seconds)
1894 gint year, month, day;
1895 gint64 full_time;
1896 GDateTime *new;
1897 gint interval;
1899 g_return_val_if_fail (datetime != NULL, NULL);
1900 g_date_time_get_ymd (datetime, &year, &month, &day);
1902 months += years * 12;
1904 if (months < -120000 || months > 120000)
1905 return NULL;
1907 if (days < -3660000 || days > 3660000)
1908 return NULL;
1910 year += months / 12;
1911 month += months % 12;
1912 if (month < 1)
1914 month += 12;
1915 year--;
1917 else if (month > 12)
1919 month -= 12;
1920 year++;
1923 day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]);
1925 /* full_time is now in unix (local) time */
1926 full_time = datetime->usec / USEC_PER_SECOND + SEC_PER_DAY *
1927 (ymd_to_days (year, month, day) + days - UNIX_EPOCH_START);
1929 interval = g_time_zone_adjust_time (datetime->tz,
1930 g_time_zone_is_dst (datetime->tz,
1931 datetime->interval),
1932 &full_time);
1934 /* move to UTC unix time */
1935 full_time -= g_time_zone_get_offset (datetime->tz, interval);
1937 /* convert back to an instant, add back fractional seconds */
1938 full_time += UNIX_EPOCH_START * SEC_PER_DAY;
1939 full_time = full_time * USEC_PER_SECOND +
1940 datetime->usec % USEC_PER_SECOND;
1942 /* do the actual addition now */
1943 full_time += (hours * USEC_PER_HOUR) +
1944 (minutes * USEC_PER_MINUTE) +
1945 (gint64) (seconds * USEC_PER_SECOND);
1947 /* find the new interval */
1948 interval = g_time_zone_find_interval (datetime->tz,
1949 G_TIME_TYPE_UNIVERSAL,
1950 INSTANT_TO_UNIX (full_time));
1952 /* convert back into local time */
1953 full_time += USEC_PER_SECOND *
1954 g_time_zone_get_offset (datetime->tz, interval);
1956 /* split into days and usec of a new datetime */
1957 new = g_date_time_alloc (datetime->tz);
1958 new->interval = interval;
1959 new->days = full_time / USEC_PER_DAY;
1960 new->usec = full_time % USEC_PER_DAY;
1962 /* XXX validate */
1964 return new;
1967 /* Compare, difference, hash, equal {{{1 */
1969 * g_date_time_compare:
1970 * @dt1: (not nullable): first #GDateTime to compare
1971 * @dt2: (not nullable): second #GDateTime to compare
1973 * A comparison function for #GDateTimes that is suitable
1974 * as a #GCompareFunc. Both #GDateTimes must be non-%NULL.
1976 * Returns: -1, 0 or 1 if @dt1 is less than, equal to or greater
1977 * than @dt2.
1979 * Since: 2.26
1981 gint
1982 g_date_time_compare (gconstpointer dt1,
1983 gconstpointer dt2)
1985 gint64 difference;
1987 difference = g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2);
1989 if (difference < 0)
1990 return -1;
1992 else if (difference > 0)
1993 return 1;
1995 else
1996 return 0;
2000 * g_date_time_difference:
2001 * @end: a #GDateTime
2002 * @begin: a #GDateTime
2004 * Calculates the difference in time between @end and @begin. The
2005 * #GTimeSpan that is returned is effectively @end - @begin (ie:
2006 * positive if the first parameter is larger).
2008 * Returns: the difference between the two #GDateTime, as a time
2009 * span expressed in microseconds.
2011 * Since: 2.26
2013 GTimeSpan
2014 g_date_time_difference (GDateTime *end,
2015 GDateTime *begin)
2017 g_return_val_if_fail (begin != NULL, 0);
2018 g_return_val_if_fail (end != NULL, 0);
2020 return g_date_time_to_instant (end) -
2021 g_date_time_to_instant (begin);
2025 * g_date_time_hash:
2026 * @datetime: (not nullable): a #GDateTime
2028 * Hashes @datetime into a #guint, suitable for use within #GHashTable.
2030 * Returns: a #guint containing the hash
2032 * Since: 2.26
2034 guint
2035 g_date_time_hash (gconstpointer datetime)
2037 return g_date_time_to_instant ((GDateTime *) datetime);
2041 * g_date_time_equal:
2042 * @dt1: (not nullable): a #GDateTime
2043 * @dt2: (not nullable): a #GDateTime
2045 * Checks to see if @dt1 and @dt2 are equal.
2047 * Equal here means that they represent the same moment after converting
2048 * them to the same time zone.
2050 * Returns: %TRUE if @dt1 and @dt2 are equal
2052 * Since: 2.26
2054 gboolean
2055 g_date_time_equal (gconstpointer dt1,
2056 gconstpointer dt2)
2058 return g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2) == 0;
2061 /* Year, Month, Day Getters {{{1 */
2063 * g_date_time_get_ymd:
2064 * @datetime: a #GDateTime.
2065 * @year: (out) (optional): the return location for the gregorian year, or %NULL.
2066 * @month: (out) (optional): the return location for the month of the year, or %NULL.
2067 * @day: (out) (optional): the return location for the day of the month, or %NULL.
2069 * Retrieves the Gregorian day, month, and year of a given #GDateTime.
2071 * Since: 2.26
2073 void
2074 g_date_time_get_ymd (GDateTime *datetime,
2075 gint *year,
2076 gint *month,
2077 gint *day)
2079 gint the_year;
2080 gint the_month;
2081 gint the_day;
2082 gint remaining_days;
2083 gint y100_cycles;
2084 gint y4_cycles;
2085 gint y1_cycles;
2086 gint preceding;
2087 gboolean leap;
2089 g_return_if_fail (datetime != NULL);
2091 remaining_days = datetime->days;
2094 * We need to convert an offset in days to its year/month/day representation.
2095 * Leap years makes this a little trickier than it should be, so we use
2096 * 400, 100 and 4 years cycles here to get to the correct year.
2099 /* Our days offset starts sets 0001-01-01 as day 1, if it was day 0 our
2100 * math would be simpler, so let's do it */
2101 remaining_days--;
2103 the_year = (remaining_days / DAYS_IN_400YEARS) * 400 + 1;
2104 remaining_days = remaining_days % DAYS_IN_400YEARS;
2106 y100_cycles = remaining_days / DAYS_IN_100YEARS;
2107 remaining_days = remaining_days % DAYS_IN_100YEARS;
2108 the_year += y100_cycles * 100;
2110 y4_cycles = remaining_days / DAYS_IN_4YEARS;
2111 remaining_days = remaining_days % DAYS_IN_4YEARS;
2112 the_year += y4_cycles * 4;
2114 y1_cycles = remaining_days / 365;
2115 the_year += y1_cycles;
2116 remaining_days = remaining_days % 365;
2118 if (y1_cycles == 4 || y100_cycles == 4) {
2119 g_assert (remaining_days == 0);
2121 /* special case that indicates that the date is actually one year before,
2122 * in the 31th of December */
2123 the_year--;
2124 the_month = 12;
2125 the_day = 31;
2126 goto end;
2129 /* now get the month and the day */
2130 leap = y1_cycles == 3 && (y4_cycles != 24 || y100_cycles == 3);
2132 g_assert (leap == GREGORIAN_LEAP(the_year));
2134 the_month = (remaining_days + 50) >> 5;
2135 preceding = (days_in_year[0][the_month - 1] + (the_month > 2 && leap));
2136 if (preceding > remaining_days)
2138 /* estimate is too large */
2139 the_month -= 1;
2140 preceding -= leap ? days_in_months[1][the_month]
2141 : days_in_months[0][the_month];
2144 remaining_days -= preceding;
2145 g_assert(0 <= remaining_days);
2147 the_day = remaining_days + 1;
2149 end:
2150 if (year)
2151 *year = the_year;
2152 if (month)
2153 *month = the_month;
2154 if (day)
2155 *day = the_day;
2159 * g_date_time_get_year:
2160 * @datetime: A #GDateTime
2162 * Retrieves the year represented by @datetime in the Gregorian calendar.
2164 * Returns: the year represented by @datetime
2166 * Since: 2.26
2168 gint
2169 g_date_time_get_year (GDateTime *datetime)
2171 gint year;
2173 g_return_val_if_fail (datetime != NULL, 0);
2175 g_date_time_get_ymd (datetime, &year, NULL, NULL);
2177 return year;
2181 * g_date_time_get_month:
2182 * @datetime: a #GDateTime
2184 * Retrieves the month of the year represented by @datetime in the Gregorian
2185 * calendar.
2187 * Returns: the month represented by @datetime
2189 * Since: 2.26
2191 gint
2192 g_date_time_get_month (GDateTime *datetime)
2194 gint month;
2196 g_return_val_if_fail (datetime != NULL, 0);
2198 g_date_time_get_ymd (datetime, NULL, &month, NULL);
2200 return month;
2204 * g_date_time_get_day_of_month:
2205 * @datetime: a #GDateTime
2207 * Retrieves the day of the month represented by @datetime in the gregorian
2208 * calendar.
2210 * Returns: the day of the month
2212 * Since: 2.26
2214 gint
2215 g_date_time_get_day_of_month (GDateTime *datetime)
2217 gint day_of_year,
2219 const guint16 *days;
2220 guint16 last = 0;
2222 g_return_val_if_fail (datetime != NULL, 0);
2224 days = days_in_year[GREGORIAN_LEAP (g_date_time_get_year (datetime)) ? 1 : 0];
2225 g_date_time_get_week_number (datetime, NULL, NULL, &day_of_year);
2227 for (i = 1; i <= 12; i++)
2229 if (days [i] >= day_of_year)
2230 return day_of_year - last;
2231 last = days [i];
2234 g_warn_if_reached ();
2235 return 0;
2238 /* Week of year / day of week getters {{{1 */
2240 * g_date_time_get_week_numbering_year:
2241 * @datetime: a #GDateTime
2243 * Returns the ISO 8601 week-numbering year in which the week containing
2244 * @datetime falls.
2246 * This function, taken together with g_date_time_get_week_of_year() and
2247 * g_date_time_get_day_of_week() can be used to determine the full ISO
2248 * week date on which @datetime falls.
2250 * This is usually equal to the normal Gregorian year (as returned by
2251 * g_date_time_get_year()), except as detailed below:
2253 * For Thursday, the week-numbering year is always equal to the usual
2254 * calendar year. For other days, the number is such that every day
2255 * within a complete week (Monday to Sunday) is contained within the
2256 * same week-numbering year.
2258 * For Monday, Tuesday and Wednesday occurring near the end of the year,
2259 * this may mean that the week-numbering year is one greater than the
2260 * calendar year (so that these days have the same week-numbering year
2261 * as the Thursday occurring early in the next year).
2263 * For Friday, Saturday and Sunday occurring near the start of the year,
2264 * this may mean that the week-numbering year is one less than the
2265 * calendar year (so that these days have the same week-numbering year
2266 * as the Thursday occurring late in the previous year).
2268 * An equivalent description is that the week-numbering year is equal to
2269 * the calendar year containing the majority of the days in the current
2270 * week (Monday to Sunday).
2272 * Note that January 1 0001 in the proleptic Gregorian calendar is a
2273 * Monday, so this function never returns 0.
2275 * Returns: the ISO 8601 week-numbering year for @datetime
2277 * Since: 2.26
2279 gint
2280 g_date_time_get_week_numbering_year (GDateTime *datetime)
2282 gint year, month, day, weekday;
2284 g_date_time_get_ymd (datetime, &year, &month, &day);
2285 weekday = g_date_time_get_day_of_week (datetime);
2287 /* January 1, 2, 3 might be in the previous year if they occur after
2288 * Thursday.
2290 * Jan 1: Friday, Saturday, Sunday => day 1: weekday 5, 6, 7
2291 * Jan 2: Saturday, Sunday => day 2: weekday 6, 7
2292 * Jan 3: Sunday => day 3: weekday 7
2294 * So we have a special case if (day - weekday) <= -4
2296 if (month == 1 && (day - weekday) <= -4)
2297 return year - 1;
2299 /* December 29, 30, 31 might be in the next year if they occur before
2300 * Thursday.
2302 * Dec 31: Monday, Tuesday, Wednesday => day 31: weekday 1, 2, 3
2303 * Dec 30: Monday, Tuesday => day 30: weekday 1, 2
2304 * Dec 29: Monday => day 29: weekday 1
2306 * So we have a special case if (day - weekday) >= 28
2308 else if (month == 12 && (day - weekday) >= 28)
2309 return year + 1;
2311 else
2312 return year;
2316 * g_date_time_get_week_of_year:
2317 * @datetime: a #GDateTime
2319 * Returns the ISO 8601 week number for the week containing @datetime.
2320 * The ISO 8601 week number is the same for every day of the week (from
2321 * Moday through Sunday). That can produce some unusual results
2322 * (described below).
2324 * The first week of the year is week 1. This is the week that contains
2325 * the first Thursday of the year. Equivalently, this is the first week
2326 * that has more than 4 of its days falling within the calendar year.
2328 * The value 0 is never returned by this function. Days contained
2329 * within a year but occurring before the first ISO 8601 week of that
2330 * year are considered as being contained in the last week of the
2331 * previous year. Similarly, the final days of a calendar year may be
2332 * considered as being part of the first ISO 8601 week of the next year
2333 * if 4 or more days of that week are contained within the new year.
2335 * Returns: the ISO 8601 week number for @datetime.
2337 * Since: 2.26
2339 gint
2340 g_date_time_get_week_of_year (GDateTime *datetime)
2342 gint weeknum;
2344 g_return_val_if_fail (datetime != NULL, 0);
2346 g_date_time_get_week_number (datetime, &weeknum, NULL, NULL);
2348 return weeknum;
2352 * g_date_time_get_day_of_week:
2353 * @datetime: a #GDateTime
2355 * Retrieves the ISO 8601 day of the week on which @datetime falls (1 is
2356 * Monday, 2 is Tuesday... 7 is Sunday).
2358 * Returns: the day of the week
2360 * Since: 2.26
2362 gint
2363 g_date_time_get_day_of_week (GDateTime *datetime)
2365 g_return_val_if_fail (datetime != NULL, 0);
2367 return (datetime->days - 1) % 7 + 1;
2370 /* Day of year getter {{{1 */
2372 * g_date_time_get_day_of_year:
2373 * @datetime: a #GDateTime
2375 * Retrieves the day of the year represented by @datetime in the Gregorian
2376 * calendar.
2378 * Returns: the day of the year
2380 * Since: 2.26
2382 gint
2383 g_date_time_get_day_of_year (GDateTime *datetime)
2385 gint doy = 0;
2387 g_return_val_if_fail (datetime != NULL, 0);
2389 g_date_time_get_week_number (datetime, NULL, NULL, &doy);
2390 return doy;
2393 /* Time component getters {{{1 */
2396 * g_date_time_get_hour:
2397 * @datetime: a #GDateTime
2399 * Retrieves the hour of the day represented by @datetime
2401 * Returns: the hour of the day
2403 * Since: 2.26
2405 gint
2406 g_date_time_get_hour (GDateTime *datetime)
2408 g_return_val_if_fail (datetime != NULL, 0);
2410 return (datetime->usec / USEC_PER_HOUR);
2414 * g_date_time_get_minute:
2415 * @datetime: a #GDateTime
2417 * Retrieves the minute of the hour represented by @datetime
2419 * Returns: the minute of the hour
2421 * Since: 2.26
2423 gint
2424 g_date_time_get_minute (GDateTime *datetime)
2426 g_return_val_if_fail (datetime != NULL, 0);
2428 return (datetime->usec % USEC_PER_HOUR) / USEC_PER_MINUTE;
2432 * g_date_time_get_second:
2433 * @datetime: a #GDateTime
2435 * Retrieves the second of the minute represented by @datetime
2437 * Returns: the second represented by @datetime
2439 * Since: 2.26
2441 gint
2442 g_date_time_get_second (GDateTime *datetime)
2444 g_return_val_if_fail (datetime != NULL, 0);
2446 return (datetime->usec % USEC_PER_MINUTE) / USEC_PER_SECOND;
2450 * g_date_time_get_microsecond:
2451 * @datetime: a #GDateTime
2453 * Retrieves the microsecond of the date represented by @datetime
2455 * Returns: the microsecond of the second
2457 * Since: 2.26
2459 gint
2460 g_date_time_get_microsecond (GDateTime *datetime)
2462 g_return_val_if_fail (datetime != NULL, 0);
2464 return (datetime->usec % USEC_PER_SECOND);
2468 * g_date_time_get_seconds:
2469 * @datetime: a #GDateTime
2471 * Retrieves the number of seconds since the start of the last minute,
2472 * including the fractional part.
2474 * Returns: the number of seconds
2476 * Since: 2.26
2478 gdouble
2479 g_date_time_get_seconds (GDateTime *datetime)
2481 g_return_val_if_fail (datetime != NULL, 0);
2483 return (datetime->usec % USEC_PER_MINUTE) / 1000000.0;
2486 /* Exporters {{{1 */
2488 * g_date_time_to_unix:
2489 * @datetime: a #GDateTime
2491 * Gives the Unix time corresponding to @datetime, rounding down to the
2492 * nearest second.
2494 * Unix time is the number of seconds that have elapsed since 1970-01-01
2495 * 00:00:00 UTC, regardless of the time zone associated with @datetime.
2497 * Returns: the Unix time corresponding to @datetime
2499 * Since: 2.26
2501 gint64
2502 g_date_time_to_unix (GDateTime *datetime)
2504 return INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
2508 * g_date_time_to_timeval:
2509 * @datetime: a #GDateTime
2510 * @tv: a #GTimeVal to modify
2512 * Stores the instant in time that @datetime represents into @tv.
2514 * The time contained in a #GTimeVal is always stored in the form of
2515 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the time
2516 * zone associated with @datetime.
2518 * On systems where 'long' is 32bit (ie: all 32bit systems and all
2519 * Windows systems), a #GTimeVal is incapable of storing the entire
2520 * range of values that #GDateTime is capable of expressing. On those
2521 * systems, this function returns %FALSE to indicate that the time is
2522 * out of range.
2524 * On systems where 'long' is 64bit, this function never fails.
2526 * Returns: %TRUE if successful, else %FALSE
2528 * Since: 2.26
2530 gboolean
2531 g_date_time_to_timeval (GDateTime *datetime,
2532 GTimeVal *tv)
2534 tv->tv_sec = INSTANT_TO_UNIX (g_date_time_to_instant (datetime));
2535 tv->tv_usec = datetime->usec % USEC_PER_SECOND;
2537 return TRUE;
2540 /* Timezone queries {{{1 */
2542 * g_date_time_get_utc_offset:
2543 * @datetime: a #GDateTime
2545 * Determines the offset to UTC in effect at the time and in the time
2546 * zone of @datetime.
2548 * The offset is the number of microseconds that you add to UTC time to
2549 * arrive at local time for the time zone (ie: negative numbers for time
2550 * zones west of GMT, positive numbers for east).
2552 * If @datetime represents UTC time, then the offset is always zero.
2554 * Returns: the number of microseconds that should be added to UTC to
2555 * get the local time
2557 * Since: 2.26
2559 GTimeSpan
2560 g_date_time_get_utc_offset (GDateTime *datetime)
2562 gint offset;
2564 g_return_val_if_fail (datetime != NULL, 0);
2566 offset = g_time_zone_get_offset (datetime->tz, datetime->interval);
2568 return (gint64) offset * USEC_PER_SECOND;
2572 * g_date_time_get_timezone:
2573 * @datetime: a #GDateTime
2575 * Get the time zone for this @datetime.
2577 * Returns: (transfer none): the time zone
2578 * Since: 2.58
2580 GTimeZone *
2581 g_date_time_get_timezone (GDateTime *datetime)
2583 g_return_val_if_fail (datetime != NULL, NULL);
2585 g_assert (datetime->tz != NULL);
2586 return datetime->tz;
2590 * g_date_time_get_timezone_abbreviation:
2591 * @datetime: a #GDateTime
2593 * Determines the time zone abbreviation to be used at the time and in
2594 * the time zone of @datetime.
2596 * For example, in Toronto this is currently "EST" during the winter
2597 * months and "EDT" during the summer months when daylight savings
2598 * time is in effect.
2600 * Returns: (transfer none): the time zone abbreviation. The returned
2601 * string is owned by the #GDateTime and it should not be
2602 * modified or freed
2604 * Since: 2.26
2606 const gchar *
2607 g_date_time_get_timezone_abbreviation (GDateTime *datetime)
2609 g_return_val_if_fail (datetime != NULL, NULL);
2611 return g_time_zone_get_abbreviation (datetime->tz, datetime->interval);
2615 * g_date_time_is_daylight_savings:
2616 * @datetime: a #GDateTime
2618 * Determines if daylight savings time is in effect at the time and in
2619 * the time zone of @datetime.
2621 * Returns: %TRUE if daylight savings time is in effect
2623 * Since: 2.26
2625 gboolean
2626 g_date_time_is_daylight_savings (GDateTime *datetime)
2628 g_return_val_if_fail (datetime != NULL, FALSE);
2630 return g_time_zone_is_dst (datetime->tz, datetime->interval);
2633 /* Timezone convert {{{1 */
2635 * g_date_time_to_timezone:
2636 * @datetime: a #GDateTime
2637 * @tz: the new #GTimeZone
2639 * Create a new #GDateTime corresponding to the same instant in time as
2640 * @datetime, but in the time zone @tz.
2642 * This call can fail in the case that the time goes out of bounds. For
2643 * example, converting 0001-01-01 00:00:00 UTC to a time zone west of
2644 * Greenwich will fail (due to the year 0 being out of range).
2646 * You should release the return value by calling g_date_time_unref()
2647 * when you are done with it.
2649 * Returns: a new #GDateTime, or %NULL
2651 * Since: 2.26
2653 GDateTime *
2654 g_date_time_to_timezone (GDateTime *datetime,
2655 GTimeZone *tz)
2657 return g_date_time_from_instant (tz, g_date_time_to_instant (datetime));
2661 * g_date_time_to_local:
2662 * @datetime: a #GDateTime
2664 * Creates a new #GDateTime corresponding to the same instant in time as
2665 * @datetime, but in the local time zone.
2667 * This call is equivalent to calling g_date_time_to_timezone() with the
2668 * time zone returned by g_time_zone_new_local().
2670 * Returns: the newly created #GDateTime
2672 * Since: 2.26
2674 GDateTime *
2675 g_date_time_to_local (GDateTime *datetime)
2677 GDateTime *new;
2678 GTimeZone *local;
2680 local = g_time_zone_new_local ();
2681 new = g_date_time_to_timezone (datetime, local);
2682 g_time_zone_unref (local);
2684 return new;
2688 * g_date_time_to_utc:
2689 * @datetime: a #GDateTime
2691 * Creates a new #GDateTime corresponding to the same instant in time as
2692 * @datetime, but in UTC.
2694 * This call is equivalent to calling g_date_time_to_timezone() with the
2695 * time zone returned by g_time_zone_new_utc().
2697 * Returns: the newly created #GDateTime
2699 * Since: 2.26
2701 GDateTime *
2702 g_date_time_to_utc (GDateTime *datetime)
2704 GDateTime *new;
2705 GTimeZone *utc;
2707 utc = g_time_zone_new_utc ();
2708 new = g_date_time_to_timezone (datetime, utc);
2709 g_time_zone_unref (utc);
2711 return new;
2714 /* Format {{{1 */
2716 static gboolean
2717 format_z (GString *outstr,
2718 gint offset,
2719 guint colons)
2721 gint hours;
2722 gint minutes;
2723 gint seconds;
2725 hours = offset / 3600;
2726 minutes = ABS (offset) / 60 % 60;
2727 seconds = ABS (offset) % 60;
2729 switch (colons)
2731 case 0:
2732 g_string_append_printf (outstr, "%+03d%02d",
2733 hours,
2734 minutes);
2735 break;
2737 case 1:
2738 g_string_append_printf (outstr, "%+03d:%02d",
2739 hours,
2740 minutes);
2741 break;
2743 case 2:
2744 g_string_append_printf (outstr, "%+03d:%02d:%02d",
2745 hours,
2746 minutes,
2747 seconds);
2748 break;
2750 case 3:
2751 g_string_append_printf (outstr, "%+03d", hours);
2753 if (minutes != 0 || seconds != 0)
2755 g_string_append_printf (outstr, ":%02d", minutes);
2757 if (seconds != 0)
2758 g_string_append_printf (outstr, ":%02d", seconds);
2760 break;
2762 default:
2763 return FALSE;
2766 return TRUE;
2769 static void
2770 format_number (GString *str,
2771 gboolean use_alt_digits,
2772 const gchar *pad,
2773 gint width,
2774 guint32 number)
2776 const gchar *ascii_digits[10] = {
2777 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
2779 const gchar **digits = ascii_digits;
2780 const gchar *tmp[10];
2781 gint i = 0;
2783 g_return_if_fail (width <= 10);
2785 #ifdef HAVE_LANGINFO_OUTDIGIT
2786 if (use_alt_digits)
2788 static const gchar *alt_digits[10];
2789 static gsize initialised;
2790 /* 2^32 has 10 digits */
2792 if G_UNLIKELY (g_once_init_enter (&initialised))
2794 #define DO_DIGIT(n) \
2795 alt_digits[n] = nl_langinfo (_NL_CTYPE_OUTDIGIT## n ##_MB)
2796 DO_DIGIT(0); DO_DIGIT(1); DO_DIGIT(2); DO_DIGIT(3); DO_DIGIT(4);
2797 DO_DIGIT(5); DO_DIGIT(6); DO_DIGIT(7); DO_DIGIT(8); DO_DIGIT(9);
2798 #undef DO_DIGIT
2799 g_once_init_leave (&initialised, TRUE);
2802 digits = alt_digits;
2804 #endif /* HAVE_LANGINFO_OUTDIGIT */
2808 tmp[i++] = digits[number % 10];
2809 number /= 10;
2811 while (number);
2813 while (pad && i < width)
2814 tmp[i++] = *pad == '0' ? digits[0] : pad;
2816 /* should really be impossible */
2817 g_assert (i <= 10);
2819 while (i)
2820 g_string_append (str, tmp[--i]);
2823 static gboolean
2824 format_ampm (GDateTime *datetime,
2825 GString *outstr,
2826 gboolean locale_is_utf8,
2827 gboolean uppercase)
2829 const gchar *ampm;
2830 gchar *tmp = NULL, *ampm_dup;
2831 gsize len;
2833 ampm = GET_AMPM (datetime);
2835 if (!ampm || ampm[0] == '\0')
2836 ampm = get_fallback_ampm (g_date_time_get_hour (datetime));
2838 if (!locale_is_utf8 && GET_AMPM_IS_LOCALE)
2840 /* This assumes that locale encoding can't have embedded NULs */
2841 ampm = tmp = g_locale_to_utf8 (ampm, -1, NULL, NULL, NULL);
2842 if (!tmp)
2843 return FALSE;
2845 if (uppercase)
2846 ampm_dup = g_utf8_strup (ampm, -1);
2847 else
2848 ampm_dup = g_utf8_strdown (ampm, -1);
2849 len = strlen (ampm_dup);
2850 if (!locale_is_utf8 && GET_AMPM_IS_LOCALE)
2852 g_free (tmp);
2853 tmp = g_locale_from_utf8 (ampm_dup, -1, NULL, &len, NULL);
2854 g_free (ampm_dup);
2855 if (!tmp)
2856 return FALSE;
2857 ampm_dup = tmp;
2859 g_string_append_len (outstr, ampm_dup, len);
2860 g_free (ampm_dup);
2862 return TRUE;
2865 static gboolean g_date_time_format_locale (GDateTime *datetime,
2866 const gchar *format,
2867 GString *outstr,
2868 gboolean locale_is_utf8);
2870 /* g_date_time_format() subroutine that takes a locale-encoded format
2871 * string and produces a locale-encoded date/time string.
2873 static gboolean
2874 g_date_time_locale_format_locale (GDateTime *datetime,
2875 const gchar *format,
2876 GString *outstr,
2877 gboolean locale_is_utf8)
2879 gchar *utf8_format;
2880 gboolean success;
2882 if (locale_is_utf8)
2883 return g_date_time_format_locale (datetime, format, outstr,
2884 locale_is_utf8);
2886 utf8_format = g_locale_to_utf8 (format, -1, NULL, NULL, NULL);
2887 if (!utf8_format)
2888 return FALSE;
2890 success = g_date_time_format_locale (datetime, utf8_format, outstr,
2891 locale_is_utf8);
2892 g_free (utf8_format);
2893 return success;
2896 /* g_date_time_format() subroutine that takes a UTF-8 format
2897 * string and produces a locale-encoded date/time string.
2899 static gboolean
2900 g_date_time_format_locale (GDateTime *datetime,
2901 const gchar *format,
2902 GString *outstr,
2903 gboolean locale_is_utf8)
2905 guint len;
2906 guint colons;
2907 gchar *tmp;
2908 gsize tmp_len;
2909 gunichar c;
2910 gboolean alt_digits = FALSE;
2911 gboolean pad_set = FALSE;
2912 const gchar *pad = "";
2913 const gchar *name;
2914 const gchar *tz;
2916 while (*format)
2918 len = strcspn (format, "%");
2919 if (len)
2921 if (locale_is_utf8)
2922 g_string_append_len (outstr, format, len);
2923 else
2925 tmp = g_locale_from_utf8 (format, len, NULL, &tmp_len, NULL);
2926 if (!tmp)
2927 return FALSE;
2928 g_string_append_len (outstr, tmp, tmp_len);
2929 g_free (tmp);
2933 format += len;
2934 if (!*format)
2935 break;
2937 g_assert (*format == '%');
2938 format++;
2939 if (!*format)
2940 break;
2942 colons = 0;
2943 alt_digits = FALSE;
2944 pad_set = FALSE;
2946 next_mod:
2947 c = g_utf8_get_char (format);
2948 format = g_utf8_next_char (format);
2949 switch (c)
2951 case 'a':
2952 name = WEEKDAY_ABBR (datetime);
2953 if (g_strcmp0 (name, "") == 0)
2954 return FALSE;
2955 if (!locale_is_utf8 && !WEEKDAY_ABBR_IS_LOCALE)
2957 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2958 if (!tmp)
2959 return FALSE;
2960 g_string_append_len (outstr, tmp, tmp_len);
2961 g_free (tmp);
2963 else
2965 g_string_append (outstr, name);
2967 break;
2968 case 'A':
2969 name = WEEKDAY_FULL (datetime);
2970 if (g_strcmp0 (name, "") == 0)
2971 return FALSE;
2972 if (!locale_is_utf8 && !WEEKDAY_FULL_IS_LOCALE)
2974 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2975 if (!tmp)
2976 return FALSE;
2977 g_string_append_len (outstr, tmp, tmp_len);
2978 g_free (tmp);
2980 else
2982 g_string_append (outstr, name);
2984 break;
2985 case 'b':
2986 name = alt_digits ? MONTH_ABBR_STANDALONE (datetime)
2987 : MONTH_ABBR_WITH_DAY (datetime);
2988 if (g_strcmp0 (name, "") == 0)
2989 return FALSE;
2990 if (!locale_is_utf8 &&
2991 ((alt_digits && !MONTH_ABBR_STANDALONE_IS_LOCALE) ||
2992 (!alt_digits && !MONTH_ABBR_WITH_DAY_IS_LOCALE)))
2994 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
2995 if (!tmp)
2996 return FALSE;
2997 g_string_append_len (outstr, tmp, tmp_len);
2998 g_free (tmp);
3000 else
3002 g_string_append (outstr, name);
3004 break;
3005 case 'B':
3006 name = alt_digits ? MONTH_FULL_STANDALONE (datetime)
3007 : MONTH_FULL_WITH_DAY (datetime);
3008 if (g_strcmp0 (name, "") == 0)
3009 return FALSE;
3010 if (!locale_is_utf8 &&
3011 ((alt_digits && !MONTH_FULL_STANDALONE_IS_LOCALE) ||
3012 (!alt_digits && !MONTH_FULL_WITH_DAY_IS_LOCALE)))
3014 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
3015 if (!tmp)
3016 return FALSE;
3017 g_string_append_len (outstr, tmp, tmp_len);
3018 g_free (tmp);
3020 else
3022 g_string_append (outstr, name);
3024 break;
3025 case 'c':
3027 if (g_strcmp0 (PREFERRED_DATE_TIME_FMT, "") == 0)
3028 return FALSE;
3029 if (!g_date_time_locale_format_locale (datetime, PREFERRED_DATE_TIME_FMT,
3030 outstr, locale_is_utf8))
3031 return FALSE;
3033 break;
3034 case 'C':
3035 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
3036 g_date_time_get_year (datetime) / 100);
3037 break;
3038 case 'd':
3039 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
3040 g_date_time_get_day_of_month (datetime));
3041 break;
3042 case 'e':
3043 format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
3044 g_date_time_get_day_of_month (datetime));
3045 break;
3046 case 'F':
3047 g_string_append_printf (outstr, "%d-%02d-%02d",
3048 g_date_time_get_year (datetime),
3049 g_date_time_get_month (datetime),
3050 g_date_time_get_day_of_month (datetime));
3051 break;
3052 case 'g':
3053 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
3054 g_date_time_get_week_numbering_year (datetime) % 100);
3055 break;
3056 case 'G':
3057 format_number (outstr, alt_digits, pad_set ? pad : 0, 0,
3058 g_date_time_get_week_numbering_year (datetime));
3059 break;
3060 case 'h':
3061 name = alt_digits ? MONTH_ABBR_STANDALONE (datetime)
3062 : MONTH_ABBR_WITH_DAY (datetime);
3063 if (g_strcmp0 (name, "") == 0)
3064 return FALSE;
3065 if (!locale_is_utf8 &&
3066 ((alt_digits && !MONTH_ABBR_STANDALONE_IS_LOCALE) ||
3067 (!alt_digits && !MONTH_ABBR_WITH_DAY_IS_LOCALE)))
3069 tmp = g_locale_from_utf8 (name, -1, NULL, &tmp_len, NULL);
3070 if (!tmp)
3071 return FALSE;
3072 g_string_append_len (outstr, tmp, tmp_len);
3073 g_free (tmp);
3075 else
3077 g_string_append (outstr, name);
3079 break;
3080 case 'H':
3081 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
3082 g_date_time_get_hour (datetime));
3083 break;
3084 case 'I':
3085 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
3086 (g_date_time_get_hour (datetime) + 11) % 12 + 1);
3087 break;
3088 case 'j':
3089 format_number (outstr, alt_digits, pad_set ? pad : "0", 3,
3090 g_date_time_get_day_of_year (datetime));
3091 break;
3092 case 'k':
3093 format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
3094 g_date_time_get_hour (datetime));
3095 break;
3096 case 'l':
3097 format_number (outstr, alt_digits, pad_set ? pad : " ", 2,
3098 (g_date_time_get_hour (datetime) + 11) % 12 + 1);
3099 break;
3100 case 'n':
3101 g_string_append_c (outstr, '\n');
3102 break;
3103 case 'm':
3104 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
3105 g_date_time_get_month (datetime));
3106 break;
3107 case 'M':
3108 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
3109 g_date_time_get_minute (datetime));
3110 break;
3111 case 'O':
3112 alt_digits = TRUE;
3113 goto next_mod;
3114 case 'p':
3115 if (!format_ampm (datetime, outstr, locale_is_utf8, TRUE))
3116 return FALSE;
3117 break;
3118 case 'P':
3119 if (!format_ampm (datetime, outstr, locale_is_utf8, FALSE))
3120 return FALSE;
3121 break;
3122 case 'r':
3124 if (g_strcmp0 (PREFERRED_12HR_TIME_FMT, "") == 0)
3125 return FALSE;
3126 if (!g_date_time_locale_format_locale (datetime, PREFERRED_12HR_TIME_FMT,
3127 outstr, locale_is_utf8))
3128 return FALSE;
3130 break;
3131 case 'R':
3132 g_string_append_printf (outstr, "%02d:%02d",
3133 g_date_time_get_hour (datetime),
3134 g_date_time_get_minute (datetime));
3135 break;
3136 case 's':
3137 g_string_append_printf (outstr, "%" G_GINT64_FORMAT, g_date_time_to_unix (datetime));
3138 break;
3139 case 'S':
3140 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
3141 g_date_time_get_second (datetime));
3142 break;
3143 case 't':
3144 g_string_append_c (outstr, '\t');
3145 break;
3146 case 'T':
3147 g_string_append_printf (outstr, "%02d:%02d:%02d",
3148 g_date_time_get_hour (datetime),
3149 g_date_time_get_minute (datetime),
3150 g_date_time_get_second (datetime));
3151 break;
3152 case 'u':
3153 format_number (outstr, alt_digits, 0, 0,
3154 g_date_time_get_day_of_week (datetime));
3155 break;
3156 case 'V':
3157 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
3158 g_date_time_get_week_of_year (datetime));
3159 break;
3160 case 'w':
3161 format_number (outstr, alt_digits, 0, 0,
3162 g_date_time_get_day_of_week (datetime) % 7);
3163 break;
3164 case 'x':
3166 if (g_strcmp0 (PREFERRED_DATE_FMT, "") == 0)
3167 return FALSE;
3168 if (!g_date_time_locale_format_locale (datetime, PREFERRED_DATE_FMT,
3169 outstr, locale_is_utf8))
3170 return FALSE;
3172 break;
3173 case 'X':
3175 if (g_strcmp0 (PREFERRED_TIME_FMT, "") == 0)
3176 return FALSE;
3177 if (!g_date_time_locale_format_locale (datetime, PREFERRED_TIME_FMT,
3178 outstr, locale_is_utf8))
3179 return FALSE;
3181 break;
3182 case 'y':
3183 format_number (outstr, alt_digits, pad_set ? pad : "0", 2,
3184 g_date_time_get_year (datetime) % 100);
3185 break;
3186 case 'Y':
3187 format_number (outstr, alt_digits, 0, 0,
3188 g_date_time_get_year (datetime));
3189 break;
3190 case 'z':
3192 gint64 offset;
3193 offset = g_date_time_get_utc_offset (datetime) / USEC_PER_SECOND;
3194 if (!format_z (outstr, (int) offset, colons))
3195 return FALSE;
3197 break;
3198 case 'Z':
3199 tz = g_date_time_get_timezone_abbreviation (datetime);
3200 tmp = NULL;
3201 tmp_len = strlen (tz);
3202 if (!locale_is_utf8)
3204 tz = tmp = g_locale_from_utf8 (tz, -1, NULL, &tmp_len, NULL);
3205 if (!tmp)
3206 return FALSE;
3208 g_string_append_len (outstr, tz, tmp_len);
3209 g_free (tmp);
3210 break;
3211 case '%':
3212 g_string_append_c (outstr, '%');
3213 break;
3214 case '-':
3215 pad_set = TRUE;
3216 pad = "";
3217 goto next_mod;
3218 case '_':
3219 pad_set = TRUE;
3220 pad = " ";
3221 goto next_mod;
3222 case '0':
3223 pad_set = TRUE;
3224 pad = "0";
3225 goto next_mod;
3226 case ':':
3227 /* Colons are only allowed before 'z' */
3228 if (*format && *format != 'z' && *format != ':')
3229 return FALSE;
3230 colons++;
3231 goto next_mod;
3232 default:
3233 return FALSE;
3237 return TRUE;
3241 * g_date_time_format:
3242 * @datetime: A #GDateTime
3243 * @format: a valid UTF-8 string, containing the format for the
3244 * #GDateTime
3246 * Creates a newly allocated string representing the requested @format.
3248 * The format strings understood by this function are a subset of the
3249 * strftime() format language as specified by C99. The \%D, \%U and \%W
3250 * conversions are not supported, nor is the 'E' modifier. The GNU
3251 * extensions \%k, \%l, \%s and \%P are supported, however, as are the
3252 * '0', '_' and '-' modifiers.
3254 * In contrast to strftime(), this function always produces a UTF-8
3255 * string, regardless of the current locale. Note that the rendering of
3256 * many formats is locale-dependent and may not match the strftime()
3257 * output exactly.
3259 * The following format specifiers are supported:
3261 * - \%a: the abbreviated weekday name according to the current locale
3262 * - \%A: the full weekday name according to the current locale
3263 * - \%b: the abbreviated month name according to the current locale
3264 * - \%B: the full month name according to the current locale
3265 * - \%c: the preferred date and time representation for the current locale
3266 * - \%C: the century number (year/100) as a 2-digit integer (00-99)
3267 * - \%d: the day of the month as a decimal number (range 01 to 31)
3268 * - \%e: the day of the month as a decimal number (range 1 to 31)
3269 * - \%F: equivalent to `%Y-%m-%d` (the ISO 8601 date format)
3270 * - \%g: the last two digits of the ISO 8601 week-based year as a
3271 * decimal number (00-99). This works well with \%V and \%u.
3272 * - \%G: the ISO 8601 week-based year as a decimal number. This works
3273 * well with \%V and \%u.
3274 * - \%h: equivalent to \%b
3275 * - \%H: the hour as a decimal number using a 24-hour clock (range 00 to 23)
3276 * - \%I: the hour as a decimal number using a 12-hour clock (range 01 to 12)
3277 * - \%j: the day of the year as a decimal number (range 001 to 366)
3278 * - \%k: the hour (24-hour clock) as a decimal number (range 0 to 23);
3279 * single digits are preceded by a blank
3280 * - \%l: the hour (12-hour clock) as a decimal number (range 1 to 12);
3281 * single digits are preceded by a blank
3282 * - \%m: the month as a decimal number (range 01 to 12)
3283 * - \%M: the minute as a decimal number (range 00 to 59)
3284 * - \%p: either "AM" or "PM" according to the given time value, or the
3285 * corresponding strings for the current locale. Noon is treated as
3286 * "PM" and midnight as "AM".
3287 * - \%P: like \%p but lowercase: "am" or "pm" or a corresponding string for
3288 * the current locale
3289 * - \%r: the time in a.m. or p.m. notation
3290 * - \%R: the time in 24-hour notation (\%H:\%M)
3291 * - \%s: the number of seconds since the Epoch, that is, since 1970-01-01
3292 * 00:00:00 UTC
3293 * - \%S: the second as a decimal number (range 00 to 60)
3294 * - \%t: a tab character
3295 * - \%T: the time in 24-hour notation with seconds (\%H:\%M:\%S)
3296 * - \%u: the ISO 8601 standard day of the week as a decimal, range 1 to 7,
3297 * Monday being 1. This works well with \%G and \%V.
3298 * - \%V: the ISO 8601 standard week number of the current year as a decimal
3299 * number, range 01 to 53, where week 1 is the first week that has at
3300 * least 4 days in the new year. See g_date_time_get_week_of_year().
3301 * This works well with \%G and \%u.
3302 * - \%w: the day of the week as a decimal, range 0 to 6, Sunday being 0.
3303 * This is not the ISO 8601 standard format -- use \%u instead.
3304 * - \%x: the preferred date representation for the current locale without
3305 * the time
3306 * - \%X: the preferred time representation for the current locale without
3307 * the date
3308 * - \%y: the year as a decimal number without the century
3309 * - \%Y: the year as a decimal number including the century
3310 * - \%z: the time zone as an offset from UTC (+hhmm)
3311 * - \%:z: the time zone as an offset from UTC (+hh:mm).
3312 * This is a gnulib strftime() extension. Since: 2.38
3313 * - \%::z: the time zone as an offset from UTC (+hh:mm:ss). This is a
3314 * gnulib strftime() extension. Since: 2.38
3315 * - \%:::z: the time zone as an offset from UTC, with : to necessary
3316 * precision (e.g., -04, +05:30). This is a gnulib strftime() extension. Since: 2.38
3317 * - \%Z: the time zone or name or abbreviation
3318 * - \%\%: a literal \% character
3320 * Some conversion specifications can be modified by preceding the
3321 * conversion specifier by one or more modifier characters. The
3322 * following modifiers are supported for many of the numeric
3323 * conversions:
3325 * - O: Use alternative numeric symbols, if the current locale supports those.
3326 * - _: Pad a numeric result with spaces. This overrides the default padding
3327 * for the specifier.
3328 * - -: Do not pad a numeric result. This overrides the default padding
3329 * for the specifier.
3330 * - 0: Pad a numeric result with zeros. This overrides the default padding
3331 * for the specifier.
3333 * Additionally, when O is used with B, b, or h, it produces the alternative
3334 * form of a month name. The alternative form should be used when the month
3335 * name is used without a day number (e.g., standalone). It is required in
3336 * some languages (Baltic, Slavic, Greek, and more) due to their grammatical
3337 * rules. For other languages there is no difference. \%OB is a GNU and BSD
3338 * strftime() extension expected to be added to the future POSIX specification,
3339 * \%Ob and \%Oh are GNU strftime() extensions. Since: 2.56
3341 * Returns: a newly allocated string formatted to the requested format
3342 * or %NULL in the case that there was an error (such as a format specifier
3343 * not being supported in the current locale). The string
3344 * should be freed with g_free().
3346 * Since: 2.26
3348 gchar *
3349 g_date_time_format (GDateTime *datetime,
3350 const gchar *format)
3352 GString *outstr;
3353 gchar *utf8;
3354 gboolean locale_is_utf8 = g_get_charset (NULL);
3356 g_return_val_if_fail (datetime != NULL, NULL);
3357 g_return_val_if_fail (format != NULL, NULL);
3358 g_return_val_if_fail (g_utf8_validate (format, -1, NULL), NULL);
3360 outstr = g_string_sized_new (strlen (format) * 2);
3362 if (!g_date_time_format_locale (datetime, format, outstr, locale_is_utf8))
3364 g_string_free (outstr, TRUE);
3365 return NULL;
3368 if (locale_is_utf8)
3369 return g_string_free (outstr, FALSE);
3371 utf8 = g_locale_to_utf8 (outstr->str, outstr->len, NULL, NULL, NULL);
3372 g_string_free (outstr, TRUE);
3373 return utf8;
3377 /* Epilogue {{{1 */
3378 /* vim:set foldmethod=marker: */