3 * Copyright (C) 2009-2010 Christian Hergert <chris@dronelabs.com>
4 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
5 * Copyright (C) 2010 Emmanuele Bassi <ebassi@linux.intel.com>
6 * Copyright © 2010 Codethink Limited
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * licence, or (at your option) any later version.
13 * This is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 * License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Authors: Christian Hergert <chris@dronelabs.com>
22 * Thiago Santos <thiago.sousa.santos@collabora.co.uk>
23 * Emmanuele Bassi <ebassi@linux.intel.com>
24 * Ryan Lortie <desrt@desrt.ca>
27 /* Algorithms within this file are based on the Calendar FAQ by
28 * Claus Tondering. It can be found at
29 * http://www.tondering.dk/claus/cal/calendar29.txt
31 * Copyright and disclaimer
32 * ------------------------
33 * This document is Copyright (C) 2008 by Claus Tondering.
34 * E-mail: claus@tondering.dk. (Please include the word
35 * "calendar" in the subject line.)
36 * The document may be freely distributed, provided this
37 * copyright notice is included and no money is charged for
40 * This document is provided "as is". No warranties are made as
51 #ifdef HAVE_LANGINFO_TIME
55 #include "gdatetime.h"
61 #include "gfileutils.h"
64 #include "gmappedfile.h"
65 #include "gstrfuncs.h"
66 #include "gtestutils.h"
68 #include "gtimezone.h"
75 #endif /* !G_OS_WIN32 */
80 * @short_description: a structure representing Date and Time
81 * @see_also: #GTimeZone
83 * #GDateTime is a structure that combines a Gregorian date and time
84 * into a single structure. It provides many conversion and methods to
85 * manipulate dates and times. Time precision is provided down to
86 * microseconds and the time can range (proleptically) from 0001-01-01
87 * 00:00:00 to 9999-12-31 23:59:59.999999. #GDateTime follows POSIX
88 * time in the sense that it is oblivious to leap seconds.
90 * #GDateTime is an immutable object; once it has been created it cannot
91 * be modified further. All modifiers will create a new #GDateTime.
92 * Nearly all such functions can fail due to the date or time going out
93 * of range, in which case %NULL will be returned.
95 * #GDateTime is reference counted: the reference count is increased by calling
96 * g_date_time_ref() and decreased by calling g_date_time_unref(). When the
97 * reference count drops to 0, the resources allocated by the #GDateTime
98 * structure are released.
100 * Many parts of the API may produce non-obvious results. As an
101 * example, adding two months to January 31st will yield March 31st
102 * whereas adding one month and then one month again will yield either
103 * March 28th or March 29th. Also note that adding 24 hours is not
104 * always the same as adding one day (since days containing daylight
105 * savings time transitions are either 23 or 25 hours in length).
107 * #GDateTime is available since GLib 2.26.
112 /* Microsecond timekeeping within Day */
115 /* TimeZone information */
119 /* 1 is 0001-01-01 in Proleptic Gregorian */
122 volatile gint ref_count
;
125 /* Time conversion {{{1 */
127 #define UNIX_EPOCH_START 719163
128 #define INSTANT_TO_UNIX(instant) \
129 ((instant)/USEC_PER_SECOND - UNIX_EPOCH_START * SEC_PER_DAY)
130 #define UNIX_TO_INSTANT(unix) \
131 (((gint64) (unix) + UNIX_EPOCH_START * SEC_PER_DAY) * USEC_PER_SECOND)
132 #define UNIX_TO_INSTANT_IS_VALID(unix) \
133 ((gint64) (unix) <= INSTANT_TO_UNIX (G_MAXINT64))
135 #define DAYS_IN_4YEARS 1461 /* days in 4 years */
136 #define DAYS_IN_100YEARS 36524 /* days in 100 years */
137 #define DAYS_IN_400YEARS 146097 /* days in 400 years */
139 #define USEC_PER_SECOND (G_GINT64_CONSTANT (1000000))
140 #define USEC_PER_MINUTE (G_GINT64_CONSTANT (60000000))
141 #define USEC_PER_HOUR (G_GINT64_CONSTANT (3600000000))
142 #define USEC_PER_MILLISECOND (G_GINT64_CONSTANT (1000))
143 #define USEC_PER_DAY (G_GINT64_CONSTANT (86400000000))
144 #define SEC_PER_DAY (G_GINT64_CONSTANT (86400))
146 #define SECS_PER_MINUTE (60)
147 #define SECS_PER_HOUR (60 * SECS_PER_MINUTE)
148 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
149 #define SECS_PER_YEAR (365 * SECS_PER_DAY)
150 #define SECS_PER_JULIAN (DAYS_PER_PERIOD * SECS_PER_DAY)
152 #define GREGORIAN_LEAP(y) ((((y) % 4) == 0) && (!((((y) % 100) == 0) && (((y) % 400) != 0))))
153 #define JULIAN_YEAR(d) ((d)->julian / 365.25)
154 #define DAYS_PER_PERIOD (G_GINT64_CONSTANT (2914695))
156 static const guint16 days_in_months
[2][13] =
158 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
159 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
162 static const guint16 days_in_year
[2][13] =
164 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
165 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
168 #ifdef HAVE_LANGINFO_TIME
170 #define GET_AMPM(d) ((g_date_time_get_hour (d) < 12) ? \
171 nl_langinfo (AM_STR) : \
172 nl_langinfo (PM_STR))
174 #define PREFERRED_DATE_TIME_FMT nl_langinfo (D_T_FMT)
175 #define PREFERRED_DATE_FMT nl_langinfo (D_FMT)
176 #define PREFERRED_TIME_FMT nl_langinfo (T_FMT)
177 #define PREFERRED_TIME_FMT nl_langinfo (T_FMT)
178 #define PREFERRED_12HR_TIME_FMT nl_langinfo (T_FMT_AMPM)
180 static const gint weekday_item
[2][7] =
182 { ABDAY_2
, ABDAY_3
, ABDAY_4
, ABDAY_5
, ABDAY_6
, ABDAY_7
, ABDAY_1
},
183 { DAY_2
, DAY_3
, DAY_4
, DAY_5
, DAY_6
, DAY_7
, DAY_1
}
186 static const gint month_item
[2][12] =
188 { ABMON_1
, ABMON_2
, ABMON_3
, ABMON_4
, ABMON_5
, ABMON_6
, ABMON_7
, ABMON_8
, ABMON_9
, ABMON_10
, ABMON_11
, ABMON_12
},
189 { MON_1
, MON_2
, MON_3
, MON_4
, MON_5
, MON_6
, MON_7
, MON_8
, MON_9
, MON_10
, MON_11
, MON_12
},
192 #define WEEKDAY_ABBR(d) nl_langinfo (weekday_item[0][g_date_time_get_day_of_week (d) - 1])
193 #define WEEKDAY_FULL(d) nl_langinfo (weekday_item[1][g_date_time_get_day_of_week (d) - 1])
194 #define MONTH_ABBR(d) nl_langinfo (month_item[0][g_date_time_get_month (d) - 1])
195 #define MONTH_FULL(d) nl_langinfo (month_item[1][g_date_time_get_month (d) - 1])
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 /* full new functions {{{1 */
910 * @year: the year component of the date
911 * @month: the month component of the date
912 * @day: the day component of the date
913 * @hour: the hour component of the date
914 * @minute: the minute component of the date
915 * @seconds: the number of seconds past the minute
917 * Creates a new #GDateTime corresponding to the given date and time in
920 * The @year must be between 1 and 9999, @month between 1 and 12 and @day
921 * between 1 and 28, 29, 30 or 31 depending on the month and the year.
923 * @hour must be between 0 and 23 and @minute must be between 0 and 59.
925 * @seconds must be at least 0.0 and must be strictly less than 60.0.
926 * It will be rounded down to the nearest microsecond.
928 * If the given time is not representable in the given time zone (for
929 * example, 02:30 on March 14th 2010 in Toronto, due to daylight savings
930 * time) then the time will be rounded up to the nearest existing time
931 * (in this case, 03:00). If this matters to you then you should verify
932 * the return value for containing the same as the numbers you gave.
934 * In the case that the given time is ambiguous in the given time zone
935 * (for example, 01:30 on November 7th 2010 in Toronto, due to daylight
936 * savings time) then the time falling within standard (ie:
937 * non-daylight) time is taken.
939 * It not considered a programmer error for the values to this function
940 * to be out of range, but in the case that they are, the function will
943 * You should release the return value by calling g_date_time_unref()
944 * when you are done with it.
946 * Returns: a new #GDateTime, or %NULL
951 g_date_time_new (GTimeZone
*tz
,
962 g_return_val_if_fail (tz
!= NULL
, NULL
);
964 if (year
< 1 || year
> 9999 ||
965 month
< 1 || month
> 12 ||
966 day
< 1 || day
> days_in_months
[GREGORIAN_LEAP (year
)][month
] ||
967 hour
< 0 || hour
> 23 ||
968 minute
< 0 || minute
> 59 ||
969 seconds
< 0.0 || seconds
>= 60.0)
972 datetime
= g_date_time_alloc (tz
);
973 datetime
->days
= ymd_to_days (year
, month
, day
);
974 datetime
->usec
= (hour
* USEC_PER_HOUR
)
975 + (minute
* USEC_PER_MINUTE
)
976 + (gint64
) (seconds
* USEC_PER_SECOND
);
978 full_time
= SEC_PER_DAY
*
979 (ymd_to_days (year
, month
, day
) - UNIX_EPOCH_START
) +
980 SECS_PER_HOUR
* hour
+
981 SECS_PER_MINUTE
* minute
+
984 datetime
->interval
= g_time_zone_adjust_time (datetime
->tz
,
985 G_TIME_TYPE_STANDARD
,
988 full_time
+= UNIX_EPOCH_START
* SEC_PER_DAY
;
989 datetime
->days
= full_time
/ SEC_PER_DAY
;
990 datetime
->usec
= (full_time
% SEC_PER_DAY
) * USEC_PER_SECOND
;
991 datetime
->usec
+= ((int) (seconds
* USEC_PER_SECOND
)) % USEC_PER_SECOND
;
997 * g_date_time_new_local:
998 * @year: the year component of the date
999 * @month: the month component of the date
1000 * @day: the day component of the date
1001 * @hour: the hour component of the date
1002 * @minute: the minute component of the date
1003 * @seconds: the number of seconds past the minute
1005 * Creates a new #GDateTime corresponding to the given date and time in
1006 * the local time zone.
1008 * This call is equivalent to calling g_date_time_new() with the time
1009 * zone returned by g_time_zone_new_local().
1011 * Returns: a #GDateTime, or %NULL
1016 g_date_time_new_local (gint year
,
1023 GDateTime
*datetime
;
1026 local
= g_time_zone_new_local ();
1027 datetime
= g_date_time_new (local
, year
, month
, day
, hour
, minute
, seconds
);
1028 g_time_zone_unref (local
);
1034 * g_date_time_new_utc:
1035 * @year: the year component of the date
1036 * @month: the month component of the date
1037 * @day: the day component of the date
1038 * @hour: the hour component of the date
1039 * @minute: the minute component of the date
1040 * @seconds: the number of seconds past the minute
1042 * Creates a new #GDateTime corresponding to the given date and time in
1045 * This call is equivalent to calling g_date_time_new() with the time
1046 * zone returned by g_time_zone_new_utc().
1048 * Returns: a #GDateTime, or %NULL
1053 g_date_time_new_utc (gint year
,
1060 GDateTime
*datetime
;
1063 utc
= g_time_zone_new_utc ();
1064 datetime
= g_date_time_new (utc
, year
, month
, day
, hour
, minute
, seconds
);
1065 g_time_zone_unref (utc
);
1074 * @datetime: a #GDateTime
1075 * @timespan: a #GTimeSpan
1077 * Creates a copy of @datetime and adds the specified timespan to the copy.
1079 * Returns: the newly created #GDateTime which should be freed with
1080 * g_date_time_unref().
1085 g_date_time_add (GDateTime
*datetime
,
1088 return g_date_time_from_instant (datetime
->tz
, timespan
+
1089 g_date_time_to_instant (datetime
));
1093 * g_date_time_add_years:
1094 * @datetime: a #GDateTime
1095 * @years: the number of years
1097 * Creates a copy of @datetime and adds the specified number of years to the
1098 * copy. Add negative values to subtract years.
1100 * Returns: the newly created #GDateTime which should be freed with
1101 * g_date_time_unref().
1106 g_date_time_add_years (GDateTime
*datetime
,
1109 gint year
, month
, day
;
1111 g_return_val_if_fail (datetime
!= NULL
, NULL
);
1113 if (years
< -10000 || years
> 10000)
1116 g_date_time_get_ymd (datetime
, &year
, &month
, &day
);
1119 /* only possible issue is if we've entered a year with no February 29
1121 if (month
== 2 && day
== 29 && !GREGORIAN_LEAP (year
))
1124 return g_date_time_replace_days (datetime
, ymd_to_days (year
, month
, day
));
1128 * g_date_time_add_months:
1129 * @datetime: a #GDateTime
1130 * @months: the number of months
1132 * Creates a copy of @datetime and adds the specified number of months to the
1133 * copy. Add negative values to subtract months.
1135 * Returns: the newly created #GDateTime which should be freed with
1136 * g_date_time_unref().
1141 g_date_time_add_months (GDateTime
*datetime
,
1144 gint year
, month
, day
;
1146 g_return_val_if_fail (datetime
!= NULL
, NULL
);
1147 g_date_time_get_ymd (datetime
, &year
, &month
, &day
);
1149 if (months
< -120000 || months
> 120000)
1152 year
+= months
/ 12;
1153 month
+= months
% 12;
1159 else if (month
> 12)
1165 day
= MIN (day
, days_in_months
[GREGORIAN_LEAP (year
)][month
]);
1167 return g_date_time_replace_days (datetime
, ymd_to_days (year
, month
, day
));
1171 * g_date_time_add_weeks:
1172 * @datetime: a #GDateTime
1173 * @weeks: the number of weeks
1175 * Creates a copy of @datetime and adds the specified number of weeks to the
1176 * copy. Add negative values to subtract weeks.
1178 * Returns: the newly created #GDateTime which should be freed with
1179 * g_date_time_unref().
1184 g_date_time_add_weeks (GDateTime
*datetime
,
1187 g_return_val_if_fail (datetime
!= NULL
, NULL
);
1189 return g_date_time_add_days (datetime
, weeks
* 7);
1193 * g_date_time_add_days:
1194 * @datetime: a #GDateTime
1195 * @days: the number of days
1197 * Creates a copy of @datetime and adds the specified number of days to the
1198 * copy. Add negative values to subtract days.
1200 * Returns: the newly created #GDateTime which should be freed with
1201 * g_date_time_unref().
1206 g_date_time_add_days (GDateTime
*datetime
,
1209 g_return_val_if_fail (datetime
!= NULL
, NULL
);
1211 if (days
< -3660000 || days
> 3660000)
1214 return g_date_time_replace_days (datetime
, datetime
->days
+ days
);
1218 * g_date_time_add_hours:
1219 * @datetime: a #GDateTime
1220 * @hours: the number of hours to add
1222 * Creates a copy of @datetime and adds the specified number of hours.
1223 * Add negative values to subtract hours.
1225 * Returns: the newly created #GDateTime which should be freed with
1226 * g_date_time_unref().
1231 g_date_time_add_hours (GDateTime
*datetime
,
1234 return g_date_time_add (datetime
, hours
* USEC_PER_HOUR
);
1238 * g_date_time_add_minutes:
1239 * @datetime: a #GDateTime
1240 * @minutes: the number of minutes to add
1242 * Creates a copy of @datetime adding the specified number of minutes.
1243 * Add negative values to subtract minutes.
1245 * Returns: the newly created #GDateTime which should be freed with
1246 * g_date_time_unref().
1251 g_date_time_add_minutes (GDateTime
*datetime
,
1254 return g_date_time_add (datetime
, minutes
* USEC_PER_MINUTE
);
1259 * g_date_time_add_seconds:
1260 * @datetime: a #GDateTime
1261 * @seconds: the number of seconds to add
1263 * Creates a copy of @datetime and adds the specified number of seconds.
1264 * Add negative values to subtract seconds.
1266 * Returns: the newly created #GDateTime which should be freed with
1267 * g_date_time_unref().
1272 g_date_time_add_seconds (GDateTime
*datetime
,
1275 return g_date_time_add (datetime
, seconds
* USEC_PER_SECOND
);
1279 * g_date_time_add_full:
1280 * @datetime: a #GDateTime
1281 * @years: the number of years to add
1282 * @months: the number of months to add
1283 * @days: the number of days to add
1284 * @hours: the number of hours to add
1285 * @minutes: the number of minutes to add
1286 * @seconds: the number of seconds to add
1288 * Creates a new #GDateTime adding the specified values to the current date and
1289 * time in @datetime. Add negative values to subtract.
1291 * Returns: the newly created #GDateTime that should be freed with
1292 * g_date_time_unref().
1297 g_date_time_add_full (GDateTime
*datetime
,
1305 gint year
, month
, day
;
1310 g_return_val_if_fail (datetime
!= NULL
, NULL
);
1311 g_date_time_get_ymd (datetime
, &year
, &month
, &day
);
1313 months
+= years
* 12;
1315 if (months
< -120000 || months
> 120000)
1318 if (days
< -3660000 || days
> 3660000)
1321 year
+= months
/ 12;
1322 month
+= months
% 12;
1328 else if (month
> 12)
1334 day
= MIN (day
, days_in_months
[GREGORIAN_LEAP (year
)][month
]);
1336 /* full_time is now in unix (local) time */
1337 full_time
= datetime
->usec
/ USEC_PER_SECOND
+ SEC_PER_DAY
*
1338 (ymd_to_days (year
, month
, day
) + days
- UNIX_EPOCH_START
);
1340 interval
= g_time_zone_adjust_time (datetime
->tz
,
1341 g_time_zone_is_dst (datetime
->tz
,
1342 datetime
->interval
),
1345 /* move to UTC unix time */
1346 full_time
-= g_time_zone_get_offset (datetime
->tz
, interval
);
1348 /* convert back to an instant, add back fractional seconds */
1349 full_time
+= UNIX_EPOCH_START
* SEC_PER_DAY
;
1350 full_time
= full_time
* USEC_PER_SECOND
+
1351 datetime
->usec
% USEC_PER_SECOND
;
1353 /* do the actual addition now */
1354 full_time
+= (hours
* USEC_PER_HOUR
) +
1355 (minutes
* USEC_PER_MINUTE
) +
1356 (gint64
) (seconds
* USEC_PER_SECOND
);
1358 /* find the new interval */
1359 interval
= g_time_zone_find_interval (datetime
->tz
,
1360 G_TIME_TYPE_UNIVERSAL
,
1361 INSTANT_TO_UNIX (full_time
));
1363 /* convert back into local time */
1364 full_time
+= USEC_PER_SECOND
*
1365 g_time_zone_get_offset (datetime
->tz
, interval
);
1367 /* split into days and usec of a new datetime */
1368 new = g_date_time_alloc (datetime
->tz
);
1369 new->interval
= interval
;
1370 new->days
= full_time
/ USEC_PER_DAY
;
1371 new->usec
= full_time
% USEC_PER_DAY
;
1378 /* Compare, difference, hash, equal {{{1 */
1380 * g_date_time_compare:
1381 * @dt1: (not nullable): first #GDateTime to compare
1382 * @dt2: (not nullable): second #GDateTime to compare
1384 * A comparison function for #GDateTimes that is suitable
1385 * as a #GCompareFunc. Both #GDateTimes must be non-%NULL.
1387 * Returns: -1, 0 or 1 if @dt1 is less than, equal to or greater
1393 g_date_time_compare (gconstpointer dt1
,
1398 difference
= g_date_time_difference ((GDateTime
*) dt1
, (GDateTime
*) dt2
);
1403 else if (difference
> 0)
1411 * g_date_time_difference:
1412 * @end: a #GDateTime
1413 * @begin: a #GDateTime
1415 * Calculates the difference in time between @end and @begin. The
1416 * #GTimeSpan that is returned is effectively @end - @begin (ie:
1417 * positive if the first parameter is larger).
1419 * Returns: the difference between the two #GDateTime, as a time
1420 * span expressed in microseconds.
1425 g_date_time_difference (GDateTime
*end
,
1428 g_return_val_if_fail (begin
!= NULL
, 0);
1429 g_return_val_if_fail (end
!= NULL
, 0);
1431 return g_date_time_to_instant (end
) -
1432 g_date_time_to_instant (begin
);
1437 * @datetime: (not nullable): a #GDateTime
1439 * Hashes @datetime into a #guint, suitable for use within #GHashTable.
1441 * Returns: a #guint containing the hash
1446 g_date_time_hash (gconstpointer datetime
)
1448 return g_date_time_to_instant ((GDateTime
*) datetime
);
1452 * g_date_time_equal:
1453 * @dt1: (not nullable): a #GDateTime
1454 * @dt2: (not nullable): a #GDateTime
1456 * Checks to see if @dt1 and @dt2 are equal.
1458 * Equal here means that they represent the same moment after converting
1459 * them to the same time zone.
1461 * Returns: %TRUE if @dt1 and @dt2 are equal
1466 g_date_time_equal (gconstpointer dt1
,
1469 return g_date_time_difference ((GDateTime
*) dt1
, (GDateTime
*) dt2
) == 0;
1472 /* Year, Month, Day Getters {{{1 */
1474 * g_date_time_get_ymd:
1475 * @datetime: a #GDateTime.
1476 * @year: (out) (optional): the return location for the gregorian year, or %NULL.
1477 * @month: (out) (optional): the return location for the month of the year, or %NULL.
1478 * @day: (out) (optional): the return location for the day of the month, or %NULL.
1480 * Retrieves the Gregorian day, month, and year of a given #GDateTime.
1485 g_date_time_get_ymd (GDateTime
*datetime
,
1493 gint remaining_days
;
1500 g_return_if_fail (datetime
!= NULL
);
1502 remaining_days
= datetime
->days
;
1505 * We need to convert an offset in days to its year/month/day representation.
1506 * Leap years makes this a little trickier than it should be, so we use
1507 * 400, 100 and 4 years cycles here to get to the correct year.
1510 /* Our days offset starts sets 0001-01-01 as day 1, if it was day 0 our
1511 * math would be simpler, so let's do it */
1514 the_year
= (remaining_days
/ DAYS_IN_400YEARS
) * 400 + 1;
1515 remaining_days
= remaining_days
% DAYS_IN_400YEARS
;
1517 y100_cycles
= remaining_days
/ DAYS_IN_100YEARS
;
1518 remaining_days
= remaining_days
% DAYS_IN_100YEARS
;
1519 the_year
+= y100_cycles
* 100;
1521 y4_cycles
= remaining_days
/ DAYS_IN_4YEARS
;
1522 remaining_days
= remaining_days
% DAYS_IN_4YEARS
;
1523 the_year
+= y4_cycles
* 4;
1525 y1_cycles
= remaining_days
/ 365;
1526 the_year
+= y1_cycles
;
1527 remaining_days
= remaining_days
% 365;
1529 if (y1_cycles
== 4 || y100_cycles
== 4) {
1530 g_assert (remaining_days
== 0);
1532 /* special case that indicates that the date is actually one year before,
1533 * in the 31th of December */
1540 /* now get the month and the day */
1541 leap
= y1_cycles
== 3 && (y4_cycles
!= 24 || y100_cycles
== 3);
1543 g_assert (leap
== GREGORIAN_LEAP(the_year
));
1545 the_month
= (remaining_days
+ 50) >> 5;
1546 preceding
= (days_in_year
[0][the_month
- 1] + (the_month
> 2 && leap
));
1547 if (preceding
> remaining_days
)
1549 /* estimate is too large */
1551 preceding
-= leap
? days_in_months
[1][the_month
]
1552 : days_in_months
[0][the_month
];
1555 remaining_days
-= preceding
;
1556 g_assert(0 <= remaining_days
);
1558 the_day
= remaining_days
+ 1;
1570 * g_date_time_get_year:
1571 * @datetime: A #GDateTime
1573 * Retrieves the year represented by @datetime in the Gregorian calendar.
1575 * Returns: the year represented by @datetime
1580 g_date_time_get_year (GDateTime
*datetime
)
1584 g_return_val_if_fail (datetime
!= NULL
, 0);
1586 g_date_time_get_ymd (datetime
, &year
, NULL
, NULL
);
1592 * g_date_time_get_month:
1593 * @datetime: a #GDateTime
1595 * Retrieves the month of the year represented by @datetime in the Gregorian
1598 * Returns: the month represented by @datetime
1603 g_date_time_get_month (GDateTime
*datetime
)
1607 g_return_val_if_fail (datetime
!= NULL
, 0);
1609 g_date_time_get_ymd (datetime
, NULL
, &month
, NULL
);
1615 * g_date_time_get_day_of_month:
1616 * @datetime: a #GDateTime
1618 * Retrieves the day of the month represented by @datetime in the gregorian
1621 * Returns: the day of the month
1626 g_date_time_get_day_of_month (GDateTime
*datetime
)
1630 const guint16
*days
;
1633 g_return_val_if_fail (datetime
!= NULL
, 0);
1635 days
= days_in_year
[GREGORIAN_LEAP (g_date_time_get_year (datetime
)) ? 1 : 0];
1636 g_date_time_get_week_number (datetime
, NULL
, NULL
, &day_of_year
);
1638 for (i
= 1; i
<= 12; i
++)
1640 if (days
[i
] >= day_of_year
)
1641 return day_of_year
- last
;
1645 g_warn_if_reached ();
1649 /* Week of year / day of week getters {{{1 */
1651 * g_date_time_get_week_numbering_year:
1652 * @datetime: a #GDateTime
1654 * Returns the ISO 8601 week-numbering year in which the week containing
1657 * This function, taken together with g_date_time_get_week_of_year() and
1658 * g_date_time_get_day_of_week() can be used to determine the full ISO
1659 * week date on which @datetime falls.
1661 * This is usually equal to the normal Gregorian year (as returned by
1662 * g_date_time_get_year()), except as detailed below:
1664 * For Thursday, the week-numbering year is always equal to the usual
1665 * calendar year. For other days, the number is such that every day
1666 * within a complete week (Monday to Sunday) is contained within the
1667 * same week-numbering year.
1669 * For Monday, Tuesday and Wednesday occurring near the end of the year,
1670 * this may mean that the week-numbering year is one greater than the
1671 * calendar year (so that these days have the same week-numbering year
1672 * as the Thursday occurring early in the next year).
1674 * For Friday, Saturday and Sunday occurring near the start of the year,
1675 * this may mean that the week-numbering year is one less than the
1676 * calendar year (so that these days have the same week-numbering year
1677 * as the Thursday occurring late in the previous year).
1679 * An equivalent description is that the week-numbering year is equal to
1680 * the calendar year containing the majority of the days in the current
1681 * week (Monday to Sunday).
1683 * Note that January 1 0001 in the proleptic Gregorian calendar is a
1684 * Monday, so this function never returns 0.
1686 * Returns: the ISO 8601 week-numbering year for @datetime
1691 g_date_time_get_week_numbering_year (GDateTime
*datetime
)
1693 gint year
, month
, day
, weekday
;
1695 g_date_time_get_ymd (datetime
, &year
, &month
, &day
);
1696 weekday
= g_date_time_get_day_of_week (datetime
);
1698 /* January 1, 2, 3 might be in the previous year if they occur after
1701 * Jan 1: Friday, Saturday, Sunday => day 1: weekday 5, 6, 7
1702 * Jan 2: Saturday, Sunday => day 2: weekday 6, 7
1703 * Jan 3: Sunday => day 3: weekday 7
1705 * So we have a special case if (day - weekday) <= -4
1707 if (month
== 1 && (day
- weekday
) <= -4)
1710 /* December 29, 30, 31 might be in the next year if they occur before
1713 * Dec 31: Monday, Tuesday, Wednesday => day 31: weekday 1, 2, 3
1714 * Dec 30: Monday, Tuesday => day 30: weekday 1, 2
1715 * Dec 29: Monday => day 29: weekday 1
1717 * So we have a special case if (day - weekday) >= 28
1719 else if (month
== 12 && (day
- weekday
) >= 28)
1727 * g_date_time_get_week_of_year:
1728 * @datetime: a #GDateTime
1730 * Returns the ISO 8601 week number for the week containing @datetime.
1731 * The ISO 8601 week number is the same for every day of the week (from
1732 * Moday through Sunday). That can produce some unusual results
1733 * (described below).
1735 * The first week of the year is week 1. This is the week that contains
1736 * the first Thursday of the year. Equivalently, this is the first week
1737 * that has more than 4 of its days falling within the calendar year.
1739 * The value 0 is never returned by this function. Days contained
1740 * within a year but occurring before the first ISO 8601 week of that
1741 * year are considered as being contained in the last week of the
1742 * previous year. Similarly, the final days of a calendar year may be
1743 * considered as being part of the first ISO 8601 week of the next year
1744 * if 4 or more days of that week are contained within the new year.
1746 * Returns: the ISO 8601 week number for @datetime.
1751 g_date_time_get_week_of_year (GDateTime
*datetime
)
1755 g_return_val_if_fail (datetime
!= NULL
, 0);
1757 g_date_time_get_week_number (datetime
, &weeknum
, NULL
, NULL
);
1763 * g_date_time_get_day_of_week:
1764 * @datetime: a #GDateTime
1766 * Retrieves the ISO 8601 day of the week on which @datetime falls (1 is
1767 * Monday, 2 is Tuesday... 7 is Sunday).
1769 * Returns: the day of the week
1774 g_date_time_get_day_of_week (GDateTime
*datetime
)
1776 g_return_val_if_fail (datetime
!= NULL
, 0);
1778 return (datetime
->days
- 1) % 7 + 1;
1781 /* Day of year getter {{{1 */
1783 * g_date_time_get_day_of_year:
1784 * @datetime: a #GDateTime
1786 * Retrieves the day of the year represented by @datetime in the Gregorian
1789 * Returns: the day of the year
1794 g_date_time_get_day_of_year (GDateTime
*datetime
)
1798 g_return_val_if_fail (datetime
!= NULL
, 0);
1800 g_date_time_get_week_number (datetime
, NULL
, NULL
, &doy
);
1804 /* Time component getters {{{1 */
1807 * g_date_time_get_hour:
1808 * @datetime: a #GDateTime
1810 * Retrieves the hour of the day represented by @datetime
1812 * Returns: the hour of the day
1817 g_date_time_get_hour (GDateTime
*datetime
)
1819 g_return_val_if_fail (datetime
!= NULL
, 0);
1821 return (datetime
->usec
/ USEC_PER_HOUR
);
1825 * g_date_time_get_minute:
1826 * @datetime: a #GDateTime
1828 * Retrieves the minute of the hour represented by @datetime
1830 * Returns: the minute of the hour
1835 g_date_time_get_minute (GDateTime
*datetime
)
1837 g_return_val_if_fail (datetime
!= NULL
, 0);
1839 return (datetime
->usec
% USEC_PER_HOUR
) / USEC_PER_MINUTE
;
1843 * g_date_time_get_second:
1844 * @datetime: a #GDateTime
1846 * Retrieves the second of the minute represented by @datetime
1848 * Returns: the second represented by @datetime
1853 g_date_time_get_second (GDateTime
*datetime
)
1855 g_return_val_if_fail (datetime
!= NULL
, 0);
1857 return (datetime
->usec
% USEC_PER_MINUTE
) / USEC_PER_SECOND
;
1861 * g_date_time_get_microsecond:
1862 * @datetime: a #GDateTime
1864 * Retrieves the microsecond of the date represented by @datetime
1866 * Returns: the microsecond of the second
1871 g_date_time_get_microsecond (GDateTime
*datetime
)
1873 g_return_val_if_fail (datetime
!= NULL
, 0);
1875 return (datetime
->usec
% USEC_PER_SECOND
);
1879 * g_date_time_get_seconds:
1880 * @datetime: a #GDateTime
1882 * Retrieves the number of seconds since the start of the last minute,
1883 * including the fractional part.
1885 * Returns: the number of seconds
1890 g_date_time_get_seconds (GDateTime
*datetime
)
1892 g_return_val_if_fail (datetime
!= NULL
, 0);
1894 return (datetime
->usec
% USEC_PER_MINUTE
) / 1000000.0;
1897 /* Exporters {{{1 */
1899 * g_date_time_to_unix:
1900 * @datetime: a #GDateTime
1902 * Gives the Unix time corresponding to @datetime, rounding down to the
1905 * Unix time is the number of seconds that have elapsed since 1970-01-01
1906 * 00:00:00 UTC, regardless of the time zone associated with @datetime.
1908 * Returns: the Unix time corresponding to @datetime
1913 g_date_time_to_unix (GDateTime
*datetime
)
1915 return INSTANT_TO_UNIX (g_date_time_to_instant (datetime
));
1919 * g_date_time_to_timeval:
1920 * @datetime: a #GDateTime
1921 * @tv: a #GTimeVal to modify
1923 * Stores the instant in time that @datetime represents into @tv.
1925 * The time contained in a #GTimeVal is always stored in the form of
1926 * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the time
1927 * zone associated with @datetime.
1929 * On systems where 'long' is 32bit (ie: all 32bit systems and all
1930 * Windows systems), a #GTimeVal is incapable of storing the entire
1931 * range of values that #GDateTime is capable of expressing. On those
1932 * systems, this function returns %FALSE to indicate that the time is
1935 * On systems where 'long' is 64bit, this function never fails.
1937 * Returns: %TRUE if successful, else %FALSE
1942 g_date_time_to_timeval (GDateTime
*datetime
,
1945 tv
->tv_sec
= INSTANT_TO_UNIX (g_date_time_to_instant (datetime
));
1946 tv
->tv_usec
= datetime
->usec
% USEC_PER_SECOND
;
1951 /* Timezone queries {{{1 */
1953 * g_date_time_get_utc_offset:
1954 * @datetime: a #GDateTime
1956 * Determines the offset to UTC in effect at the time and in the time
1957 * zone of @datetime.
1959 * The offset is the number of microseconds that you add to UTC time to
1960 * arrive at local time for the time zone (ie: negative numbers for time
1961 * zones west of GMT, positive numbers for east).
1963 * If @datetime represents UTC time, then the offset is always zero.
1965 * Returns: the number of microseconds that should be added to UTC to
1966 * get the local time
1971 g_date_time_get_utc_offset (GDateTime
*datetime
)
1975 g_return_val_if_fail (datetime
!= NULL
, 0);
1977 offset
= g_time_zone_get_offset (datetime
->tz
, datetime
->interval
);
1979 return (gint64
) offset
* USEC_PER_SECOND
;
1983 * g_date_time_get_timezone_abbreviation:
1984 * @datetime: a #GDateTime
1986 * Determines the time zone abbreviation to be used at the time and in
1987 * the time zone of @datetime.
1989 * For example, in Toronto this is currently "EST" during the winter
1990 * months and "EDT" during the summer months when daylight savings
1991 * time is in effect.
1993 * Returns: (transfer none): the time zone abbreviation. The returned
1994 * string is owned by the #GDateTime and it should not be
2000 g_date_time_get_timezone_abbreviation (GDateTime
*datetime
)
2002 g_return_val_if_fail (datetime
!= NULL
, NULL
);
2004 return g_time_zone_get_abbreviation (datetime
->tz
, datetime
->interval
);
2008 * g_date_time_is_daylight_savings:
2009 * @datetime: a #GDateTime
2011 * Determines if daylight savings time is in effect at the time and in
2012 * the time zone of @datetime.
2014 * Returns: %TRUE if daylight savings time is in effect
2019 g_date_time_is_daylight_savings (GDateTime
*datetime
)
2021 g_return_val_if_fail (datetime
!= NULL
, FALSE
);
2023 return g_time_zone_is_dst (datetime
->tz
, datetime
->interval
);
2026 /* Timezone convert {{{1 */
2028 * g_date_time_to_timezone:
2029 * @datetime: a #GDateTime
2030 * @tz: the new #GTimeZone
2032 * Create a new #GDateTime corresponding to the same instant in time as
2033 * @datetime, but in the time zone @tz.
2035 * This call can fail in the case that the time goes out of bounds. For
2036 * example, converting 0001-01-01 00:00:00 UTC to a time zone west of
2037 * Greenwich will fail (due to the year 0 being out of range).
2039 * You should release the return value by calling g_date_time_unref()
2040 * when you are done with it.
2042 * Returns: a new #GDateTime, or %NULL
2047 g_date_time_to_timezone (GDateTime
*datetime
,
2050 return g_date_time_from_instant (tz
, g_date_time_to_instant (datetime
));
2054 * g_date_time_to_local:
2055 * @datetime: a #GDateTime
2057 * Creates a new #GDateTime corresponding to the same instant in time as
2058 * @datetime, but in the local time zone.
2060 * This call is equivalent to calling g_date_time_to_timezone() with the
2061 * time zone returned by g_time_zone_new_local().
2063 * Returns: the newly created #GDateTime
2068 g_date_time_to_local (GDateTime
*datetime
)
2073 local
= g_time_zone_new_local ();
2074 new = g_date_time_to_timezone (datetime
, local
);
2075 g_time_zone_unref (local
);
2081 * g_date_time_to_utc:
2082 * @datetime: a #GDateTime
2084 * Creates a new #GDateTime corresponding to the same instant in time as
2085 * @datetime, but in UTC.
2087 * This call is equivalent to calling g_date_time_to_timezone() with the
2088 * time zone returned by g_time_zone_new_utc().
2090 * Returns: the newly created #GDateTime
2095 g_date_time_to_utc (GDateTime
*datetime
)
2100 utc
= g_time_zone_new_utc ();
2101 new = g_date_time_to_timezone (datetime
, utc
);
2102 g_time_zone_unref (utc
);
2110 format_z (GString
*outstr
,
2118 hours
= offset
/ 3600;
2119 minutes
= ABS (offset
) / 60 % 60;
2120 seconds
= ABS (offset
) % 60;
2125 g_string_append_printf (outstr
, "%+03d%02d",
2131 g_string_append_printf (outstr
, "%+03d:%02d",
2137 g_string_append_printf (outstr
, "%+03d:%02d:%02d",
2144 g_string_append_printf (outstr
, "%+03d", hours
);
2146 if (minutes
!= 0 || seconds
!= 0)
2148 g_string_append_printf (outstr
, ":%02d", minutes
);
2151 g_string_append_printf (outstr
, ":%02d", seconds
);
2163 format_number (GString
*str
,
2164 gboolean use_alt_digits
,
2169 const gchar
*ascii_digits
[10] = {
2170 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
2172 const gchar
**digits
= ascii_digits
;
2173 const gchar
*tmp
[10];
2176 g_return_if_fail (width
<= 10);
2178 #ifdef HAVE_LANGINFO_OUTDIGIT
2181 static const gchar
*alt_digits
[10];
2182 static gsize initialised
;
2183 /* 2^32 has 10 digits */
2185 if G_UNLIKELY (g_once_init_enter (&initialised
))
2187 #define DO_DIGIT(n) \
2188 alt_digits[n] = nl_langinfo (_NL_CTYPE_OUTDIGIT## n ##_MB)
2189 DO_DIGIT(0); DO_DIGIT(1); DO_DIGIT(2); DO_DIGIT(3); DO_DIGIT(4);
2190 DO_DIGIT(5); DO_DIGIT(6); DO_DIGIT(7); DO_DIGIT(8); DO_DIGIT(9);
2192 g_once_init_leave (&initialised
, TRUE
);
2195 digits
= alt_digits
;
2197 #endif /* HAVE_LANGINFO_OUTDIGIT */
2201 tmp
[i
++] = digits
[number
% 10];
2206 while (pad
&& i
< width
)
2207 tmp
[i
++] = *pad
== '0' ? digits
[0] : pad
;
2209 /* should really be impossible */
2213 g_string_append (str
, tmp
[--i
]);
2217 format_ampm (GDateTime
*datetime
,
2219 gboolean locale_is_utf8
,
2223 gchar
*tmp
= NULL
, *ampm_dup
;
2226 ampm
= GET_AMPM (datetime
);
2228 if (!ampm
|| ampm
[0] == '\0')
2229 ampm
= get_fallback_ampm (g_date_time_get_hour (datetime
));
2231 #if defined (HAVE_LANGINFO_TIME)
2232 if (!locale_is_utf8
)
2234 /* This assumes that locale encoding can't have embedded NULs */
2235 ampm
= tmp
= g_locale_to_utf8 (ampm
, -1, NULL
, NULL
, NULL
);
2241 ampm_dup
= g_utf8_strup (ampm
, -1);
2243 ampm_dup
= g_utf8_strdown (ampm
, -1);
2244 len
= strlen (ampm_dup
);
2245 if (!locale_is_utf8
)
2247 #if defined (HAVE_LANGINFO_TIME)
2250 tmp
= g_locale_from_utf8 (ampm_dup
, -1, NULL
, &len
, NULL
);
2256 g_string_append_len (outstr
, ampm_dup
, len
);
2262 static gboolean
g_date_time_format_locale (GDateTime
*datetime
,
2263 const gchar
*format
,
2265 gboolean locale_is_utf8
);
2267 /* g_date_time_format() subroutine that takes a locale-encoded format
2268 * string and produces a locale-encoded date/time string.
2271 g_date_time_locale_format_locale (GDateTime
*datetime
,
2272 const gchar
*format
,
2274 gboolean locale_is_utf8
)
2280 return g_date_time_format_locale (datetime
, format
, outstr
,
2283 utf8_format
= g_locale_to_utf8 (format
, -1, NULL
, NULL
, NULL
);
2287 success
= g_date_time_format_locale (datetime
, utf8_format
, outstr
,
2289 g_free (utf8_format
);
2293 /* g_date_time_format() subroutine that takes a UTF-8 format
2294 * string and produces a locale-encoded date/time string.
2297 g_date_time_format_locale (GDateTime
*datetime
,
2298 const gchar
*format
,
2300 gboolean locale_is_utf8
)
2307 gboolean alt_digits
= FALSE
;
2308 gboolean pad_set
= FALSE
;
2315 len
= strcspn (format
, "%");
2319 g_string_append_len (outstr
, format
, len
);
2322 tmp
= g_locale_from_utf8 (format
, len
, NULL
, &tmp_len
, NULL
);
2325 g_string_append_len (outstr
, tmp
, tmp_len
);
2334 g_assert (*format
== '%');
2344 c
= g_utf8_get_char (format
);
2345 format
= g_utf8_next_char (format
);
2349 name
= WEEKDAY_ABBR (datetime
);
2350 #if !defined (HAVE_LANGINFO_TIME)
2351 if (!locale_is_utf8
)
2353 tmp
= g_locale_from_utf8 (name
, -1, NULL
, &tmp_len
, NULL
);
2356 g_string_append_len (outstr
, tmp
, tmp_len
);
2362 g_string_append (outstr
, name
);
2366 name
= WEEKDAY_FULL (datetime
);
2367 #if !defined (HAVE_LANGINFO_TIME)
2368 if (!locale_is_utf8
)
2370 tmp
= g_locale_from_utf8 (name
, -1, NULL
, &tmp_len
, NULL
);
2373 g_string_append_len (outstr
, tmp
, tmp_len
);
2379 g_string_append (outstr
, name
);
2383 name
= MONTH_ABBR (datetime
);
2384 #if !defined (HAVE_LANGINFO_TIME)
2385 if (!locale_is_utf8
)
2387 tmp
= g_locale_from_utf8 (name
, -1, NULL
, &tmp_len
, NULL
);
2390 g_string_append_len (outstr
, tmp
, tmp_len
);
2396 g_string_append (outstr
, name
);
2400 name
= MONTH_FULL (datetime
);
2401 #if !defined (HAVE_LANGINFO_TIME)
2402 if (!locale_is_utf8
)
2404 tmp
= g_locale_from_utf8 (name
, -1, NULL
, &tmp_len
, NULL
);
2407 g_string_append_len (outstr
, tmp
, tmp_len
);
2413 g_string_append (outstr
, name
);
2418 if (!g_date_time_locale_format_locale (datetime
, PREFERRED_DATE_TIME_FMT
,
2419 outstr
, locale_is_utf8
))
2424 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2425 g_date_time_get_year (datetime
) / 100);
2428 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2429 g_date_time_get_day_of_month (datetime
));
2432 format_number (outstr
, alt_digits
, pad_set
? pad
: " ", 2,
2433 g_date_time_get_day_of_month (datetime
));
2436 g_string_append_printf (outstr
, "%d-%02d-%02d",
2437 g_date_time_get_year (datetime
),
2438 g_date_time_get_month (datetime
),
2439 g_date_time_get_day_of_month (datetime
));
2442 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2443 g_date_time_get_week_numbering_year (datetime
) % 100);
2446 format_number (outstr
, alt_digits
, pad_set
? pad
: 0, 0,
2447 g_date_time_get_week_numbering_year (datetime
));
2450 name
= MONTH_ABBR (datetime
);
2451 #if !defined (HAVE_LANGINFO_TIME)
2452 if (!locale_is_utf8
)
2454 tmp
= g_locale_from_utf8 (name
, -1, NULL
, &tmp_len
, NULL
);
2457 g_string_append_len (outstr
, tmp
, tmp_len
);
2463 g_string_append (outstr
, name
);
2467 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2468 g_date_time_get_hour (datetime
));
2471 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2472 (g_date_time_get_hour (datetime
) + 11) % 12 + 1);
2475 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 3,
2476 g_date_time_get_day_of_year (datetime
));
2479 format_number (outstr
, alt_digits
, pad_set
? pad
: " ", 2,
2480 g_date_time_get_hour (datetime
));
2483 format_number (outstr
, alt_digits
, pad_set
? pad
: " ", 2,
2484 (g_date_time_get_hour (datetime
) + 11) % 12 + 1);
2487 g_string_append_c (outstr
, '\n');
2490 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2491 g_date_time_get_month (datetime
));
2494 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2495 g_date_time_get_minute (datetime
));
2501 if (!format_ampm (datetime
, outstr
, locale_is_utf8
, TRUE
))
2505 if (!format_ampm (datetime
, outstr
, locale_is_utf8
, FALSE
))
2510 if (!g_date_time_locale_format_locale (datetime
, PREFERRED_12HR_TIME_FMT
,
2511 outstr
, locale_is_utf8
))
2516 g_string_append_printf (outstr
, "%02d:%02d",
2517 g_date_time_get_hour (datetime
),
2518 g_date_time_get_minute (datetime
));
2521 g_string_append_printf (outstr
, "%" G_GINT64_FORMAT
, g_date_time_to_unix (datetime
));
2524 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2525 g_date_time_get_second (datetime
));
2528 g_string_append_c (outstr
, '\t');
2531 g_string_append_printf (outstr
, "%02d:%02d:%02d",
2532 g_date_time_get_hour (datetime
),
2533 g_date_time_get_minute (datetime
),
2534 g_date_time_get_second (datetime
));
2537 format_number (outstr
, alt_digits
, 0, 0,
2538 g_date_time_get_day_of_week (datetime
));
2541 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2542 g_date_time_get_week_of_year (datetime
));
2545 format_number (outstr
, alt_digits
, 0, 0,
2546 g_date_time_get_day_of_week (datetime
) % 7);
2550 if (!g_date_time_locale_format_locale (datetime
, PREFERRED_DATE_FMT
,
2551 outstr
, locale_is_utf8
))
2557 if (!g_date_time_locale_format_locale (datetime
, PREFERRED_TIME_FMT
,
2558 outstr
, locale_is_utf8
))
2563 format_number (outstr
, alt_digits
, pad_set
? pad
: "0", 2,
2564 g_date_time_get_year (datetime
) % 100);
2567 format_number (outstr
, alt_digits
, 0, 0,
2568 g_date_time_get_year (datetime
));
2573 offset
= g_date_time_get_utc_offset (datetime
) / USEC_PER_SECOND
;
2574 if (!format_z (outstr
, (int) offset
, colons
))
2579 tz
= g_date_time_get_timezone_abbreviation (datetime
);
2580 tmp_len
= strlen (tz
);
2581 if (!locale_is_utf8
)
2583 tz
= tmp
= g_locale_from_utf8 (tz
, -1, NULL
, &tmp_len
, NULL
);
2587 g_string_append_len (outstr
, tz
, tmp_len
);
2588 if (!locale_is_utf8
)
2592 g_string_append_c (outstr
, '%');
2607 /* Colons are only allowed before 'z' */
2608 if (*format
&& *format
!= 'z' && *format
!= ':')
2621 * g_date_time_format:
2622 * @datetime: A #GDateTime
2623 * @format: a valid UTF-8 string, containing the format for the
2626 * Creates a newly allocated string representing the requested @format.
2628 * The format strings understood by this function are a subset of the
2629 * strftime() format language as specified by C99. The \%D, \%U and \%W
2630 * conversions are not supported, nor is the 'E' modifier. The GNU
2631 * extensions \%k, \%l, \%s and \%P are supported, however, as are the
2632 * '0', '_' and '-' modifiers.
2634 * In contrast to strftime(), this function always produces a UTF-8
2635 * string, regardless of the current locale. Note that the rendering of
2636 * many formats is locale-dependent and may not match the strftime()
2639 * The following format specifiers are supported:
2641 * - \%a: the abbreviated weekday name according to the current locale
2642 * - \%A: the full weekday name according to the current locale
2643 * - \%b: the abbreviated month name according to the current locale
2644 * - \%B: the full month name according to the current locale
2645 * - \%c: the preferred date and time representation for the current locale
2646 * - \%C: the century number (year/100) as a 2-digit integer (00-99)
2647 * - \%d: the day of the month as a decimal number (range 01 to 31)
2648 * - \%e: the day of the month as a decimal number (range 1 to 31)
2649 * - \%F: equivalent to `%Y-%m-%d` (the ISO 8601 date format)
2650 * - \%g: the last two digits of the ISO 8601 week-based year as a
2651 * decimal number (00-99). This works well with \%V and \%u.
2652 * - \%G: the ISO 8601 week-based year as a decimal number. This works
2653 * well with \%V and \%u.
2654 * - \%h: equivalent to \%b
2655 * - \%H: the hour as a decimal number using a 24-hour clock (range 00 to 23)
2656 * - \%I: the hour as a decimal number using a 12-hour clock (range 01 to 12)
2657 * - \%j: the day of the year as a decimal number (range 001 to 366)
2658 * - \%k: the hour (24-hour clock) as a decimal number (range 0 to 23);
2659 * single digits are preceded by a blank
2660 * - \%l: the hour (12-hour clock) as a decimal number (range 1 to 12);
2661 * single digits are preceded by a blank
2662 * - \%m: the month as a decimal number (range 01 to 12)
2663 * - \%M: the minute as a decimal number (range 00 to 59)
2664 * - \%p: either "AM" or "PM" according to the given time value, or the
2665 * corresponding strings for the current locale. Noon is treated as
2666 * "PM" and midnight as "AM".
2667 * - \%P: like \%p but lowercase: "am" or "pm" or a corresponding string for
2668 * the current locale
2669 * - \%r: the time in a.m. or p.m. notation
2670 * - \%R: the time in 24-hour notation (\%H:\%M)
2671 * - \%s: the number of seconds since the Epoch, that is, since 1970-01-01
2673 * - \%S: the second as a decimal number (range 00 to 60)
2674 * - \%t: a tab character
2675 * - \%T: the time in 24-hour notation with seconds (\%H:\%M:\%S)
2676 * - \%u: the ISO 8601 standard day of the week as a decimal, range 1 to 7,
2677 * Monday being 1. This works well with \%G and \%V.
2678 * - \%V: the ISO 8601 standard week number of the current year as a decimal
2679 * number, range 01 to 53, where week 1 is the first week that has at
2680 * least 4 days in the new year. See g_date_time_get_week_of_year().
2681 * This works well with \%G and \%u.
2682 * - \%w: the day of the week as a decimal, range 0 to 6, Sunday being 0.
2683 * This is not the ISO 8601 standard format -- use \%u instead.
2684 * - \%x: the preferred date representation for the current locale without
2686 * - \%X: the preferred time representation for the current locale without
2688 * - \%y: the year as a decimal number without the century
2689 * - \%Y: the year as a decimal number including the century
2690 * - \%z: the time zone as an offset from UTC (+hhmm)
2691 * - \%:z: the time zone as an offset from UTC (+hh:mm).
2692 * This is a gnulib strftime() extension. Since: 2.38
2693 * - \%::z: the time zone as an offset from UTC (+hh:mm:ss). This is a
2694 * gnulib strftime() extension. Since: 2.38
2695 * - \%:::z: the time zone as an offset from UTC, with : to necessary
2696 * precision (e.g., -04, +05:30). This is a gnulib strftime() extension. Since: 2.38
2697 * - \%Z: the time zone or name or abbreviation
2698 * - \%\%: a literal \% character
2700 * Some conversion specifications can be modified by preceding the
2701 * conversion specifier by one or more modifier characters. The
2702 * following modifiers are supported for many of the numeric
2705 * - O: Use alternative numeric symbols, if the current locale supports those.
2706 * - _: Pad a numeric result with spaces. This overrides the default padding
2707 * for the specifier.
2708 * - -: Do not pad a numeric result. This overrides the default padding
2709 * for the specifier.
2710 * - 0: Pad a numeric result with zeros. This overrides the default padding
2711 * for the specifier.
2713 * Returns: a newly allocated string formatted to the requested format
2714 * or %NULL in the case that there was an error. The string
2715 * should be freed with g_free().
2720 g_date_time_format (GDateTime
*datetime
,
2721 const gchar
*format
)
2725 gboolean locale_is_utf8
= g_get_charset (NULL
);
2727 g_return_val_if_fail (datetime
!= NULL
, NULL
);
2728 g_return_val_if_fail (format
!= NULL
, NULL
);
2729 g_return_val_if_fail (g_utf8_validate (format
, -1, NULL
), NULL
);
2731 outstr
= g_string_sized_new (strlen (format
) * 2);
2733 if (!g_date_time_format_locale (datetime
, format
, outstr
, locale_is_utf8
))
2735 g_string_free (outstr
, TRUE
);
2740 return g_string_free (outstr
, FALSE
);
2742 utf8
= g_locale_to_utf8 (outstr
->str
, outstr
->len
, NULL
, NULL
, NULL
);
2743 g_string_free (outstr
, TRUE
);
2749 /* vim:set foldmethod=marker: */