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
41 * This document is provided "as is". No warranties are made as
49 /* langinfo.h in glibc 2.27 defines ALTMON_* only if _GNU_SOURCE is defined. */
57 #ifdef HAVE_LANGINFO_TIME
61 #include "gdatetime.h"
67 #include "gfileutils.h"
70 #include "gmappedfile.h"
71 #include "gstrfuncs.h"
72 #include "gtestutils.h"
74 #include "gtimezone.h"
81 #endif /* !G_OS_WIN32 */
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.
118 /* Microsecond timekeeping within Day */
121 /* TimeZone information */
125 /* 1 is 0001-01-01 in Proleptic Gregorian */
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
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
240 get_month_name_standalone (gint month
)
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");
263 return C_("full month name", "February");
265 return C_("full month name", "March");
267 return C_("full month name", "April");
269 return C_("full month name", "May");
271 return C_("full month name", "June");
273 return C_("full month name", "July");
275 return C_("full month name", "August");
277 return C_("full month name", "September");
279 return C_("full month name", "October");
281 return C_("full month name", "November");
283 return C_("full month name", "December");
286 g_warning ("Invalid month number %d", month
);
293 get_month_name_abbr_standalone (gint month
)
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");
317 return C_("abbreviated month name", "Feb");
319 return C_("abbreviated month name", "Mar");
321 return C_("abbreviated month name", "Apr");
323 return C_("abbreviated month name", "May");
325 return C_("abbreviated month name", "Jun");
327 return C_("abbreviated month name", "Jul");
329 return C_("abbreviated month name", "Aug");
331 return C_("abbreviated month name", "Sep");
333 return C_("abbreviated month name", "Oct");
335 return C_("abbreviated month name", "Nov");
337 return C_("abbreviated month name", "Dec");
340 g_warning ("Invalid month number %d", month
);
347 get_weekday_name (gint day
)
352 return C_("full weekday name", "Monday");
354 return C_("full weekday name", "Tuesday");
356 return C_("full weekday name", "Wednesday");
358 return C_("full weekday name", "Thursday");
360 return C_("full weekday name", "Friday");
362 return C_("full weekday name", "Saturday");
364 return C_("full weekday name", "Sunday");
367 g_warning ("Invalid week day number %d", day
);
374 get_weekday_name_abbr (gint day
)
379 return C_("abbreviated weekday name", "Mon");
381 return C_("abbreviated weekday name", "Tue");
383 return C_("abbreviated weekday name", "Wed");
385 return C_("abbreviated weekday name", "Thu");
387 return C_("abbreviated weekday name", "Fri");
389 return C_("abbreviated weekday name", "Sat");
391 return C_("abbreviated weekday name", "Sun");
394 g_warning ("Invalid week day number %d", day
);
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
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
433 get_month_name_with_day (gint month
)
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");
457 return C_("full month name with day", "February");
459 return C_("full month name with day", "March");
461 return C_("full month name with day", "April");
463 return C_("full month name with day", "May");
465 return C_("full month name with day", "June");
467 return C_("full month name with day", "July");
469 return C_("full month name with day", "August");
471 return C_("full month name with day", "September");
473 return C_("full month name with day", "October");
475 return C_("full month name with day", "November");
477 return C_("full month name with day", "December");
480 g_warning ("Invalid month number %d", month
);
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
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
520 get_month_name_abbr_with_day (gint month
)
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");
544 return C_("abbreviated month name with day", "Feb");
546 return C_("abbreviated month name with day", "Mar");
548 return C_("abbreviated month name with day", "Apr");
550 return C_("abbreviated month name with day", "May");
552 return C_("abbreviated month name with day", "Jun");
554 return C_("abbreviated month name with day", "Jul");
556 return C_("abbreviated month name with day", "Aug");
558 return C_("abbreviated month name with day", "Sep");
560 return C_("abbreviated month name with day", "Oct");
562 return C_("abbreviated month name with day", "Nov");
564 return C_("abbreviated month name with day", "Dec");
567 g_warning ("Invalid month number %d", month
);
573 #endif /* HAVE_LANGINFO_ABALTMON */
575 /* Format AM/PM indicator if the locale does not have a localized version. */
577 get_fallback_ampm (gint hour
)
580 /* Translators: 'before midday' indicator */
581 return C_("GDateTime", "AM");
583 /* Translators: 'after midday' indicator */
584 return C_("GDateTime", "PM");
588 ymd_to_days (gint year
,
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)
607 g_date_time_get_week_number (GDateTime
*datetime
,
612 gint a
, b
, c
, d
, e
, f
, g
, n
, s
, month
, day
, year
;
614 g_date_time_get_ymd (datetime
, &year
, &month
, &day
);
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);
623 f
= day
- 1 + (31 * (month
- 1));
628 b
= (a
/ 4) - (a
/ 100) + (a
/ 400);
629 c
= ((a
- 1) / 4) - ((a
- 1) / 100) + ((a
- 1) / 400);
632 f
= day
+ (((153 * (month
- 3)) + 2) / 5) + 58 + s
;
642 *week_number
= 53 - ((g
- s
) / 5);
643 else if (n
> 364 + s
)
646 *week_number
= (n
/ 7) + 1;
650 *day_of_week
= d
+ 1;
653 *day_of_year
= f
+ 1;
659 g_date_time_alloc (GTimeZone
*tz
)
663 datetime
= g_slice_new0 (GDateTime
);
664 datetime
->tz
= g_time_zone_ref (tz
);
665 datetime
->ref_count
= 1;
672 * @datetime: a #GDateTime
674 * Atomically increments the reference count of @datetime by one.
676 * Returns: the #GDateTime with the reference count increased
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
);
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
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 */
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.
730 g_date_time_to_instant (GDateTime
*datetime
)
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
;
741 * g_date_time_from_instant:
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.
750 g_date_time_from_instant (GTimeZone
*tz
,
756 if (instant
< 0 || instant
> G_GINT64_CONSTANT (1000000000000000000))
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
;
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
);
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
801 g_date_time_deal_with_date_change (GDateTime
*datetime
)
807 if (datetime
->days
< 1 || datetime
->days
> 3652059)
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
,
822 full_time
+= UNIX_EPOCH_START
* SEC_PER_DAY
;
823 full_time
*= USEC_PER_SECOND
;
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 */
835 g_date_time_replace_days (GDateTime
*datetime
,
840 new = g_date_time_alloc (datetime
->tz
);
841 new->interval
= datetime
->interval
;
842 new->usec
= datetime
->usec
;
845 if (!g_date_time_deal_with_date_change (new))
847 g_date_time_unref (new);
854 /* now/unix/timeval Constructors {{{1 */
857 * g_date_time_new_from_timeval:
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
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
879 g_date_time_new_from_timeval (GTimeZone
*tz
,
882 if ((gint64
) tv
->tv_sec
> G_MAXINT64
- 1 ||
883 !UNIX_TO_INSTANT_IS_VALID ((gint64
) tv
->tv_sec
+ 1))
886 return g_date_time_from_instant (tz
, tv
->tv_usec
+
887 UNIX_TO_INSTANT (tv
->tv_sec
));
891 * g_date_time_new_from_unix:
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
912 g_date_time_new_from_unix (GTimeZone
*tz
,
915 if (!UNIX_TO_INSTANT_IS_VALID (secs
))
918 return g_date_time_from_instant (tz
, UNIX_TO_INSTANT (secs
));
922 * g_date_time_new_now:
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
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
941 g_date_time_new_now (GTimeZone
*tz
)
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
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
964 g_date_time_new_now_local (void)
969 local
= g_time_zone_new_local ();
970 datetime
= g_date_time_new_now (local
);
971 g_time_zone_unref (local
);
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
989 g_date_time_new_now_utc (void)
994 utc
= g_time_zone_new_utc ();
995 datetime
= g_date_time_new_now (utc
);
996 g_time_zone_unref (utc
);
1002 * g_date_time_new_from_unix_local:
1005 * Creates a #GDateTime corresponding to the given Unix time @t in the
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
1022 g_date_time_new_from_unix_local (gint64 t
)
1024 GDateTime
*datetime
;
1027 local
= g_time_zone_new_local ();
1028 datetime
= g_date_time_new_from_unix (local
, t
);
1029 g_time_zone_unref (local
);
1035 * g_date_time_new_from_unix_utc:
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
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
1054 g_date_time_new_from_unix_utc (gint64 t
)
1056 GDateTime
*datetime
;
1059 utc
= g_time_zone_new_utc ();
1060 datetime
= g_date_time_new_from_unix (utc
, t
);
1061 g_time_zone_unref (utc
);
1067 * g_date_time_new_from_timeval_local:
1070 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
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
1088 g_date_time_new_from_timeval_local (const GTimeVal
*tv
)
1090 GDateTime
*datetime
;
1093 local
= g_time_zone_new_local ();
1094 datetime
= g_date_time_new_from_timeval (local
, tv
);
1095 g_time_zone_unref (local
);
1101 * g_date_time_new_from_timeval_utc:
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
1120 g_date_time_new_from_timeval_utc (const GTimeVal
*tv
)
1122 GDateTime
*datetime
;
1125 utc
= g_time_zone_new_utc ();
1126 datetime
= g_date_time_new_from_timeval (utc
, tv
);
1127 g_time_zone_unref (utc
);
1132 /* Parse integers in the form d (week days), dd (hours etc), ddd (ordinal days) or dddd (years) */
1134 get_iso8601_int (const gchar
*text
, gsize length
, gint
*value
)
1138 if (length
< 1 || length
> 4)
1141 for (i
= 0; i
< length
; i
++)
1143 const gchar c
= text
[i
];
1144 if (c
< '0' || c
> '9')
1146 v
= v
* 10 + (c
- '0');
1153 /* Parse seconds in the form ss or ss.sss (variable length decimal) */
1155 get_iso8601_seconds (const gchar
*text
, gsize length
, gdouble
*value
)
1158 gdouble divisor
= 1, v
= 0;
1163 for (i
= 0; i
< 2; i
++)
1165 const gchar c
= text
[i
];
1166 if (c
< '0' || c
> '9')
1168 v
= v
* 10 + (c
- '0');
1171 if (length
> 2 && !(text
[i
] == '.' || text
[i
] == ','))
1177 for (; i
< length
; i
++)
1179 const gchar c
= text
[i
];
1180 if (c
< '0' || c
> '9')
1182 v
= v
* 10 + (c
- '0');
1186 *value
= v
/ divisor
;
1191 g_date_time_new_ordinal (GTimeZone
*tz
, gint year
, gint ordinal_day
, gint hour
, gint minute
, gdouble seconds
)
1195 if (ordinal_day
< 1 || ordinal_day
> (GREGORIAN_LEAP (year
) ? 366 : 365))
1198 dt
= g_date_time_new (tz
, year
, 1, 1, hour
, minute
, seconds
);
1199 dt
->days
+= ordinal_day
- 1;
1205 g_date_time_new_week (GTimeZone
*tz
, gint year
, gint week
, gint week_day
, gint hour
, gint minute
, gdouble seconds
)
1208 gint max_week
, jan4_week_day
, ordinal_day
;
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)
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)
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);
1233 return g_date_time_new_ordinal (tz
, year
, ordinal_day
, hour
, minute
, seconds
);
1237 parse_iso8601_date (const gchar
*text
, gsize length
,
1238 gint hour
, gint minute
, gdouble seconds
, GTimeZone
*tz
)
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
))
1248 return g_date_time_new (tz
, year
, month
, day
, hour
, minute
, seconds
);
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
))
1257 return g_date_time_new_ordinal (tz
, year
, ordinal_day
, hour
, minute
, seconds
);
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
))
1267 return g_date_time_new_week (tz
, year
, week
, week_day
, hour
, minute
, seconds
);
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
))
1277 return g_date_time_new_week (tz
, year
, week
, week_day
, hour
, minute
, seconds
);
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
))
1287 return g_date_time_new (tz
, year
, month
, day
, hour
, minute
, seconds
);
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
))
1296 return g_date_time_new_ordinal (tz
, year
, ordinal_day
, hour
, minute
, seconds
);
1303 parse_iso8601_timezone (const gchar
*text
, gsize length
, gssize
*tz_offset
)
1305 gint i
, tz_length
, offset_sign
= 1, offset_hours
, offset_minutes
;
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;
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
))
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
))
1341 else if (tz_length
== 3)
1343 if (!get_iso8601_int (text
+ i
+ 1, 2, &offset_hours
))
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));
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
);
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
);
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
);
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,
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:
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
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
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)
1456 if (!parse_iso8601_time (text
+ date_length
+ 1, length
- (date_length
+ 1),
1457 &hour
, &minute
, &seconds
, &tz
))
1459 if (tz
== NULL
&& default_tz
== NULL
)
1462 datetime
= parse_iso8601_date (text
, date_length
, hour
, minute
, seconds
, tz
? tz
: default_tz
);
1466 g_time_zone_unref (tz
);
1470 /* full new functions {{{1 */
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
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
1516 g_date_time_new (GTimeZone
*tz
,
1524 GDateTime
*datetime
;
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)
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
+
1554 datetime
->interval
= g_time_zone_adjust_time (datetime
->tz
,
1555 G_TIME_TYPE_STANDARD
,
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
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
) {
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
;
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
1597 g_date_time_new_local (gint year
,
1604 GDateTime
*datetime
;
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
);
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
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
1634 g_date_time_new_utc (gint year
,
1641 GDateTime
*datetime
;
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
);
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().
1666 g_date_time_add (GDateTime
*datetime
,
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().
1690 g_date_time_add_years (GDateTime
*datetime
,
1693 gint year
, month
, day
;
1695 g_return_val_if_fail (datetime
!= NULL
, NULL
);
1697 if (years
< -10000 || years
> 10000)
1700 g_date_time_get_ymd (datetime
, &year
, &month
, &day
);
1703 /* only possible issue is if we've entered a year with no February 29
1705 if (month
== 2 && day
== 29 && !GREGORIAN_LEAP (year
))
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().
1730 g_date_time_add_months (GDateTime
*datetime
,
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)
1741 year
+= months
/ 12;
1742 month
+= months
% 12;
1748 else if (month
> 12)
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().
1773 g_date_time_add_weeks (GDateTime
*datetime
,
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().
1795 g_date_time_add_days (GDateTime
*datetime
,
1798 g_return_val_if_fail (datetime
!= NULL
, NULL
);
1800 if (days
< -3660000 || days
> 3660000)
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().
1820 g_date_time_add_hours (GDateTime
*datetime
,
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().
1840 g_date_time_add_minutes (GDateTime
*datetime
,
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().
1861 g_date_time_add_seconds (GDateTime
*datetime
,
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().
1886 g_date_time_add_full (GDateTime
*datetime
,
1894 gint year
, month
, day
;
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)
1907 if (days
< -3660000 || days
> 3660000)
1910 year
+= months
/ 12;
1911 month
+= months
% 12;
1917 else if (month
> 12)
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
),
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
;
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
1982 g_date_time_compare (gconstpointer dt1
,
1987 difference
= g_date_time_difference ((GDateTime
*) dt1
, (GDateTime
*) dt2
);
1992 else if (difference
> 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.
2014 g_date_time_difference (GDateTime
*end
,
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
);
2026 * @datetime: (not nullable): a #GDateTime
2028 * Hashes @datetime into a #guint, suitable for use within #GHashTable.
2030 * Returns: a #guint containing the hash
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
2055 g_date_time_equal (gconstpointer dt1
,
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.
2074 g_date_time_get_ymd (GDateTime
*datetime
,
2082 gint remaining_days
;
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 */
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 */
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 */
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;
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
2169 g_date_time_get_year (GDateTime
*datetime
)
2173 g_return_val_if_fail (datetime
!= NULL
, 0);
2175 g_date_time_get_ymd (datetime
, &year
, NULL
, NULL
);
2181 * g_date_time_get_month:
2182 * @datetime: a #GDateTime
2184 * Retrieves the month of the year represented by @datetime in the Gregorian
2187 * Returns: the month represented by @datetime
2192 g_date_time_get_month (GDateTime
*datetime
)
2196 g_return_val_if_fail (datetime
!= NULL
, 0);
2198 g_date_time_get_ymd (datetime
, NULL
, &month
, NULL
);
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
2210 * Returns: the day of the month
2215 g_date_time_get_day_of_month (GDateTime
*datetime
)
2219 const guint16
*days
;
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
;
2234 g_warn_if_reached ();
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
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
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
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)
2299 /* December 29, 30, 31 might be in the next year if they occur before
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)
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.
2340 g_date_time_get_week_of_year (GDateTime
*datetime
)
2344 g_return_val_if_fail (datetime
!= NULL
, 0);
2346 g_date_time_get_week_number (datetime
, &weeknum
, NULL
, NULL
);
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
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
2378 * Returns: the day of the year
2383 g_date_time_get_day_of_year (GDateTime
*datetime
)
2387 g_return_val_if_fail (datetime
!= NULL
, 0);
2389 g_date_time_get_week_number (datetime
, NULL
, NULL
, &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
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
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
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
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
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
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
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
2524 * On systems where 'long' is 64bit, this function never fails.
2526 * Returns: %TRUE if successful, else %FALSE
2531 g_date_time_to_timeval (GDateTime
*datetime
,
2534 tv
->tv_sec
= INSTANT_TO_UNIX (g_date_time_to_instant (datetime
));
2535 tv
->tv_usec
= datetime
->usec
% USEC_PER_SECOND
;
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
2560 g_date_time_get_utc_offset (GDateTime
*datetime
)
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
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
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
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
2654 g_date_time_to_timezone (GDateTime
*datetime
,
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
2675 g_date_time_to_local (GDateTime
*datetime
)
2680 local
= g_time_zone_new_local ();
2681 new = g_date_time_to_timezone (datetime
, local
);
2682 g_time_zone_unref (local
);
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
2702 g_date_time_to_utc (GDateTime
*datetime
)
2707 utc
= g_time_zone_new_utc ();
2708 new = g_date_time_to_timezone (datetime
, utc
);
2709 g_time_zone_unref (utc
);
2717 format_z (GString
*outstr
,
2725 hours
= offset
/ 3600;
2726 minutes
= ABS (offset
) / 60 % 60;
2727 seconds
= ABS (offset
) % 60;
2732 g_string_append_printf (outstr
, "%+03d%02d",
2738 g_string_append_printf (outstr
, "%+03d:%02d",
2744 g_string_append_printf (outstr
, "%+03d:%02d:%02d",
2751 g_string_append_printf (outstr
, "%+03d", hours
);
2753 if (minutes
!= 0 || seconds
!= 0)
2755 g_string_append_printf (outstr
, ":%02d", minutes
);
2758 g_string_append_printf (outstr
, ":%02d", seconds
);
2770 format_number (GString
*str
,
2771 gboolean use_alt_digits
,
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];
2783 g_return_if_fail (width
<= 10);
2785 #ifdef HAVE_LANGINFO_OUTDIGIT
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);
2799 g_once_init_leave (&initialised
, TRUE
);
2802 digits
= alt_digits
;
2804 #endif /* HAVE_LANGINFO_OUTDIGIT */
2808 tmp
[i
++] = digits
[number
% 10];
2813 while (pad
&& i
< width
)
2814 tmp
[i
++] = *pad
== '0' ? digits
[0] : pad
;
2816 /* should really be impossible */
2820 g_string_append (str
, tmp
[--i
]);
2824 format_ampm (GDateTime
*datetime
,
2826 gboolean locale_is_utf8
,
2830 gchar
*tmp
= NULL
, *ampm_dup
;
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
);
2846 ampm_dup
= g_utf8_strup (ampm
, -1);
2848 ampm_dup
= g_utf8_strdown (ampm
, -1);
2849 len
= strlen (ampm_dup
);
2850 if (!locale_is_utf8
&& GET_AMPM_IS_LOCALE
)
2853 tmp
= g_locale_from_utf8 (ampm_dup
, -1, NULL
, &len
, NULL
);
2859 g_string_append_len (outstr
, ampm_dup
, len
);
2865 static gboolean
g_date_time_format_locale (GDateTime
*datetime
,
2866 const gchar
*format
,
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.
2874 g_date_time_locale_format_locale (GDateTime
*datetime
,
2875 const gchar
*format
,
2877 gboolean locale_is_utf8
)
2883 return g_date_time_format_locale (datetime
, format
, outstr
,
2886 utf8_format
= g_locale_to_utf8 (format
, -1, NULL
, NULL
, NULL
);
2890 success
= g_date_time_format_locale (datetime
, utf8_format
, outstr
,
2892 g_free (utf8_format
);
2896 /* g_date_time_format() subroutine that takes a UTF-8 format
2897 * string and produces a locale-encoded date/time string.
2900 g_date_time_format_locale (GDateTime
*datetime
,
2901 const gchar
*format
,
2903 gboolean locale_is_utf8
)
2910 gboolean alt_digits
= FALSE
;
2911 gboolean pad_set
= FALSE
;
2912 const gchar
*pad
= "";
2918 len
= strcspn (format
, "%");
2922 g_string_append_len (outstr
, format
, len
);
2925 tmp
= g_locale_from_utf8 (format
, len
, NULL
, &tmp_len
, NULL
);
2928 g_string_append_len (outstr
, tmp
, tmp_len
);
2937 g_assert (*format
== '%');
2947 c
= g_utf8_get_char (format
);
2948 format
= g_utf8_next_char (format
);
2952 name
= WEEKDAY_ABBR (datetime
);
2953 if (g_strcmp0 (name
, "") == 0)
2955 if (!locale_is_utf8
&& !WEEKDAY_ABBR_IS_LOCALE
)
2957 tmp
= g_locale_from_utf8 (name
, -1, NULL
, &tmp_len
, NULL
);
2960 g_string_append_len (outstr
, tmp
, tmp_len
);
2965 g_string_append (outstr
, name
);
2969 name
= WEEKDAY_FULL (datetime
);
2970 if (g_strcmp0 (name
, "") == 0)
2972 if (!locale_is_utf8
&& !WEEKDAY_FULL_IS_LOCALE
)
2974 tmp
= g_locale_from_utf8 (name
, -1, NULL
, &tmp_len
, NULL
);
2977 g_string_append_len (outstr
, tmp
, tmp_len
);
2982 g_string_append (outstr
, name
);
2986 name
= alt_digits
? MONTH_ABBR_STANDALONE (datetime
)
2987 : MONTH_ABBR_WITH_DAY (datetime
);
2988 if (g_strcmp0 (name
, "") == 0)
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
);
2997 g_string_append_len (outstr
, tmp
, tmp_len
);
3002 g_string_append (outstr
, name
);
3006 name
= alt_digits
? MONTH_FULL_STANDALONE (datetime
)
3007 : MONTH_FULL_WITH_DAY (datetime
);
3008 if (g_strcmp0 (name
, "") == 0)
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
);
3017 g_string_append_len (outstr
, tmp
, tmp_len
);
3022 g_string_append (outstr
, name
);
3027 if (g_strcmp0 (PREFERRED_DATE_TIME_FMT
, "") == 0)
3029 if (!g_date_time_locale_format_locale (datetime
, PREFERRED_DATE_TIME_FMT
,
3030 outstr
, locale_is_utf8
))
3035 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
3036 g_date_time_get_year (datetime
) / 100);
3039 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
3040 g_date_time_get_day_of_month (datetime
));
3043 format_number (outstr
, alt_digits
, pad_set
? pad
: " ", 2,
3044 g_date_time_get_day_of_month (datetime
));
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
));
3053 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
3054 g_date_time_get_week_numbering_year (datetime
) % 100);
3057 format_number (outstr
, alt_digits
, pad_set
? pad
: 0, 0,
3058 g_date_time_get_week_numbering_year (datetime
));
3061 name
= alt_digits
? MONTH_ABBR_STANDALONE (datetime
)
3062 : MONTH_ABBR_WITH_DAY (datetime
);
3063 if (g_strcmp0 (name
, "") == 0)
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
);
3072 g_string_append_len (outstr
, tmp
, tmp_len
);
3077 g_string_append (outstr
, name
);
3081 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
3082 g_date_time_get_hour (datetime
));
3085 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
3086 (g_date_time_get_hour (datetime
) + 11) % 12 + 1);
3089 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 3,
3090 g_date_time_get_day_of_year (datetime
));
3093 format_number (outstr
, alt_digits
, pad_set
? pad
: " ", 2,
3094 g_date_time_get_hour (datetime
));
3097 format_number (outstr
, alt_digits
, pad_set
? pad
: " ", 2,
3098 (g_date_time_get_hour (datetime
) + 11) % 12 + 1);
3101 g_string_append_c (outstr
, '\n');
3104 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
3105 g_date_time_get_month (datetime
));
3108 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
3109 g_date_time_get_minute (datetime
));
3115 if (!format_ampm (datetime
, outstr
, locale_is_utf8
, TRUE
))
3119 if (!format_ampm (datetime
, outstr
, locale_is_utf8
, FALSE
))
3124 if (g_strcmp0 (PREFERRED_12HR_TIME_FMT
, "") == 0)
3126 if (!g_date_time_locale_format_locale (datetime
, PREFERRED_12HR_TIME_FMT
,
3127 outstr
, locale_is_utf8
))
3132 g_string_append_printf (outstr
, "%02d:%02d",
3133 g_date_time_get_hour (datetime
),
3134 g_date_time_get_minute (datetime
));
3137 g_string_append_printf (outstr
, "%" G_GINT64_FORMAT
, g_date_time_to_unix (datetime
));
3140 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
3141 g_date_time_get_second (datetime
));
3144 g_string_append_c (outstr
, '\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
));
3153 format_number (outstr
, alt_digits
, 0, 0,
3154 g_date_time_get_day_of_week (datetime
));
3157 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
3158 g_date_time_get_week_of_year (datetime
));
3161 format_number (outstr
, alt_digits
, 0, 0,
3162 g_date_time_get_day_of_week (datetime
) % 7);
3166 if (g_strcmp0 (PREFERRED_DATE_FMT
, "") == 0)
3168 if (!g_date_time_locale_format_locale (datetime
, PREFERRED_DATE_FMT
,
3169 outstr
, locale_is_utf8
))
3175 if (g_strcmp0 (PREFERRED_TIME_FMT
, "") == 0)
3177 if (!g_date_time_locale_format_locale (datetime
, PREFERRED_TIME_FMT
,
3178 outstr
, locale_is_utf8
))
3183 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
3184 g_date_time_get_year (datetime
) % 100);
3187 format_number (outstr
, alt_digits
, 0, 0,
3188 g_date_time_get_year (datetime
));
3193 offset
= g_date_time_get_utc_offset (datetime
) / USEC_PER_SECOND
;
3194 if (!format_z (outstr
, (int) offset
, colons
))
3199 tz
= g_date_time_get_timezone_abbreviation (datetime
);
3201 tmp_len
= strlen (tz
);
3202 if (!locale_is_utf8
)
3204 tz
= tmp
= g_locale_from_utf8 (tz
, -1, NULL
, &tmp_len
, NULL
);
3208 g_string_append_len (outstr
, tz
, tmp_len
);
3212 g_string_append_c (outstr
, '%');
3227 /* Colons are only allowed before 'z' */
3228 if (*format
&& *format
!= 'z' && *format
!= ':')
3241 * g_date_time_format:
3242 * @datetime: A #GDateTime
3243 * @format: a valid UTF-8 string, containing the format for the
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()
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
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
3306 * - \%X: the preferred time representation for the current locale without
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
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().
3349 g_date_time_format (GDateTime
*datetime
,
3350 const gchar
*format
)
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
);
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
);
3378 /* vim:set foldmethod=marker: */