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"
49 * @short_description: a structure representing a time zone
50 * @see_also: #GDateTime
52 * #GTimeZone is a structure that represents a time zone, at no
53 * particular point in time. It is refcounted and immutable.
55 * Each time zone has an identifier (for example, ‘Europe/London’) which is
56 * platform dependent. See g_time_zone_new() for information on the identifier
57 * formats. The identifier of a time zone can be retrieved using
58 * g_time_zone_get_identifier().
60 * A time zone contains a number of intervals. Each interval has
61 * an abbreviation to describe it (for example, ‘PDT’), an offet to UTC and a
62 * flag indicating if the daylight savings time is in effect during that
63 * interval. A time zone always has at least one interval — interval 0. Note
64 * that interval abbreviations are not the same as time zone identifiers
65 * (apart from ‘UTC’), and cannot be passed to g_time_zone_new().
67 * Every UTC time is contained within exactly one interval, but a given
68 * local time may be contained within zero, one or two intervals (due to
69 * incontinuities associated with daylight savings time).
71 * An interval may refer to a specific period of time (eg: the duration
72 * of daylight savings time during 2010) or it may refer to many periods
73 * of time that share the same properties (eg: all periods of daylight
74 * savings time). It is also possible (usually for political reasons)
75 * that some properties (like the abbreviation) change between intervals
76 * without other properties changing.
78 * #GTimeZone is available since GLib 2.26.
84 * #GTimeZone is an opaque structure whose members cannot be accessed
90 /* IANA zoneinfo file format {{{1 */
93 typedef struct { gchar bytes[8]; } gint64_be;
94 typedef struct { gchar bytes[4]; } gint32_be;
95 typedef struct { gchar bytes[4]; } guint32_be;
97 static inline gint64 gint64_from_be (const gint64_be be) {
98 gint64 tmp; memcpy (&tmp, &be, sizeof tmp); return GINT64_FROM_BE (tmp);
101 static inline gint32 gint32_from_be (const gint32_be be) {
102 gint32 tmp; memcpy (&tmp, &be, sizeof tmp); return GINT32_FROM_BE (tmp);
105 static inline guint32 guint32_from_be (const guint32_be be) {
106 guint32 tmp; memcpy (&tmp, &be, sizeof tmp); return GUINT32_FROM_BE (tmp);
109 /* The layout of an IANA timezone file header */
114 guchar tzh_reserved[15];
116 guint32_be tzh_ttisgmtcnt;
117 guint32_be tzh_ttisstdcnt;
118 guint32_be tzh_leapcnt;
119 guint32_be tzh_timecnt;
120 guint32_be tzh_typecnt;
121 guint32_be tzh_charcnt;
131 /* A Transition Date structure for TZ Rules, an intermediate structure
132 for parsing MSWindows and Environment-variable time zones. It
133 Generalizes MSWindows's SYSTEMTIME struct.
147 /* POSIX Timezone abbreviations are typically 3 or 4 characters, but
148 Microsoft uses 32-character names. We'll use one larger to ensure
149 we have room for the terminating \0.
153 /* A MSWindows-style time zone transition rule. Generalizes the
154 MSWindows TIME_ZONE_INFORMATION struct. Also used to compose time
155 zones from tzset-style identifiers.
162 TimeZoneDate dlt_start;
163 TimeZoneDate dlt_end;
164 gchar std_name[NAME_SIZE];
165 gchar dlt_name[NAME_SIZE];
168 /* GTimeZone's internal representation of a Daylight Savings (Summer)
178 /* GTimeZone's representation of a transition time to or from Daylight
179 Savings (Summer) time and Standard time for the zone. */
186 /* GTimeZone structure */
190 GArray *t_info; /* Array of TransitionInfo */
191 GArray *transitions; /* Array of Transition */
195 G_LOCK_DEFINE_STATIC (time_zones);
196 static GHashTable/*<string?, GTimeZone>*/ *time_zones;
198 #define MIN_TZYEAR 1916 /* Daylight Savings started in WWI */
199 #define MAX_TZYEAR 2999 /* And it's not likely ever to go away, but
200 there's no point in getting carried
207 * Decreases the reference count on @tz.
212 g_time_zone_unref (GTimeZone *tz)
217 ref_count = g_atomic_int_get (&tz->ref_count);
219 g_assert (ref_count > 0);
223 if (tz->name != NULL)
227 /* someone else might have grabbed a ref in the meantime */
228 if G_UNLIKELY (g_atomic_int_get (&tz->ref_count) != 1)
230 G_UNLOCK(time_zones);
234 g_hash_table_remove (time_zones, tz->name);
235 G_UNLOCK(time_zones);
238 if (tz->t_info != NULL)
241 for (idx = 0; idx < tz->t_info->len; idx++)
243 TransitionInfo *info = &g_array_index (tz->t_info, TransitionInfo, idx);
244 g_free (info->abbrev);
246 g_array_free (tz->t_info, TRUE);
248 if (tz->transitions != NULL)
249 g_array_free (tz->transitions, TRUE);
252 g_slice_free (GTimeZone, tz);
255 else if G_UNLIKELY (!g_atomic_int_compare_and_exchange (&tz->ref_count,
265 * Increases the reference count on @tz.
267 * Returns: a new reference to @tz.
272 g_time_zone_ref (GTimeZone *tz)
274 g_assert (tz->ref_count > 0);
276 g_atomic_int_inc (&tz->ref_count);
281 /* fake zoneinfo creation (for RFC3339/ISO 8601 timezones) {{{1 */
283 * parses strings of the form h or hh[[:]mm[[[:]ss]]] where:
289 parse_time (const gchar *time_,
292 if (*time_ < '0' || '9' < *time_)
295 *offset = 60 * 60 * (*time_++ - '0');
302 if (*time_ < '0' || '9' < *time_)
306 *offset += 60 * 60 * (*time_++ - '0');
308 if (*offset > 23 * 60 * 60)
318 if (*time_ < '0' || '5' < *time_)
321 *offset += 10 * 60 * (*time_++ - '0');
323 if (*time_ < '0' || '9' < *time_)
326 *offset += 60 * (*time_++ - '0');
334 if (*time_ < '0' || '5' < *time_)
337 *offset += 10 * (*time_++ - '0');
339 if (*time_ < '0' || '9' < *time_)
342 *offset += *time_++ - '0';
344 return *time_ == '\0';
348 parse_constant_offset (const gchar *name,
351 if (g_strcmp0 (name, "UTC") == 0)
357 if (*name >= '0' && '9' >= *name)
358 return parse_time (name, offset);
367 return parse_time (name, offset);
370 if (parse_time (name, offset))
384 zone_for_constant_offset (GTimeZone *gtz, const gchar *name)
389 if (name == NULL || !parse_constant_offset (name, &offset))
392 info.gmt_offset = offset;
394 info.abbrev = g_strdup (name);
396 gtz->name = g_strdup (name);
397 gtz->t_info = g_array_sized_new (FALSE, TRUE, sizeof (TransitionInfo), 1);
398 g_array_append_val (gtz->t_info, info);
400 /* Constant offset, no transitions */
401 gtz->transitions = NULL;
406 zone_info_unix (const gchar *identifier,
407 gchar **out_identifier)
410 GMappedFile *file = NULL;
411 GBytes *zoneinfo = NULL;
412 gchar *resolved_identifier = NULL;
415 tzdir = getenv ("TZDIR");
417 tzdir = "/usr/share/zoneinfo";
419 /* identifier can be a relative or absolute path name;
420 if relative, it is interpreted starting from /usr/share/zoneinfo
421 while the POSIX standard says it should start with :,
422 glibc allows both syntaxes, so we should too */
423 if (identifier != NULL)
425 resolved_identifier = g_strdup (identifier);
427 if (*identifier == ':')
430 if (g_path_is_absolute (identifier))
431 filename = g_strdup (identifier);
433 filename = g_build_filename (tzdir, identifier, NULL);
437 gsize prefix_len = 0;
438 gchar *canonical_path = NULL;
439 GError *read_link_err = NULL;
441 filename = g_strdup ("/etc/localtime");
443 /* Resolve the actual timezone pointed to by /etc/localtime. */
444 resolved_identifier = g_file_read_link (filename, &read_link_err);
445 if (resolved_identifier == NULL)
447 gboolean not_a_symlink = g_error_matches (read_link_err,
450 g_clear_error (&read_link_err);
452 /* Fallback to the content of /var/db/zoneinfo or /etc/timezone
453 * if /etc/localtime is not a symlink. /var/db/zoneinfo is
454 * where 'tzsetup' program on FreeBSD and DragonflyBSD stores
455 * the timezone chosen by the user. /etc/timezone is where user
456 * choice is expressed on Gentoo OpenRC and others. */
457 if (not_a_symlink && (g_file_get_contents ("/var/db/zoneinfo",
458 &resolved_identifier,
460 g_file_get_contents ("/etc/timezone",
461 &resolved_identifier,
463 g_strchomp (resolved_identifier);
467 g_assert (resolved_identifier == NULL);
473 /* Resolve relative path */
474 canonical_path = g_canonicalize_filename (resolved_identifier, "/etc");
475 g_free (resolved_identifier);
476 resolved_identifier = g_steal_pointer (&canonical_path);
479 /* Strip the prefix and slashes if possible. */
480 if (g_str_has_prefix (resolved_identifier, tzdir))
482 prefix_len = strlen (tzdir);
483 while (*(resolved_identifier + prefix_len) == '/')
488 memmove (resolved_identifier, resolved_identifier + prefix_len,
489 strlen (resolved_identifier) - prefix_len + 1 /* nul terminator */);
491 g_free (canonical_path);
494 file = g_mapped_file_new (filename, FALSE, NULL);
497 zoneinfo = g_bytes_new_with_free_func (g_mapped_file_get_contents (file),
498 g_mapped_file_get_length (file),
499 (GDestroyNotify)g_mapped_file_unref,
500 g_mapped_file_ref (file));
501 g_mapped_file_unref (file);
504 g_assert (resolved_identifier != NULL);
507 if (out_identifier != NULL)
508 *out_identifier = g_steal_pointer (&resolved_identifier);
510 g_free (resolved_identifier);
517 init_zone_from_iana_info (GTimeZone *gtz,
519 gchar *identifier /* (transfer full) */)
523 guint32 time_count, type_count;
524 guint8 *tz_transitions, *tz_type_index, *tz_ttinfo;
526 gsize timesize = sizeof (gint32);
527 const struct tzhead *header = g_bytes_get_data (zoneinfo, &size);
529 g_return_if_fail (size >= sizeof (struct tzhead) &&
530 memcmp (header, "TZif", 4) == 0);
532 if (header->tzh_version == '2')
534 /* Skip ahead to the newer 64-bit data if it's available. */
535 header = (const struct tzhead *)
536 (((const gchar *) (header + 1)) +
537 guint32_from_be(header->tzh_ttisgmtcnt) +
538 guint32_from_be(header->tzh_ttisstdcnt) +
539 8 * guint32_from_be(header->tzh_leapcnt) +
540 5 * guint32_from_be(header->tzh_timecnt) +
541 6 * guint32_from_be(header->tzh_typecnt) +
542 guint32_from_be(header->tzh_charcnt));
543 timesize = sizeof (gint64);
545 time_count = guint32_from_be(header->tzh_timecnt);
546 type_count = guint32_from_be(header->tzh_typecnt);
548 tz_transitions = ((guint8 *) (header) + sizeof (*header));
549 tz_type_index = tz_transitions + timesize * time_count;
550 tz_ttinfo = tz_type_index + time_count;
551 tz_abbrs = tz_ttinfo + sizeof (struct ttinfo) * type_count;
553 gtz->name = g_steal_pointer (&identifier);
554 gtz->t_info = g_array_sized_new (FALSE, TRUE, sizeof (TransitionInfo),
556 gtz->transitions = g_array_sized_new (FALSE, TRUE, sizeof (Transition),
559 for (index = 0; index < type_count; index++)
561 TransitionInfo t_info;
562 struct ttinfo info = ((struct ttinfo*)tz_ttinfo)[index];
563 t_info.gmt_offset = gint32_from_be (info.tt_gmtoff);
564 t_info.is_dst = info.tt_isdst ? TRUE : FALSE;
565 t_info.abbrev = g_strdup ((gchar *) &tz_abbrs[info.tt_abbrind]);
566 g_array_append_val (gtz->t_info, t_info);
569 for (index = 0; index < time_count; index++)
572 if (header->tzh_version == '2')
573 trans.time = gint64_from_be (((gint64_be*)tz_transitions)[index]);
575 trans.time = gint32_from_be (((gint32_be*)tz_transitions)[index]);
576 trans.info_index = tz_type_index[index];
577 g_assert (trans.info_index >= 0);
578 g_assert ((guint) trans.info_index < gtz->t_info->len);
579 g_array_append_val (gtz->transitions, trans);
583 #elif defined (G_OS_WIN32)
586 copy_windows_systemtime (SYSTEMTIME *s_time, TimeZoneDate *tzdate)
588 tzdate->sec = s_time->wSecond;
589 tzdate->min = s_time->wMinute;
590 tzdate->hour = s_time->wHour;
591 tzdate->mon = s_time->wMonth;
592 tzdate->year = s_time->wYear;
593 tzdate->wday = s_time->wDayOfWeek ? s_time->wDayOfWeek : 7;
597 tzdate->mday = s_time->wDay;
601 tzdate->week = s_time->wDay;
604 /* UTC = local time + bias while local time = UTC + offset */
606 rule_from_windows_time_zone_info (TimeZoneRule *rule,
607 TIME_ZONE_INFORMATION *tzi)
610 if (tzi->StandardDate.wMonth)
612 rule->std_offset = -(tzi->Bias + tzi->StandardBias) * 60;
613 rule->dlt_offset = -(tzi->Bias + tzi->DaylightBias) * 60;
614 copy_windows_systemtime (&(tzi->DaylightDate), &(rule->dlt_start));
616 copy_windows_systemtime (&(tzi->StandardDate), &(rule->dlt_end));
622 rule->std_offset = -tzi->Bias * 60;
623 rule->dlt_start.mon = 0;
625 strncpy (rule->std_name, (gchar*)tzi->StandardName, NAME_SIZE - 1);
626 strncpy (rule->dlt_name, (gchar*)tzi->DaylightName, NAME_SIZE - 1);
630 windows_default_tzname (void)
632 const gchar *subkey =
633 "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation";
635 gchar *key_name = NULL;
636 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, subkey, 0,
637 KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
640 if (RegQueryValueExA (key, "TimeZoneKeyName", NULL, NULL,
641 NULL, &size) == ERROR_SUCCESS)
643 key_name = g_malloc ((gint)size);
644 if (RegQueryValueExA (key, "TimeZoneKeyName", NULL, NULL,
645 (LPBYTE)key_name, &size) != ERROR_SUCCESS)
661 SYSTEMTIME StandardDate;
662 SYSTEMTIME DaylightDate;
666 system_time_copy (SYSTEMTIME *orig, SYSTEMTIME *target)
668 g_return_if_fail (orig != NULL);
669 g_return_if_fail (target != NULL);
671 target->wYear = orig->wYear;
672 target->wMonth = orig->wMonth;
673 target->wDayOfWeek = orig->wDayOfWeek;
674 target->wDay = orig->wDay;
675 target->wHour = orig->wHour;
676 target->wMinute = orig->wMinute;
677 target->wSecond = orig->wSecond;
678 target->wMilliseconds = orig->wMilliseconds;
682 register_tzi_to_tzi (RegTZI *reg, TIME_ZONE_INFORMATION *tzi)
684 g_return_if_fail (reg != NULL);
685 g_return_if_fail (tzi != NULL);
686 tzi->Bias = reg->Bias;
687 system_time_copy (&(reg->StandardDate), &(tzi->StandardDate));
688 tzi->StandardBias = reg->StandardBias;
689 system_time_copy (&(reg->DaylightDate), &(tzi->DaylightDate));
690 tzi->DaylightBias = reg->DaylightBias;
694 rules_from_windows_time_zone (const gchar *identifier,
695 gchar **out_identifier,
696 TimeZoneRule **rules)
699 gchar *subkey, *subkey_dynamic;
700 gchar *key_name = NULL;
701 const gchar *reg_key =
702 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\";
703 TIME_ZONE_INFORMATION tzi;
706 RegTZI regtzi, regtzi_prev;
708 g_assert (out_identifier != NULL);
709 g_assert (rules != NULL);
711 *out_identifier = NULL;
716 key_name = windows_default_tzname ();
718 key_name = g_strdup (identifier);
723 subkey = g_strconcat (reg_key, key_name, NULL);
724 subkey_dynamic = g_strconcat (subkey, "\\Dynamic DST", NULL);
726 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, subkey, 0,
727 KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
729 size = sizeof tzi.StandardName;
730 if (RegQueryValueExA (key, "Std", NULL, NULL,
731 (LPBYTE)&(tzi.StandardName), &size) != ERROR_SUCCESS)
734 size = sizeof tzi.DaylightName;
736 if (RegQueryValueExA (key, "Dlt", NULL, NULL,
737 (LPBYTE)&(tzi.DaylightName), &size) != ERROR_SUCCESS)
741 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, subkey_dynamic, 0,
742 KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
749 if (RegQueryValueExA (key, "FirstEntry", NULL, NULL,
750 (LPBYTE) &first, &size) != ERROR_SUCCESS)
754 if (RegQueryValueExA (key, "LastEntry", NULL, NULL,
755 (LPBYTE) &last, &size) != ERROR_SUCCESS)
758 rules_num = last - first + 2;
759 *rules = g_new0 (TimeZoneRule, rules_num);
761 for (year = first, i = 0; year <= last; year++)
763 s = g_strdup_printf ("%d", year);
765 size = sizeof regtzi;
766 if (RegQueryValueExA (key, s, NULL, NULL,
767 (LPBYTE) ®tzi, &size) != ERROR_SUCCESS)
776 if (year > first && memcmp (®tzi_prev, ®tzi, sizeof regtzi) == 0)
779 memcpy (®tzi_prev, ®tzi, sizeof regtzi);
781 register_tzi_to_tzi (®tzi, &tzi);
782 rule_from_windows_time_zone_info (&(*rules)[i], &tzi);
783 (*rules)[i++].start_year = year;
791 else if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, subkey, 0,
792 KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
794 size = sizeof regtzi;
795 if (RegQueryValueExA (key, "TZI", NULL, NULL,
796 (LPBYTE) ®tzi, &size) == ERROR_SUCCESS)
799 *rules = g_new0 (TimeZoneRule, 2);
800 register_tzi_to_tzi (®tzi, &tzi);
801 rule_from_windows_time_zone_info (&(*rules)[0], &tzi);
807 g_free (subkey_dynamic);
812 (*rules)[0].start_year = MIN_TZYEAR;
813 if ((*rules)[rules_num - 2].start_year < MAX_TZYEAR)
814 (*rules)[rules_num - 1].start_year = MAX_TZYEAR;
816 (*rules)[rules_num - 1].start_year = (*rules)[rules_num - 2].start_year + 1;
818 *out_identifier = g_steal_pointer (&key_name);
831 find_relative_date (TimeZoneDate *buffer)
835 g_date_clear (&date, 1);
838 /* Get last day if last is needed, first day otherwise */
839 if (buffer->mon == 13 || buffer->mon == 14) /* Julian Date */
841 g_date_set_dmy (&date, 1, 1, buffer->year);
842 if (wday >= 59 && buffer->mon == 13 && g_date_is_leap_year (buffer->year))
843 g_date_add_days (&date, wday);
845 g_date_add_days (&date, wday - 1);
846 buffer->mon = (int) g_date_get_month (&date);
847 buffer->mday = (int) g_date_get_day (&date);
853 guint days_in_month = g_date_days_in_month (buffer->mon, buffer->year);
854 GDateWeekday first_wday;
856 g_date_set_dmy (&date, 1, buffer->mon, buffer->year);
857 first_wday = g_date_get_weekday (&date);
859 if (first_wday > wday)
861 /* week is 1 <= w <= 5, we need 0-based */
862 days = 7 * (buffer->week - 1) + wday - first_wday;
864 while (days > days_in_month)
867 g_date_add_days (&date, days);
869 buffer->mday = g_date_get_day (&date);
873 /* Offset is previous offset of local time. Returns 0 if month is 0 */
875 boundary_for_year (TimeZoneDate *boundary,
881 const guint64 unix_epoch_start = 719163L;
882 const guint64 seconds_per_day = 86400L;
888 if (boundary->year == 0)
893 find_relative_date (&buffer);
896 g_assert (buffer.year == year);
897 g_date_clear (&date, 1);
898 g_date_set_dmy (&date, buffer.mday, buffer.mon, buffer.year);
899 return ((g_date_get_julian (&date) - unix_epoch_start) * seconds_per_day +
900 buffer.hour * 3600 + buffer.min * 60 + buffer.sec - offset);
904 fill_transition_info_from_rule (TransitionInfo *info,
908 gint offset = is_dst ? rule->dlt_offset : rule->std_offset;
909 gchar *name = is_dst ? rule->dlt_name : rule->std_name;
911 info->gmt_offset = offset;
912 info->is_dst = is_dst;
915 info->abbrev = g_strdup (name);
918 info->abbrev = g_strdup_printf ("%+03d%02d",
920 (int) abs (offset / 60) % 60);
924 init_zone_from_rules (GTimeZone *gtz,
927 gchar *identifier /* (transfer full) */)
929 guint type_count = 0, trans_count = 0, info_index = 0;
930 guint ri; /* rule index */
931 gboolean skip_first_std_trans = TRUE;
937 /* Last rule only contains max year */
938 for (ri = 0; ri < rules_num - 1; ri++)
940 if (rules[ri].dlt_start.mon || rules[ri].dlt_end.mon)
942 guint rulespan = (rules[ri + 1].start_year - rules[ri].start_year);
943 guint transitions = rules[ri].dlt_start.mon > 0 ? 1 : 0;
944 transitions += rules[ri].dlt_end.mon > 0 ? 1 : 0;
945 type_count += rules[ri].dlt_start.mon > 0 ? 2 : 1;
946 trans_count += transitions * rulespan;
952 gtz->name = g_steal_pointer (&identifier);
953 gtz->t_info = g_array_sized_new (FALSE, TRUE, sizeof (TransitionInfo), type_count);
954 gtz->transitions = g_array_sized_new (FALSE, TRUE, sizeof (Transition), trans_count);
956 last_offset = rules[0].std_offset;
958 for (ri = 0; ri < rules_num - 1; ri++)
960 if ((rules[ri].std_offset || rules[ri].dlt_offset) &&
961 rules[ri].dlt_start.mon == 0 && rules[ri].dlt_end.mon == 0)
963 TransitionInfo std_info;
965 fill_transition_info_from_rule (&std_info, &(rules[ri]), FALSE);
966 g_array_append_val (gtz->t_info, std_info);
969 ((rules[ri - 1].dlt_start.mon > 12 &&
970 rules[ri - 1].dlt_start.wday > rules[ri - 1].dlt_end.wday) ||
971 rules[ri - 1].dlt_start.mon > rules[ri - 1].dlt_end.mon))
973 /* The previous rule was a southern hemisphere rule that
974 starts the year with DST, so we need to add a
975 transition to return to standard time */
976 guint year = rules[ri].start_year;
977 gint64 std_time = boundary_for_year (&rules[ri].dlt_end,
979 Transition std_trans = {std_time, info_index};
980 g_array_append_val (gtz->transitions, std_trans);
983 last_offset = rules[ri].std_offset;
985 skip_first_std_trans = TRUE;
989 const guint start_year = rules[ri].start_year;
990 const guint end_year = rules[ri + 1].start_year;
993 TransitionInfo std_info, dlt_info;
994 if (rules[ri].dlt_start.mon > 12)
995 dlt_first = rules[ri].dlt_start.wday > rules[ri].dlt_end.wday;
997 dlt_first = rules[ri].dlt_start.mon > rules[ri].dlt_end.mon;
998 /* Standard rules are always even, because before the first
999 transition is always standard time, and 0 is even. */
1000 fill_transition_info_from_rule (&std_info, &(rules[ri]), FALSE);
1001 fill_transition_info_from_rule (&dlt_info, &(rules[ri]), TRUE);
1003 g_array_append_val (gtz->t_info, std_info);
1004 g_array_append_val (gtz->t_info, dlt_info);
1006 /* Transition dates. We hope that a year which ends daylight
1007 time in a southern-hemisphere country (i.e., one that
1008 begins the year in daylight time) will include a rule
1009 which has only a dlt_end. */
1010 for (year = start_year; year < end_year; year++)
1012 gint32 dlt_offset = (dlt_first ? last_offset :
1013 rules[ri].dlt_offset);
1014 gint32 std_offset = (dlt_first ? rules[ri].std_offset :
1016 /* NB: boundary_for_year returns 0 if mon == 0 */
1017 gint64 std_time = boundary_for_year (&rules[ri].dlt_end,
1019 gint64 dlt_time = boundary_for_year (&rules[ri].dlt_start,
1021 Transition std_trans = {std_time, info_index};
1022 Transition dlt_trans = {dlt_time, info_index + 1};
1023 last_offset = (dlt_first ? rules[ri].dlt_offset :
1024 rules[ri].std_offset);
1027 if (skip_first_std_trans)
1028 skip_first_std_trans = FALSE;
1030 g_array_append_val (gtz->transitions, std_trans);
1032 g_array_append_val (gtz->transitions, dlt_trans);
1037 g_array_append_val (gtz->transitions, dlt_trans);
1039 g_array_append_val (gtz->transitions, std_trans);
1047 ((rules[ri - 1].dlt_start.mon > 12 &&
1048 rules[ri - 1].dlt_start.wday > rules[ri - 1].dlt_end.wday) ||
1049 rules[ri - 1].dlt_start.mon > rules[ri - 1].dlt_end.mon))
1051 /* The previous rule was a southern hemisphere rule that
1052 starts the year with DST, so we need to add a
1053 transition to return to standard time */
1054 TransitionInfo info;
1055 guint year = rules[ri].start_year;
1057 fill_transition_info_from_rule (&info, &(rules[ri - 1]), FALSE);
1058 g_array_append_val (gtz->t_info, info);
1059 trans.time = boundary_for_year (&rules[ri - 1].dlt_end,
1061 trans.info_index = info_index;
1062 g_array_append_val (gtz->transitions, trans);
1067 * parses date[/time] for parsing TZ environment variable
1069 * date is either Mm.w.d, Jn or N
1076 * time is either h or hh[[:]mm[[[:]ss]]]
1082 parse_mwd_boundary (gchar **pos, TimeZoneDate *boundary)
1084 gint month, week, day;
1086 if (**pos == '\0' || **pos < '0' || '9' < **pos)
1089 month = *(*pos)++ - '0';
1091 if ((month == 1 && **pos >= '0' && '2' >= **pos) ||
1092 (month == 0 && **pos >= '0' && '9' >= **pos))
1095 month += *(*pos)++ - '0';
1098 if (*(*pos)++ != '.' || month == 0)
1101 if (**pos == '\0' || **pos < '1' || '5' < **pos)
1104 week = *(*pos)++ - '0';
1106 if (*(*pos)++ != '.')
1109 if (**pos == '\0' || **pos < '0' || '6' < **pos)
1112 day = *(*pos)++ - '0';
1118 boundary->mon = month;
1119 boundary->week = week;
1120 boundary->wday = day;
1124 /* Different implementations of tzset interpret the Julian day field
1125 differently. For example, Linux specifies that it should be 1-based
1126 (1 Jan is JD 1) for both Jn and n formats, while zOS and BSD
1127 specify that a Jn JD is 1-based while an n JD is 0-based. Rather
1128 than trying to follow different specs, we will follow GDate's
1129 practice thatIn order to keep it simple, we will follow Linux's
1133 parse_julian_boundary (gchar** pos, TimeZoneDate *boundary,
1134 gboolean ignore_leap)
1139 while (**pos >= '0' && '9' >= **pos)
1142 day += *(*pos)++ - '0';
1145 if (day < 1 || 365 < day)
1148 g_date_clear (&date, 1);
1149 g_date_set_julian (&date, day);
1151 boundary->mon = (int) g_date_get_month (&date);
1152 boundary->mday = (int) g_date_get_day (&date);
1155 if (!ignore_leap && day >= 59)
1162 parse_tz_boundary (const gchar *identifier,
1163 TimeZoneDate *boundary)
1167 pos = (gchar*)identifier;
1168 /* Month-week-weekday */
1172 if (!parse_mwd_boundary (&pos, boundary))
1175 /* Julian date which ignores Feb 29 in leap years */
1176 else if (*pos == 'J')
1179 if (!parse_julian_boundary (&pos, boundary, FALSE))
1182 /* Julian date which counts Feb 29 in leap years */
1183 else if (*pos >= '0' && '9' >= *pos)
1185 if (!parse_julian_boundary (&pos, boundary, TRUE))
1197 if (!parse_time (++pos, &offset))
1200 boundary->hour = offset / 3600;
1201 boundary->min = (offset / 60) % 60;
1202 boundary->sec = offset % 3600;
1213 return *pos == '\0';
1218 create_ruleset_from_rule (TimeZoneRule **rules, TimeZoneRule *rule)
1220 *rules = g_new0 (TimeZoneRule, 2);
1222 (*rules)[0].start_year = MIN_TZYEAR;
1223 (*rules)[1].start_year = MAX_TZYEAR;
1225 (*rules)[0].std_offset = -rule->std_offset;
1226 (*rules)[0].dlt_offset = -rule->dlt_offset;
1227 (*rules)[0].dlt_start = rule->dlt_start;
1228 (*rules)[0].dlt_end = rule->dlt_end;
1229 strcpy ((*rules)[0].std_name, rule->std_name);
1230 strcpy ((*rules)[0].dlt_name, rule->dlt_name);
1235 parse_offset (gchar **pos, gint32 *target)
1238 gchar *target_pos = *pos;
1241 while (**pos == '+' || **pos == '-' || **pos == ':' ||
1242 (**pos >= '0' && '9' >= **pos))
1245 buffer = g_strndup (target_pos, *pos - target_pos);
1246 ret = parse_constant_offset (buffer, target);
1253 parse_identifier_boundary (gchar **pos, TimeZoneDate *target)
1256 gchar *target_pos = *pos;
1259 while (**pos != ',' && **pos != '\0')
1261 buffer = g_strndup (target_pos, *pos - target_pos);
1262 ret = parse_tz_boundary (buffer, target);
1269 set_tz_name (gchar **pos, gchar *buffer, guint size)
1271 gchar *name_pos = *pos;
1274 /* Name is ASCII alpha (Is this necessarily true?) */
1275 while (g_ascii_isalpha (**pos))
1278 /* Name should be three or more alphabetic characters */
1279 if (*pos - name_pos < 3)
1282 memset (buffer, 0, NAME_SIZE);
1283 /* name_pos isn't 0-terminated, so we have to limit the length expressly */
1284 len = *pos - name_pos > size - 1 ? size - 1 : *pos - name_pos;
1285 strncpy (buffer, name_pos, len);
1290 parse_identifier_boundaries (gchar **pos, TimeZoneRule *tzr)
1292 if (*(*pos)++ != ',')
1296 if (!parse_identifier_boundary (pos, &(tzr->dlt_start)) || *(*pos)++ != ',')
1300 if (!parse_identifier_boundary (pos, &(tzr->dlt_end)))
1306 * Creates an array of TimeZoneRule from a TZ environment variable
1307 * type of identifier. Should free rules afterwards
1310 rules_from_identifier (const gchar *identifier,
1311 gchar **out_identifier,
1312 TimeZoneRule **rules)
1317 g_assert (out_identifier != NULL);
1318 g_assert (rules != NULL);
1320 *out_identifier = NULL;
1326 pos = (gchar*)identifier;
1327 memset (&tzr, 0, sizeof (tzr));
1328 /* Standard offset */
1329 if (!(set_tz_name (&pos, tzr.std_name, NAME_SIZE)) ||
1330 !parse_offset (&pos, &(tzr.std_offset)))
1335 *out_identifier = g_strdup (identifier);
1336 return create_ruleset_from_rule (rules, &tzr);
1340 if (!(set_tz_name (&pos, tzr.dlt_name, NAME_SIZE)))
1342 parse_offset (&pos, &(tzr.dlt_offset));
1343 if (tzr.dlt_offset == 0) /* No daylight offset given, assume it's 1
1344 hour earlier that standard */
1345 tzr.dlt_offset = tzr.std_offset - 3600;
1348 /* Windows allows us to use the US DST boundaries if they're not given */
1351 guint rules_num = 0;
1353 /* Use US rules, Windows' default is Pacific Standard Time */
1354 if ((rules_num = rules_from_windows_time_zone ("Pacific Standard Time",
1358 for (i = 0; i < rules_num - 1; i++)
1360 (*rules)[i].std_offset = - tzr.std_offset;
1361 (*rules)[i].dlt_offset = - tzr.dlt_offset;
1362 strcpy ((*rules)[i].std_name, tzr.std_name);
1363 strcpy ((*rules)[i].dlt_name, tzr.dlt_name);
1374 /* Start and end required (format 2) */
1375 if (!parse_identifier_boundaries (&pos, &tzr))
1378 *out_identifier = g_strdup (identifier);
1379 return create_ruleset_from_rule (rules, &tzr);
1382 /* Construction {{{1 */
1385 * @identifier: (nullable): a timezone identifier
1387 * Creates a #GTimeZone corresponding to @identifier.
1389 * @identifier can either be an RFC3339/ISO 8601 time offset or
1390 * something that would pass as a valid value for the `TZ` environment
1391 * variable (including %NULL).
1393 * In Windows, @identifier can also be the unlocalized name of a time
1394 * zone for standard time, for example "Pacific Standard Time".
1396 * Valid RFC3339 time offsets are `"Z"` (for UTC) or
1397 * `"±hh:mm"`. ISO 8601 additionally specifies
1398 * `"±hhmm"` and `"±hh"`. Offsets are
1399 * time values to be added to Coordinated Universal Time (UTC) to get
1402 * In UNIX, the `TZ` environment variable typically corresponds
1403 * to the name of a file in the zoneinfo database, or string in
1404 * "std offset [dst [offset],start[/time],end[/time]]" (POSIX) format.
1405 * There are no spaces in the specification. The name of standard
1406 * and daylight savings time zone must be three or more alphabetic
1407 * characters. Offsets are time values to be added to local time to
1408 * get Coordinated Universal Time (UTC) and should be
1409 * `"[±]hh[[:]mm[:ss]]"`. Dates are either
1410 * `"Jn"` (Julian day with n between 1 and 365, leap
1411 * years not counted), `"n"` (zero-based Julian day
1412 * with n between 0 and 365) or `"Mm.w.d"` (day d
1413 * (0 <= d <= 6) of week w (1 <= w <= 5) of month m (1 <= m <= 12), day
1414 * 0 is a Sunday). Times are in local wall clock time, the default is
1417 * In Windows, the "tzn[+|–]hh[:mm[:ss]][dzn]" format is used, but also
1418 * accepts POSIX format. The Windows format uses US rules for all time
1419 * zones; daylight savings time is 60 minutes behind the standard time
1420 * with date and time of change taken from Pacific Standard Time.
1421 * Offsets are time values to be added to the local time to get
1422 * Coordinated Universal Time (UTC).
1424 * g_time_zone_new_local() calls this function with the value of the
1425 * `TZ` environment variable. This function itself is independent of
1426 * the value of `TZ`, but if @identifier is %NULL then `/etc/localtime`
1427 * will be consulted to discover the correct time zone on UNIX and the
1428 * registry will be consulted or GetTimeZoneInformation() will be used
1429 * to get the local time zone on Windows.
1431 * If intervals are not available, only time zone rules from `TZ`
1432 * environment variable or other means, then they will be computed
1433 * from year 1900 to 2037. If the maximum year for the rules is
1434 * available and it is greater than 2037, then it will followed
1438 * [RFC3339 §5.6](http://tools.ietf.org/html/rfc3339#section-5.6)
1439 * for a precise definition of valid RFC3339 time offsets
1440 * (the `time-offset` expansion) and ISO 8601 for the
1441 * full list of valid time offsets. See
1442 * [The GNU C Library manual](http://www.gnu.org/s/libc/manual/html_node/TZ-Variable.html)
1443 * for an explanation of the possible
1444 * values of the `TZ` environment variable. See
1445 * [Microsoft Time Zone Index Values](http://msdn.microsoft.com/en-us/library/ms912391%28v=winembedded.11%29.aspx)
1446 * for the list of time zones on Windows.
1448 * You should release the return value by calling g_time_zone_unref()
1449 * when you are done with it.
1451 * Returns: the requested timezone
1456 g_time_zone_new (const gchar *identifier)
1458 GTimeZone *tz = NULL;
1459 TimeZoneRule *rules;
1461 gchar *resolved_identifier = NULL;
1463 G_LOCK (time_zones);
1464 if (time_zones == NULL)
1465 time_zones = g_hash_table_new (g_str_hash, g_str_equal);
1469 tz = g_hash_table_lookup (time_zones, identifier);
1472 g_atomic_int_inc (&tz->ref_count);
1473 G_UNLOCK (time_zones);
1478 tz = g_slice_new0 (GTimeZone);
1481 zone_for_constant_offset (tz, identifier);
1483 if (tz->t_info == NULL &&
1484 (rules_num = rules_from_identifier (identifier, &resolved_identifier, &rules)))
1486 init_zone_from_rules (tz, rules, rules_num, g_steal_pointer (&resolved_identifier));
1490 if (tz->t_info == NULL)
1493 GBytes *zoneinfo = zone_info_unix (identifier, &resolved_identifier);
1494 if (zoneinfo != NULL)
1496 init_zone_from_iana_info (tz, zoneinfo, g_steal_pointer (&resolved_identifier));
1497 g_bytes_unref (zoneinfo);
1499 #elif defined (G_OS_WIN32)
1500 if ((rules_num = rules_from_windows_time_zone (identifier,
1501 &resolved_identifier,
1504 init_zone_from_rules (tz, rules, rules_num, g_steal_pointer (&resolved_identifier));
1510 #if defined (G_OS_WIN32)
1511 if (tz->t_info == NULL)
1513 if (identifier == NULL)
1515 TIME_ZONE_INFORMATION tzi;
1517 if (GetTimeZoneInformation (&tzi) != TIME_ZONE_ID_INVALID)
1519 rules = g_new0 (TimeZoneRule, 2);
1521 rule_from_windows_time_zone_info (&rules[0], &tzi);
1523 memset (rules[0].std_name, 0, NAME_SIZE);
1524 memset (rules[0].dlt_name, 0, NAME_SIZE);
1526 rules[0].start_year = MIN_TZYEAR;
1527 rules[1].start_year = MAX_TZYEAR;
1529 init_zone_from_rules (tz, rules, 2, windows_default_tzname ());
1537 g_free (resolved_identifier);
1539 /* Always fall back to UTC. */
1540 if (tz->t_info == NULL)
1541 zone_for_constant_offset (tz, "UTC");
1543 g_assert (tz->name != NULL);
1544 g_assert (tz->t_info != NULL);
1546 if (tz->t_info != NULL)
1549 g_hash_table_insert (time_zones, tz->name, tz);
1551 g_atomic_int_inc (&tz->ref_count);
1552 G_UNLOCK (time_zones);
1558 * g_time_zone_new_utc:
1560 * Creates a #GTimeZone corresponding to UTC.
1562 * This is equivalent to calling g_time_zone_new() with a value like
1563 * "Z", "UTC", "+00", etc.
1565 * You should release the return value by calling g_time_zone_unref()
1566 * when you are done with it.
1568 * Returns: the universal timezone
1573 g_time_zone_new_utc (void)
1575 return g_time_zone_new ("UTC");
1579 * g_time_zone_new_local:
1581 * Creates a #GTimeZone corresponding to local time. The local time
1582 * zone may change between invocations to this function; for example,
1583 * if the system administrator changes it.
1585 * This is equivalent to calling g_time_zone_new() with the value of
1586 * the `TZ` environment variable (including the possibility of %NULL).
1588 * You should release the return value by calling g_time_zone_unref()
1589 * when you are done with it.
1591 * Returns: the local timezone
1596 g_time_zone_new_local (void)
1598 return g_time_zone_new (getenv ("TZ"));
1602 * g_time_zone_new_offset:
1603 * @seconds: offset to UTC, in seconds
1605 * Creates a #GTimeZone corresponding to the given constant offset from UTC,
1608 * This is equivalent to calling g_time_zone_new() with a string in the form
1609 * `[+|-]hh[:mm[:ss]]`.
1611 * Returns: (transfer full): a timezone at the given offset from UTC
1615 g_time_zone_new_offset (gint32 seconds)
1617 GTimeZone *tz = NULL;
1618 gchar *identifier = NULL;
1620 /* Seemingly, we should be using @seconds directly to set the
1621 * #TransitionInfo.gmt_offset to avoid all this string building and parsing.
1622 * However, we always need to set the #GTimeZone.name to a constructed
1623 * string anyway, so we might as well reuse its code. */
1624 identifier = g_strdup_printf ("%c%02u:%02u:%02u",
1625 (seconds >= 0) ? '+' : '-',
1626 (ABS (seconds) / 60) / 60,
1627 (ABS (seconds) / 60) % 60,
1628 ABS (seconds) % 60);
1629 tz = g_time_zone_new (identifier);
1630 g_free (identifier);
1632 g_assert (g_time_zone_get_offset (tz, 0) == seconds);
1637 #define TRANSITION(n) g_array_index (tz->transitions, Transition, n)
1638 #define TRANSITION_INFO(n) g_array_index (tz->t_info, TransitionInfo, n)
1640 /* Internal helpers {{{1 */
1641 /* NB: Interval 0 is before the first transition, so there's no
1642 * transition structure to point to which TransitionInfo to
1643 * use. Rule-based zones are set up so that TI 0 is always standard
1644 * time (which is what's in effect before Daylight time got started
1645 * in the early 20th century), but IANA tzfiles don't follow that
1646 * convention. The tzfile documentation says to use the first
1647 * standard-time (i.e., non-DST) tinfo, so that's what we do.
1649 inline static const TransitionInfo*
1650 interval_info (GTimeZone *tz,
1654 g_return_val_if_fail (tz->t_info != NULL, NULL);
1655 if (interval && tz->transitions && interval <= tz->transitions->len)
1656 index = (TRANSITION(interval - 1)).info_index;
1659 for (index = 0; index < tz->t_info->len; index++)
1661 TransitionInfo *tzinfo = &(TRANSITION_INFO(index));
1662 if (!tzinfo->is_dst)
1668 return &(TRANSITION_INFO(index));
1671 inline static gint64
1672 interval_start (GTimeZone *tz,
1675 if (!interval || tz->transitions == NULL || tz->transitions->len == 0)
1677 if (interval > tz->transitions->len)
1678 interval = tz->transitions->len;
1679 return (TRANSITION(interval - 1)).time;
1682 inline static gint64
1683 interval_end (GTimeZone *tz,
1686 if (tz->transitions && interval < tz->transitions->len)
1688 gint64 lim = (TRANSITION(interval)).time;
1689 return lim - (lim != G_MININT64);
1694 inline static gint32
1695 interval_offset (GTimeZone *tz,
1698 g_return_val_if_fail (tz->t_info != NULL, 0);
1699 return interval_info (tz, interval)->gmt_offset;
1702 inline static gboolean
1703 interval_isdst (GTimeZone *tz,
1706 g_return_val_if_fail (tz->t_info != NULL, 0);
1707 return interval_info (tz, interval)->is_dst;
1711 inline static gchar*
1712 interval_abbrev (GTimeZone *tz,
1715 g_return_val_if_fail (tz->t_info != NULL, 0);
1716 return interval_info (tz, interval)->abbrev;
1719 inline static gint64
1720 interval_local_start (GTimeZone *tz,
1724 return interval_start (tz, interval) + interval_offset (tz, interval);
1729 inline static gint64
1730 interval_local_end (GTimeZone *tz,
1733 if (tz->transitions && interval < tz->transitions->len)
1734 return interval_end (tz, interval) + interval_offset (tz, interval);
1740 interval_valid (GTimeZone *tz,
1743 if ( tz->transitions == NULL)
1744 return interval == 0;
1745 return interval <= tz->transitions->len;
1748 /* g_time_zone_find_interval() {{{1 */
1751 * g_time_zone_adjust_time:
1753 * @type: the #GTimeType of @time_
1754 * @time_: a pointer to a number of seconds since January 1, 1970
1756 * Finds an interval within @tz that corresponds to the given @time_,
1757 * possibly adjusting @time_ if required to fit into an interval.
1758 * The meaning of @time_ depends on @type.
1760 * This function is similar to g_time_zone_find_interval(), with the
1761 * difference that it always succeeds (by making the adjustments
1764 * In any of the cases where g_time_zone_find_interval() succeeds then
1765 * this function returns the same value, without modifying @time_.
1767 * This function may, however, modify @time_ in order to deal with
1768 * non-existent times. If the non-existent local @time_ of 02:30 were
1769 * requested on March 14th 2010 in Toronto then this function would
1770 * adjust @time_ to be 03:00 and return the interval containing the
1773 * Returns: the interval containing @time_, never -1
1778 g_time_zone_adjust_time (GTimeZone *tz,
1783 gboolean interval_is_dst;
1785 if (tz->transitions == NULL)
1788 intervals = tz->transitions->len;
1790 /* find the interval containing *time UTC
1791 * TODO: this could be binary searched (or better) */
1792 for (i = 0; i <= intervals; i++)
1793 if (*time_ <= interval_end (tz, i))
1796 g_assert (interval_start (tz, i) <= *time_ && *time_ <= interval_end (tz, i));
1798 if (type != G_TIME_TYPE_UNIVERSAL)
1800 if (*time_ < interval_local_start (tz, i))
1801 /* if time came before the start of this interval... */
1805 /* if it's not in the previous interval... */
1806 if (*time_ > interval_local_end (tz, i))
1808 /* it doesn't exist. fast-forward it. */
1810 *time_ = interval_local_start (tz, i);
1814 else if (*time_ > interval_local_end (tz, i))
1815 /* if time came after the end of this interval... */
1819 /* if it's not in the next interval... */
1820 if (*time_ < interval_local_start (tz, i))
1821 /* it doesn't exist. fast-forward it. */
1822 *time_ = interval_local_start (tz, i);
1827 interval_is_dst = interval_isdst (tz, i);
1828 if ((interval_is_dst && type != G_TIME_TYPE_DAYLIGHT) ||
1829 (!interval_is_dst && type == G_TIME_TYPE_DAYLIGHT))
1831 /* it's in this interval, but dst flag doesn't match.
1832 * check neighbours for a better fit. */
1833 if (i && *time_ <= interval_local_end (tz, i - 1))
1836 else if (i < intervals &&
1837 *time_ >= interval_local_start (tz, i + 1))
1847 * g_time_zone_find_interval:
1849 * @type: the #GTimeType of @time_
1850 * @time_: a number of seconds since January 1, 1970
1852 * Finds an the interval within @tz that corresponds to the given @time_.
1853 * The meaning of @time_ depends on @type.
1855 * If @type is %G_TIME_TYPE_UNIVERSAL then this function will always
1856 * succeed (since universal time is monotonic and continuous).
1858 * Otherwise @time_ is treated as local time. The distinction between
1859 * %G_TIME_TYPE_STANDARD and %G_TIME_TYPE_DAYLIGHT is ignored except in
1860 * the case that the given @time_ is ambiguous. In Toronto, for example,
1861 * 01:30 on November 7th 2010 occurred twice (once inside of daylight
1862 * savings time and the next, an hour later, outside of daylight savings
1863 * time). In this case, the different value of @type would result in a
1864 * different interval being returned.
1866 * It is still possible for this function to fail. In Toronto, for
1867 * example, 02:00 on March 14th 2010 does not exist (due to the leap
1868 * forward to begin daylight savings time). -1 is returned in that
1871 * Returns: the interval containing @time_, or -1 in case of failure
1876 g_time_zone_find_interval (GTimeZone *tz,
1881 gboolean interval_is_dst;
1883 if (tz->transitions == NULL)
1885 intervals = tz->transitions->len;
1886 for (i = 0; i <= intervals; i++)
1887 if (time_ <= interval_end (tz, i))
1890 if (type == G_TIME_TYPE_UNIVERSAL)
1893 if (time_ < interval_local_start (tz, i))
1895 if (time_ > interval_local_end (tz, --i))
1899 else if (time_ > interval_local_end (tz, i))
1901 if (time_ < interval_local_start (tz, ++i))
1907 interval_is_dst = interval_isdst (tz, i);
1908 if ((interval_is_dst && type != G_TIME_TYPE_DAYLIGHT) ||
1909 (!interval_is_dst && type == G_TIME_TYPE_DAYLIGHT))
1911 if (i && time_ <= interval_local_end (tz, i - 1))
1914 else if (i < intervals && time_ >= interval_local_start (tz, i + 1))
1922 /* Public API accessors {{{1 */
1925 * g_time_zone_get_abbreviation:
1927 * @interval: an interval within the timezone
1929 * Determines the time zone abbreviation to be used during a particular
1930 * @interval of time in the time zone @tz.
1932 * For example, in Toronto this is currently "EST" during the winter
1933 * months and "EDT" during the summer months when daylight savings time
1936 * Returns: the time zone abbreviation, which belongs to @tz
1941 g_time_zone_get_abbreviation (GTimeZone *tz,
1944 g_return_val_if_fail (interval_valid (tz, (guint)interval), NULL);
1946 return interval_abbrev (tz, (guint)interval);
1950 * g_time_zone_get_offset:
1952 * @interval: an interval within the timezone
1954 * Determines the offset to UTC in effect during a particular @interval
1955 * of time in the time zone @tz.
1957 * The offset is the number of seconds that you add to UTC time to
1958 * arrive at local time for @tz (ie: negative numbers for time zones
1959 * west of GMT, positive numbers for east).
1961 * Returns: the number of seconds that should be added to UTC to get the
1967 g_time_zone_get_offset (GTimeZone *tz,
1970 g_return_val_if_fail (interval_valid (tz, (guint)interval), 0);
1972 return interval_offset (tz, (guint)interval);
1976 * g_time_zone_is_dst:
1978 * @interval: an interval within the timezone
1980 * Determines if daylight savings time is in effect during a particular
1981 * @interval of time in the time zone @tz.
1983 * Returns: %TRUE if daylight savings time is in effect
1988 g_time_zone_is_dst (GTimeZone *tz,
1991 g_return_val_if_fail (interval_valid (tz, interval), FALSE);
1993 if (tz->transitions == NULL)
1996 return interval_isdst (tz, (guint)interval);
2000 * g_time_zone_get_identifier:
2003 * Get the identifier of this #GTimeZone, as passed to g_time_zone_new().
2004 * If the identifier passed at construction time was not recognised, `UTC` will
2005 * be returned. If it was %NULL, the identifier of the local timezone at
2006 * construction time will be returned.
2008 * The identifier will be returned in the same format as provided at
2009 * construction time: if provided as a time offset, that will be returned by
2012 * Returns: identifier for this timezone
2016 g_time_zone_get_identifier (GTimeZone *tz)
2018 g_return_val_if_fail (tz != NULL, NULL);
2024 /* vim:set foldmethod=marker: */