2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
24 #include "gtimezone.h"
30 #include "gmappedfile.h"
31 #include "gtestutils.h"
32 #include "gfileutils.h"
33 #include "gstrfuncs.h"
38 #include "gdatetime.h"
52 * @short_description: a structure representing a time zone
53 * @see_also: #GDateTime
55 * #GTimeZone is a structure that represents a time zone, at no
56 * particular point in time. It is refcounted and immutable.
58 * Each time zone has an identifier (for example, ‘Europe/London’) which is
59 * platform dependent. See g_time_zone_new() for information on the identifier
60 * formats. The identifier of a time zone can be retrieved using
61 * g_time_zone_get_identifier().
63 * A time zone contains a number of intervals. Each interval has
64 * an abbreviation to describe it (for example, ‘PDT’), an offset to UTC and a
65 * flag indicating if the daylight savings time is in effect during that
66 * interval. A time zone always has at least one interval — interval 0. Note
67 * that interval abbreviations are not the same as time zone identifiers
68 * (apart from ‘UTC’), and cannot be passed to g_time_zone_new().
70 * Every UTC time is contained within exactly one interval, but a given
71 * local time may be contained within zero, one or two intervals (due to
72 * incontinuities associated with daylight savings time).
74 * An interval may refer to a specific period of time (eg: the duration
75 * of daylight savings time during 2010) or it may refer to many periods
76 * of time that share the same properties (eg: all periods of daylight
77 * savings time). It is also possible (usually for political reasons)
78 * that some properties (like the abbreviation) change between intervals
79 * without other properties changing.
81 * #GTimeZone is available since GLib 2.26.
87 * #GTimeZone is an opaque structure whose members cannot be accessed
93 /* IANA zoneinfo file format {{{1 */
96 typedef struct { gchar bytes[8]; } gint64_be;
97 typedef struct { gchar bytes[4]; } gint32_be;
98 typedef struct { gchar bytes[4]; } guint32_be;
100 static inline gint64 gint64_from_be (const gint64_be be) {
101 gint64 tmp; memcpy (&tmp, &be, sizeof tmp); return GINT64_FROM_BE (tmp);
104 static inline gint32 gint32_from_be (const gint32_be be) {
105 gint32 tmp; memcpy (&tmp, &be, sizeof tmp); return GINT32_FROM_BE (tmp);
108 static inline guint32 guint32_from_be (const guint32_be be) {
109 guint32 tmp; memcpy (&tmp, &be, sizeof tmp); return GUINT32_FROM_BE (tmp);
112 /* The layout of an IANA timezone file header */
117 guchar tzh_reserved[15];
119 guint32_be tzh_ttisgmtcnt;
120 guint32_be tzh_ttisstdcnt;
121 guint32_be tzh_leapcnt;
122 guint32_be tzh_timecnt;
123 guint32_be tzh_typecnt;
124 guint32_be tzh_charcnt;
134 /* A Transition Date structure for TZ Rules, an intermediate structure
135 for parsing MSWindows and Environment-variable time zones. It
136 Generalizes MSWindows's SYSTEMTIME struct.
145 gint32 offset; /* hour*3600 + min*60 + sec; can be negative. */
148 /* POSIX Timezone abbreviations are typically 3 or 4 characters, but
149 Microsoft uses 32-character names. We'll use one larger to ensure
150 we have room for the terminating \0.
154 /* A MSWindows-style time zone transition rule. Generalizes the
155 MSWindows TIME_ZONE_INFORMATION struct. Also used to compose time
156 zones from tzset-style identifiers.
163 TimeZoneDate dlt_start;
164 TimeZoneDate dlt_end;
165 gchar std_name[NAME_SIZE];
166 gchar dlt_name[NAME_SIZE];
169 /* GTimeZone's internal representation of a Daylight Savings (Summer)
179 /* GTimeZone's representation of a transition time to or from Daylight
180 Savings (Summer) time and Standard time for the zone. */
187 /* GTimeZone structure */
191 GArray *t_info; /* Array of TransitionInfo */
192 GArray *transitions; /* Array of Transition */
196 G_LOCK_DEFINE_STATIC (time_zones);
197 static GHashTable/*<string?, GTimeZone>*/ *time_zones;
198 G_LOCK_DEFINE_STATIC (tz_local);
199 static GTimeZone *tz_local = NULL;
201 #define MIN_TZYEAR 1916 /* Daylight Savings started in WWI */
202 #define MAX_TZYEAR 2999 /* And it's not likely ever to go away, but
203 there's no point in getting carried
207 static GTimeZone *parse_footertz (const gchar *, size_t);
214 * Decreases the reference count on @tz.
219 g_time_zone_unref (GTimeZone *tz)
224 ref_count = g_atomic_int_get (&tz->ref_count);
226 g_assert (ref_count > 0);
230 if (tz->name != NULL)
234 /* someone else might have grabbed a ref in the meantime */
235 if G_UNLIKELY (g_atomic_int_get (&tz->ref_count) != 1)
237 G_UNLOCK(time_zones);
241 g_hash_table_remove (time_zones, tz->name);
242 G_UNLOCK(time_zones);
245 if (tz->t_info != NULL)
248 for (idx = 0; idx < tz->t_info->len; idx++)
250 TransitionInfo *info = &g_array_index (tz->t_info, TransitionInfo, idx);
251 g_free (info->abbrev);
253 g_array_free (tz->t_info, TRUE);
255 if (tz->transitions != NULL)
256 g_array_free (tz->transitions, TRUE);
259 g_slice_free (GTimeZone, tz);
262 else if G_UNLIKELY (!g_atomic_int_compare_and_exchange (&tz->ref_count,
272 * Increases the reference count on @tz.
274 * Returns: a new reference to @tz.
279 g_time_zone_ref (GTimeZone *tz)
281 g_assert (tz->ref_count > 0);
283 g_atomic_int_inc (&tz->ref_count);
288 /* fake zoneinfo creation (for RFC3339/ISO 8601 timezones) {{{1 */
290 * parses strings of the form h or hh[[:]mm[[[:]ss]]] where:
294 * If RFC8536, TIME_ is a transition time sans sign,
295 * so colons are required before mm and ss, and hh can be up to 167.
296 * See Internet RFC 8536 section 3.3.1:
297 * https://tools.ietf.org/html/rfc8536#section-3.3.1
298 * and POSIX Base Definitions 8.3 TZ rule time:
299 * https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03
302 parse_time (const gchar *time_,
306 if (*time_ < '0' || '9' < *time_)
309 *offset = 60 * 60 * (*time_++ - '0');
316 if (*time_ < '0' || '9' < *time_)
320 *offset += 60 * 60 * (*time_++ - '0');
324 /* Internet RFC 8536 section 3.3.1 and POSIX 8.3 TZ together say
325 that a transition time must be of the form [+-]hh[:mm[:ss]] where
326 the hours part can range from -167 to 167. */
327 if ('0' <= *time_ && *time_ <= '9')
330 *offset += 60 * 60 * (*time_++ - '0');
332 if (*offset > 167 * 60 * 60)
335 else if (*offset > 24 * 60 * 60)
347 if (*time_ < '0' || '5' < *time_)
350 *offset += 10 * 60 * (*time_++ - '0');
352 if (*time_ < '0' || '9' < *time_)
355 *offset += 60 * (*time_++ - '0');
365 if (*time_ < '0' || '5' < *time_)
368 *offset += 10 * (*time_++ - '0');
370 if (*time_ < '0' || '9' < *time_)
373 *offset += *time_++ - '0';
375 return *time_ == '\0';
379 parse_constant_offset (const gchar *name,
383 /* Internet RFC 8536 section 3.3.1 and POSIX 8.3 TZ together say
384 that a transition time must be numeric. */
385 if (!rfc8536 && g_strcmp0 (name, "UTC") == 0)
391 if (*name >= '0' && '9' >= *name)
392 return parse_time (name, offset, rfc8536);
398 /* Internet RFC 8536 section 3.3.1 requires a numeric zone. */
399 return !rfc8536 && !*name;
402 return parse_time (name, offset, rfc8536);
405 if (parse_time (name, offset, rfc8536))
419 zone_for_constant_offset (GTimeZone *gtz, const gchar *name)
424 if (name == NULL || !parse_constant_offset (name, &offset, FALSE))
427 info.gmt_offset = offset;
429 info.abbrev = g_strdup (name);
431 gtz->name = g_strdup (name);
432 gtz->t_info = g_array_sized_new (FALSE, TRUE, sizeof (TransitionInfo), 1);
433 g_array_append_val (gtz->t_info, info);
435 /* Constant offset, no transitions */
436 gtz->transitions = NULL;
441 zone_info_unix (const gchar *identifier,
442 gchar **out_identifier)
445 GMappedFile *file = NULL;
446 GBytes *zoneinfo = NULL;
447 gchar *resolved_identifier = NULL;
450 tzdir = g_getenv ("TZDIR");
452 tzdir = "/usr/share/zoneinfo";
454 /* identifier can be a relative or absolute path name;
455 if relative, it is interpreted starting from /usr/share/zoneinfo
456 while the POSIX standard says it should start with :,
457 glibc allows both syntaxes, so we should too */
458 if (identifier != NULL)
460 resolved_identifier = g_strdup (identifier);
462 if (*identifier == ':')
465 if (g_path_is_absolute (identifier))
466 filename = g_strdup (identifier);
468 filename = g_build_filename (tzdir, identifier, NULL);
472 gsize prefix_len = 0;
473 gchar *canonical_path = NULL;
474 GError *read_link_err = NULL;
476 filename = g_strdup ("/etc/localtime");
478 /* Resolve the actual timezone pointed to by /etc/localtime. */
479 resolved_identifier = g_file_read_link (filename, &read_link_err);
480 if (resolved_identifier == NULL)
482 gboolean not_a_symlink = g_error_matches (read_link_err,
485 g_clear_error (&read_link_err);
487 /* Fallback to the content of /var/db/zoneinfo or /etc/timezone
488 * if /etc/localtime is not a symlink. /var/db/zoneinfo is
489 * where 'tzsetup' program on FreeBSD and DragonflyBSD stores
490 * the timezone chosen by the user. /etc/timezone is where user
491 * choice is expressed on Gentoo OpenRC and others. */
492 if (not_a_symlink && (g_file_get_contents ("/var/db/zoneinfo",
493 &resolved_identifier,
495 g_file_get_contents ("/etc/timezone",
496 &resolved_identifier,
498 g_strchomp (resolved_identifier);
502 g_assert (resolved_identifier == NULL);
508 /* Resolve relative path */
509 canonical_path = g_canonicalize_filename (resolved_identifier, "/etc");
510 g_free (resolved_identifier);
511 resolved_identifier = g_steal_pointer (&canonical_path);
514 /* Strip the prefix and slashes if possible. */
515 if (g_str_has_prefix (resolved_identifier, tzdir))
517 prefix_len = strlen (tzdir);
518 while (*(resolved_identifier + prefix_len) == '/')
523 memmove (resolved_identifier, resolved_identifier + prefix_len,
524 strlen (resolved_identifier) - prefix_len + 1 /* nul terminator */);
526 g_free (canonical_path);
529 file = g_mapped_file_new (filename, FALSE, NULL);
532 zoneinfo = g_bytes_new_with_free_func (g_mapped_file_get_contents (file),
533 g_mapped_file_get_length (file),
534 (GDestroyNotify)g_mapped_file_unref,
535 g_mapped_file_ref (file));
536 g_mapped_file_unref (file);
539 g_assert (resolved_identifier != NULL);
542 if (out_identifier != NULL)
543 *out_identifier = g_steal_pointer (&resolved_identifier);
545 g_free (resolved_identifier);
552 init_zone_from_iana_info (GTimeZone *gtz,
554 gchar *identifier /* (transfer full) */)
558 guint32 time_count, type_count;
559 guint8 *tz_transitions, *tz_type_index, *tz_ttinfo;
561 gsize timesize = sizeof (gint32);
562 gconstpointer header_data = g_bytes_get_data (zoneinfo, &size);
563 const gchar *data = header_data;
564 const struct tzhead *header = header_data;
565 GTimeZone *footertz = NULL;
566 guint extra_time_count = 0, extra_type_count = 0;
567 gint64 last_explicit_transition_time;
569 g_return_if_fail (size >= sizeof (struct tzhead) &&
570 memcmp (header, "TZif", 4) == 0);
572 /* FIXME: Handle invalid TZif files better (Issue#1088). */
574 if (header->tzh_version >= '2')
576 /* Skip ahead to the newer 64-bit data if it's available. */
577 header = (const struct tzhead *)
578 (((const gchar *) (header + 1)) +
579 guint32_from_be(header->tzh_ttisgmtcnt) +
580 guint32_from_be(header->tzh_ttisstdcnt) +
581 8 * guint32_from_be(header->tzh_leapcnt) +
582 5 * guint32_from_be(header->tzh_timecnt) +
583 6 * guint32_from_be(header->tzh_typecnt) +
584 guint32_from_be(header->tzh_charcnt));
585 timesize = sizeof (gint64);
587 time_count = guint32_from_be(header->tzh_timecnt);
588 type_count = guint32_from_be(header->tzh_typecnt);
590 if (header->tzh_version >= '2')
592 const gchar *footer = (((const gchar *) (header + 1))
593 + guint32_from_be(header->tzh_ttisgmtcnt)
594 + guint32_from_be(header->tzh_ttisstdcnt)
595 + 12 * guint32_from_be(header->tzh_leapcnt)
598 + guint32_from_be(header->tzh_charcnt));
599 const gchar *footerlast;
601 g_return_if_fail (footer <= data + size - 2 && footer[0] == '\n');
602 footerlast = memchr (footer + 1, '\n', data + size - (footer + 1));
603 g_return_if_fail (footerlast);
604 footerlen = footerlast + 1 - footer;
607 footertz = parse_footertz (footer, footerlen);
608 g_return_if_fail (footertz);
609 extra_type_count = footertz->t_info->len;
610 extra_time_count = footertz->transitions->len;
614 tz_transitions = ((guint8 *) (header) + sizeof (*header));
615 tz_type_index = tz_transitions + timesize * time_count;
616 tz_ttinfo = tz_type_index + time_count;
617 tz_abbrs = tz_ttinfo + sizeof (struct ttinfo) * type_count;
619 gtz->name = g_steal_pointer (&identifier);
620 gtz->t_info = g_array_sized_new (FALSE, TRUE, sizeof (TransitionInfo),
621 type_count + extra_type_count);
622 gtz->transitions = g_array_sized_new (FALSE, TRUE, sizeof (Transition),
623 time_count + extra_time_count);
625 for (index = 0; index < type_count; index++)
627 TransitionInfo t_info;
628 struct ttinfo info = ((struct ttinfo*)tz_ttinfo)[index];
629 t_info.gmt_offset = gint32_from_be (info.tt_gmtoff);
630 t_info.is_dst = info.tt_isdst ? TRUE : FALSE;
631 t_info.abbrev = g_strdup ((gchar *) &tz_abbrs[info.tt_abbrind]);
632 g_array_append_val (gtz->t_info, t_info);
635 for (index = 0; index < time_count; index++)
638 if (header->tzh_version >= '2')
639 trans.time = gint64_from_be (((gint64_be*)tz_transitions)[index]);
641 trans.time = gint32_from_be (((gint32_be*)tz_transitions)[index]);
642 last_explicit_transition_time = trans.time;
643 trans.info_index = tz_type_index[index];
644 g_assert (trans.info_index >= 0);
645 g_assert ((guint) trans.info_index < gtz->t_info->len);
646 g_array_append_val (gtz->transitions, trans);
651 /* Append footer time types. Don't bother to coalesce
652 duplicates with existing time types. */
653 for (index = 0; index < extra_type_count; index++)
655 TransitionInfo t_info;
656 TransitionInfo *footer_t_info
657 = &g_array_index (footertz->t_info, TransitionInfo, index);
658 t_info.gmt_offset = footer_t_info->gmt_offset;
659 t_info.is_dst = footer_t_info->is_dst;
660 t_info.abbrev = g_steal_pointer (&footer_t_info->abbrev);
661 g_array_append_val (gtz->t_info, t_info);
664 /* Append footer transitions that follow the last explicit
666 for (index = 0; index < extra_time_count; index++)
668 Transition *footer_transition
669 = &g_array_index (footertz->transitions, Transition, index);
671 || last_explicit_transition_time < footer_transition->time)
674 trans.time = footer_transition->time;
675 trans.info_index = type_count + footer_transition->info_index;
676 g_array_append_val (gtz->transitions, trans);
680 g_time_zone_unref (footertz);
684 #elif defined (G_OS_WIN32)
687 copy_windows_systemtime (SYSTEMTIME *s_time, TimeZoneDate *tzdate)
690 = s_time->wHour * 3600 + s_time->wMinute * 60 + s_time->wSecond;
691 tzdate->mon = s_time->wMonth;
692 tzdate->year = s_time->wYear;
693 tzdate->wday = s_time->wDayOfWeek ? s_time->wDayOfWeek : 7;
697 tzdate->mday = s_time->wDay;
701 tzdate->week = s_time->wDay;
704 /* UTC = local time + bias while local time = UTC + offset */
706 rule_from_windows_time_zone_info (TimeZoneRule *rule,
707 TIME_ZONE_INFORMATION *tzi)
709 gchar *std_name, *dlt_name;
711 std_name = g_utf16_to_utf8 ((gunichar2 *)tzi->StandardName, -1, NULL, NULL, NULL);
712 if (std_name == NULL)
715 dlt_name = g_utf16_to_utf8 ((gunichar2 *)tzi->DaylightName, -1, NULL, NULL, NULL);
716 if (dlt_name == NULL)
723 if (tzi->StandardDate.wMonth)
725 rule->std_offset = -(tzi->Bias + tzi->StandardBias) * 60;
726 rule->dlt_offset = -(tzi->Bias + tzi->DaylightBias) * 60;
727 copy_windows_systemtime (&(tzi->DaylightDate), &(rule->dlt_start));
729 copy_windows_systemtime (&(tzi->StandardDate), &(rule->dlt_end));
734 rule->std_offset = -tzi->Bias * 60;
735 rule->dlt_start.mon = 0;
737 strncpy (rule->std_name, std_name, NAME_SIZE - 1);
738 strncpy (rule->dlt_name, dlt_name, NAME_SIZE - 1);
747 windows_default_tzname (void)
749 const gunichar2 *subkey =
750 L"SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation";
752 gchar *key_name = NULL;
753 gunichar2 *key_name_w = NULL;
754 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE, subkey, 0,
755 KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
758 if (RegQueryValueExW (key, L"TimeZoneKeyName", NULL, NULL,
759 NULL, &size) == ERROR_SUCCESS)
761 key_name_w = g_malloc ((gint)size);
763 if (key_name_w == NULL ||
764 RegQueryValueExW (key, L"TimeZoneKeyName", NULL, NULL,
765 (LPBYTE)key_name_w, &size) != ERROR_SUCCESS)
771 key_name = g_utf16_to_utf8 (key_name_w, -1, NULL, NULL, NULL);
783 SYSTEMTIME StandardDate;
784 SYSTEMTIME DaylightDate;
788 system_time_copy (SYSTEMTIME *orig, SYSTEMTIME *target)
790 g_return_if_fail (orig != NULL);
791 g_return_if_fail (target != NULL);
793 target->wYear = orig->wYear;
794 target->wMonth = orig->wMonth;
795 target->wDayOfWeek = orig->wDayOfWeek;
796 target->wDay = orig->wDay;
797 target->wHour = orig->wHour;
798 target->wMinute = orig->wMinute;
799 target->wSecond = orig->wSecond;
800 target->wMilliseconds = orig->wMilliseconds;
804 register_tzi_to_tzi (RegTZI *reg, TIME_ZONE_INFORMATION *tzi)
806 g_return_if_fail (reg != NULL);
807 g_return_if_fail (tzi != NULL);
808 tzi->Bias = reg->Bias;
809 system_time_copy (&(reg->StandardDate), &(tzi->StandardDate));
810 tzi->StandardBias = reg->StandardBias;
811 system_time_copy (&(reg->DaylightDate), &(tzi->DaylightDate));
812 tzi->DaylightBias = reg->DaylightBias;
816 rules_from_windows_time_zone (const gchar *identifier,
817 gchar **out_identifier,
818 TimeZoneRule **rules,
819 gboolean copy_identifier)
822 gchar *subkey = NULL;
823 gchar *subkey_dynamic = NULL;
824 gchar *key_name = NULL;
825 const gchar *reg_key =
826 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\";
827 TIME_ZONE_INFORMATION tzi;
830 RegTZI regtzi, regtzi_prev;
831 WCHAR winsyspath[MAX_PATH];
832 gunichar2 *subkey_w, *subkey_dynamic_w;
834 subkey_dynamic_w = NULL;
836 if (GetSystemDirectoryW (winsyspath, MAX_PATH) == 0)
839 g_assert (copy_identifier == FALSE || out_identifier != NULL);
840 g_assert (rules != NULL);
843 *out_identifier = NULL;
849 key_name = windows_default_tzname ();
851 key_name = g_strdup (identifier);
856 subkey = g_strconcat (reg_key, key_name, NULL);
857 subkey_w = g_utf8_to_utf16 (subkey, -1, NULL, NULL, NULL);
858 if (subkey_w == NULL)
859 goto utf16_conv_failed;
861 subkey_dynamic = g_strconcat (subkey, "\\Dynamic DST", NULL);
862 subkey_dynamic_w = g_utf8_to_utf16 (subkey_dynamic, -1, NULL, NULL, NULL);
863 if (subkey_dynamic_w == NULL)
864 goto utf16_conv_failed;
866 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE, subkey_w, 0,
867 KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
868 goto utf16_conv_failed;
870 size = sizeof tzi.StandardName;
872 /* use RegLoadMUIStringW() to query MUI_Std from the registry if possible, otherwise
873 fallback to querying Std */
874 if (RegLoadMUIStringW (key, L"MUI_Std", tzi.StandardName,
875 size, &size, 0, winsyspath) != ERROR_SUCCESS)
877 size = sizeof tzi.StandardName;
878 if (RegQueryValueExW (key, L"Std", NULL, NULL,
879 (LPBYTE)&(tzi.StandardName), &size) != ERROR_SUCCESS)
880 goto registry_failed;
883 size = sizeof tzi.DaylightName;
885 /* use RegLoadMUIStringW() to query MUI_Dlt from the registry if possible, otherwise
886 fallback to querying Dlt */
887 if (RegLoadMUIStringW (key, L"MUI_Dlt", tzi.DaylightName,
888 size, &size, 0, winsyspath) != ERROR_SUCCESS)
890 size = sizeof tzi.DaylightName;
891 if (RegQueryValueExW (key, L"Dlt", NULL, NULL,
892 (LPBYTE)&(tzi.DaylightName), &size) != ERROR_SUCCESS)
893 goto registry_failed;
897 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE, subkey_dynamic_w, 0,
898 KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
905 if (RegQueryValueExW (key, L"FirstEntry", NULL, NULL,
906 (LPBYTE) &first, &size) != ERROR_SUCCESS)
907 goto registry_failed;
910 if (RegQueryValueExW (key, L"LastEntry", NULL, NULL,
911 (LPBYTE) &last, &size) != ERROR_SUCCESS)
912 goto registry_failed;
914 rules_num = last - first + 2;
915 *rules = g_new0 (TimeZoneRule, rules_num);
917 for (year = first, i = 0; *rules != NULL && year <= last; year++)
919 gboolean failed = FALSE;
920 swprintf_s (s, 11, L"%d", year);
924 size = sizeof regtzi;
925 if (RegQueryValueExW (key, s, NULL, NULL,
926 (LPBYTE) ®tzi, &size) != ERROR_SUCCESS)
937 if (year > first && memcmp (®tzi_prev, ®tzi, sizeof regtzi) == 0)
940 memcpy (®tzi_prev, ®tzi, sizeof regtzi);
942 register_tzi_to_tzi (®tzi, &tzi);
944 if (!rule_from_windows_time_zone_info (&(*rules)[i], &tzi))
951 (*rules)[i++].start_year = year;
959 else if (RegOpenKeyExW (HKEY_LOCAL_MACHINE, subkey_w, 0,
960 KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
962 size = sizeof regtzi;
963 if (RegQueryValueExW (key, L"TZI", NULL, NULL,
964 (LPBYTE) ®tzi, &size) == ERROR_SUCCESS)
967 *rules = g_new0 (TimeZoneRule, 2);
968 register_tzi_to_tzi (®tzi, &tzi);
970 if (!rule_from_windows_time_zone_info (&(*rules)[0], &tzi))
981 g_free (subkey_dynamic_w);
982 g_free (subkey_dynamic);
988 (*rules)[0].start_year = MIN_TZYEAR;
989 if ((*rules)[rules_num - 2].start_year < MAX_TZYEAR)
990 (*rules)[rules_num - 1].start_year = MAX_TZYEAR;
992 (*rules)[rules_num - 1].start_year = (*rules)[rules_num - 2].start_year + 1;
995 *out_identifier = g_steal_pointer (&key_name);
1010 find_relative_date (TimeZoneDate *buffer)
1014 g_date_clear (&date, 1);
1015 wday = buffer->wday;
1017 /* Get last day if last is needed, first day otherwise */
1018 if (buffer->mon == 13 || buffer->mon == 14) /* Julian Date */
1020 g_date_set_dmy (&date, 1, 1, buffer->year);
1021 if (wday >= 59 && buffer->mon == 13 && g_date_is_leap_year (buffer->year))
1022 g_date_add_days (&date, wday);
1024 g_date_add_days (&date, wday - 1);
1025 buffer->mon = (int) g_date_get_month (&date);
1026 buffer->mday = (int) g_date_get_day (&date);
1032 guint days_in_month = g_date_get_days_in_month (buffer->mon, buffer->year);
1033 GDateWeekday first_wday;
1035 g_date_set_dmy (&date, 1, buffer->mon, buffer->year);
1036 first_wday = g_date_get_weekday (&date);
1038 if (first_wday > wday)
1040 /* week is 1 <= w <= 5, we need 0-based */
1041 days = 7 * (buffer->week - 1) + wday - first_wday;
1043 while (days > days_in_month)
1046 g_date_add_days (&date, days);
1048 buffer->mday = g_date_get_day (&date);
1052 /* Offset is previous offset of local time. Returns 0 if month is 0 */
1054 boundary_for_year (TimeZoneDate *boundary,
1058 TimeZoneDate buffer;
1060 const guint64 unix_epoch_start = 719163L;
1061 const guint64 seconds_per_day = 86400L;
1067 if (boundary->year == 0)
1072 find_relative_date (&buffer);
1075 g_assert (buffer.year == year);
1076 g_date_clear (&date, 1);
1077 g_date_set_dmy (&date, buffer.mday, buffer.mon, buffer.year);
1078 return ((g_date_get_julian (&date) - unix_epoch_start) * seconds_per_day +
1079 buffer.offset - offset);
1083 fill_transition_info_from_rule (TransitionInfo *info,
1087 gint offset = is_dst ? rule->dlt_offset : rule->std_offset;
1088 gchar *name = is_dst ? rule->dlt_name : rule->std_name;
1090 info->gmt_offset = offset;
1091 info->is_dst = is_dst;
1094 info->abbrev = g_strdup (name);
1097 info->abbrev = g_strdup_printf ("%+03d%02d",
1098 (int) offset / 3600,
1099 (int) abs (offset / 60) % 60);
1103 init_zone_from_rules (GTimeZone *gtz,
1104 TimeZoneRule *rules,
1106 gchar *identifier /* (transfer full) */)
1108 guint type_count = 0, trans_count = 0, info_index = 0;
1109 guint ri; /* rule index */
1110 gboolean skip_first_std_trans = TRUE;
1116 /* Last rule only contains max year */
1117 for (ri = 0; ri < rules_num - 1; ri++)
1119 if (rules[ri].dlt_start.mon || rules[ri].dlt_end.mon)
1121 guint rulespan = (rules[ri + 1].start_year - rules[ri].start_year);
1122 guint transitions = rules[ri].dlt_start.mon > 0 ? 1 : 0;
1123 transitions += rules[ri].dlt_end.mon > 0 ? 1 : 0;
1124 type_count += rules[ri].dlt_start.mon > 0 ? 2 : 1;
1125 trans_count += transitions * rulespan;
1131 gtz->name = g_steal_pointer (&identifier);
1132 gtz->t_info = g_array_sized_new (FALSE, TRUE, sizeof (TransitionInfo), type_count);
1133 gtz->transitions = g_array_sized_new (FALSE, TRUE, sizeof (Transition), trans_count);
1135 last_offset = rules[0].std_offset;
1137 for (ri = 0; ri < rules_num - 1; ri++)
1139 if ((rules[ri].std_offset || rules[ri].dlt_offset) &&
1140 rules[ri].dlt_start.mon == 0 && rules[ri].dlt_end.mon == 0)
1142 TransitionInfo std_info;
1144 fill_transition_info_from_rule (&std_info, &(rules[ri]), FALSE);
1145 g_array_append_val (gtz->t_info, std_info);
1148 ((rules[ri - 1].dlt_start.mon > 12 &&
1149 rules[ri - 1].dlt_start.wday > rules[ri - 1].dlt_end.wday) ||
1150 rules[ri - 1].dlt_start.mon > rules[ri - 1].dlt_end.mon))
1152 /* The previous rule was a southern hemisphere rule that
1153 starts the year with DST, so we need to add a
1154 transition to return to standard time */
1155 guint year = rules[ri].start_year;
1156 gint64 std_time = boundary_for_year (&rules[ri].dlt_end,
1158 Transition std_trans = {std_time, info_index};
1159 g_array_append_val (gtz->transitions, std_trans);
1162 last_offset = rules[ri].std_offset;
1164 skip_first_std_trans = TRUE;
1168 const guint start_year = rules[ri].start_year;
1169 const guint end_year = rules[ri + 1].start_year;
1172 TransitionInfo std_info, dlt_info;
1173 if (rules[ri].dlt_start.mon > 12)
1174 dlt_first = rules[ri].dlt_start.wday > rules[ri].dlt_end.wday;
1176 dlt_first = rules[ri].dlt_start.mon > rules[ri].dlt_end.mon;
1177 /* Standard rules are always even, because before the first
1178 transition is always standard time, and 0 is even. */
1179 fill_transition_info_from_rule (&std_info, &(rules[ri]), FALSE);
1180 fill_transition_info_from_rule (&dlt_info, &(rules[ri]), TRUE);
1182 g_array_append_val (gtz->t_info, std_info);
1183 g_array_append_val (gtz->t_info, dlt_info);
1185 /* Transition dates. We hope that a year which ends daylight
1186 time in a southern-hemisphere country (i.e., one that
1187 begins the year in daylight time) will include a rule
1188 which has only a dlt_end. */
1189 for (year = start_year; year < end_year; year++)
1191 gint32 dlt_offset = (dlt_first ? last_offset :
1192 rules[ri].dlt_offset);
1193 gint32 std_offset = (dlt_first ? rules[ri].std_offset :
1195 /* NB: boundary_for_year returns 0 if mon == 0 */
1196 gint64 std_time = boundary_for_year (&rules[ri].dlt_end,
1198 gint64 dlt_time = boundary_for_year (&rules[ri].dlt_start,
1200 Transition std_trans = {std_time, info_index};
1201 Transition dlt_trans = {dlt_time, info_index + 1};
1202 last_offset = (dlt_first ? rules[ri].dlt_offset :
1203 rules[ri].std_offset);
1206 if (skip_first_std_trans)
1207 skip_first_std_trans = FALSE;
1209 g_array_append_val (gtz->transitions, std_trans);
1211 g_array_append_val (gtz->transitions, dlt_trans);
1216 g_array_append_val (gtz->transitions, dlt_trans);
1218 g_array_append_val (gtz->transitions, std_trans);
1226 ((rules[ri - 1].dlt_start.mon > 12 &&
1227 rules[ri - 1].dlt_start.wday > rules[ri - 1].dlt_end.wday) ||
1228 rules[ri - 1].dlt_start.mon > rules[ri - 1].dlt_end.mon))
1230 /* The previous rule was a southern hemisphere rule that
1231 starts the year with DST, so we need to add a
1232 transition to return to standard time */
1233 TransitionInfo info;
1234 guint year = rules[ri].start_year;
1236 fill_transition_info_from_rule (&info, &(rules[ri - 1]), FALSE);
1237 g_array_append_val (gtz->t_info, info);
1238 trans.time = boundary_for_year (&rules[ri - 1].dlt_end,
1240 trans.info_index = info_index;
1241 g_array_append_val (gtz->transitions, trans);
1246 * parses date[/time] for parsing TZ environment variable
1248 * date is either Mm.w.d, Jn or N
1255 * time is either h or hh[[:]mm[[[:]ss]]]
1261 parse_mwd_boundary (gchar **pos, TimeZoneDate *boundary)
1263 gint month, week, day;
1265 if (**pos == '\0' || **pos < '0' || '9' < **pos)
1268 month = *(*pos)++ - '0';
1270 if ((month == 1 && **pos >= '0' && '2' >= **pos) ||
1271 (month == 0 && **pos >= '0' && '9' >= **pos))
1274 month += *(*pos)++ - '0';
1277 if (*(*pos)++ != '.' || month == 0)
1280 if (**pos == '\0' || **pos < '1' || '5' < **pos)
1283 week = *(*pos)++ - '0';
1285 if (*(*pos)++ != '.')
1288 if (**pos == '\0' || **pos < '0' || '6' < **pos)
1291 day = *(*pos)++ - '0';
1297 boundary->mon = month;
1298 boundary->week = week;
1299 boundary->wday = day;
1304 * This parses two slightly different ways of specifying
1307 * - ignore_leap == TRUE
1309 * Jn This specifies the Julian day with n between 1 and 365. Leap days
1310 * are not counted. In this format, February 29 can't be represented;
1311 * February 28 is day 59, and March 1 is always day 60.
1313 * - ignore_leap == FALSE
1315 * n This specifies the zero-based Julian day with n between 0 and 365.
1316 * February 29 is counted in leap years.
1319 parse_julian_boundary (gchar** pos, TimeZoneDate *boundary,
1320 gboolean ignore_leap)
1325 while (**pos >= '0' && '9' >= **pos)
1328 day += *(*pos)++ - '0';
1333 if (day < 1 || 365 < day)
1340 if (day < 0 || 365 < day)
1342 /* GDate wants day in range 1->366 */
1346 g_date_clear (&date, 1);
1347 g_date_set_julian (&date, day);
1349 boundary->mon = (int) g_date_get_month (&date);
1350 boundary->mday = (int) g_date_get_day (&date);
1357 parse_tz_boundary (const gchar *identifier,
1358 TimeZoneDate *boundary)
1362 pos = (gchar*)identifier;
1363 /* Month-week-weekday */
1367 if (!parse_mwd_boundary (&pos, boundary))
1370 /* Julian date which ignores Feb 29 in leap years */
1371 else if (*pos == 'J')
1374 if (!parse_julian_boundary (&pos, boundary, TRUE))
1377 /* Julian date which counts Feb 29 in leap years */
1378 else if (*pos >= '0' && '9' >= *pos)
1380 if (!parse_julian_boundary (&pos, boundary, FALSE))
1389 return parse_constant_offset (pos + 1, &boundary->offset, TRUE);
1392 boundary->offset = 2 * 60 * 60;
1393 return *pos == '\0';
1398 create_ruleset_from_rule (TimeZoneRule **rules, TimeZoneRule *rule)
1400 *rules = g_new0 (TimeZoneRule, 2);
1402 (*rules)[0].start_year = MIN_TZYEAR;
1403 (*rules)[1].start_year = MAX_TZYEAR;
1405 (*rules)[0].std_offset = -rule->std_offset;
1406 (*rules)[0].dlt_offset = -rule->dlt_offset;
1407 (*rules)[0].dlt_start = rule->dlt_start;
1408 (*rules)[0].dlt_end = rule->dlt_end;
1409 strcpy ((*rules)[0].std_name, rule->std_name);
1410 strcpy ((*rules)[0].dlt_name, rule->dlt_name);
1415 parse_offset (gchar **pos, gint32 *target)
1418 gchar *target_pos = *pos;
1421 while (**pos == '+' || **pos == '-' || **pos == ':' ||
1422 (**pos >= '0' && '9' >= **pos))
1425 buffer = g_strndup (target_pos, *pos - target_pos);
1426 ret = parse_constant_offset (buffer, target, FALSE);
1433 parse_identifier_boundary (gchar **pos, TimeZoneDate *target)
1436 gchar *target_pos = *pos;
1439 while (**pos != ',' && **pos != '\0')
1441 buffer = g_strndup (target_pos, *pos - target_pos);
1442 ret = parse_tz_boundary (buffer, target);
1449 set_tz_name (gchar **pos, gchar *buffer, guint size)
1451 gboolean quoted = **pos == '<';
1452 gchar *name_pos = *pos;
1460 while (g_ascii_isalnum (**pos) || **pos == '-' || **pos == '+');
1465 while (g_ascii_isalpha (**pos))
1468 /* Name should be three or more characters */
1469 /* FIXME: Should return FALSE if the name is too long.
1470 This should simplify code later in this function. */
1471 if (*pos - name_pos < 3)
1474 memset (buffer, 0, size);
1475 /* name_pos isn't 0-terminated, so we have to limit the length expressly */
1476 len = *pos - name_pos > size - 1 ? size - 1 : *pos - name_pos;
1477 strncpy (buffer, name_pos, len);
1483 parse_identifier_boundaries (gchar **pos, TimeZoneRule *tzr)
1485 if (*(*pos)++ != ',')
1489 if (!parse_identifier_boundary (pos, &(tzr->dlt_start)) || *(*pos)++ != ',')
1493 if (!parse_identifier_boundary (pos, &(tzr->dlt_end)))
1499 * Creates an array of TimeZoneRule from a TZ environment variable
1500 * type of identifier. Should free rules afterwards
1503 rules_from_identifier (const gchar *identifier,
1504 gchar **out_identifier,
1505 TimeZoneRule **rules)
1510 g_assert (out_identifier != NULL);
1511 g_assert (rules != NULL);
1513 *out_identifier = NULL;
1519 pos = (gchar*)identifier;
1520 memset (&tzr, 0, sizeof (tzr));
1521 /* Standard offset */
1522 if (!(set_tz_name (&pos, tzr.std_name, NAME_SIZE)) ||
1523 !parse_offset (&pos, &(tzr.std_offset)))
1528 *out_identifier = g_strdup (identifier);
1529 return create_ruleset_from_rule (rules, &tzr);
1533 if (!(set_tz_name (&pos, tzr.dlt_name, NAME_SIZE)))
1535 parse_offset (&pos, &(tzr.dlt_offset));
1536 if (tzr.dlt_offset == 0) /* No daylight offset given, assume it's 1
1537 hour earlier that standard */
1538 tzr.dlt_offset = tzr.std_offset - 3600;
1541 /* Windows allows us to use the US DST boundaries if they're not given */
1544 guint rules_num = 0;
1546 /* Use US rules, Windows' default is Pacific Standard Time */
1547 if ((rules_num = rules_from_windows_time_zone ("Pacific Standard Time",
1552 /* We don't want to hardcode our identifier here as
1553 * "Pacific Standard Time", use what was passed in
1555 *out_identifier = g_strdup (identifier);
1557 for (i = 0; i < rules_num - 1; i++)
1559 (*rules)[i].std_offset = - tzr.std_offset;
1560 (*rules)[i].dlt_offset = - tzr.dlt_offset;
1561 strcpy ((*rules)[i].std_name, tzr.std_name);
1562 strcpy ((*rules)[i].dlt_name, tzr.dlt_name);
1573 /* Start and end required (format 2) */
1574 if (!parse_identifier_boundaries (&pos, &tzr))
1577 *out_identifier = g_strdup (identifier);
1578 return create_ruleset_from_rule (rules, &tzr);
1583 parse_footertz (const gchar *footer, size_t footerlen)
1585 gchar *tzstring = g_strndup (footer + 1, footerlen - 2);
1586 GTimeZone *footertz = NULL;
1588 /* FIXME: it might make sense to modify rules_from_identifier to
1589 allow NULL to be passed instead of &ident, saving the strdup/free
1590 pair. The allocation for tzstring could also be avoided by
1591 passing a gsize identifier_len argument to rules_from_identifier
1592 and changing the code in that function to stop assuming that
1593 identifier is nul-terminated. */
1595 TimeZoneRule *rules;
1596 guint rules_num = rules_from_identifier (tzstring, &ident, &rules);
1602 footertz = g_slice_new0 (GTimeZone);
1603 init_zone_from_rules (footertz, rules, rules_num, NULL);
1604 footertz->ref_count++;
1611 /* Construction {{{1 */
1614 * @identifier: (nullable): a timezone identifier
1616 * Creates a #GTimeZone corresponding to @identifier.
1618 * @identifier can either be an RFC3339/ISO 8601 time offset or
1619 * something that would pass as a valid value for the `TZ` environment
1620 * variable (including %NULL).
1622 * In Windows, @identifier can also be the unlocalized name of a time
1623 * zone for standard time, for example "Pacific Standard Time".
1625 * Valid RFC3339 time offsets are `"Z"` (for UTC) or
1626 * `"±hh:mm"`. ISO 8601 additionally specifies
1627 * `"±hhmm"` and `"±hh"`. Offsets are
1628 * time values to be added to Coordinated Universal Time (UTC) to get
1631 * In UNIX, the `TZ` environment variable typically corresponds
1632 * to the name of a file in the zoneinfo database, an absolute path to a file
1633 * somewhere else, or a string in
1634 * "std offset [dst [offset],start[/time],end[/time]]" (POSIX) format.
1635 * There are no spaces in the specification. The name of standard
1636 * and daylight savings time zone must be three or more alphabetic
1637 * characters. Offsets are time values to be added to local time to
1638 * get Coordinated Universal Time (UTC) and should be
1639 * `"[±]hh[[:]mm[:ss]]"`. Dates are either
1640 * `"Jn"` (Julian day with n between 1 and 365, leap
1641 * years not counted), `"n"` (zero-based Julian day
1642 * with n between 0 and 365) or `"Mm.w.d"` (day d
1643 * (0 <= d <= 6) of week w (1 <= w <= 5) of month m (1 <= m <= 12), day
1644 * 0 is a Sunday). Times are in local wall clock time, the default is
1647 * In Windows, the "tzn[+|–]hh[:mm[:ss]][dzn]" format is used, but also
1648 * accepts POSIX format. The Windows format uses US rules for all time
1649 * zones; daylight savings time is 60 minutes behind the standard time
1650 * with date and time of change taken from Pacific Standard Time.
1651 * Offsets are time values to be added to the local time to get
1652 * Coordinated Universal Time (UTC).
1654 * g_time_zone_new_local() calls this function with the value of the
1655 * `TZ` environment variable. This function itself is independent of
1656 * the value of `TZ`, but if @identifier is %NULL then `/etc/localtime`
1657 * will be consulted to discover the correct time zone on UNIX and the
1658 * registry will be consulted or GetTimeZoneInformation() will be used
1659 * to get the local time zone on Windows.
1661 * If intervals are not available, only time zone rules from `TZ`
1662 * environment variable or other means, then they will be computed
1663 * from year 1900 to 2037. If the maximum year for the rules is
1664 * available and it is greater than 2037, then it will followed
1668 * [RFC3339 §5.6](http://tools.ietf.org/html/rfc3339#section-5.6)
1669 * for a precise definition of valid RFC3339 time offsets
1670 * (the `time-offset` expansion) and ISO 8601 for the
1671 * full list of valid time offsets. See
1672 * [The GNU C Library manual](http://www.gnu.org/s/libc/manual/html_node/TZ-Variable.html)
1673 * for an explanation of the possible
1674 * values of the `TZ` environment variable. See
1675 * [Microsoft Time Zone Index Values](http://msdn.microsoft.com/en-us/library/ms912391%28v=winembedded.11%29.aspx)
1676 * for the list of time zones on Windows.
1678 * You should release the return value by calling g_time_zone_unref()
1679 * when you are done with it.
1681 * Returns: the requested timezone
1686 g_time_zone_new (const gchar *identifier)
1688 GTimeZone *tz = NULL;
1689 TimeZoneRule *rules;
1691 gchar *resolved_identifier = NULL;
1693 G_LOCK (time_zones);
1694 if (time_zones == NULL)
1695 time_zones = g_hash_table_new (g_str_hash, g_str_equal);
1699 tz = g_hash_table_lookup (time_zones, identifier);
1702 g_atomic_int_inc (&tz->ref_count);
1703 G_UNLOCK (time_zones);
1708 tz = g_slice_new0 (GTimeZone);
1711 zone_for_constant_offset (tz, identifier);
1713 if (tz->t_info == NULL &&
1714 (rules_num = rules_from_identifier (identifier, &resolved_identifier, &rules)))
1716 init_zone_from_rules (tz, rules, rules_num, g_steal_pointer (&resolved_identifier));
1720 if (tz->t_info == NULL)
1723 GBytes *zoneinfo = zone_info_unix (identifier, &resolved_identifier);
1724 if (zoneinfo != NULL)
1726 init_zone_from_iana_info (tz, zoneinfo, g_steal_pointer (&resolved_identifier));
1727 g_bytes_unref (zoneinfo);
1729 #elif defined (G_OS_WIN32)
1730 if ((rules_num = rules_from_windows_time_zone (identifier,
1731 &resolved_identifier,
1735 init_zone_from_rules (tz, rules, rules_num, g_steal_pointer (&resolved_identifier));
1741 #if defined (G_OS_WIN32)
1742 if (tz->t_info == NULL)
1744 if (identifier == NULL)
1746 TIME_ZONE_INFORMATION tzi;
1748 if (GetTimeZoneInformation (&tzi) != TIME_ZONE_ID_INVALID)
1750 rules = g_new0 (TimeZoneRule, 2);
1752 if (rule_from_windows_time_zone_info (&rules[0], &tzi))
1754 memset (rules[0].std_name, 0, NAME_SIZE);
1755 memset (rules[0].dlt_name, 0, NAME_SIZE);
1757 rules[0].start_year = MIN_TZYEAR;
1758 rules[1].start_year = MAX_TZYEAR;
1760 init_zone_from_rules (tz, rules, 2, windows_default_tzname ());
1769 g_free (resolved_identifier);
1771 /* Always fall back to UTC. */
1772 if (tz->t_info == NULL)
1773 zone_for_constant_offset (tz, "UTC");
1775 g_assert (tz->name != NULL);
1776 g_assert (tz->t_info != NULL);
1778 if (tz->t_info != NULL)
1781 g_hash_table_insert (time_zones, tz->name, tz);
1783 g_atomic_int_inc (&tz->ref_count);
1784 G_UNLOCK (time_zones);
1790 * g_time_zone_new_utc:
1792 * Creates a #GTimeZone corresponding to UTC.
1794 * This is equivalent to calling g_time_zone_new() with a value like
1795 * "Z", "UTC", "+00", etc.
1797 * You should release the return value by calling g_time_zone_unref()
1798 * when you are done with it.
1800 * Returns: the universal timezone
1805 g_time_zone_new_utc (void)
1807 static GTimeZone *utc = NULL;
1808 static gsize initialised;
1810 if (g_once_init_enter (&initialised))
1812 utc = g_time_zone_new ("UTC");
1813 g_once_init_leave (&initialised, TRUE);
1816 return g_time_zone_ref (utc);
1820 * g_time_zone_new_local:
1822 * Creates a #GTimeZone corresponding to local time. The local time
1823 * zone may change between invocations to this function; for example,
1824 * if the system administrator changes it.
1826 * This is equivalent to calling g_time_zone_new() with the value of
1827 * the `TZ` environment variable (including the possibility of %NULL).
1829 * You should release the return value by calling g_time_zone_unref()
1830 * when you are done with it.
1832 * Returns: the local timezone
1837 g_time_zone_new_local (void)
1839 const gchar *tzenv = g_getenv ("TZ");
1844 /* Is time zone changed and must be flushed? */
1845 if (tz_local && g_strcmp0 (g_time_zone_get_identifier (tz_local), tzenv))
1846 g_clear_pointer (&tz_local, g_time_zone_unref);
1848 if (tz_local == NULL)
1849 tz_local = g_time_zone_new (tzenv);
1851 tz = g_time_zone_ref (tz_local);
1853 G_UNLOCK (tz_local);
1859 * g_time_zone_new_offset:
1860 * @seconds: offset to UTC, in seconds
1862 * Creates a #GTimeZone corresponding to the given constant offset from UTC,
1865 * This is equivalent to calling g_time_zone_new() with a string in the form
1866 * `[+|-]hh[:mm[:ss]]`.
1868 * Returns: (transfer full): a timezone at the given offset from UTC
1872 g_time_zone_new_offset (gint32 seconds)
1874 GTimeZone *tz = NULL;
1875 gchar *identifier = NULL;
1877 /* Seemingly, we should be using @seconds directly to set the
1878 * #TransitionInfo.gmt_offset to avoid all this string building and parsing.
1879 * However, we always need to set the #GTimeZone.name to a constructed
1880 * string anyway, so we might as well reuse its code. */
1881 identifier = g_strdup_printf ("%c%02u:%02u:%02u",
1882 (seconds >= 0) ? '+' : '-',
1883 (ABS (seconds) / 60) / 60,
1884 (ABS (seconds) / 60) % 60,
1885 ABS (seconds) % 60);
1886 tz = g_time_zone_new (identifier);
1887 g_free (identifier);
1889 g_assert (g_time_zone_get_offset (tz, 0) == seconds);
1894 #define TRANSITION(n) g_array_index (tz->transitions, Transition, n)
1895 #define TRANSITION_INFO(n) g_array_index (tz->t_info, TransitionInfo, n)
1897 /* Internal helpers {{{1 */
1898 /* NB: Interval 0 is before the first transition, so there's no
1899 * transition structure to point to which TransitionInfo to
1900 * use. Rule-based zones are set up so that TI 0 is always standard
1901 * time (which is what's in effect before Daylight time got started
1902 * in the early 20th century), but IANA tzfiles don't follow that
1903 * convention. The tzfile documentation says to use the first
1904 * standard-time (i.e., non-DST) tinfo, so that's what we do.
1906 inline static const TransitionInfo*
1907 interval_info (GTimeZone *tz,
1911 g_return_val_if_fail (tz->t_info != NULL, NULL);
1912 if (interval && tz->transitions && interval <= tz->transitions->len)
1913 index = (TRANSITION(interval - 1)).info_index;
1916 for (index = 0; index < tz->t_info->len; index++)
1918 TransitionInfo *tzinfo = &(TRANSITION_INFO(index));
1919 if (!tzinfo->is_dst)
1925 return &(TRANSITION_INFO(index));
1928 inline static gint64
1929 interval_start (GTimeZone *tz,
1932 if (!interval || tz->transitions == NULL || tz->transitions->len == 0)
1934 if (interval > tz->transitions->len)
1935 interval = tz->transitions->len;
1936 return (TRANSITION(interval - 1)).time;
1939 inline static gint64
1940 interval_end (GTimeZone *tz,
1943 if (tz->transitions && interval < tz->transitions->len)
1945 gint64 lim = (TRANSITION(interval)).time;
1946 return lim - (lim != G_MININT64);
1951 inline static gint32
1952 interval_offset (GTimeZone *tz,
1955 g_return_val_if_fail (tz->t_info != NULL, 0);
1956 return interval_info (tz, interval)->gmt_offset;
1959 inline static gboolean
1960 interval_isdst (GTimeZone *tz,
1963 g_return_val_if_fail (tz->t_info != NULL, 0);
1964 return interval_info (tz, interval)->is_dst;
1968 inline static gchar*
1969 interval_abbrev (GTimeZone *tz,
1972 g_return_val_if_fail (tz->t_info != NULL, 0);
1973 return interval_info (tz, interval)->abbrev;
1976 inline static gint64
1977 interval_local_start (GTimeZone *tz,
1981 return interval_start (tz, interval) + interval_offset (tz, interval);
1986 inline static gint64
1987 interval_local_end (GTimeZone *tz,
1990 if (tz->transitions && interval < tz->transitions->len)
1991 return interval_end (tz, interval) + interval_offset (tz, interval);
1997 interval_valid (GTimeZone *tz,
2000 if ( tz->transitions == NULL)
2001 return interval == 0;
2002 return interval <= tz->transitions->len;
2005 /* g_time_zone_find_interval() {{{1 */
2008 * g_time_zone_adjust_time:
2010 * @type: the #GTimeType of @time_
2011 * @time_: a pointer to a number of seconds since January 1, 1970
2013 * Finds an interval within @tz that corresponds to the given @time_,
2014 * possibly adjusting @time_ if required to fit into an interval.
2015 * The meaning of @time_ depends on @type.
2017 * This function is similar to g_time_zone_find_interval(), with the
2018 * difference that it always succeeds (by making the adjustments
2021 * In any of the cases where g_time_zone_find_interval() succeeds then
2022 * this function returns the same value, without modifying @time_.
2024 * This function may, however, modify @time_ in order to deal with
2025 * non-existent times. If the non-existent local @time_ of 02:30 were
2026 * requested on March 14th 2010 in Toronto then this function would
2027 * adjust @time_ to be 03:00 and return the interval containing the
2030 * Returns: the interval containing @time_, never -1
2035 g_time_zone_adjust_time (GTimeZone *tz,
2040 gboolean interval_is_dst;
2042 if (tz->transitions == NULL)
2045 intervals = tz->transitions->len;
2047 /* find the interval containing *time UTC
2048 * TODO: this could be binary searched (or better) */
2049 for (i = 0; i <= intervals; i++)
2050 if (*time_ <= interval_end (tz, i))
2053 g_assert (interval_start (tz, i) <= *time_ && *time_ <= interval_end (tz, i));
2055 if (type != G_TIME_TYPE_UNIVERSAL)
2057 if (*time_ < interval_local_start (tz, i))
2058 /* if time came before the start of this interval... */
2062 /* if it's not in the previous interval... */
2063 if (*time_ > interval_local_end (tz, i))
2065 /* it doesn't exist. fast-forward it. */
2067 *time_ = interval_local_start (tz, i);
2071 else if (*time_ > interval_local_end (tz, i))
2072 /* if time came after the end of this interval... */
2076 /* if it's not in the next interval... */
2077 if (*time_ < interval_local_start (tz, i))
2078 /* it doesn't exist. fast-forward it. */
2079 *time_ = interval_local_start (tz, i);
2084 interval_is_dst = interval_isdst (tz, i);
2085 if ((interval_is_dst && type != G_TIME_TYPE_DAYLIGHT) ||
2086 (!interval_is_dst && type == G_TIME_TYPE_DAYLIGHT))
2088 /* it's in this interval, but dst flag doesn't match.
2089 * check neighbours for a better fit. */
2090 if (i && *time_ <= interval_local_end (tz, i - 1))
2093 else if (i < intervals &&
2094 *time_ >= interval_local_start (tz, i + 1))
2104 * g_time_zone_find_interval:
2106 * @type: the #GTimeType of @time_
2107 * @time_: a number of seconds since January 1, 1970
2109 * Finds an interval within @tz that corresponds to the given @time_.
2110 * The meaning of @time_ depends on @type.
2112 * If @type is %G_TIME_TYPE_UNIVERSAL then this function will always
2113 * succeed (since universal time is monotonic and continuous).
2115 * Otherwise @time_ is treated as local time. The distinction between
2116 * %G_TIME_TYPE_STANDARD and %G_TIME_TYPE_DAYLIGHT is ignored except in
2117 * the case that the given @time_ is ambiguous. In Toronto, for example,
2118 * 01:30 on November 7th 2010 occurred twice (once inside of daylight
2119 * savings time and the next, an hour later, outside of daylight savings
2120 * time). In this case, the different value of @type would result in a
2121 * different interval being returned.
2123 * It is still possible for this function to fail. In Toronto, for
2124 * example, 02:00 on March 14th 2010 does not exist (due to the leap
2125 * forward to begin daylight savings time). -1 is returned in that
2128 * Returns: the interval containing @time_, or -1 in case of failure
2133 g_time_zone_find_interval (GTimeZone *tz,
2138 gboolean interval_is_dst;
2140 if (tz->transitions == NULL)
2142 intervals = tz->transitions->len;
2143 for (i = 0; i <= intervals; i++)
2144 if (time_ <= interval_end (tz, i))
2147 if (type == G_TIME_TYPE_UNIVERSAL)
2150 if (time_ < interval_local_start (tz, i))
2152 if (time_ > interval_local_end (tz, --i))
2156 else if (time_ > interval_local_end (tz, i))
2158 if (time_ < interval_local_start (tz, ++i))
2164 interval_is_dst = interval_isdst (tz, i);
2165 if ((interval_is_dst && type != G_TIME_TYPE_DAYLIGHT) ||
2166 (!interval_is_dst && type == G_TIME_TYPE_DAYLIGHT))
2168 if (i && time_ <= interval_local_end (tz, i - 1))
2171 else if (i < intervals && time_ >= interval_local_start (tz, i + 1))
2179 /* Public API accessors {{{1 */
2182 * g_time_zone_get_abbreviation:
2184 * @interval: an interval within the timezone
2186 * Determines the time zone abbreviation to be used during a particular
2187 * @interval of time in the time zone @tz.
2189 * For example, in Toronto this is currently "EST" during the winter
2190 * months and "EDT" during the summer months when daylight savings time
2193 * Returns: the time zone abbreviation, which belongs to @tz
2198 g_time_zone_get_abbreviation (GTimeZone *tz,
2201 g_return_val_if_fail (interval_valid (tz, (guint)interval), NULL);
2203 return interval_abbrev (tz, (guint)interval);
2207 * g_time_zone_get_offset:
2209 * @interval: an interval within the timezone
2211 * Determines the offset to UTC in effect during a particular @interval
2212 * of time in the time zone @tz.
2214 * The offset is the number of seconds that you add to UTC time to
2215 * arrive at local time for @tz (ie: negative numbers for time zones
2216 * west of GMT, positive numbers for east).
2218 * Returns: the number of seconds that should be added to UTC to get the
2224 g_time_zone_get_offset (GTimeZone *tz,
2227 g_return_val_if_fail (interval_valid (tz, (guint)interval), 0);
2229 return interval_offset (tz, (guint)interval);
2233 * g_time_zone_is_dst:
2235 * @interval: an interval within the timezone
2237 * Determines if daylight savings time is in effect during a particular
2238 * @interval of time in the time zone @tz.
2240 * Returns: %TRUE if daylight savings time is in effect
2245 g_time_zone_is_dst (GTimeZone *tz,
2248 g_return_val_if_fail (interval_valid (tz, interval), FALSE);
2250 if (tz->transitions == NULL)
2253 return interval_isdst (tz, (guint)interval);
2257 * g_time_zone_get_identifier:
2260 * Get the identifier of this #GTimeZone, as passed to g_time_zone_new().
2261 * If the identifier passed at construction time was not recognised, `UTC` will
2262 * be returned. If it was %NULL, the identifier of the local timezone at
2263 * construction time will be returned.
2265 * The identifier will be returned in the same format as provided at
2266 * construction time: if provided as a time offset, that will be returned by
2269 * Returns: identifier for this timezone
2273 g_time_zone_get_identifier (GTimeZone *tz)
2275 g_return_val_if_fail (tz != NULL, NULL);
2281 /* vim:set foldmethod=marker: */