+ return;
+
+ info.gmt_offset = offset;
+ info.is_dst = FALSE;
+ info.abbrev = g_strdup (name);
+
+
+ gtz->t_info = g_array_sized_new (FALSE, TRUE, sizeof (TransitionInfo), 1);
+ g_array_append_val (gtz->t_info, info);
+
+ /* Constant offset, no transitions */
+ gtz->transitions = NULL;
+}
+
+#ifdef G_OS_UNIX
+static GBytes*
+zone_info_unix (const gchar *identifier)
+{
+ gchar *filename;
+ GMappedFile *file = NULL;
+ GBytes *zoneinfo = NULL;
+
+ /* identifier can be a relative or absolute path name;
+ if relative, it is interpreted starting from /usr/share/zoneinfo
+ while the POSIX standard says it should start with :,
+ glibc allows both syntaxes, so we should too */
+ if (identifier != NULL)
+ {
+ const gchar *tzdir;
+
+ tzdir = getenv ("TZDIR");
+ if (tzdir == NULL)
+ tzdir = "/usr/share/zoneinfo";
+
+ if (*identifier == ':')
+ identifier ++;
+
+ if (g_path_is_absolute (identifier))
+ filename = g_strdup (identifier);
+ else
+ filename = g_build_filename (tzdir, identifier, NULL);
+ }
+ else
+ filename = g_strdup ("/etc/localtime");
+
+ file = g_mapped_file_new (filename, FALSE, NULL);
+ if (file != NULL)
+ {
+ zoneinfo = g_bytes_new_with_free_func (g_mapped_file_get_contents (file),
+ g_mapped_file_get_length (file),
+ (GDestroyNotify)g_mapped_file_unref,
+ g_mapped_file_ref (file));
+ g_mapped_file_unref (file);
+ }
+ g_free (filename);
+ return zoneinfo;
+}
+
+static void
+init_zone_from_iana_info (GTimeZone *gtz, GBytes *zoneinfo)
+{
+ gsize size;
+ guint index;
+ guint32 time_count, type_count;
+ guint8 *tz_transitions, *tz_type_index, *tz_ttinfo;
+ guint8 *tz_abbrs;
+ gsize timesize = sizeof (gint32);
+ const struct tzhead *header = g_bytes_get_data (zoneinfo, &size);
+
+ g_return_if_fail (size >= sizeof (struct tzhead) &&
+ memcmp (header, "TZif", 4) == 0);
+
+ if (header->tzh_version == '2')
+ {
+ /* Skip ahead to the newer 64-bit data if it's available. */
+ header = (const struct tzhead *)
+ (((const gchar *) (header + 1)) +
+ guint32_from_be(header->tzh_ttisgmtcnt) +
+ guint32_from_be(header->tzh_ttisstdcnt) +
+ 8 * guint32_from_be(header->tzh_leapcnt) +
+ 5 * guint32_from_be(header->tzh_timecnt) +
+ 6 * guint32_from_be(header->tzh_typecnt) +
+ guint32_from_be(header->tzh_charcnt));
+ timesize = sizeof (gint64);
+ }
+ time_count = guint32_from_be(header->tzh_timecnt);
+ type_count = guint32_from_be(header->tzh_typecnt);
+
+ tz_transitions = ((guint8 *) (header) + sizeof (*header));
+ tz_type_index = tz_transitions + timesize * time_count;
+ tz_ttinfo = tz_type_index + time_count;
+ tz_abbrs = tz_ttinfo + sizeof (struct ttinfo) * type_count;
+
+ gtz->t_info = g_array_sized_new (FALSE, TRUE, sizeof (TransitionInfo),
+ type_count);
+ gtz->transitions = g_array_sized_new (FALSE, TRUE, sizeof (Transition),
+ time_count);
+
+ for (index = 0; index < type_count; index++)
+ {
+ TransitionInfo t_info;
+ struct ttinfo info = ((struct ttinfo*)tz_ttinfo)[index];
+ t_info.gmt_offset = gint32_from_be (info.tt_gmtoff);
+ t_info.is_dst = info.tt_isdst ? TRUE : FALSE;
+ t_info.abbrev = g_strdup ((gchar *) &tz_abbrs[info.tt_abbrind]);
+ g_array_append_val (gtz->t_info, t_info);
+ }
+
+ for (index = 0; index < time_count; index++)
+ {
+ Transition trans;
+ if (header->tzh_version == '2')
+ trans.time = gint64_from_be (((gint64_be*)tz_transitions)[index]);
+ else
+ trans.time = gint32_from_be (((gint32_be*)tz_transitions)[index]);
+ trans.info_index = tz_type_index[index];
+ g_assert (trans.info_index >= 0);
+ g_assert (trans.info_index < gtz->t_info->len);
+ g_array_append_val (gtz->transitions, trans);
+ }
+}
+
+#elif defined (G_OS_WIN32)
+
+static void
+copy_windows_systemtime (SYSTEMTIME *s_time, TimeZoneDate *tzdate)
+{
+ tzdate->sec = s_time->wSecond;
+ tzdate->min = s_time->wMinute;
+ tzdate->hour = s_time->wHour;
+ tzdate->mon = s_time->wMonth;
+ tzdate->year = s_time->wYear;
+ tzdate->wday = s_time->wDayOfWeek ? s_time->wDayOfWeek : 7;
+
+ if (s_time->wYear)
+ {
+ tzdate->mday = s_time->wDay;
+ tzdate->wday = 0;
+ }
+ else
+ tzdate->week = s_time->wDay;
+}
+
+/* UTC = local time + bias while local time = UTC + offset */
+static void
+rule_from_windows_time_zone_info (TimeZoneRule *rule,
+ TIME_ZONE_INFORMATION *tzi)
+{
+ /* Set offset */
+ if (tzi->StandardDate.wMonth)
+ {
+ rule->std_offset = -(tzi->Bias + tzi->StandardBias) * 60;
+ rule->dlt_offset = -(tzi->Bias + tzi->DaylightBias) * 60;
+ copy_windows_systemtime (&(tzi->DaylightDate), &(rule->dlt_start));
+
+ copy_windows_systemtime (&(tzi->StandardDate), &(rule->dlt_end));
+
+ }
+
+ else
+ {
+ rule->std_offset = -tzi->Bias * 60;
+ rule->dlt_start.mon = 0;
+ }
+ strncpy (rule->std_name, (gchar*)tzi->StandardName, NAME_SIZE - 1);
+ strncpy (rule->dlt_name, (gchar*)tzi->DaylightName, NAME_SIZE - 1);
+}
+
+static gchar*
+windows_default_tzname (void)
+{
+ const gchar *subkey =
+ "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation";
+ HKEY key;
+ gchar *key_name = NULL;
+ if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, subkey, 0,
+ KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
+ {
+ DWORD size = 0;
+ if (RegQueryValueExA (key, "TimeZoneKeyName", NULL, NULL,
+ NULL, &size) == ERROR_SUCCESS)
+ {
+ key_name = g_malloc ((gint)size);
+ if (RegQueryValueExA (key, "TimeZoneKeyName", NULL, NULL,
+ (LPBYTE)key_name, &size) != ERROR_SUCCESS)
+ {
+ g_free (key_name);
+ key_name = NULL;
+ }
+ }
+ RegCloseKey (key);
+ }
+ return key_name;
+}
+
+typedef struct
+{
+ LONG Bias;
+ LONG StandardBias;
+ LONG DaylightBias;
+ SYSTEMTIME StandardDate;
+ SYSTEMTIME DaylightDate;
+} RegTZI;
+
+static void
+system_time_copy (SYSTEMTIME *orig, SYSTEMTIME *target)
+{
+ g_return_if_fail (orig != NULL);
+ g_return_if_fail (target != NULL);
+
+ target->wYear = orig->wYear;
+ target->wMonth = orig->wMonth;
+ target->wDayOfWeek = orig->wDayOfWeek;
+ target->wDay = orig->wDay;
+ target->wHour = orig->wHour;
+ target->wMinute = orig->wMinute;
+ target->wSecond = orig->wSecond;
+ target->wMilliseconds = orig->wMilliseconds;
+}
+
+static void
+register_tzi_to_tzi (RegTZI *reg, TIME_ZONE_INFORMATION *tzi)
+{
+ g_return_if_fail (reg != NULL);
+ g_return_if_fail (tzi != NULL);
+ tzi->Bias = reg->Bias;
+ system_time_copy (&(reg->StandardDate), &(tzi->StandardDate));
+ tzi->StandardBias = reg->StandardBias;
+ system_time_copy (&(reg->DaylightDate), &(tzi->DaylightDate));
+ tzi->DaylightBias = reg->DaylightBias;
+}
+
+static gint
+rules_from_windows_time_zone (const gchar *identifier, TimeZoneRule **rules)
+{
+ HKEY key;
+ gchar *subkey, *subkey_dynamic;
+ gchar *key_name = NULL;
+ const gchar *reg_key =
+ "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\";
+ TIME_ZONE_INFORMATION tzi;
+ DWORD size;
+ gint rules_num = 0;
+ RegTZI regtzi, regtzi_prev;
+
+ *rules = NULL;
+ key_name = NULL;
+
+ if (!identifier)
+ key_name = windows_default_tzname ();
+ else
+ key_name = g_strdup (identifier);
+
+ if (!key_name)
+ return 0;
+
+ subkey = g_strconcat (reg_key, key_name, NULL);
+ subkey_dynamic = g_strconcat (subkey, "\\Dynamic DST", NULL);
+
+ if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, subkey, 0,
+ KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
+ return 0;
+ size = sizeof tzi.StandardName;
+ if (RegQueryValueExA (key, "Std", NULL, NULL,
+ (LPBYTE)&(tzi.StandardName), &size) != ERROR_SUCCESS)
+ goto failed;
+
+ size = sizeof tzi.DaylightName;
+
+ if (RegQueryValueExA (key, "Dlt", NULL, NULL,
+ (LPBYTE)&(tzi.DaylightName), &size) != ERROR_SUCCESS)
+ goto failed;
+
+ RegCloseKey (key);
+ if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, subkey_dynamic, 0,
+ KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
+ {
+ DWORD first, last;
+ int year, i;
+ gchar *s;
+
+ size = sizeof first;
+ if (RegQueryValueExA (key, "FirstEntry", NULL, NULL,
+ (LPBYTE) &first, &size) != ERROR_SUCCESS)
+ goto failed;
+
+ size = sizeof last;
+ if (RegQueryValueExA (key, "LastEntry", NULL, NULL,
+ (LPBYTE) &last, &size) != ERROR_SUCCESS)
+ goto failed;
+
+ rules_num = last - first + 2;
+ *rules = g_new0 (TimeZoneRule, rules_num);
+
+ for (year = first, i = 0; year <= last; year++)
+ {
+ s = g_strdup_printf ("%d", year);
+
+ size = sizeof regtzi;
+ if (RegQueryValueExA (key, s, NULL, NULL,
+ (LPBYTE) ®tzi, &size) != ERROR_SUCCESS)
+ {
+ g_free (*rules);
+ *rules = NULL;
+ break;
+ }
+
+ g_free (s);
+
+ if (year > first && memcmp (®tzi_prev, ®tzi, sizeof regtzi) == 0)
+ continue;
+ else
+ memcpy (®tzi_prev, ®tzi, sizeof regtzi);
+
+ register_tzi_to_tzi (®tzi, &tzi);
+ rule_from_windows_time_zone_info (&(*rules)[i], &tzi);
+ (*rules)[i++].start_year = year;
+ }
+
+ rules_num = i + 1;
+
+failed:
+ RegCloseKey (key);
+ }
+ else if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, subkey, 0,
+ KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
+ {
+ size = sizeof regtzi;
+ if (RegQueryValueExA (key, "TZI", NULL, NULL,
+ (LPBYTE) ®tzi, &size) == ERROR_SUCCESS)
+ {
+ rules_num = 2;
+ *rules = g_new0 (TimeZoneRule, 2);
+ register_tzi_to_tzi (®tzi, &tzi);
+ rule_from_windows_time_zone_info (&(*rules)[0], &tzi);
+ }
+
+ RegCloseKey (key);
+ }
+
+ g_free (subkey_dynamic);
+ g_free (subkey);
+ g_free (key_name);
+
+ if (*rules)
+ {
+ (*rules)[0].start_year = MIN_TZYEAR;
+ if ((*rules)[rules_num - 2].start_year < MAX_TZYEAR)
+ (*rules)[rules_num - 1].start_year = MAX_TZYEAR;
+ else
+ (*rules)[rules_num - 1].start_year = (*rules)[rules_num - 2].start_year + 1;
+
+ return rules_num;
+ }
+ else
+ return 0;
+}
+
+#endif
+
+static void
+find_relative_date (TimeZoneDate *buffer)
+{
+ gint wday;
+ GDate date;
+ g_date_clear (&date, 1);
+ wday = buffer->wday;
+
+ /* Get last day if last is needed, first day otherwise */
+ if (buffer->mon == 13 || buffer->mon == 14) /* Julian Date */
+ {
+ g_date_set_dmy (&date, 1, 1, buffer->year);
+ if (wday >= 59 && buffer->mon == 13 && g_date_is_leap_year (buffer->year))
+ g_date_add_days (&date, wday);
+ else
+ g_date_add_days (&date, wday - 1);
+ buffer->mon = (int) g_date_get_month (&date);
+ buffer->mday = (int) g_date_get_day (&date);
+ buffer->wday = 0;
+ }
+ else /* M.W.D */
+ {
+ guint days;
+ guint days_in_month = g_date_days_in_month (buffer->mon, buffer->year);
+ GDateWeekday first_wday;
+
+ g_date_set_dmy (&date, 1, buffer->mon, buffer->year);
+ first_wday = g_date_get_weekday (&date);
+
+ if (first_wday > wday)
+ ++(buffer->week);
+ /* week is 1 <= w <= 5, we need 0-based */
+ days = 7 * (buffer->week - 1) + wday - first_wday;
+
+ while (days > days_in_month)
+ days -= 7;
+
+ g_date_add_days (&date, days);
+
+ buffer->mday = g_date_get_day (&date);
+ }
+}
+
+/* Offset is previous offset of local time. Returns 0 if month is 0 */
+static gint64
+boundary_for_year (TimeZoneDate *boundary,
+ gint year,
+ gint32 offset)
+{
+ TimeZoneDate buffer;
+ GDate date;
+ const guint64 unix_epoch_start = 719163L;
+ const guint64 seconds_per_day = 86400L;
+
+ if (!boundary->mon)
+ return 0;
+ buffer = *boundary;
+
+ if (boundary->year == 0)
+ {
+ buffer.year = year;
+
+ if (buffer.wday)
+ find_relative_date (&buffer);
+ }
+
+ g_assert (buffer.year == year);
+ g_date_clear (&date, 1);
+ g_date_set_dmy (&date, buffer.mday, buffer.mon, buffer.year);
+ return ((g_date_get_julian (&date) - unix_epoch_start) * seconds_per_day +
+ buffer.hour * 3600 + buffer.min * 60 + buffer.sec - offset);
+}
+
+static void
+fill_transition_info_from_rule (TransitionInfo *info,
+ TimeZoneRule *rule,
+ gboolean is_dst)
+{
+ gint offset = is_dst ? rule->dlt_offset : rule->std_offset;
+ gchar *name = is_dst ? rule->dlt_name : rule->std_name;
+
+ info->gmt_offset = offset;
+ info->is_dst = is_dst;
+
+ if (name)
+ info->abbrev = g_strdup (name);
+
+ else
+ info->abbrev = g_strdup_printf ("%+03d%02d",
+ (int) offset / 3600,
+ (int) abs (offset / 60) % 60);
+}
+
+static void
+init_zone_from_rules (GTimeZone *gtz,
+ TimeZoneRule *rules,
+ gint rules_num)
+{
+ guint type_count = 0, trans_count = 0, info_index = 0;
+ guint ri; /* rule index */
+ gboolean skip_first_std_trans = TRUE;
+ gint32 last_offset;
+
+ type_count = 0;
+ trans_count = 0;