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
52 #ifdef HAVE_LANGINFO_TIME
56 #include "gdatetime.h"
62 #include "gfileutils.h"
65 #include "gmappedfile.h"
66 #include "gstrfuncs.h"
67 #include "gtestutils.h"
69 #include "gtimezone.h"
76 #endif /* !G_OS_WIN32 */
81 * @short_description: a structure representing Date and Time
82 * @see_also: #GTimeZone
84 * #GDateTime is a structure that combines a Gregorian date and time
85 * into a single structure. It provides many conversion and methods to
86 * manipulate dates and times. Time precision is provided down to
87 * microseconds and the time can range (proleptically) from 0001-01-01
88 * 00:00:00 to 9999-12-31 23:59:59.999999. #GDateTime follows POSIX
89 * time in the sense that it is oblivious to leap seconds.
91 * #GDateTime is an immutable object; once it has been created it cannot
92 * be modified further. All modifiers will create a new #GDateTime.
93 * Nearly all such functions can fail due to the date or time going out
94 * of range, in which case %NULL will be returned.
96 * #GDateTime is reference counted: the reference count is increased by calling
97 * g_date_time_ref() and decreased by calling g_date_time_unref(). When the
98 * reference count drops to 0, the resources allocated by the #GDateTime
99 * structure are released.
101 * Many parts of the API may produce non-obvious results. As an
102 * example, adding two months to January 31st will yield March 31st
103 * whereas adding one month and then one month again will yield either
104 * March 28th or March 29th. Also note that adding 24 hours is not
105 * always the same as adding one day (since days containing daylight
106 * savings time transitions are either 23 or 25 hours in length).
108 * #GDateTime is available since GLib 2.26.
113 /* Microsecond timekeeping within Day */
116 /* TimeZone information */
120 /* 1 is 0001-01-01 in Proleptic Gregorian */
123 volatile gint ref_count
;
126 /* Time conversion {{{1 */
128 #define UNIX_EPOCH_START 719163
129 #define INSTANT_TO_UNIX(instant) \
130 ((instant)/USEC_PER_SECOND - UNIX_EPOCH_START * SEC_PER_DAY)
131 #define UNIX_TO_INSTANT(unix) \
132 (((gint64) (unix) + UNIX_EPOCH_START * SEC_PER_DAY) * USEC_PER_SECOND)
133 #define UNIX_TO_INSTANT_IS_VALID(unix) \
134 ((gint64) (unix) <= INSTANT_TO_UNIX (G_MAXINT64))
136 #define DAYS_IN_4YEARS 1461 /* days in 4 years */
137 #define DAYS_IN_100YEARS 36524 /* days in 100 years */
138 #define DAYS_IN_400YEARS 146097 /* days in 400 years */
140 #define USEC_PER_SECOND (G_GINT64_CONSTANT (1000000))
141 #define USEC_PER_MINUTE (G_GINT64_CONSTANT (60000000))
142 #define USEC_PER_HOUR (G_GINT64_CONSTANT (3600000000))
143 #define USEC_PER_MILLISECOND (G_GINT64_CONSTANT (1000))
144 #define USEC_PER_DAY (G_GINT64_CONSTANT (86400000000))
145 #define SEC_PER_DAY (G_GINT64_CONSTANT (86400))
147 #define SECS_PER_MINUTE (60)
148 #define SECS_PER_HOUR (60 * SECS_PER_MINUTE)
149 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
150 #define SECS_PER_YEAR (365 * SECS_PER_DAY)
151 #define SECS_PER_JULIAN (DAYS_PER_PERIOD * SECS_PER_DAY)
153 #define GREGORIAN_LEAP(y) ((((y) % 4) == 0) && (!((((y) % 100) == 0) && (((y) % 400) != 0))))
154 #define JULIAN_YEAR(d) ((d)->julian / 365.25)
155 #define DAYS_PER_PERIOD (G_GINT64_CONSTANT (2914695))
157 static const guint16 days_in_months
[2][13] =
159 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
160 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
163 static const guint16 days_in_year
[2][13] =
165 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
166 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
169 #ifdef HAVE_LANGINFO_TIME
171 #define GET_AMPM(d) ((g_date_time_get_hour (d) < 12) ? \
172 nl_langinfo (AM_STR) : \
173 nl_langinfo (PM_STR))
175 #define PREFERRED_DATE_TIME_FMT nl_langinfo (D_T_FMT)
176 #define PREFERRED_DATE_FMT nl_langinfo (D_FMT)
177 #define PREFERRED_TIME_FMT nl_langinfo (T_FMT)
178 #define PREFERRED_12HR_TIME_FMT nl_langinfo (T_FMT_AMPM)
180 static const gint weekday_item
[2][7] =
182 { ABDAY_2
, ABDAY_3
, ABDAY_4
, ABDAY_5
, ABDAY_6
, ABDAY_7
, ABDAY_1
},
183 { DAY_2
, DAY_3
, DAY_4
, DAY_5
, DAY_6
, DAY_7
, DAY_1
}
186 static const gint month_item
[2][12] =
188 { ABMON_1
, ABMON_2
, ABMON_3
, ABMON_4
, ABMON_5
, ABMON_6
, ABMON_7
, ABMON_8
, ABMON_9
, ABMON_10
, ABMON_11
, ABMON_12
},
189 { MON_1
, MON_2
, MON_3
, MON_4
, MON_5
, MON_6
, MON_7
, MON_8
, MON_9
, MON_10
, MON_11
, MON_12
},
192 #define WEEKDAY_ABBR(d) nl_langinfo (weekday_item[0][g_date_time_get_day_of_week (d) - 1])
193 #define WEEKDAY_FULL(d) nl_langinfo (weekday_item[1][g_date_time_get_day_of_week (d) - 1])
194 #define MONTH_ABBR(d) nl_langinfo (month_item[0][g_date_time_get_month (d) - 1])
195 #define MONTH_FULL(d) nl_langinfo (month_item[1][g_date_time_get_month (d) - 1])
199 #define GET_AMPM(d) (get_fallback_ampm (g_date_time_get_hour (d)))
201 /* Translators: this is the preferred format for expressing the date and the time */
202 #define PREFERRED_DATE_TIME_FMT C_("GDateTime", "%a %b %e %H:%M:%S %Y")
204 /* Translators: this is the preferred format for expressing the date */
205 #define PREFERRED_DATE_FMT C_("GDateTime", "%m/%d/%y")
207 /* Translators: this is the preferred format for expressing the time */
208 #define PREFERRED_TIME_FMT C_("GDateTime", "%H:%M:%S")
210 /* Translators: this is the preferred format for expressing 12 hour time */
211 #define PREFERRED_12HR_TIME_FMT C_("GDateTime", "%I:%M:%S %p")
213 #define WEEKDAY_ABBR(d) (get_weekday_name_abbr (g_date_time_get_day_of_week (d)))
214 #define WEEKDAY_FULL(d) (get_weekday_name (g_date_time_get_day_of_week (d)))
215 #define MONTH_ABBR(d) (get_month_name_abbr (g_date_time_get_month (d)))
216 #define MONTH_FULL(d) (get_month_name (g_date_time_get_month (d)))
219 get_month_name (gint month
)
224 return C_("full month name", "January");
226 return C_("full month name", "February");
228 return C_("full month name", "March");
230 return C_("full month name", "April");
232 return C_("full month name", "May");
234 return C_("full month name", "June");
236 return C_("full month name", "July");
238 return C_("full month name", "August");
240 return C_("full month name", "September");
242 return C_("full month name", "October");
244 return C_("full month name", "November");
246 return C_("full month name", "December");
249 g_warning ("Invalid month number %d", month
);
256 get_month_name_abbr (gint month
)
261 return C_("abbreviated month name", "Jan");
263 return C_("abbreviated month name", "Feb");
265 return C_("abbreviated month name", "Mar");
267 return C_("abbreviated month name", "Apr");
269 return C_("abbreviated month name", "May");
271 return C_("abbreviated month name", "Jun");
273 return C_("abbreviated month name", "Jul");
275 return C_("abbreviated month name", "Aug");
277 return C_("abbreviated month name", "Sep");
279 return C_("abbreviated month name", "Oct");
281 return C_("abbreviated month name", "Nov");
283 return C_("abbreviated month name", "Dec");
286 g_warning ("Invalid month number %d", month
);
293 get_weekday_name (gint day
)
298 return C_("full weekday name", "Monday");
300 return C_("full weekday name", "Tuesday");
302 return C_("full weekday name", "Wednesday");
304 return C_("full weekday name", "Thursday");
306 return C_("full weekday name", "Friday");
308 return C_("full weekday name", "Saturday");
310 return C_("full weekday name", "Sunday");
313 g_warning ("Invalid week day number %d", day
);
320 get_weekday_name_abbr (gint day
)
325 return C_("abbreviated weekday name", "Mon");
327 return C_("abbreviated weekday name", "Tue");
329 return C_("abbreviated weekday name", "Wed");
331 return C_("abbreviated weekday name", "Thu");
333 return C_("abbreviated weekday name", "Fri");
335 return C_("abbreviated weekday name", "Sat");
337 return C_("abbreviated weekday name", "Sun");
340 g_warning ("Invalid week day number %d", day
);
346 #endif /* HAVE_LANGINFO_TIME */
348 /* Format AM/PM indicator if the locale does not have a localized version. */
350 get_fallback_ampm (gint hour
)
353 /* Translators: 'before midday' indicator */
354 return C_("GDateTime", "AM");
356 /* Translators: 'after midday' indicator */
357 return C_("GDateTime", "PM");
361 ymd_to_days (gint year
,
367 days
= (year
- 1) * 365 + ((year
- 1) / 4) - ((year
- 1) / 100)
368 + ((year
- 1) / 400);
370 days
+= days_in_year
[0][month
- 1];
371 if (GREGORIAN_LEAP (year
) && month
> 2)
380 g_date_time_get_week_number (GDateTime
*datetime
,
385 gint a
, b
, c
, d
, e
, f
, g
, n
, s
, month
, day
, year
;
387 g_date_time_get_ymd (datetime
, &year
, &month
, &day
);
391 a
= g_date_time_get_year (datetime
) - 1;
392 b
= (a
/ 4) - (a
/ 100) + (a
/ 400);
393 c
= ((a
- 1) / 4) - ((a
- 1) / 100) + ((a
- 1) / 400);
396 f
= day
- 1 + (31 * (month
- 1));
401 b
= (a
/ 4) - (a
/ 100) + (a
/ 400);
402 c
= ((a
- 1) / 4) - ((a
- 1) / 100) + ((a
- 1) / 400);
405 f
= day
+ (((153 * (month
- 3)) + 2) / 5) + 58 + s
;
415 *week_number
= 53 - ((g
- s
) / 5);
416 else if (n
> 364 + s
)
419 *week_number
= (n
/ 7) + 1;
423 *day_of_week
= d
+ 1;
426 *day_of_year
= f
+ 1;
432 g_date_time_alloc (GTimeZone
*tz
)
436 datetime
= g_slice_new0 (GDateTime
);
437 datetime
->tz
= g_time_zone_ref (tz
);
438 datetime
->ref_count
= 1;
445 * @datetime: a #GDateTime
447 * Atomically increments the reference count of @datetime by one.
449 * Returns: the #GDateTime with the reference count increased
454 g_date_time_ref (GDateTime
*datetime
)
456 g_return_val_if_fail (datetime
!= NULL
, NULL
);
457 g_return_val_if_fail (datetime
->ref_count
> 0, NULL
);
459 g_atomic_int_inc (&datetime
->ref_count
);
466 * @datetime: a #GDateTime
468 * Atomically decrements the reference count of @datetime by one.
470 * When the reference count reaches zero, the resources allocated by
471 * @datetime are freed
476 g_date_time_unref (GDateTime
*datetime
)
478 g_return_if_fail (datetime
!= NULL
);
479 g_return_if_fail (datetime
->ref_count
> 0);
481 if (g_atomic_int_dec_and_test (&datetime
->ref_count
))
483 g_time_zone_unref (datetime
->tz
);
484 g_slice_free (GDateTime
, datetime
);
488 /* Internal state transformers {{{1 */
490 * g_date_time_to_instant:
491 * @datetime: a #GDateTime
493 * Convert a @datetime into an instant.
495 * An instant is a number that uniquely describes a particular
496 * microsecond in time, taking time zone considerations into account.
497 * (ie: "03:00 -0400" is the same instant as "02:00 -0500").
499 * An instant is always positive but we use a signed return value to
500 * avoid troubles with C.
503 g_date_time_to_instant (GDateTime
*datetime
)
507 offset
= g_time_zone_get_offset (datetime
->tz
, datetime
->interval
);
508 offset
*= USEC_PER_SECOND
;
510 return datetime
->days
* USEC_PER_DAY
+ datetime
->usec
- offset
;
514 * g_date_time_from_instant:
516 * @instant: a instant in time
518 * Creates a #GDateTime from a time zone and an instant.
520 * This might fail if the time ends up being out of range.
523 g_date_time_from_instant (GTimeZone
*tz
,
529 if (instant
< 0 || instant
> G_GINT64_CONSTANT (1000000000000000000))
532 datetime
= g_date_time_alloc (tz
);
533 datetime
->interval
= g_time_zone_find_interval (tz
,
534 G_TIME_TYPE_UNIVERSAL
,
535 INSTANT_TO_UNIX (instant
));
536 offset
= g_time_zone_get_offset (datetime
->tz
, datetime
->interval
);
537 offset
*= USEC_PER_SECOND
;
541 datetime
->days
= instant
/ USEC_PER_DAY
;
542 datetime
->usec
= instant
% USEC_PER_DAY
;
544 if (datetime
->days
< 1 || 3652059 < datetime
->days
)
546 g_date_time_unref (datetime
);
555 * g_date_time_deal_with_date_change:
556 * @datetime: a #GDateTime
558 * This function should be called whenever the date changes by adding
559 * days, months or years. It does three things.
561 * First, we ensure that the date falls between 0001-01-01 and
562 * 9999-12-31 and return %FALSE if it does not.
564 * Next we update the ->interval field.
566 * Finally, we ensure that the resulting date and time pair exists (by
567 * ensuring that our time zone has an interval containing it) and
568 * adjusting as required. For example, if we have the time 02:30:00 on
569 * March 13 2010 in Toronto and we add 1 day to it, we would end up with
570 * 2:30am on March 14th, which doesn't exist. In that case, we bump the
574 g_date_time_deal_with_date_change (GDateTime
*datetime
)
580 if (datetime
->days
< 1 || datetime
->days
> 3652059)
583 was_dst
= g_time_zone_is_dst (datetime
->tz
, datetime
->interval
);
585 full_time
= datetime
->days
* USEC_PER_DAY
+ datetime
->usec
;
588 usec
= full_time
% USEC_PER_SECOND
;
589 full_time
/= USEC_PER_SECOND
;
590 full_time
-= UNIX_EPOCH_START
* SEC_PER_DAY
;
592 datetime
->interval
= g_time_zone_adjust_time (datetime
->tz
,
595 full_time
+= UNIX_EPOCH_START
* SEC_PER_DAY
;
596 full_time
*= USEC_PER_SECOND
;
599 datetime
->days
= full_time
/ USEC_PER_DAY
;
600 datetime
->usec
= full_time
% USEC_PER_DAY
;
602 /* maybe daylight time caused us to shift to a different day,
603 * but it definitely didn't push us into a different year */
608 g_date_time_replace_days (GDateTime
*datetime
,
613 new = g_date_time_alloc (datetime
->tz
);
614 new->interval
= datetime
->interval
;
615 new->usec
= datetime
->usec
;
618 if (!g_date_time_deal_with_date_change (new))
620 g_date_time_unref (new);
627 /* now/unix/timeval Constructors {{{1 */
630 * g_date_time_new_from_timeval:
634 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
635 * given time zone @tz.
637 * The time contained in a #GTimeVal is always stored in the form of
638 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
641 * This call can fail (returning %NULL) if @tv represents a time outside
642 * of the supported range of #GDateTime.
644 * You should release the return value by calling g_date_time_unref()
645 * when you are done with it.
647 * Returns: a new #GDateTime, or %NULL
652 g_date_time_new_from_timeval (GTimeZone
*tz
,
655 if ((gint64
) tv
->tv_sec
> G_MAXINT64
- 1 ||
656 !UNIX_TO_INSTANT_IS_VALID ((gint64
) tv
->tv_sec
+ 1))
659 return g_date_time_from_instant (tz
, tv
->tv_usec
+
660 UNIX_TO_INSTANT (tv
->tv_sec
));
664 * g_date_time_new_from_unix:
668 * Creates a #GDateTime corresponding to the given Unix time @t in the
669 * given time zone @tz.
671 * Unix time is the number of seconds that have elapsed since 1970-01-01
672 * 00:00:00 UTC, regardless of the time zone given.
674 * This call can fail (returning %NULL) if @t represents a time outside
675 * of the supported range of #GDateTime.
677 * You should release the return value by calling g_date_time_unref()
678 * when you are done with it.
680 * Returns: a new #GDateTime, or %NULL
685 g_date_time_new_from_unix (GTimeZone
*tz
,
688 if (!UNIX_TO_INSTANT_IS_VALID (secs
))
691 return g_date_time_from_instant (tz
, UNIX_TO_INSTANT (secs
));
695 * g_date_time_new_now:
698 * Creates a #GDateTime corresponding to this exact instant in the given
699 * time zone @tz. The time is as accurate as the system allows, to a
700 * maximum accuracy of 1 microsecond.
702 * This function will always succeed unless the system clock is set to
703 * truly insane values (or unless GLib is still being used after the
706 * You should release the return value by calling g_date_time_unref()
707 * when you are done with it.
709 * Returns: a new #GDateTime, or %NULL
714 g_date_time_new_now (GTimeZone
*tz
)
718 g_get_current_time (&tv
);
720 return g_date_time_new_from_timeval (tz
, &tv
);
724 * g_date_time_new_now_local:
726 * Creates a #GDateTime corresponding to this exact instant in the local
729 * This is equivalent to calling g_date_time_new_now() with the time
730 * zone returned by g_time_zone_new_local().
732 * Returns: a new #GDateTime, or %NULL
737 g_date_time_new_now_local (void)
742 local
= g_time_zone_new_local ();
743 datetime
= g_date_time_new_now (local
);
744 g_time_zone_unref (local
);
750 * g_date_time_new_now_utc:
752 * Creates a #GDateTime corresponding to this exact instant in UTC.
754 * This is equivalent to calling g_date_time_new_now() with the time
755 * zone returned by g_time_zone_new_utc().
757 * Returns: a new #GDateTime, or %NULL
762 g_date_time_new_now_utc (void)
767 utc
= g_time_zone_new_utc ();
768 datetime
= g_date_time_new_now (utc
);
769 g_time_zone_unref (utc
);
775 * g_date_time_new_from_unix_local:
778 * Creates a #GDateTime corresponding to the given Unix time @t in the
781 * Unix time is the number of seconds that have elapsed since 1970-01-01
782 * 00:00:00 UTC, regardless of the local time offset.
784 * This call can fail (returning %NULL) if @t represents a time outside
785 * of the supported range of #GDateTime.
787 * You should release the return value by calling g_date_time_unref()
788 * when you are done with it.
790 * Returns: a new #GDateTime, or %NULL
795 g_date_time_new_from_unix_local (gint64 t
)
800 local
= g_time_zone_new_local ();
801 datetime
= g_date_time_new_from_unix (local
, t
);
802 g_time_zone_unref (local
);
808 * g_date_time_new_from_unix_utc:
811 * Creates a #GDateTime corresponding to the given Unix time @t in UTC.
813 * Unix time is the number of seconds that have elapsed since 1970-01-01
816 * This call can fail (returning %NULL) if @t represents a time outside
817 * of the supported range of #GDateTime.
819 * You should release the return value by calling g_date_time_unref()
820 * when you are done with it.
822 * Returns: a new #GDateTime, or %NULL
827 g_date_time_new_from_unix_utc (gint64 t
)
832 utc
= g_time_zone_new_utc ();
833 datetime
= g_date_time_new_from_unix (utc
, t
);
834 g_time_zone_unref (utc
);
840 * g_date_time_new_from_timeval_local:
843 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the
846 * The time contained in a #GTimeVal is always stored in the form of
847 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the
850 * This call can fail (returning %NULL) if @tv represents a time outside
851 * of the supported range of #GDateTime.
853 * You should release the return value by calling g_date_time_unref()
854 * when you are done with it.
856 * Returns: a new #GDateTime, or %NULL
861 g_date_time_new_from_timeval_local (const GTimeVal
*tv
)
866 local
= g_time_zone_new_local ();
867 datetime
= g_date_time_new_from_timeval (local
, tv
);
868 g_time_zone_unref (local
);
874 * g_date_time_new_from_timeval_utc:
877 * Creates a #GDateTime corresponding to the given #GTimeVal @tv in UTC.
879 * The time contained in a #GTimeVal is always stored in the form of
880 * seconds elapsed since 1970-01-01 00:00:00 UTC.
882 * This call can fail (returning %NULL) if @tv represents a time outside
883 * of the supported range of #GDateTime.
885 * You should release the return value by calling g_date_time_unref()
886 * when you are done with it.
888 * Returns: a new #GDateTime, or %NULL
893 g_date_time_new_from_timeval_utc (const GTimeVal
*tv
)
898 utc
= g_time_zone_new_utc ();
899 datetime
= g_date_time_new_from_timeval (utc
, tv
);
900 g_time_zone_unref (utc
);
905 /* Parse integers in the form d (week days), dd (hours etc), ddd (ordinal days) or dddd (years) */
907 get_iso8601_int (const gchar
*text
, gsize length
, gint
*value
)
911 if (length
< 1 || length
> 4)
914 for (i
= 0; i
< length
; i
++)
916 const gchar c
= text
[i
];
917 if (c
< '0' || c
> '9')
919 v
= v
* 10 + (c
- '0');
926 /* Parse seconds in the form ss or ss.sss (variable length decimal) */
928 get_iso8601_seconds (const gchar
*text
, gsize length
, gdouble
*value
)
931 gdouble multiplier
= 0.1, v
= 0;
936 for (i
= 0; i
< 2; i
++)
938 const gchar c
= text
[i
];
939 if (c
< '0' || c
> '9')
941 v
= v
* 10 + (c
- '0');
944 if (length
> 2 && !(text
[i
] == '.' || text
[i
] == ','))
950 for (; i
< length
; i
++)
952 const gchar c
= text
[i
];
953 if (c
< '0' || c
> '9')
955 v
+= (c
- '0') * multiplier
;
964 g_date_time_new_ordinal (GTimeZone
*tz
, gint year
, gint ordinal_day
, gint hour
, gint minute
, gdouble seconds
)
968 if (ordinal_day
< 1 || ordinal_day
> (GREGORIAN_LEAP (year
) ? 366 : 365))
971 dt
= g_date_time_new (tz
, year
, 1, 1, hour
, minute
, seconds
);
972 dt
->days
+= ordinal_day
- 1;
978 g_date_time_new_week (GTimeZone
*tz
, gint year
, gint week
, gint week_day
, gint hour
, gint minute
, gdouble seconds
)
981 gint max_week
, jan4_week_day
, ordinal_day
;
984 p
= (year
* 365 + (year
/ 4) - (year
/ 100) + (year
/ 400)) % 7;
985 max_week
= p
== 4 ? 53 : 52;
987 if (week
< 1 || week
> max_week
|| week_day
< 1 || week_day
> 7)
990 dt
= g_date_time_new (tz
, year
, 1, 4, 0, 0, 0);
991 g_date_time_get_week_number (dt
, NULL
, &jan4_week_day
, NULL
);
992 ordinal_day
= (week
* 7) + week_day
- (jan4_week_day
+ 3);
996 ordinal_day
+= GREGORIAN_LEAP (year
) ? 366 : 365;
998 else if (ordinal_day
> (GREGORIAN_LEAP (year
) ? 366 : 365))
1000 ordinal_day
-= (GREGORIAN_LEAP (year
) ? 366 : 365);
1004 return g_date_time_new_ordinal (tz
, year
, ordinal_day
, hour
, minute
, seconds
);
1008 parse_iso8601_date (const gchar
*text
, gsize length
,
1009 gint hour
, gint minute
, gdouble seconds
, GTimeZone
*tz
)
1012 if (length
== 10 && text
[4] == '-' && text
[7] == '-')
1014 int year
, month
, day
;
1015 if (!get_iso8601_int (text
, 4, &year
) ||
1016 !get_iso8601_int (text
+ 5, 2, &month
) ||
1017 !get_iso8601_int (text
+ 8, 2, &day
))
1019 return g_date_time_new (tz
, year
, month
, day
, hour
, minute
, seconds
);
1022 else if (length
== 8 && text
[4] == '-')
1024 gint year
, ordinal_day
;
1025 if (!get_iso8601_int (text
, 4, &year
) ||
1026 !get_iso8601_int (text
+ 5, 3, &ordinal_day
))
1028 return g_date_time_new_ordinal (tz
, year
, ordinal_day
, hour
, minute
, seconds
);
1031 else if (length
== 10 && text
[4] == '-' && text
[5] == 'W' && text
[8] == '-')
1033 gint year
, week
, week_day
;
1034 if (!get_iso8601_int (text
, 4, &year
) ||
1035 !get_iso8601_int (text
+ 6, 2, &week
) ||
1036 !get_iso8601_int (text
+ 9, 1, &week_day
))
1038 return g_date_time_new_week (tz
, year
, week
, week_day
, hour
, minute
, seconds
);
1041 else if (length
== 8 && text
[4] == 'W')
1043 gint year
, week
, week_day
;
1044 if (!get_iso8601_int (text
, 4, &year
) ||
1045 !get_iso8601_int (text
+ 5, 2, &week
) ||
1046 !get_iso8601_int (text
+ 7, 1, &week_day
))
1048 return g_date_time_new_week (tz
, year
, week
, week_day
, hour
, minute
, seconds
);
1051 else if (length
== 8)
1053 int year
, month
, day
;
1054 if (!get_iso8601_int (text
, 4, &year
) ||
1055 !get_iso8601_int (text
+ 4, 2, &month
) ||
1056 !get_iso8601_int (text
+ 6, 2, &day
))
1058 return g_date_time_new (tz
, year
, month
, day
, hour
, minute
, seconds
);
1061 else if (length
== 7)
1063 gint year
, ordinal_day
;
1064 if (!get_iso8601_int (text
, 4, &year
) ||
1065 !get_iso8601_int (text
+ 4, 3, &ordinal_day
))
1067 return g_date_time_new_ordinal (tz
, year
, ordinal_day
, hour
, minute
, seconds
);
1074 parse_iso8601_timezone (const gchar
*text
, gsize length
, gssize
*tz_offset
)
1076 gint i
, tz_length
, offset_sign
= 1, offset_hours
, offset_minutes
;
1079 /* UTC uses Z suffix */
1080 if (length
> 0 && text
[length
- 1] == 'Z')
1082 *tz_offset
= length
- 1;
1083 return g_time_zone_new_utc ();
1086 /* Look for '+' or '-' of offset */
1087 for (i
= length
- 1; i
>= 0; i
--)
1088 if (text
[i
] == '+' || text
[i
] == '-')
1090 offset_sign
= text
[i
] == '-' ? -1 : 1;
1095 tz_length
= length
- i
;
1097 /* +hh:mm or -hh:mm */
1098 if (tz_length
== 6 && text
[i
+3] == ':')
1100 if (!get_iso8601_int (text
+ i
+ 1, 2, &offset_hours
) ||
1101 !get_iso8601_int (text
+ i
+ 4, 2, &offset_minutes
))
1104 /* +hhmm or -hhmm */
1105 else if (tz_length
== 5)
1107 if (!get_iso8601_int (text
+ i
+ 1, 2, &offset_hours
) ||
1108 !get_iso8601_int (text
+ i
+ 3, 2, &offset_minutes
))
1112 else if (tz_length
== 3)
1114 if (!get_iso8601_int (text
+ i
+ 1, 2, &offset_hours
))
1122 tz
= g_time_zone_new (text
+ i
);
1124 /* Double-check that the GTimeZone matches our interpretation of the timezone.
1125 * Failure would indicate a bug either here of in the GTimeZone code. */
1126 g_assert (g_time_zone_get_offset (tz
, 0) == offset_sign
* (offset_hours
* 3600 + offset_minutes
* 60));
1132 parse_iso8601_time (const gchar
*text
, gsize length
,
1133 gint
*hour
, gint
*minute
, gdouble
*seconds
, GTimeZone
**tz
)
1135 gssize tz_offset
= -1;
1137 /* Check for timezone suffix */
1138 *tz
= parse_iso8601_timezone (text
, length
, &tz_offset
);
1142 /* hh:mm:ss(.sss) */
1143 if (length
>= 8 && text
[2] == ':' && text
[5] == ':')
1145 return get_iso8601_int (text
, 2, hour
) &&
1146 get_iso8601_int (text
+ 3, 2, minute
) &&
1147 get_iso8601_seconds (text
+ 6, length
- 6, seconds
);
1150 else if (length
>= 6)
1152 return get_iso8601_int (text
, 2, hour
) &&
1153 get_iso8601_int (text
+ 2, 2, minute
) &&
1154 get_iso8601_seconds (text
+ 4, length
- 4, seconds
);
1161 * g_date_time_new_from_iso8601:
1162 * @text: an ISO 8601 formatted time string.
1163 * @default_tz: (nullable): a #GTimeZone to use if the text doesn't contain a
1164 * timezone, or %NULL.
1166 * Creates a #GDateTime corresponding to the given
1167 * [ISO 8601 formatted string](https://en.wikipedia.org/wiki/ISO_8601)
1168 * @text. ISO 8601 strings of the form <date><sep><time><tz> are supported.
1170 * <sep> is the separator and can be either 'T', 't' or ' '.
1172 * <date> is in the form:
1174 * - `YYYY-MM-DD` - Year/month/day, e.g. 2016-08-24.
1175 * - `YYYYMMDD` - Same as above without dividers.
1176 * - `YYYY-DDD` - Ordinal day where DDD is from 001 to 366, e.g. 2016-237.
1177 * - `YYYYDDD` - Same as above without dividers.
1178 * - `YYYY-Www-D` - Week day where ww is from 01 to 52 and D from 1-7,
1180 * - `YYYYWwwD` - Same as above without dividers.
1182 * <time> is in the form:
1184 * - `hh:mm:ss(.sss)` - Hours, minutes, seconds (subseconds), e.g. 22:10:42.123.
1185 * - `hhmmss(.sss)` - Same as above without dividers.
1187 * <tz> is an optional timezone suffix of the form:
1190 * - `+hh:mm` or `-hh:mm` - Offset from UTC in hours and minutes, e.g. +12:00.
1191 * - `+hh` or `-hh` - Offset from UTC in hours, e.g. +12.
1193 * If the timezone is not provided in @text it must be provided in @default_tz
1194 * (this field is otherwise ignored).
1196 * This call can fail (returning %NULL) if @text is not a valid ISO 8601
1199 * You should release the return value by calling g_date_time_unref()
1200 * when you are done with it.
1202 * Returns: (transfer full) (nullable): a new #GDateTime, or %NULL
1207 g_date_time_new_from_iso8601 (const gchar
*text
, GTimeZone
*default_tz
)
1209 gint length
, date_length
= -1;
1210 gint hour
= 0, minute
= 0;
1211 gdouble seconds
= 0.0;
1212 GTimeZone
*tz
= NULL
;
1213 GDateTime
*datetime
= NULL
;
1215 g_return_val_if_fail (text
!= NULL
, NULL
);
1217 /* Count length of string and find date / time separator ('T', 't', or ' ') */
1218 for (length
= 0; text
[length
] != '\0'; length
++)
1220 if (date_length
< 0 && (text
[length
] == 'T' || text
[length
] == 't' || text
[length
] == ' '))
1221 date_length
= length
;
1224 if (date_length
< 0)
1227 if (!parse_iso8601_time (text
+ date_length
+ 1, length
- (date_length
+ 1),
1228 &hour
, &minute
, &seconds
, &tz
))
1230 if (tz
== NULL
&& default_tz
== NULL
)
1233 datetime
= parse_iso8601_date (text
, date_length
, hour
, minute
, seconds
, tz
? tz
: default_tz
);
1237 g_time_zone_unref (tz
);
1241 /* full new functions {{{1 */
1246 * @year: the year component of the date
1247 * @month: the month component of the date
1248 * @day: the day component of the date
1249 * @hour: the hour component of the date
1250 * @minute: the minute component of the date
1251 * @seconds: the number of seconds past the minute
1253 * Creates a new #GDateTime corresponding to the given date and time in
1254 * the time zone @tz.
1256 * The @year must be between 1 and 9999, @month between 1 and 12 and @day
1257 * between 1 and 28, 29, 30 or 31 depending on the month and the year.
1259 * @hour must be between 0 and 23 and @minute must be between 0 and 59.
1261 * @seconds must be at least 0.0 and must be strictly less than 60.0.
1262 * It will be rounded down to the nearest microsecond.
1264 * If the given time is not representable in the given time zone (for
1265 * example, 02:30 on March 14th 2010 in Toronto, due to daylight savings
1266 * time) then the time will be rounded up to the nearest existing time
1267 * (in this case, 03:00). If this matters to you then you should verify
1268 * the return value for containing the same as the numbers you gave.
1270 * In the case that the given time is ambiguous in the given time zone
1271 * (for example, 01:30 on November 7th 2010 in Toronto, due to daylight
1272 * savings time) then the time falling within standard (ie:
1273 * non-daylight) time is taken.
1275 * It not considered a programmer error for the values to this function
1276 * to be out of range, but in the case that they are, the function will
1279 * You should release the return value by calling g_date_time_unref()
1280 * when you are done with it.
1282 * Returns: a new #GDateTime, or %NULL
1287 g_date_time_new (GTimeZone
*tz
,
1295 GDateTime
*datetime
;
1299 g_return_val_if_fail (tz
!= NULL
, NULL
);
1301 if (year
< 1 || year
> 9999 ||
1302 month
< 1 || month
> 12 ||
1303 day
< 1 || day
> days_in_months
[GREGORIAN_LEAP (year
)][month
] ||
1304 hour
< 0 || hour
> 23 ||
1305 minute
< 0 || minute
> 59 ||
1306 seconds
< 0.0 || seconds
>= 60.0)
1309 datetime
= g_date_time_alloc (tz
);
1310 datetime
->days
= ymd_to_days (year
, month
, day
);
1311 datetime
->usec
= (hour
* USEC_PER_HOUR
)
1312 + (minute
* USEC_PER_MINUTE
)
1313 + (gint64
) (seconds
* USEC_PER_SECOND
);
1315 full_time
= SEC_PER_DAY
*
1316 (ymd_to_days (year
, month
, day
) - UNIX_EPOCH_START
) +
1317 SECS_PER_HOUR
* hour
+
1318 SECS_PER_MINUTE
* minute
+
1321 datetime
->interval
= g_time_zone_adjust_time (datetime
->tz
,
1322 G_TIME_TYPE_STANDARD
,
1325 /* This is the correct way to convert a scaled FP value to integer.
1326 * If this surprises you, please observe that (int)(1.000001 * 1e6)
1327 * is 1000000. This is not a problem with precision, it's just how
1329 * See https://bugzilla.gnome.org/show_bug.cgi?id=697715. */
1330 usec
= seconds
* USEC_PER_SECOND
;
1331 if ((usec
+ 1) * 1e-6 <= seconds
) {
1335 full_time
+= UNIX_EPOCH_START
* SEC_PER_DAY
;
1336 datetime
->days
= full_time
/ SEC_PER_DAY
;
1337 datetime
->usec
= (full_time
% SEC_PER_DAY
) * USEC_PER_SECOND
;
1338 datetime
->usec
+= usec
% USEC_PER_SECOND
;
1344 * g_date_time_new_local:
1345 * @year: the year component of the date
1346 * @month: the month component of the date
1347 * @day: the day component of the date
1348 * @hour: the hour component of the date
1349 * @minute: the minute component of the date
1350 * @seconds: the number of seconds past the minute
1352 * Creates a new #GDateTime corresponding to the given date and time in
1353 * the local time zone.
1355 * This call is equivalent to calling g_date_time_new() with the time
1356 * zone returned by g_time_zone_new_local().
1358 * Returns: a #GDateTime, or %NULL
1363 g_date_time_new_local (gint year
,
1370 GDateTime
*datetime
;
1373 local
= g_time_zone_new_local ();
1374 datetime
= g_date_time_new (local
, year
, month
, day
, hour
, minute
, seconds
);
1375 g_time_zone_unref (local
);
1381 * g_date_time_new_utc:
1382 * @year: the year component of the date
1383 * @month: the month component of the date
1384 * @day: the day component of the date
1385 * @hour: the hour component of the date
1386 * @minute: the minute component of the date
1387 * @seconds: the number of seconds past the minute
1389 * Creates a new #GDateTime corresponding to the given date and time in
1392 * This call is equivalent to calling g_date_time_new() with the time
1393 * zone returned by g_time_zone_new_utc().
1395 * Returns: a #GDateTime, or %NULL
1400 g_date_time_new_utc (gint year
,
1407 GDateTime
*datetime
;
1410 utc
= g_time_zone_new_utc ();
1411 datetime
= g_date_time_new (utc
, year
, month
, day
, hour
, minute
, seconds
);
1412 g_time_zone_unref (utc
);
1421 * @datetime: a #GDateTime
1422 * @timespan: a #GTimeSpan
1424 * Creates a copy of @datetime and adds the specified timespan to the copy.
1426 * Returns: the newly created #GDateTime which should be freed with
1427 * g_date_time_unref().
1432 g_date_time_add (GDateTime
*datetime
,
1435 return g_date_time_from_instant (datetime
->tz
, timespan
+
1436 g_date_time_to_instant (datetime
));
1440 * g_date_time_add_years:
1441 * @datetime: a #GDateTime
1442 * @years: the number of years
1444 * Creates a copy of @datetime and adds the specified number of years to the
1445 * copy. Add negative values to subtract years.
1447 * Returns: the newly created #GDateTime which should be freed with
1448 * g_date_time_unref().
1453 g_date_time_add_years (GDateTime
*datetime
,
1456 gint year
, month
, day
;
1458 g_return_val_if_fail (datetime
!= NULL
, NULL
);
1460 if (years
< -10000 || years
> 10000)
1463 g_date_time_get_ymd (datetime
, &year
, &month
, &day
);
1466 /* only possible issue is if we've entered a year with no February 29
1468 if (month
== 2 && day
== 29 && !GREGORIAN_LEAP (year
))
1471 return g_date_time_replace_days (datetime
, ymd_to_days (year
, month
, day
));
1475 * g_date_time_add_months:
1476 * @datetime: a #GDateTime
1477 * @months: the number of months
1479 * Creates a copy of @datetime and adds the specified number of months to the
1480 * copy. Add negative values to subtract months.
1482 * Returns: the newly created #GDateTime which should be freed with
1483 * g_date_time_unref().
1488 g_date_time_add_months (GDateTime
*datetime
,
1491 gint year
, month
, day
;
1493 g_return_val_if_fail (datetime
!= NULL
, NULL
);
1494 g_date_time_get_ymd (datetime
, &year
, &month
, &day
);
1496 if (months
< -120000 || months
> 120000)
1499 year
+= months
/ 12;
1500 month
+= months
% 12;
1506 else if (month
> 12)
1512 day
= MIN (day
, days_in_months
[GREGORIAN_LEAP (year
)][month
]);
1514 return g_date_time_replace_days (datetime
, ymd_to_days (year
, month
, day
));
1518 * g_date_time_add_weeks:
1519 * @datetime: a #GDateTime
1520 * @weeks: the number of weeks
1522 * Creates a copy of @datetime and adds the specified number of weeks to the
1523 * copy. Add negative values to subtract weeks.
1525 * Returns: the newly created #GDateTime which should be freed with
1526 * g_date_time_unref().
1531 g_date_time_add_weeks (GDateTime
*datetime
,
1534 g_return_val_if_fail (datetime
!= NULL
, NULL
);
1536 return g_date_time_add_days (datetime
, weeks
* 7);
1540 * g_date_time_add_days:
1541 * @datetime: a #GDateTime
1542 * @days: the number of days
1544 * Creates a copy of @datetime and adds the specified number of days to the
1545 * copy. Add negative values to subtract days.
1547 * Returns: the newly created #GDateTime which should be freed with
1548 * g_date_time_unref().
1553 g_date_time_add_days (GDateTime
*datetime
,
1556 g_return_val_if_fail (datetime
!= NULL
, NULL
);
1558 if (days
< -3660000 || days
> 3660000)
1561 return g_date_time_replace_days (datetime
, datetime
->days
+ days
);
1565 * g_date_time_add_hours:
1566 * @datetime: a #GDateTime
1567 * @hours: the number of hours to add
1569 * Creates a copy of @datetime and adds the specified number of hours.
1570 * Add negative values to subtract hours.
1572 * Returns: the newly created #GDateTime which should be freed with
1573 * g_date_time_unref().
1578 g_date_time_add_hours (GDateTime
*datetime
,
1581 return g_date_time_add (datetime
, hours
* USEC_PER_HOUR
);
1585 * g_date_time_add_minutes:
1586 * @datetime: a #GDateTime
1587 * @minutes: the number of minutes to add
1589 * Creates a copy of @datetime adding the specified number of minutes.
1590 * Add negative values to subtract minutes.
1592 * Returns: the newly created #GDateTime which should be freed with
1593 * g_date_time_unref().
1598 g_date_time_add_minutes (GDateTime
*datetime
,
1601 return g_date_time_add (datetime
, minutes
* USEC_PER_MINUTE
);
1606 * g_date_time_add_seconds:
1607 * @datetime: a #GDateTime
1608 * @seconds: the number of seconds to add
1610 * Creates a copy of @datetime and adds the specified number of seconds.
1611 * Add negative values to subtract seconds.
1613 * Returns: the newly created #GDateTime which should be freed with
1614 * g_date_time_unref().
1619 g_date_time_add_seconds (GDateTime
*datetime
,
1622 return g_date_time_add (datetime
, seconds
* USEC_PER_SECOND
);
1626 * g_date_time_add_full:
1627 * @datetime: a #GDateTime
1628 * @years: the number of years to add
1629 * @months: the number of months to add
1630 * @days: the number of days to add
1631 * @hours: the number of hours to add
1632 * @minutes: the number of minutes to add
1633 * @seconds: the number of seconds to add
1635 * Creates a new #GDateTime adding the specified values to the current date and
1636 * time in @datetime. Add negative values to subtract.
1638 * Returns: the newly created #GDateTime that should be freed with
1639 * g_date_time_unref().
1644 g_date_time_add_full (GDateTime
*datetime
,
1652 gint year
, month
, day
;
1657 g_return_val_if_fail (datetime
!= NULL
, NULL
);
1658 g_date_time_get_ymd (datetime
, &year
, &month
, &day
);
1660 months
+= years
* 12;
1662 if (months
< -120000 || months
> 120000)
1665 if (days
< -3660000 || days
> 3660000)
1668 year
+= months
/ 12;
1669 month
+= months
% 12;
1675 else if (month
> 12)
1681 day
= MIN (day
, days_in_months
[GREGORIAN_LEAP (year
)][month
]);
1683 /* full_time is now in unix (local) time */
1684 full_time
= datetime
->usec
/ USEC_PER_SECOND
+ SEC_PER_DAY
*
1685 (ymd_to_days (year
, month
, day
) + days
- UNIX_EPOCH_START
);
1687 interval
= g_time_zone_adjust_time (datetime
->tz
,
1688 g_time_zone_is_dst (datetime
->tz
,
1689 datetime
->interval
),
1692 /* move to UTC unix time */
1693 full_time
-= g_time_zone_get_offset (datetime
->tz
, interval
);
1695 /* convert back to an instant, add back fractional seconds */
1696 full_time
+= UNIX_EPOCH_START
* SEC_PER_DAY
;
1697 full_time
= full_time
* USEC_PER_SECOND
+
1698 datetime
->usec
% USEC_PER_SECOND
;
1700 /* do the actual addition now */
1701 full_time
+= (hours
* USEC_PER_HOUR
) +
1702 (minutes
* USEC_PER_MINUTE
) +
1703 (gint64
) (seconds
* USEC_PER_SECOND
);
1705 /* find the new interval */
1706 interval
= g_time_zone_find_interval (datetime
->tz
,
1707 G_TIME_TYPE_UNIVERSAL
,
1708 INSTANT_TO_UNIX (full_time
));
1710 /* convert back into local time */
1711 full_time
+= USEC_PER_SECOND
*
1712 g_time_zone_get_offset (datetime
->tz
, interval
);
1714 /* split into days and usec of a new datetime */
1715 new = g_date_time_alloc (datetime
->tz
);
1716 new->interval
= interval
;
1717 new->days
= full_time
/ USEC_PER_DAY
;
1718 new->usec
= full_time
% USEC_PER_DAY
;
1725 /* Compare, difference, hash, equal {{{1 */
1727 * g_date_time_compare:
1728 * @dt1: (not nullable): first #GDateTime to compare
1729 * @dt2: (not nullable): second #GDateTime to compare
1731 * A comparison function for #GDateTimes that is suitable
1732 * as a #GCompareFunc. Both #GDateTimes must be non-%NULL.
1734 * Returns: -1, 0 or 1 if @dt1 is less than, equal to or greater
1740 g_date_time_compare (gconstpointer dt1
,
1745 difference
= g_date_time_difference ((GDateTime
*) dt1
, (GDateTime
*) dt2
);
1750 else if (difference
> 0)
1758 * g_date_time_difference:
1759 * @end: a #GDateTime
1760 * @begin: a #GDateTime
1762 * Calculates the difference in time between @end and @begin. The
1763 * #GTimeSpan that is returned is effectively @end - @begin (ie:
1764 * positive if the first parameter is larger).
1766 * Returns: the difference between the two #GDateTime, as a time
1767 * span expressed in microseconds.
1772 g_date_time_difference (GDateTime
*end
,
1775 g_return_val_if_fail (begin
!= NULL
, 0);
1776 g_return_val_if_fail (end
!= NULL
, 0);
1778 return g_date_time_to_instant (end
) -
1779 g_date_time_to_instant (begin
);
1784 * @datetime: (not nullable): a #GDateTime
1786 * Hashes @datetime into a #guint, suitable for use within #GHashTable.
1788 * Returns: a #guint containing the hash
1793 g_date_time_hash (gconstpointer datetime
)
1795 return g_date_time_to_instant ((GDateTime
*) datetime
);
1799 * g_date_time_equal:
1800 * @dt1: (not nullable): a #GDateTime
1801 * @dt2: (not nullable): a #GDateTime
1803 * Checks to see if @dt1 and @dt2 are equal.
1805 * Equal here means that they represent the same moment after converting
1806 * them to the same time zone.
1808 * Returns: %TRUE if @dt1 and @dt2 are equal
1813 g_date_time_equal (gconstpointer dt1
,
1816 return g_date_time_difference ((GDateTime
*) dt1
, (GDateTime
*) dt2
) == 0;
1819 /* Year, Month, Day Getters {{{1 */
1821 * g_date_time_get_ymd:
1822 * @datetime: a #GDateTime.
1823 * @year: (out) (optional): the return location for the gregorian year, or %NULL.
1824 * @month: (out) (optional): the return location for the month of the year, or %NULL.
1825 * @day: (out) (optional): the return location for the day of the month, or %NULL.
1827 * Retrieves the Gregorian day, month, and year of a given #GDateTime.
1832 g_date_time_get_ymd (GDateTime
*datetime
,
1840 gint remaining_days
;
1847 g_return_if_fail (datetime
!= NULL
);
1849 remaining_days
= datetime
->days
;
1852 * We need to convert an offset in days to its year/month/day representation.
1853 * Leap years makes this a little trickier than it should be, so we use
1854 * 400, 100 and 4 years cycles here to get to the correct year.
1857 /* Our days offset starts sets 0001-01-01 as day 1, if it was day 0 our
1858 * math would be simpler, so let's do it */
1861 the_year
= (remaining_days
/ DAYS_IN_400YEARS
) * 400 + 1;
1862 remaining_days
= remaining_days
% DAYS_IN_400YEARS
;
1864 y100_cycles
= remaining_days
/ DAYS_IN_100YEARS
;
1865 remaining_days
= remaining_days
% DAYS_IN_100YEARS
;
1866 the_year
+= y100_cycles
* 100;
1868 y4_cycles
= remaining_days
/ DAYS_IN_4YEARS
;
1869 remaining_days
= remaining_days
% DAYS_IN_4YEARS
;
1870 the_year
+= y4_cycles
* 4;
1872 y1_cycles
= remaining_days
/ 365;
1873 the_year
+= y1_cycles
;
1874 remaining_days
= remaining_days
% 365;
1876 if (y1_cycles
== 4 || y100_cycles
== 4) {
1877 g_assert (remaining_days
== 0);
1879 /* special case that indicates that the date is actually one year before,
1880 * in the 31th of December */
1887 /* now get the month and the day */
1888 leap
= y1_cycles
== 3 && (y4_cycles
!= 24 || y100_cycles
== 3);
1890 g_assert (leap
== GREGORIAN_LEAP(the_year
));
1892 the_month
= (remaining_days
+ 50) >> 5;
1893 preceding
= (days_in_year
[0][the_month
- 1] + (the_month
> 2 && leap
));
1894 if (preceding
> remaining_days
)
1896 /* estimate is too large */
1898 preceding
-= leap
? days_in_months
[1][the_month
]
1899 : days_in_months
[0][the_month
];
1902 remaining_days
-= preceding
;
1903 g_assert(0 <= remaining_days
);
1905 the_day
= remaining_days
+ 1;
1917 * g_date_time_get_year:
1918 * @datetime: A #GDateTime
1920 * Retrieves the year represented by @datetime in the Gregorian calendar.
1922 * Returns: the year represented by @datetime
1927 g_date_time_get_year (GDateTime
*datetime
)
1931 g_return_val_if_fail (datetime
!= NULL
, 0);
1933 g_date_time_get_ymd (datetime
, &year
, NULL
, NULL
);
1939 * g_date_time_get_month:
1940 * @datetime: a #GDateTime
1942 * Retrieves the month of the year represented by @datetime in the Gregorian
1945 * Returns: the month represented by @datetime
1950 g_date_time_get_month (GDateTime
*datetime
)
1954 g_return_val_if_fail (datetime
!= NULL
, 0);
1956 g_date_time_get_ymd (datetime
, NULL
, &month
, NULL
);
1962 * g_date_time_get_day_of_month:
1963 * @datetime: a #GDateTime
1965 * Retrieves the day of the month represented by @datetime in the gregorian
1968 * Returns: the day of the month
1973 g_date_time_get_day_of_month (GDateTime
*datetime
)
1977 const guint16
*days
;
1980 g_return_val_if_fail (datetime
!= NULL
, 0);
1982 days
= days_in_year
[GREGORIAN_LEAP (g_date_time_get_year (datetime
)) ? 1 : 0];
1983 g_date_time_get_week_number (datetime
, NULL
, NULL
, &day_of_year
);
1985 for (i
= 1; i
<= 12; i
++)
1987 if (days
[i
] >= day_of_year
)
1988 return day_of_year
- last
;
1992 g_warn_if_reached ();
1996 /* Week of year / day of week getters {{{1 */
1998 * g_date_time_get_week_numbering_year:
1999 * @datetime: a #GDateTime
2001 * Returns the ISO 8601 week-numbering year in which the week containing
2004 * This function, taken together with g_date_time_get_week_of_year() and
2005 * g_date_time_get_day_of_week() can be used to determine the full ISO
2006 * week date on which @datetime falls.
2008 * This is usually equal to the normal Gregorian year (as returned by
2009 * g_date_time_get_year()), except as detailed below:
2011 * For Thursday, the week-numbering year is always equal to the usual
2012 * calendar year. For other days, the number is such that every day
2013 * within a complete week (Monday to Sunday) is contained within the
2014 * same week-numbering year.
2016 * For Monday, Tuesday and Wednesday occurring near the end of the year,
2017 * this may mean that the week-numbering year is one greater than the
2018 * calendar year (so that these days have the same week-numbering year
2019 * as the Thursday occurring early in the next year).
2021 * For Friday, Saturday and Sunday occurring near the start of the year,
2022 * this may mean that the week-numbering year is one less than the
2023 * calendar year (so that these days have the same week-numbering year
2024 * as the Thursday occurring late in the previous year).
2026 * An equivalent description is that the week-numbering year is equal to
2027 * the calendar year containing the majority of the days in the current
2028 * week (Monday to Sunday).
2030 * Note that January 1 0001 in the proleptic Gregorian calendar is a
2031 * Monday, so this function never returns 0.
2033 * Returns: the ISO 8601 week-numbering year for @datetime
2038 g_date_time_get_week_numbering_year (GDateTime
*datetime
)
2040 gint year
, month
, day
, weekday
;
2042 g_date_time_get_ymd (datetime
, &year
, &month
, &day
);
2043 weekday
= g_date_time_get_day_of_week (datetime
);
2045 /* January 1, 2, 3 might be in the previous year if they occur after
2048 * Jan 1: Friday, Saturday, Sunday => day 1: weekday 5, 6, 7
2049 * Jan 2: Saturday, Sunday => day 2: weekday 6, 7
2050 * Jan 3: Sunday => day 3: weekday 7
2052 * So we have a special case if (day - weekday) <= -4
2054 if (month
== 1 && (day
- weekday
) <= -4)
2057 /* December 29, 30, 31 might be in the next year if they occur before
2060 * Dec 31: Monday, Tuesday, Wednesday => day 31: weekday 1, 2, 3
2061 * Dec 30: Monday, Tuesday => day 30: weekday 1, 2
2062 * Dec 29: Monday => day 29: weekday 1
2064 * So we have a special case if (day - weekday) >= 28
2066 else if (month
== 12 && (day
- weekday
) >= 28)
2074 * g_date_time_get_week_of_year:
2075 * @datetime: a #GDateTime
2077 * Returns the ISO 8601 week number for the week containing @datetime.
2078 * The ISO 8601 week number is the same for every day of the week (from
2079 * Moday through Sunday). That can produce some unusual results
2080 * (described below).
2082 * The first week of the year is week 1. This is the week that contains
2083 * the first Thursday of the year. Equivalently, this is the first week
2084 * that has more than 4 of its days falling within the calendar year.
2086 * The value 0 is never returned by this function. Days contained
2087 * within a year but occurring before the first ISO 8601 week of that
2088 * year are considered as being contained in the last week of the
2089 * previous year. Similarly, the final days of a calendar year may be
2090 * considered as being part of the first ISO 8601 week of the next year
2091 * if 4 or more days of that week are contained within the new year.
2093 * Returns: the ISO 8601 week number for @datetime.
2098 g_date_time_get_week_of_year (GDateTime
*datetime
)
2102 g_return_val_if_fail (datetime
!= NULL
, 0);
2104 g_date_time_get_week_number (datetime
, &weeknum
, NULL
, NULL
);
2110 * g_date_time_get_day_of_week:
2111 * @datetime: a #GDateTime
2113 * Retrieves the ISO 8601 day of the week on which @datetime falls (1 is
2114 * Monday, 2 is Tuesday... 7 is Sunday).
2116 * Returns: the day of the week
2121 g_date_time_get_day_of_week (GDateTime
*datetime
)
2123 g_return_val_if_fail (datetime
!= NULL
, 0);
2125 return (datetime
->days
- 1) % 7 + 1;
2128 /* Day of year getter {{{1 */
2130 * g_date_time_get_day_of_year:
2131 * @datetime: a #GDateTime
2133 * Retrieves the day of the year represented by @datetime in the Gregorian
2136 * Returns: the day of the year
2141 g_date_time_get_day_of_year (GDateTime
*datetime
)
2145 g_return_val_if_fail (datetime
!= NULL
, 0);
2147 g_date_time_get_week_number (datetime
, NULL
, NULL
, &doy
);
2151 /* Time component getters {{{1 */
2154 * g_date_time_get_hour:
2155 * @datetime: a #GDateTime
2157 * Retrieves the hour of the day represented by @datetime
2159 * Returns: the hour of the day
2164 g_date_time_get_hour (GDateTime
*datetime
)
2166 g_return_val_if_fail (datetime
!= NULL
, 0);
2168 return (datetime
->usec
/ USEC_PER_HOUR
);
2172 * g_date_time_get_minute:
2173 * @datetime: a #GDateTime
2175 * Retrieves the minute of the hour represented by @datetime
2177 * Returns: the minute of the hour
2182 g_date_time_get_minute (GDateTime
*datetime
)
2184 g_return_val_if_fail (datetime
!= NULL
, 0);
2186 return (datetime
->usec
% USEC_PER_HOUR
) / USEC_PER_MINUTE
;
2190 * g_date_time_get_second:
2191 * @datetime: a #GDateTime
2193 * Retrieves the second of the minute represented by @datetime
2195 * Returns: the second represented by @datetime
2200 g_date_time_get_second (GDateTime
*datetime
)
2202 g_return_val_if_fail (datetime
!= NULL
, 0);
2204 return (datetime
->usec
% USEC_PER_MINUTE
) / USEC_PER_SECOND
;
2208 * g_date_time_get_microsecond:
2209 * @datetime: a #GDateTime
2211 * Retrieves the microsecond of the date represented by @datetime
2213 * Returns: the microsecond of the second
2218 g_date_time_get_microsecond (GDateTime
*datetime
)
2220 g_return_val_if_fail (datetime
!= NULL
, 0);
2222 return (datetime
->usec
% USEC_PER_SECOND
);
2226 * g_date_time_get_seconds:
2227 * @datetime: a #GDateTime
2229 * Retrieves the number of seconds since the start of the last minute,
2230 * including the fractional part.
2232 * Returns: the number of seconds
2237 g_date_time_get_seconds (GDateTime
*datetime
)
2239 g_return_val_if_fail (datetime
!= NULL
, 0);
2241 return (datetime
->usec
% USEC_PER_MINUTE
) / 1000000.0;
2244 /* Exporters {{{1 */
2246 * g_date_time_to_unix:
2247 * @datetime: a #GDateTime
2249 * Gives the Unix time corresponding to @datetime, rounding down to the
2252 * Unix time is the number of seconds that have elapsed since 1970-01-01
2253 * 00:00:00 UTC, regardless of the time zone associated with @datetime.
2255 * Returns: the Unix time corresponding to @datetime
2260 g_date_time_to_unix (GDateTime
*datetime
)
2262 return INSTANT_TO_UNIX (g_date_time_to_instant (datetime
));
2266 * g_date_time_to_timeval:
2267 * @datetime: a #GDateTime
2268 * @tv: a #GTimeVal to modify
2270 * Stores the instant in time that @datetime represents into @tv.
2272 * The time contained in a #GTimeVal is always stored in the form of
2273 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the time
2274 * zone associated with @datetime.
2276 * On systems where 'long' is 32bit (ie: all 32bit systems and all
2277 * Windows systems), a #GTimeVal is incapable of storing the entire
2278 * range of values that #GDateTime is capable of expressing. On those
2279 * systems, this function returns %FALSE to indicate that the time is
2282 * On systems where 'long' is 64bit, this function never fails.
2284 * Returns: %TRUE if successful, else %FALSE
2289 g_date_time_to_timeval (GDateTime
*datetime
,
2292 tv
->tv_sec
= INSTANT_TO_UNIX (g_date_time_to_instant (datetime
));
2293 tv
->tv_usec
= datetime
->usec
% USEC_PER_SECOND
;
2298 /* Timezone queries {{{1 */
2300 * g_date_time_get_utc_offset:
2301 * @datetime: a #GDateTime
2303 * Determines the offset to UTC in effect at the time and in the time
2304 * zone of @datetime.
2306 * The offset is the number of microseconds that you add to UTC time to
2307 * arrive at local time for the time zone (ie: negative numbers for time
2308 * zones west of GMT, positive numbers for east).
2310 * If @datetime represents UTC time, then the offset is always zero.
2312 * Returns: the number of microseconds that should be added to UTC to
2313 * get the local time
2318 g_date_time_get_utc_offset (GDateTime
*datetime
)
2322 g_return_val_if_fail (datetime
!= NULL
, 0);
2324 offset
= g_time_zone_get_offset (datetime
->tz
, datetime
->interval
);
2326 return (gint64
) offset
* USEC_PER_SECOND
;
2330 * g_date_time_get_timezone_abbreviation:
2331 * @datetime: a #GDateTime
2333 * Determines the time zone abbreviation to be used at the time and in
2334 * the time zone of @datetime.
2336 * For example, in Toronto this is currently "EST" during the winter
2337 * months and "EDT" during the summer months when daylight savings
2338 * time is in effect.
2340 * Returns: (transfer none): the time zone abbreviation. The returned
2341 * string is owned by the #GDateTime and it should not be
2347 g_date_time_get_timezone_abbreviation (GDateTime
*datetime
)
2349 g_return_val_if_fail (datetime
!= NULL
, NULL
);
2351 return g_time_zone_get_abbreviation (datetime
->tz
, datetime
->interval
);
2355 * g_date_time_is_daylight_savings:
2356 * @datetime: a #GDateTime
2358 * Determines if daylight savings time is in effect at the time and in
2359 * the time zone of @datetime.
2361 * Returns: %TRUE if daylight savings time is in effect
2366 g_date_time_is_daylight_savings (GDateTime
*datetime
)
2368 g_return_val_if_fail (datetime
!= NULL
, FALSE
);
2370 return g_time_zone_is_dst (datetime
->tz
, datetime
->interval
);
2373 /* Timezone convert {{{1 */
2375 * g_date_time_to_timezone:
2376 * @datetime: a #GDateTime
2377 * @tz: the new #GTimeZone
2379 * Create a new #GDateTime corresponding to the same instant in time as
2380 * @datetime, but in the time zone @tz.
2382 * This call can fail in the case that the time goes out of bounds. For
2383 * example, converting 0001-01-01 00:00:00 UTC to a time zone west of
2384 * Greenwich will fail (due to the year 0 being out of range).
2386 * You should release the return value by calling g_date_time_unref()
2387 * when you are done with it.
2389 * Returns: a new #GDateTime, or %NULL
2394 g_date_time_to_timezone (GDateTime
*datetime
,
2397 return g_date_time_from_instant (tz
, g_date_time_to_instant (datetime
));
2401 * g_date_time_to_local:
2402 * @datetime: a #GDateTime
2404 * Creates a new #GDateTime corresponding to the same instant in time as
2405 * @datetime, but in the local time zone.
2407 * This call is equivalent to calling g_date_time_to_timezone() with the
2408 * time zone returned by g_time_zone_new_local().
2410 * Returns: the newly created #GDateTime
2415 g_date_time_to_local (GDateTime
*datetime
)
2420 local
= g_time_zone_new_local ();
2421 new = g_date_time_to_timezone (datetime
, local
);
2422 g_time_zone_unref (local
);
2428 * g_date_time_to_utc:
2429 * @datetime: a #GDateTime
2431 * Creates a new #GDateTime corresponding to the same instant in time as
2432 * @datetime, but in UTC.
2434 * This call is equivalent to calling g_date_time_to_timezone() with the
2435 * time zone returned by g_time_zone_new_utc().
2437 * Returns: the newly created #GDateTime
2442 g_date_time_to_utc (GDateTime
*datetime
)
2447 utc
= g_time_zone_new_utc ();
2448 new = g_date_time_to_timezone (datetime
, utc
);
2449 g_time_zone_unref (utc
);
2457 format_z (GString
*outstr
,
2465 hours
= offset
/ 3600;
2466 minutes
= ABS (offset
) / 60 % 60;
2467 seconds
= ABS (offset
) % 60;
2472 g_string_append_printf (outstr
, "%+03d%02d",
2478 g_string_append_printf (outstr
, "%+03d:%02d",
2484 g_string_append_printf (outstr
, "%+03d:%02d:%02d",
2491 g_string_append_printf (outstr
, "%+03d", hours
);
2493 if (minutes
!= 0 || seconds
!= 0)
2495 g_string_append_printf (outstr
, ":%02d", minutes
);
2498 g_string_append_printf (outstr
, ":%02d", seconds
);
2510 format_number (GString
*str
,
2511 gboolean use_alt_digits
,
2516 const gchar
*ascii_digits
[10] = {
2517 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
2519 const gchar
**digits
= ascii_digits
;
2520 const gchar
*tmp
[10];
2523 g_return_if_fail (width
<= 10);
2525 #ifdef HAVE_LANGINFO_OUTDIGIT
2528 static const gchar
*alt_digits
[10];
2529 static gsize initialised
;
2530 /* 2^32 has 10 digits */
2532 if G_UNLIKELY (g_once_init_enter (&initialised
))
2534 #define DO_DIGIT(n) \
2535 alt_digits[n] = nl_langinfo (_NL_CTYPE_OUTDIGIT## n ##_MB)
2536 DO_DIGIT(0); DO_DIGIT(1); DO_DIGIT(2); DO_DIGIT(3); DO_DIGIT(4);
2537 DO_DIGIT(5); DO_DIGIT(6); DO_DIGIT(7); DO_DIGIT(8); DO_DIGIT(9);
2539 g_once_init_leave (&initialised
, TRUE
);
2542 digits
= alt_digits
;
2544 #endif /* HAVE_LANGINFO_OUTDIGIT */
2548 tmp
[i
++] = digits
[number
% 10];
2553 while (pad
&& i
< width
)
2554 tmp
[i
++] = *pad
== '0' ? digits
[0] : pad
;
2556 /* should really be impossible */
2560 g_string_append (str
, tmp
[--i
]);
2564 format_ampm (GDateTime
*datetime
,
2566 gboolean locale_is_utf8
,
2570 gchar
*tmp
= NULL
, *ampm_dup
;
2573 ampm
= GET_AMPM (datetime
);
2575 if (!ampm
|| ampm
[0] == '\0')
2576 ampm
= get_fallback_ampm (g_date_time_get_hour (datetime
));
2578 #if defined (HAVE_LANGINFO_TIME)
2579 if (!locale_is_utf8
)
2581 /* This assumes that locale encoding can't have embedded NULs */
2582 ampm
= tmp
= g_locale_to_utf8 (ampm
, -1, NULL
, NULL
, NULL
);
2588 ampm_dup
= g_utf8_strup (ampm
, -1);
2590 ampm_dup
= g_utf8_strdown (ampm
, -1);
2591 len
= strlen (ampm_dup
);
2592 if (!locale_is_utf8
)
2594 #if defined (HAVE_LANGINFO_TIME)
2597 tmp
= g_locale_from_utf8 (ampm_dup
, -1, NULL
, &len
, NULL
);
2603 g_string_append_len (outstr
, ampm_dup
, len
);
2609 static gboolean
g_date_time_format_locale (GDateTime
*datetime
,
2610 const gchar
*format
,
2612 gboolean locale_is_utf8
);
2614 /* g_date_time_format() subroutine that takes a locale-encoded format
2615 * string and produces a locale-encoded date/time string.
2618 g_date_time_locale_format_locale (GDateTime
*datetime
,
2619 const gchar
*format
,
2621 gboolean locale_is_utf8
)
2627 return g_date_time_format_locale (datetime
, format
, outstr
,
2630 utf8_format
= g_locale_to_utf8 (format
, -1, NULL
, NULL
, NULL
);
2634 success
= g_date_time_format_locale (datetime
, utf8_format
, outstr
,
2636 g_free (utf8_format
);
2640 /* g_date_time_format() subroutine that takes a UTF-8 format
2641 * string and produces a locale-encoded date/time string.
2644 g_date_time_format_locale (GDateTime
*datetime
,
2645 const gchar
*format
,
2647 gboolean locale_is_utf8
)
2654 gboolean alt_digits
= FALSE
;
2655 gboolean pad_set
= FALSE
;
2662 len
= strcspn (format
, "%");
2666 g_string_append_len (outstr
, format
, len
);
2669 tmp
= g_locale_from_utf8 (format
, len
, NULL
, &tmp_len
, NULL
);
2672 g_string_append_len (outstr
, tmp
, tmp_len
);
2681 g_assert (*format
== '%');
2691 c
= g_utf8_get_char (format
);
2692 format
= g_utf8_next_char (format
);
2696 name
= WEEKDAY_ABBR (datetime
);
2697 if (g_strcmp0 (name
, "") == 0)
2699 #if !defined (HAVE_LANGINFO_TIME)
2700 if (!locale_is_utf8
)
2702 tmp
= g_locale_from_utf8 (name
, -1, NULL
, &tmp_len
, NULL
);
2705 g_string_append_len (outstr
, tmp
, tmp_len
);
2711 g_string_append (outstr
, name
);
2715 name
= WEEKDAY_FULL (datetime
);
2716 if (g_strcmp0 (name
, "") == 0)
2718 #if !defined (HAVE_LANGINFO_TIME)
2719 if (!locale_is_utf8
)
2721 tmp
= g_locale_from_utf8 (name
, -1, NULL
, &tmp_len
, NULL
);
2724 g_string_append_len (outstr
, tmp
, tmp_len
);
2730 g_string_append (outstr
, name
);
2734 name
= MONTH_ABBR (datetime
);
2735 if (g_strcmp0 (name
, "") == 0)
2737 #if !defined (HAVE_LANGINFO_TIME)
2738 if (!locale_is_utf8
)
2740 tmp
= g_locale_from_utf8 (name
, -1, NULL
, &tmp_len
, NULL
);
2743 g_string_append_len (outstr
, tmp
, tmp_len
);
2749 g_string_append (outstr
, name
);
2753 name
= MONTH_FULL (datetime
);
2754 if (g_strcmp0 (name
, "") == 0)
2756 #if !defined (HAVE_LANGINFO_TIME)
2757 if (!locale_is_utf8
)
2759 tmp
= g_locale_from_utf8 (name
, -1, NULL
, &tmp_len
, NULL
);
2762 g_string_append_len (outstr
, tmp
, tmp_len
);
2768 g_string_append (outstr
, name
);
2773 if (g_strcmp0 (PREFERRED_DATE_TIME_FMT
, "") == 0)
2775 if (!g_date_time_locale_format_locale (datetime
, PREFERRED_DATE_TIME_FMT
,
2776 outstr
, locale_is_utf8
))
2781 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2782 g_date_time_get_year (datetime
) / 100);
2785 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2786 g_date_time_get_day_of_month (datetime
));
2789 format_number (outstr
, alt_digits
, pad_set
? pad
: " ", 2,
2790 g_date_time_get_day_of_month (datetime
));
2793 g_string_append_printf (outstr
, "%d-%02d-%02d",
2794 g_date_time_get_year (datetime
),
2795 g_date_time_get_month (datetime
),
2796 g_date_time_get_day_of_month (datetime
));
2799 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2800 g_date_time_get_week_numbering_year (datetime
) % 100);
2803 format_number (outstr
, alt_digits
, pad_set
? pad
: 0, 0,
2804 g_date_time_get_week_numbering_year (datetime
));
2807 name
= MONTH_ABBR (datetime
);
2808 if (g_strcmp0 (name
, "") == 0)
2810 #if !defined (HAVE_LANGINFO_TIME)
2811 if (!locale_is_utf8
)
2813 tmp
= g_locale_from_utf8 (name
, -1, NULL
, &tmp_len
, NULL
);
2816 g_string_append_len (outstr
, tmp
, tmp_len
);
2822 g_string_append (outstr
, name
);
2826 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2827 g_date_time_get_hour (datetime
));
2830 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2831 (g_date_time_get_hour (datetime
) + 11) % 12 + 1);
2834 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 3,
2835 g_date_time_get_day_of_year (datetime
));
2838 format_number (outstr
, alt_digits
, pad_set
? pad
: " ", 2,
2839 g_date_time_get_hour (datetime
));
2842 format_number (outstr
, alt_digits
, pad_set
? pad
: " ", 2,
2843 (g_date_time_get_hour (datetime
) + 11) % 12 + 1);
2846 g_string_append_c (outstr
, '\n');
2849 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2850 g_date_time_get_month (datetime
));
2853 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2854 g_date_time_get_minute (datetime
));
2860 if (!format_ampm (datetime
, outstr
, locale_is_utf8
, TRUE
))
2864 if (!format_ampm (datetime
, outstr
, locale_is_utf8
, FALSE
))
2869 if (g_strcmp0 (PREFERRED_12HR_TIME_FMT
, "") == 0)
2871 if (!g_date_time_locale_format_locale (datetime
, PREFERRED_12HR_TIME_FMT
,
2872 outstr
, locale_is_utf8
))
2877 g_string_append_printf (outstr
, "%02d:%02d",
2878 g_date_time_get_hour (datetime
),
2879 g_date_time_get_minute (datetime
));
2882 g_string_append_printf (outstr
, "%" G_GINT64_FORMAT
, g_date_time_to_unix (datetime
));
2885 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2886 g_date_time_get_second (datetime
));
2889 g_string_append_c (outstr
, '\t');
2892 g_string_append_printf (outstr
, "%02d:%02d:%02d",
2893 g_date_time_get_hour (datetime
),
2894 g_date_time_get_minute (datetime
),
2895 g_date_time_get_second (datetime
));
2898 format_number (outstr
, alt_digits
, 0, 0,
2899 g_date_time_get_day_of_week (datetime
));
2902 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2903 g_date_time_get_week_of_year (datetime
));
2906 format_number (outstr
, alt_digits
, 0, 0,
2907 g_date_time_get_day_of_week (datetime
) % 7);
2911 if (g_strcmp0 (PREFERRED_DATE_FMT
, "") == 0)
2913 if (!g_date_time_locale_format_locale (datetime
, PREFERRED_DATE_FMT
,
2914 outstr
, locale_is_utf8
))
2920 if (g_strcmp0 (PREFERRED_TIME_FMT
, "") == 0)
2922 if (!g_date_time_locale_format_locale (datetime
, PREFERRED_TIME_FMT
,
2923 outstr
, locale_is_utf8
))
2928 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2929 g_date_time_get_year (datetime
) % 100);
2932 format_number (outstr
, alt_digits
, 0, 0,
2933 g_date_time_get_year (datetime
));
2938 offset
= g_date_time_get_utc_offset (datetime
) / USEC_PER_SECOND
;
2939 if (!format_z (outstr
, (int) offset
, colons
))
2944 tz
= g_date_time_get_timezone_abbreviation (datetime
);
2945 tmp_len
= strlen (tz
);
2946 if (!locale_is_utf8
)
2948 tz
= tmp
= g_locale_from_utf8 (tz
, -1, NULL
, &tmp_len
, NULL
);
2952 g_string_append_len (outstr
, tz
, tmp_len
);
2953 if (!locale_is_utf8
)
2957 g_string_append_c (outstr
, '%');
2972 /* Colons are only allowed before 'z' */
2973 if (*format
&& *format
!= 'z' && *format
!= ':')
2986 * g_date_time_format:
2987 * @datetime: A #GDateTime
2988 * @format: a valid UTF-8 string, containing the format for the
2991 * Creates a newly allocated string representing the requested @format.
2993 * The format strings understood by this function are a subset of the
2994 * strftime() format language as specified by C99. The \%D, \%U and \%W
2995 * conversions are not supported, nor is the 'E' modifier. The GNU
2996 * extensions \%k, \%l, \%s and \%P are supported, however, as are the
2997 * '0', '_' and '-' modifiers.
2999 * In contrast to strftime(), this function always produces a UTF-8
3000 * string, regardless of the current locale. Note that the rendering of
3001 * many formats is locale-dependent and may not match the strftime()
3004 * The following format specifiers are supported:
3006 * - \%a: the abbreviated weekday name according to the current locale
3007 * - \%A: the full weekday name according to the current locale
3008 * - \%b: the abbreviated month name according to the current locale
3009 * - \%B: the full month name according to the current locale
3010 * - \%c: the preferred date and time representation for the current locale
3011 * - \%C: the century number (year/100) as a 2-digit integer (00-99)
3012 * - \%d: the day of the month as a decimal number (range 01 to 31)
3013 * - \%e: the day of the month as a decimal number (range 1 to 31)
3014 * - \%F: equivalent to `%Y-%m-%d` (the ISO 8601 date format)
3015 * - \%g: the last two digits of the ISO 8601 week-based year as a
3016 * decimal number (00-99). This works well with \%V and \%u.
3017 * - \%G: the ISO 8601 week-based year as a decimal number. This works
3018 * well with \%V and \%u.
3019 * - \%h: equivalent to \%b
3020 * - \%H: the hour as a decimal number using a 24-hour clock (range 00 to 23)
3021 * - \%I: the hour as a decimal number using a 12-hour clock (range 01 to 12)
3022 * - \%j: the day of the year as a decimal number (range 001 to 366)
3023 * - \%k: the hour (24-hour clock) as a decimal number (range 0 to 23);
3024 * single digits are preceded by a blank
3025 * - \%l: the hour (12-hour clock) as a decimal number (range 1 to 12);
3026 * single digits are preceded by a blank
3027 * - \%m: the month as a decimal number (range 01 to 12)
3028 * - \%M: the minute as a decimal number (range 00 to 59)
3029 * - \%p: either "AM" or "PM" according to the given time value, or the
3030 * corresponding strings for the current locale. Noon is treated as
3031 * "PM" and midnight as "AM".
3032 * - \%P: like \%p but lowercase: "am" or "pm" or a corresponding string for
3033 * the current locale
3034 * - \%r: the time in a.m. or p.m. notation
3035 * - \%R: the time in 24-hour notation (\%H:\%M)
3036 * - \%s: the number of seconds since the Epoch, that is, since 1970-01-01
3038 * - \%S: the second as a decimal number (range 00 to 60)
3039 * - \%t: a tab character
3040 * - \%T: the time in 24-hour notation with seconds (\%H:\%M:\%S)
3041 * - \%u: the ISO 8601 standard day of the week as a decimal, range 1 to 7,
3042 * Monday being 1. This works well with \%G and \%V.
3043 * - \%V: the ISO 8601 standard week number of the current year as a decimal
3044 * number, range 01 to 53, where week 1 is the first week that has at
3045 * least 4 days in the new year. See g_date_time_get_week_of_year().
3046 * This works well with \%G and \%u.
3047 * - \%w: the day of the week as a decimal, range 0 to 6, Sunday being 0.
3048 * This is not the ISO 8601 standard format -- use \%u instead.
3049 * - \%x: the preferred date representation for the current locale without
3051 * - \%X: the preferred time representation for the current locale without
3053 * - \%y: the year as a decimal number without the century
3054 * - \%Y: the year as a decimal number including the century
3055 * - \%z: the time zone as an offset from UTC (+hhmm)
3056 * - \%:z: the time zone as an offset from UTC (+hh:mm).
3057 * This is a gnulib strftime() extension. Since: 2.38
3058 * - \%::z: the time zone as an offset from UTC (+hh:mm:ss). This is a
3059 * gnulib strftime() extension. Since: 2.38
3060 * - \%:::z: the time zone as an offset from UTC, with : to necessary
3061 * precision (e.g., -04, +05:30). This is a gnulib strftime() extension. Since: 2.38
3062 * - \%Z: the time zone or name or abbreviation
3063 * - \%\%: a literal \% character
3065 * Some conversion specifications can be modified by preceding the
3066 * conversion specifier by one or more modifier characters. The
3067 * following modifiers are supported for many of the numeric
3070 * - O: Use alternative numeric symbols, if the current locale supports those.
3071 * - _: Pad a numeric result with spaces. This overrides the default padding
3072 * for the specifier.
3073 * - -: Do not pad a numeric result. This overrides the default padding
3074 * for the specifier.
3075 * - 0: Pad a numeric result with zeros. This overrides the default padding
3076 * for the specifier.
3078 * Returns: a newly allocated string formatted to the requested format
3079 * or %NULL in the case that there was an error (such as a format specifier
3080 * not being supported in the current locale). The string
3081 * should be freed with g_free().
3086 g_date_time_format (GDateTime
*datetime
,
3087 const gchar
*format
)
3091 gboolean locale_is_utf8
= g_get_charset (NULL
);
3093 g_return_val_if_fail (datetime
!= NULL
, NULL
);
3094 g_return_val_if_fail (format
!= NULL
, NULL
);
3095 g_return_val_if_fail (g_utf8_validate (format
, -1, NULL
), NULL
);
3097 outstr
= g_string_sized_new (strlen (format
) * 2);
3099 if (!g_date_time_format_locale (datetime
, format
, outstr
, locale_is_utf8
))
3101 g_string_free (outstr
, TRUE
);
3106 return g_string_free (outstr
, FALSE
);
3108 utf8
= g_locale_to_utf8 (outstr
->str
, outstr
->len
, NULL
, NULL
, NULL
);
3109 g_string_free (outstr
, TRUE
);
3115 /* vim:set foldmethod=marker: */