* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Ryan Lortie <desrt@desrt.ca>
*/
/**
* GTimeZone:
*
- * #GDateTime is an opaque structure whose members cannot be accessed
+ * #GTimeZone is an opaque structure whose members cannot be accessed
* directly.
*
* Since: 2.26
**/
-/* zoneinfo file format {{{1 */
+/* IANA zoneinfo file format {{{1 */
/* unaligned */
typedef struct { gchar bytes[8]; } gint64_be;
guint32 tmp; memcpy (&tmp, &be, sizeof tmp); return GUINT32_FROM_BE (tmp);
}
+/* The layout of an IANA timezone file header */
struct tzhead
{
gchar tzh_magic[4];
guint8 tt_abbrind;
};
-typedef struct
-{
- gint32 gmt_offset;
- gboolean is_dst;
- gboolean is_standard;
- gboolean is_gmt;
- gchar *abbrev;
-} TransitionInfo;
-
-typedef struct
-{
- gint64 time;
- gint info_index;
-} Transition;
-
+/* A Transition Date structure for TZ Rules, an intermediate structure
+ for parsing MSWindows and Environment-variable time zones. It
+ Generalizes MSWindows's SYSTEMTIME struct.
+ */
typedef struct
{
gint year;
gint hour;
gint min;
gint sec;
- gboolean isstd;
- gboolean isgmt;
} TimeZoneDate;
+/* POSIX Timezone abbreviations are typically 3 or 4 characters, but
+ Microsoft uses 32-character names. We'll use one larger to ensure
+ we have room for the terminating \0.
+ */
+#define NAME_SIZE 33
+
+/* A MSWindows-style time zone transition rule. Generalizes the
+ MSWindows TIME_ZONE_INFORMATION struct. Also used to compose time
+ zones from tzset-style identifiers.
+ */
typedef struct
{
gint start_year;
gint32 dlt_offset;
TimeZoneDate dlt_start;
TimeZoneDate dlt_end;
- const gchar *std_name;
- const gchar *dlt_name;
+ gchar std_name[NAME_SIZE];
+ gchar dlt_name[NAME_SIZE];
} TimeZoneRule;
+/* GTimeZone's internal representation of a Daylight Savings (Summer)
+ time interval.
+ */
+typedef struct
+{
+ gint32 gmt_offset;
+ gboolean is_dst;
+ gchar *abbrev;
+} TransitionInfo;
+
+/* GTimeZone's representation of a transition time to or from Daylight
+ Savings (Summer) time and Standard time for the zone. */
+typedef struct
+{
+ gint64 time;
+ gint info_index;
+} Transition;
-/* GTimeZone structure and lifecycle {{{1 */
+/* GTimeZone structure */
struct _GTimeZone
{
gchar *name;
- GArray *t_info;
- GArray *transitions;
+ GArray *t_info; /* Array of TransitionInfo */
+ GArray *transitions; /* Array of Transition */
gint ref_count;
};
G_LOCK_DEFINE_STATIC (time_zones);
static GHashTable/*<string?, GTimeZone>*/ *time_zones;
-#define MIN_TZYEAR 1900
-#define MAX_TZYEAR 2038
+#define MIN_TZYEAR 1916 /* Daylight Savings started in WWI */
+#define MAX_TZYEAR 2999 /* And it's not likely ever to go away, but
+ there's no point in getting carried
+ away. */
/**
* g_time_zone_unref:
G_UNLOCK(time_zones);
}
- g_array_free (tz->t_info, TRUE);
+ if (tz->t_info != NULL)
+ {
+ gint idx;
+ for (idx = 0; idx < tz->t_info->len; idx++)
+ {
+ TransitionInfo *info = &g_array_index (tz->t_info, TransitionInfo, idx);
+ g_free (info->abbrev);
+ }
+ g_array_free (tz->t_info, TRUE);
+ }
if (tz->transitions != NULL)
g_array_free (tz->transitions, TRUE);
g_free (tz->name);
info.gmt_offset = offset;
info.is_dst = FALSE;
- info.is_standard = TRUE;
- info.is_gmt = TRUE;
info.abbrev = g_strdup (name);
{
gsize size;
guint index;
- guint32 time_count, type_count, leap_count, isgmt_count;
- guint32 isstd_count, char_count ;
- gpointer tz_transitions, tz_type_index, tz_ttinfo;
- gpointer tz_leaps, tz_isgmt, tz_isstd;
- gchar* tz_abbrs;
- guint timesize = sizeof (gint32), countsize = sizeof (gint32);
+ 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) &&
}
time_count = guint32_from_be(header->tzh_timecnt);
type_count = guint32_from_be(header->tzh_typecnt);
- leap_count = guint32_from_be(header->tzh_leapcnt);
- isgmt_count = guint32_from_be(header->tzh_ttisgmtcnt);
- isstd_count = guint32_from_be(header->tzh_ttisstdcnt);
- char_count = guint32_from_be(header->tzh_charcnt);
-
- g_assert (type_count == isgmt_count);
- g_assert (type_count == isstd_count);
- tz_transitions = (gpointer)(header + 1);
+ 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;
- tz_leaps = tz_abbrs + char_count;
- tz_isstd = tz_leaps + (timesize + countsize) * leap_count;
- tz_isgmt = tz_isstd + isstd_count;
gtz->t_info = g_array_sized_new (FALSE, TRUE, sizeof (TransitionInfo),
type_count);
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.is_standard = ((guint8*)tz_isstd)[index] ? TRUE : FALSE;
- t_info.is_gmt = ((guint8*)tz_isgmt)[index] ? TRUE : FALSE;
- t_info.abbrev = g_strdup (&tz_abbrs[info.tt_abbrind]);
+ t_info.abbrev = g_strdup ((gchar *) &tz_abbrs[info.tt_abbrind]);
g_array_append_val (gtz->t_info, t_info);
}
trans.time = gint64_from_be (((gint64_be*)tz_transitions)[index]);
else
trans.time = gint32_from_be (((gint32_be*)tz_transitions)[index]);
- trans.info_index = ((guint8*)tz_type_index)[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);
}
- g_bytes_unref (zoneinfo);
}
#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 *rules,
- LONG Bias,
- LONG StandardBias,
- LONG DaylightBias,
- SYSTEMTIME StandardDate,
- SYSTEMTIME DaylightDate)
+rule_from_windows_time_zone_info (TimeZoneRule *rule,
+ TIME_ZONE_INFORMATION *tzi)
{
/* Set offset */
- if (StandardDate.wMonth)
+ if (tzi->StandardDate.wMonth)
{
- rules->std_offset = -(Bias + StandardBias) * 60;
- rules->dlt_offset = -(Bias + DaylightBias) * 60;
+ rule->std_offset = -(tzi->Bias + tzi->StandardBias) * 60;
+ rule->dlt_offset = -(tzi->Bias + tzi->DaylightBias) * 60;
+ copy_windows_systemtime (&(tzi->DaylightDate), &(rule->dlt_start));
- rules->dlt_start.sec = DaylightDate.wSecond;
- rules->dlt_start.min = DaylightDate.wMinute;
- rules->dlt_start.hour = DaylightDate.wHour;
- rules->dlt_start.mon = DaylightDate.wMonth;
- rules->dlt_start.year = DaylightDate.wYear;
- rules->dlt_start.wday = DaylightDate.wDayOfWeek? DaylightDate.wDayOfWeek : 7;
+ copy_windows_systemtime (&(tzi->StandardDate), &(rule->dlt_end));
- if (DaylightDate.wYear)
- {
- rules->dlt_start.mday = DaylightDate.wDay;
- rules->dlt_start.wday = 0;
- }
- else
- rules->dlt_start.week = DaylightDate.wDay;
-
- rules->dlt_start.isstd = FALSE;
- rules->dlt_start.isgmt = FALSE;
+ }
- rules->dlt_end.sec = StandardDate.wSecond;
- rules->dlt_end.min = StandardDate.wMinute;
- rules->dlt_end.hour = StandardDate.wHour;
- rules->dlt_end.mday = StandardDate.wDay;
- rules->dlt_end.mon = StandardDate.wMonth;
- rules->dlt_end.year = StandardDate.wYear;
- rules->dlt_end.wday = StandardDate.wDayOfWeek? StandardDate.wDayOfWeek : 7;
+ 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);
+}
- if (StandardDate.wYear)
+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)
{
- rules->dlt_end.mday = StandardDate.wDay;
- rules->dlt_end.wday = 0;
+ 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;
+ }
}
- else
- rules->dlt_end.week = StandardDate.wDay;
-
- rules->dlt_end.isstd = FALSE;
- rules->dlt_end.isgmt = FALSE;
+ RegCloseKey (key);
}
+ return key_name;
+}
- else
- {
- rules->std_offset = -Bias * 60;
+typedef struct
+{
+ LONG Bias;
+ LONG StandardBias;
+ LONG DaylightBias;
+ SYSTEMTIME StandardDate;
+ SYSTEMTIME DaylightDate;
+} RegTZI;
- rules->dlt_start.mon = 0;
- }
+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 gboolean
-rules_from_windows_time_zone (const gchar *identifier,
- TimeZoneRule **rules,
- gint *rules_num,
- gchar **std_name,
- gchar **dlt_name)
+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;
-
- /* REG_TZI_FORMAT */
- struct {
- LONG Bias;
- LONG StandardBias;
- LONG DaylightBias;
- SYSTEMTIME StandardDate;
- SYSTEMTIME DaylightDate;
- } tzi, tzi_prev;
+ 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;
- *rules_num = 0;
- *std_name = NULL;
- *dlt_name = NULL;
-
key_name = NULL;
if (!identifier)
- {
- subkey = "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation";
-
- if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, subkey, 0,
- KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
- {
- size = 0;
- if (RegQueryValueExA (key, "TimeZoneKeyName", NULL, NULL,
- NULL, &size) == ERROR_SUCCESS)
- {
- key_name = g_malloc (size);
-
- if (RegQueryValueExA (key, "TimeZoneKeyName", NULL, NULL,
- (LPBYTE) key_name, &size) != ERROR_SUCCESS)
- {
- g_free (key_name);
- key_name = NULL;
- }
- }
-
- RegCloseKey (key);
- }
- }
+ key_name = windows_default_tzname ();
else
key_name = g_strdup (identifier);
if (!key_name)
- return FALSE;
-
- subkey = g_strconcat ("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\",
- key_name,
- NULL);
+ 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)
{
(LPBYTE) &last, &size) != ERROR_SUCCESS)
goto failed;
- *rules_num = last - first + 2;
- *rules = g_new0 (TimeZoneRule, *rules_num);
+ 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 tzi;
+ size = sizeof regtzi;
if (RegQueryValueExA (key, s, NULL, NULL,
- (LPBYTE) &tzi, &size) != ERROR_SUCCESS)
+ (LPBYTE) ®tzi, &size) != ERROR_SUCCESS)
{
g_free (*rules);
*rules = NULL;
g_free (s);
- if (year > first && memcmp (&tzi_prev, &tzi, sizeof tzi) == 0)
+ if (year > first && memcmp (®tzi_prev, ®tzi, sizeof regtzi) == 0)
continue;
else
- memcpy (&tzi_prev, &tzi, sizeof tzi);
-
- rule_from_windows_time_zone_info (&(*rules)[i], tzi.Bias,
- tzi.StandardBias, tzi.DaylightBias,
- tzi.StandardDate, tzi.DaylightDate);
+ 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;
+ rules_num = i + 1;
failed:
RegCloseKey (key);
else if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, subkey, 0,
KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
{
- size = sizeof tzi;
+ size = sizeof regtzi;
if (RegQueryValueExA (key, "TZI", NULL, NULL,
- (LPBYTE) &tzi, &size) == ERROR_SUCCESS)
+ (LPBYTE) ®tzi, &size) == ERROR_SUCCESS)
{
- *rules_num = 2;
+ rules_num = 2;
*rules = g_new0 (TimeZoneRule, 2);
-
- rule_from_windows_time_zone_info (&(*rules)[0], tzi.Bias,
- tzi.StandardBias, tzi.DaylightBias,
- tzi.StandardDate, tzi.DaylightDate);
+ register_tzi_to_tzi (®tzi, &tzi);
+ rule_from_windows_time_zone_info (&(*rules)[0], &tzi);
}
RegCloseKey (key);
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;
+ 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;
+ (*rules)[rules_num - 1].start_year = (*rules)[rules_num - 2].start_year + 1;
- return TRUE;
+ return rules_num;
}
else
- return FALSE;
+ return 0;
}
#endif
static void
-find_relative_date (TimeZoneDate *buffer,
- GTimeZone *tz)
+find_relative_date (TimeZoneDate *buffer)
{
- GDateTime *dt;
gint wday;
-
+ GDate date;
+ g_date_clear (&date, 1);
wday = buffer->wday;
/* Get last day if last is needed, first day otherwise */
- dt = g_date_time_new (tz,
- buffer->year,
- buffer->mon + (buffer->week < 5? 0 : 1),
- buffer->week < 5? 1 : 0,
- buffer->hour, buffer->min, buffer->sec);
-
- buffer->wday = g_date_time_get_day_of_week (dt);
- buffer->mday = g_date_time_get_day_of_month (dt);
-
- if (buffer->week < 5)
+ if (buffer->mon == 13 || buffer->mon == 14) /* Julian Date */
{
- if (wday < buffer->wday)
- buffer->wday -= 7;
-
- buffer->mday += (buffer->week - 1) * 7;
+ 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;
- else if (wday > buffer->wday)
- buffer->wday += 7;
+ while (days > days_in_month)
+ days -= 7;
- buffer->mday += wday - buffer->wday;
- buffer->wday = wday;
+ g_date_add_days (&date, days);
- g_date_time_unref (dt);
+ buffer->mday = g_date_get_day (&date);
+ }
}
-/* Offset is previous offset of local time */
+/* Offset is previous offset of local time. Returns 0 if month is 0 */
static gint64
boundary_for_year (TimeZoneDate *boundary,
gint year,
- gint32 prev_offset,
- gint32 std_offset)
+ gint32 offset)
{
TimeZoneDate buffer;
- GDateTime *dt;
- GTimeZone *tz;
- gint64 t;
- gint32 offset;
- gchar *identifier;
+ GDate date;
+ const guint64 unix_epoch_start = 719163L;
+ const guint64 seconds_per_day = 86400L;
+ if (!boundary->mon)
+ return 0;
buffer = *boundary;
- if (boundary->isgmt)
- offset = 0;
- else if (boundary->isstd)
- offset = std_offset;
- else
- offset = prev_offset;
-
- G_UNLOCK (time_zones);
-
- identifier = g_strdup_printf ("%+03d:%02d:%02d",
- (int) offset / 3600,
- (int) abs (offset / 60) % 60,
- (int) abs (offset) % 3600);
- tz = g_time_zone_new (identifier);
- g_free (identifier);
-
if (boundary->year == 0)
{
buffer.year = year;
if (buffer.wday)
- find_relative_date (&buffer, tz);
+ 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);
+}
- dt = g_date_time_new (tz,
- buffer.year, buffer.mon, buffer.mday,
- buffer.hour, buffer.min, buffer.sec);
- t = g_date_time_to_unix (dt);
- g_date_time_unref (dt);
+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;
- g_time_zone_unref (tz);
+ info->gmt_offset = offset;
+ info->is_dst = is_dst;
- G_LOCK (time_zones);
+ if (name)
+ info->abbrev = g_strdup (name);
- return t;
+ else
+ info->abbrev = g_strdup_printf ("%+03d%02d",
+ (int) offset / 3600,
+ (int) abs (offset / 60) % 60);
}
static void
TimeZoneRule *rules,
gint rules_num)
{
- TransitionInfo info[2];
- Transition trans;
- gint type_count, trans_count;
- gint year, i, x, y;
+ 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;
/* Last rule only contains max year */
- for (i = 0; i < rules_num - 1; i++)
+ for (ri = 0; ri < rules_num - 1; ri++)
{
- if (rules[i].dlt_start.mon)
+ if (rules[ri].dlt_start.mon || rules[ri].dlt_end.mon)
{
- type_count += 2;
- trans_count += 2 * (rules[i+1].start_year - rules[i].start_year);
+ guint rulespan = (rules[ri + 1].start_year - rules[ri].start_year);
+ guint transitions = rules[ri].dlt_start.mon > 0 ? 1 : 0;
+ transitions += rules[ri].dlt_end.mon > 0 ? 1 : 0;
+ type_count += rules[ri].dlt_start.mon > 0 ? 2 : 1;
+ trans_count += transitions * rulespan;
}
else
type_count++;
}
- x = 0;
- y = 0;
-
- /* If standard time happens before daylight time in first rule
- * with daylight, skip first transition so the minimum is in
- * standard time and the first transition is in daylight time */
- for (i = 0; i < rules_num - 1 && rules[0].dlt_start.mon == 0; i++);
-
- if (i < rules_num -1 && rules[i].dlt_start.mon > 0 &&
- rules[i].dlt_start.mon > rules[i].dlt_end.mon)
- {
- trans_count--;
- x = -1;
- }
-
gtz->t_info = g_array_sized_new (FALSE, TRUE, sizeof (TransitionInfo), type_count);
gtz->transitions = g_array_sized_new (FALSE, TRUE, sizeof (Transition), trans_count);
last_offset = rules[0].std_offset;
- for (i = 0; i < rules_num - 1; i++)
+ for (ri = 0; ri < rules_num - 1; ri++)
{
- if (rules[i].dlt_start.mon)
+ if ((rules[ri].std_offset || rules[ri].dlt_offset) &&
+ rules[ri].dlt_start.mon == 0 && rules[ri].dlt_end.mon == 0)
{
+ TransitionInfo std_info;
/* Standard */
- info[0].gmt_offset = rules[i].std_offset;
- info[0].is_dst = FALSE;
- info[0].is_standard = rules[i].dlt_end.isstd;
- info[0].is_gmt = rules[i].dlt_end.isgmt;
-
- if (rules[i].std_name)
- info[0].abbrev = g_strdup (rules[i].std_name);
-
- else
- info[0].abbrev = g_strdup_printf ("%+03d%02d",
- (int) rules[i].std_offset / 3600,
- (int) abs (rules[i].std_offset / 60) % 60);
+ fill_transition_info_from_rule (&std_info, &(rules[ri]), FALSE);
+ g_array_append_val (gtz->t_info, std_info);
-
- /* Daylight */
- info[1].gmt_offset = rules[i].dlt_offset;
- info[1].is_dst = TRUE;
- info[1].is_standard = rules[i].dlt_start.isstd;
- info[1].is_gmt = rules[i].dlt_start.isgmt;
-
- if (rules[i].dlt_name)
- info[1].abbrev = g_strdup (rules[i].dlt_name);
-
- else
- info[1].abbrev = g_strdup_printf ("%+03d%02d",
- (int) rules[i].dlt_offset / 3600,
- (int) abs (rules[i].dlt_offset / 60) % 60);
-
- if (rules[i].dlt_start.mon < rules[i].dlt_end.mon)
+ if (ri > 0 &&
+ ((rules[ri - 1].dlt_start.mon > 12 &&
+ rules[ri - 1].dlt_start.wday > rules[ri - 1].dlt_end.wday) ||
+ rules[ri - 1].dlt_start.mon > rules[ri - 1].dlt_end.mon))
{
- g_array_append_val (gtz->t_info, info[1]);
- g_array_append_val (gtz->t_info, info[0]);
+ /* The previous rule was a southern hemisphere rule that
+ starts the year with DST, so we need to add a
+ transition to return to standard time */
+ guint year = rules[ri].start_year;
+ gint64 std_time = boundary_for_year (&rules[ri].dlt_end,
+ year, last_offset);
+ Transition std_trans = {std_time, info_index};
+ g_array_append_val (gtz->transitions, std_trans);
+
}
+ last_offset = rules[ri].std_offset;
+ ++info_index;
+ skip_first_std_trans = TRUE;
+ }
+ else if (rules[ri].std_offset || rules[ri].dlt_offset)
+ {
+ const guint start_year = rules[ri].start_year;
+ const guint end_year = rules[ri + 1].start_year;
+ gboolean dlt_first;
+ guint year;
+ TransitionInfo std_info, dlt_info;
+ if (rules[ri].dlt_start.mon > 12)
+ dlt_first = rules[ri].dlt_start.wday > rules[ri].dlt_end.wday;
else
+ dlt_first = rules[ri].dlt_start.mon > rules[ri].dlt_end.mon;
+ /* Standard rules are always even, because before the first
+ transition is always standard time, and 0 is even. */
+ fill_transition_info_from_rule (&std_info, &(rules[ri]), FALSE);
+ fill_transition_info_from_rule (&dlt_info, &(rules[ri]), TRUE);
+
+ g_array_append_val (gtz->t_info, std_info);
+ g_array_append_val (gtz->t_info, dlt_info);
+
+ /* Transition dates. We hope that a year which ends daylight
+ time in a southern-hemisphere country (i.e., one that
+ begins the year in daylight time) will include a rule
+ which has only a dlt_end. */
+ for (year = start_year; year < end_year; year++)
{
- g_array_append_val (gtz->t_info, info[0]);
- g_array_append_val (gtz->t_info, info[1]);
- }
-
- /* Transition dates */
- for (year = rules[i].start_year; year < rules[i+1].start_year; year++)
- {
- if (rules[i].dlt_start.mon < rules[i].dlt_end.mon)
+ gint32 dlt_offset = (dlt_first ? last_offset :
+ rules[ri].dlt_offset);
+ gint32 std_offset = (dlt_first ? rules[ri].std_offset :
+ last_offset);
+ /* NB: boundary_for_year returns 0 if mon == 0 */
+ gint64 std_time = boundary_for_year (&rules[ri].dlt_end,
+ year, dlt_offset);
+ gint64 dlt_time = boundary_for_year (&rules[ri].dlt_start,
+ year, std_offset);
+ Transition std_trans = {std_time, info_index};
+ Transition dlt_trans = {dlt_time, info_index + 1};
+ last_offset = (dlt_first ? rules[ri].dlt_offset :
+ rules[ri].std_offset);
+ if (dlt_first)
{
- /* Daylight Data */
- trans.info_index = y;
- trans.time = boundary_for_year (&rules[i].dlt_start, year,
- last_offset, rules[i].std_offset);
- g_array_insert_val (gtz->transitions, x++, trans);
- last_offset = rules[i].dlt_offset;
-
- /* Standard Data */
- trans.info_index = y+1;
- trans.time = boundary_for_year (&rules[i].dlt_end, year,
- last_offset, rules[i].std_offset);
- g_array_insert_val (gtz->transitions, x++, trans);
- last_offset = rules[i].std_offset;
+ if (skip_first_std_trans)
+ skip_first_std_trans = FALSE;
+ else if (std_time)
+ g_array_append_val (gtz->transitions, std_trans);
+ if (dlt_time)
+ g_array_append_val (gtz->transitions, dlt_trans);
}
else
{
- /* Standard Data */
- trans.info_index = y;
- trans.time = boundary_for_year (&rules[i].dlt_end, year,
- last_offset, rules[i].std_offset);
- if (x >= 0)
- g_array_insert_val (gtz->transitions, x++, trans);
- else
- x++;
- last_offset = rules[i].std_offset;
-
- /* Daylight Data */
- trans.info_index = y+1;
- trans.time = boundary_for_year (&rules[i].dlt_start, year,
- last_offset, rules[i].std_offset);
- g_array_insert_val (gtz->transitions, x++, trans);
- last_offset = rules[i].dlt_offset;
+ if (dlt_time)
+ g_array_append_val (gtz->transitions, dlt_trans);
+ if (std_time)
+ g_array_append_val (gtz->transitions, std_trans);
}
}
- y += 2;
- }
- else
- {
- /* Standard */
- info[0].gmt_offset = rules[i].std_offset;
- info[0].is_dst = FALSE;
- info[0].is_standard = FALSE;
- info[0].is_gmt = FALSE;
-
- if (rules[i].std_name)
- info[0].abbrev = g_strdup (rules[i].std_name);
-
- else
- info[0].abbrev = g_strdup_printf ("%+03d%02d",
- (int) rules[i].std_offset / 3600,
- (int) abs (rules[i].std_offset / 60) % 60);
-
- g_array_append_val (gtz->t_info, info[0]);
-
- last_offset = rules[i].std_offset;
-
- y++;
+ info_index += 2;
}
}
+ if (ri > 0 &&
+ ((rules[ri - 1].dlt_start.mon > 12 &&
+ rules[ri - 1].dlt_start.wday > rules[ri - 1].dlt_end.wday) ||
+ rules[ri - 1].dlt_start.mon > rules[ri - 1].dlt_end.mon))
+ {
+ /* The previous rule was a southern hemisphere rule that
+ starts the year with DST, so we need to add a
+ transition to return to standard time */
+ TransitionInfo info;
+ guint year = rules[ri].start_year;
+ Transition trans;
+ fill_transition_info_from_rule (&info, &(rules[ri - 1]), FALSE);
+ g_array_append_val (gtz->t_info, info);
+ trans.time = boundary_for_year (&rules[ri - 1].dlt_end,
+ year, last_offset);
+ trans.info_index = info_index;
+ g_array_append_val (gtz->transitions, trans);
+ }
}
/*
* - ss is 00 to 59
*/
static gboolean
-parse_tz_boundary (const gchar *identifier,
- TimeZoneDate *boundary)
+parse_mwd_boundary (gchar **pos, TimeZoneDate *boundary)
{
- const gchar *pos;
gint month, week, day;
- GDate *date;
- pos = identifier;
+ if (**pos == '\0' || **pos < '0' || '9' < **pos)
+ return FALSE;
+
+ month = *(*pos)++ - '0';
- if (*pos == 'M') /* Relative date */
+ if ((month == 1 && **pos >= '0' && '2' >= **pos) ||
+ (month == 0 && **pos >= '0' && '9' >= **pos))
{
- pos++;
+ month *= 10;
+ month += *(*pos)++ - '0';
+ }
- if (*pos == '\0' || *pos < '0' || '9' < *pos)
- return FALSE;
+ if (*(*pos)++ != '.' || month == 0)
+ return FALSE;
- month = *pos++ - '0';
+ if (**pos == '\0' || **pos < '1' || '5' < **pos)
+ return FALSE;
- if ((month == 1 && *pos >= '0' && '2' >= *pos) ||
- (month == 0 && *pos >= '0' && '9' >= *pos))
- {
- month *= 10;
- month += *pos++ - '0';
- }
+ week = *(*pos)++ - '0';
- if (*pos++ != '.' || month == 0)
- return FALSE;
+ if (*(*pos)++ != '.')
+ return FALSE;
- if (*pos == '\0' || *pos < '1' || '5' < *pos)
- return FALSE;
+ if (**pos == '\0' || **pos < '0' || '6' < **pos)
+ return FALSE;
- week = *pos++ - '0';
+ day = *(*pos)++ - '0';
- if (*pos++ != '.')
- return FALSE;
+ if (!day)
+ day += 7;
- if (*pos == '\0' || *pos < '0' || '6' < *pos)
- return FALSE;
+ boundary->year = 0;
+ boundary->mon = month;
+ boundary->week = week;
+ boundary->wday = day;
+ return TRUE;
+}
- day = *pos++ - '0';
+/* Different implementations of tzset interpret the Julian day field
+ differently. For example, Linux specifies that it should be 1-based
+ (1 Jan is JD 1) for both Jn and n formats, while zOS and BSD
+ specify that a Jn JD is 1-based while an n JD is 0-based. Rather
+ than trying to follow different specs, we will follow GDate's
+ practice thatIn order to keep it simple, we will follow Linux's
+ practice. */
- if (!day)
- day += 7;
+static gboolean
+parse_julian_boundary (gchar** pos, TimeZoneDate *boundary,
+ gboolean ignore_leap)
+{
+ gint day = 0;
+ GDate date;
- boundary->year = 0;
- boundary->mon = month;
- boundary->week = week;
- boundary->wday = day;
+ while (**pos >= '0' && '9' >= **pos)
+ {
+ day *= 10;
+ day += *(*pos)++ - '0';
}
- else if (*pos == 'J') /* Julian day */
- {
- pos++;
+ if (day < 1 || 365 < day)
+ return FALSE;
- day = 0;
- while (*pos >= '0' && '9' >= *pos)
- {
- day *= 10;
- day += *pos++ - '0';
- }
+ g_date_clear (&date, 1);
+ g_date_set_julian (&date, day);
+ boundary->year = 0;
+ boundary->mon = (int) g_date_get_month (&date);
+ boundary->mday = (int) g_date_get_day (&date);
+ boundary->wday = 0;
- if (day < 1 || 365 < day)
- return FALSE;
+ if (!ignore_leap && day >= 59)
+ boundary->mday++;
- date = g_date_new_julian (day);
- boundary->year = 0;
- boundary->mon = (int) g_date_get_month (date);
- boundary->mday = (int) g_date_get_day (date);
- boundary->wday = 0;
- g_date_free (date);
- }
+ return TRUE;
+}
- else if (*pos >= '0' && '9' >= *pos) /* Zero-based Julian day */
- {
- day = 0;
- while (*pos >= '0' && '9' >= *pos)
- {
- day *= 10;
- day += *pos++ - '0';
- }
+static gboolean
+parse_tz_boundary (const gchar *identifier,
+ TimeZoneDate *boundary)
+{
+ gchar *pos;
- if (day < 0 || 365 < day)
+ pos = (gchar*)identifier;
+ /* Month-week-weekday */
+ if (*pos == 'M')
+ {
+ ++pos;
+ if (!parse_mwd_boundary (&pos, boundary))
+ return FALSE;
+ }
+ /* Julian date which ignores Feb 29 in leap years */
+ else if (*pos == 'J')
+ {
+ ++pos;
+ if (!parse_julian_boundary (&pos, boundary, FALSE))
+ return FALSE ;
+ }
+ /* Julian date which counts Feb 29 in leap years */
+ else if (*pos >= '0' && '9' >= *pos)
+ {
+ if (!parse_julian_boundary (&pos, boundary, TRUE))
return FALSE;
-
- date = g_date_new_julian (day >= 59? day : day + 1);
- boundary->year = 0;
- boundary->mon = (int) g_date_get_month (date);
- boundary->mday = (int) g_date_get_day (date);
- boundary->wday = 0;
- g_date_free (date);
-
- /* February 29 */
- if (day == 59)
- boundary->mday++;
}
-
else
return FALSE;
/* Time */
- boundary->isstd = FALSE;
- boundary->isgmt = FALSE;
if (*pos == '/')
{
}
}
-/*
- * Creates an array of TimeZoneRule from a TZ environment variable
- * type of identifier. Should free rules, std_name and dlt_name
- * afterwards
- */
-static gboolean
-rules_from_identifier (const gchar *identifier,
- TimeZoneRule **rules,
- gint *rules_num,
- gchar **std_name,
- gchar **dlt_name)
+static gint
+create_ruleset_from_rule (TimeZoneRule **rules, TimeZoneRule *rule)
{
- const gchar *std_name_pos, *std_offset_pos;
- const gchar *dlt_name_pos, *dlt_offset_pos;
+ *rules = g_new0 (TimeZoneRule, 2);
- const gchar *start_date_pos, *end_date_pos;
+ (*rules)[0].start_year = MIN_TZYEAR;
+ (*rules)[1].start_year = MAX_TZYEAR;
+
+ (*rules)[0].std_offset = -rule->std_offset;
+ (*rules)[0].dlt_offset = -rule->dlt_offset;
+ (*rules)[0].dlt_start = rule->dlt_start;
+ (*rules)[0].dlt_end = rule->dlt_end;
+ strcpy ((*rules)[0].std_name, rule->std_name);
+ strcpy ((*rules)[0].dlt_name, rule->dlt_name);
+ return 2;
+}
- const gchar *pos;
+static gboolean
+parse_offset (gchar **pos, gint32 *target)
+{
gchar *buffer;
+ gchar *target_pos = *pos;
gboolean ret;
- gint32 std_offset, dlt_offset;
- TimeZoneDate dlt_start, dlt_end;
-
- if (!identifier)
- return FALSE;
-
- pos = identifier;
-
- std_name_pos = pos;
-
- /* Name is alpha */
- while ((*pos >= 'a' && 'z' >= *pos) || (*pos >= 'A' && 'Z' >= *pos))
- pos++;
-
- /* Offset for standard required (format 1) */
- if (*pos == '\0')
- return FALSE;
+ while (**pos == '+' || **pos == '-' || **pos == ':' ||
+ (**pos >= '0' && '9' >= **pos))
+ ++(*pos);
- /* Name should be three or more alphabetic characters */
- if (pos - identifier < 3)
- return FALSE;
-
- std_offset_pos = pos;
-
- /* Standard offset */
- while (*pos == '+' || *pos == '-' || *pos == ':' || (*pos >= '0' && '9' >= *pos))
- pos++;
-
- buffer = g_strndup (std_offset_pos, pos - std_offset_pos);
- ret = parse_constant_offset (buffer, &std_offset);
+ buffer = g_strndup (target_pos, *pos - target_pos);
+ ret = parse_constant_offset (buffer, target);
g_free (buffer);
- if (!ret)
- return FALSE;
-
- dlt_name_pos = pos;
- dlt_offset_pos = NULL;
-
- /* Format 2 */
- if (*pos != '\0')
- {
- /* Name is alpha */
- while ((*pos >= 'a' && 'z' >= *pos) || (*pos >= 'A' && 'Z' >= *pos))
- pos++;
-
- /* Name should be three or more alphabetic characters */
- if (pos - identifier < 3)
- return FALSE;
-
- dlt_offset_pos = pos;
-
-#ifndef G_OS_WIN32
- /* Start and end required (format 2) */
- if (*pos == '\0')
- return FALSE;
-#else
- if (*pos != '\0')
- {
-#endif
-
- /* Default offset is 1 hour less from standard offset */
- if (*pos++ == ',')
- dlt_offset = std_offset - 60 * 60;
-
- else
- {
- /* Daylight offset */
- while (*pos == '+' || *pos == '-' || *pos == ':' || (*pos >= '0' && '9' >= *pos))
- pos++;
-
- buffer = g_strndup (dlt_offset_pos, pos - dlt_offset_pos);
- ret = parse_constant_offset (buffer, &dlt_offset);
- g_free (buffer);
-
- if (!ret)
- return FALSE;
-
- /* Start and end required (format 2) */
- if (*pos++ != ',')
- return FALSE;
- }
+ return ret;
+}
- /* Start date */
- start_date_pos = pos;
+static gboolean
+parse_identifier_boundary (gchar **pos, TimeZoneDate *target)
+{
+ gchar *buffer;
+ gchar *target_pos = *pos;
+ gboolean ret;
- while (*pos != ',' && *pos != '\0')
- pos++;
+ while (**pos != ',' && **pos != '\0')
+ ++(*pos);
+ buffer = g_strndup (target_pos, *pos - target_pos);
+ ret = parse_tz_boundary (buffer, target);
+ g_free (buffer);
- /* End required (format 2) */
- if (*pos == '\0')
- return FALSE;
+ return ret;
+}
- buffer = g_strndup (start_date_pos, pos++ - start_date_pos);
- ret = parse_tz_boundary (buffer, &dlt_start);
- g_free (buffer);
+static gboolean
+set_tz_name (gchar **pos, gchar *buffer, guint size)
+{
+ gchar *name_pos = *pos;
+ guint len;
- if (!ret)
- return FALSE;
+ /* Name is ASCII alpha (Is this necessarily true?) */
+ while (g_ascii_isalpha (**pos))
+ ++(*pos);
- /* End date */
- end_date_pos = pos;
+ /* Name should be three or more alphabetic characters */
+ if (*pos - name_pos < 3)
+ return FALSE;
- while (*pos != '\0')
- pos++;
+ memset (buffer, 0, NAME_SIZE);
+ /* name_pos isn't 0-terminated, so we have to limit the length expressly */
+ len = *pos - name_pos > size - 1 ? size - 1 : *pos - name_pos;
+ strncpy (buffer, name_pos, len);
+ return TRUE;
+}
- buffer = g_strndup (end_date_pos, pos - end_date_pos);
- ret = parse_tz_boundary (buffer, &dlt_end);
- g_free (buffer);
+static gboolean
+parse_identifier_boundaries (gchar **pos, TimeZoneRule *tzr)
+{
+ if (*(*pos)++ != ',')
+ return FALSE;
- if (!ret)
- return FALSE;
+ /* Start date */
+ if (!parse_identifier_boundary (pos, &(tzr->dlt_start)) || *(*pos)++ != ',')
+ return FALSE;
-#ifdef G_OS_WIN32
- }
-#endif
- }
+ /* End date */
+ if (!parse_identifier_boundary (pos, &(tzr->dlt_end)))
+ return FALSE;
+ return TRUE;
+}
+/*
+ * Creates an array of TimeZoneRule from a TZ environment variable
+ * type of identifier. Should free rules afterwards
+ */
+static gint
+rules_from_identifier (const gchar *identifier,
+ TimeZoneRule **rules)
+{
+ gchar *pos;
+ TimeZoneRule tzr;
- *std_name = g_strndup (std_name_pos, std_offset_pos - std_name_pos);
+ if (!identifier)
+ return 0;
- if (dlt_name_pos != pos)
- *dlt_name = g_strndup (dlt_name_pos, dlt_offset_pos - dlt_name_pos);
- else
- {
- *dlt_name = NULL;
- dlt_start.mon = 0;
- }
+ pos = (gchar*)identifier;
+ memset (&tzr, 0, sizeof (tzr));
+ /* Standard offset */
+ if (!(set_tz_name (&pos, tzr.std_name, NAME_SIZE)) ||
+ !parse_offset (&pos, &(tzr.std_offset)))
+ return 0;
+ if (*pos == 0)
+ return create_ruleset_from_rule (rules, &tzr);
+ /* Format 2 */
+ if (!(set_tz_name (&pos, tzr.dlt_name, NAME_SIZE)))
+ return 0;
+ parse_offset (&pos, &(tzr.dlt_offset));
+ if (tzr.dlt_offset == 0) /* No daylight offset given, assume it's 1
+ hour earlier that standard */
+ tzr.dlt_offset = tzr.std_offset - 3600;
+ if (*pos == '\0')
#ifdef G_OS_WIN32
- /* If doesn't have offset for daylight then it is Windows format */
- if (dlt_offset_pos == pos)
+ /* Windows allows us to use the US DST boundaries if they're not given */
{
int i;
+ guint rules_num = 0;
/* Use US rules, Windows' default is Pacific Standard Time */
- dlt_offset = std_offset - 60 * 60;
-
- if (rules_from_windows_time_zone ("Pacific Standard Time", rules, rules_num, NULL, NULL))
+ if ((rules_num = rules_from_windows_time_zone ("Pacific Standard Time",
+ rules)))
{
- for (i = 0; i < *rules_num - 1; i++)
+ for (i = 0; i < rules_num - 1; i++)
{
- (*rules)[i].std_offset = -std_offset;
- (*rules)[i].dlt_offset = -dlt_offset;
- (*rules)[i].std_name = *std_name;
- (*rules)[i].dlt_name = *dlt_name;
+ (*rules)[i].std_offset = - tzr.std_offset;
+ (*rules)[i].dlt_offset = - tzr.dlt_offset;
+ strcpy ((*rules)[i].std_name, tzr.std_name);
+ strcpy ((*rules)[i].dlt_name, tzr.dlt_name);
}
- return TRUE;
+ return rules_num;
}
else
- return FALSE;
+ return 0;
}
+#else
+ return 0;
#endif
+ /* Start and end required (format 2) */
+ if (!parse_identifier_boundaries (&pos, &tzr))
+ return 0;
-
- *rules_num = 2;
- *rules = g_new0 (TimeZoneRule, 2);
-
- (*rules)[0].start_year = MIN_TZYEAR;
- (*rules)[1].start_year = MAX_TZYEAR;
-
- (*rules)[0].std_offset = -std_offset;
- (*rules)[0].dlt_offset = -dlt_offset;
- (*rules)[0].dlt_start = dlt_start;
- (*rules)[0].dlt_end = dlt_end;
- (*rules)[0].std_name = *std_name;
- (*rules)[0].dlt_name = *dlt_name;
-
- return TRUE;
+ return create_ruleset_from_rule (rules, &tzr);
}
/* Construction {{{1 */
* Creates a #GTimeZone corresponding to @identifier.
*
* @identifier can either be an RFC3339/ISO 8601 time offset or
- * something that would pass as a valid value for the
- * <varname>TZ</varname> environment variable (including %NULL).
+ * something that would pass as a valid value for the `TZ` environment
+ * variable (including %NULL).
+ *
+ * In Windows, @identifier can also be the unlocalized name of a time
+ * zone for standard time, for example "Pacific Standard Time".
*
- * Valid RFC3339 time offsets are <literal>"Z"</literal> (for UTC) or
- * <literal>"±hh:mm"</literal>. ISO 8601 additionally specifies
- * <literal>"±hhmm"</literal> and <literal>"±hh"</literal>. Offsets are
+ * Valid RFC3339 time offsets are `"Z"` (for UTC) or
+ * `"±hh:mm"`. ISO 8601 additionally specifies
+ * `"±hhmm"` and `"±hh"`. Offsets are
* time values to be added to Coordinated Universal Time (UTC) to get
* the local time.
*
- * In Unix, the <varname>TZ</varname> environment variable typically
- * corresponds to the name of a file in the zoneinfo database, or
- * string in "std offset [dst [offset],start[/time],end[/time]]"
- * (POSIX) format. There are no spaces in the specification. The
- * name of standard and daylight savings time zone must be three or more
- * alphabetic characters. Offsets are time values to be added to local
- * time to get Coordinated Universal Time (UTC) and should be
- * <literal>"[±]hh[[:]mm[:ss]]"</literal>. Dates are either
- * <literal>"Jn"</literal> (Julian day with n between 1 and 365, leap
- * years not counted), <literal>"n"</literal> (zero-based Julian day
- * with n between 0 and 365) or <literal>"Mm.w.d"</literal> (day d
+ * In UNIX, the `TZ` environment variable typically corresponds
+ * to the name of a file in the zoneinfo database, or string in
+ * "std offset [dst [offset],start[/time],end[/time]]" (POSIX) format.
+ * There are no spaces in the specification. The name of standard
+ * and daylight savings time zone must be three or more alphabetic
+ * characters. Offsets are time values to be added to local time to
+ * get Coordinated Universal Time (UTC) and should be
+ * `"[±]hh[[:]mm[:ss]]"`. Dates are either
+ * `"Jn"` (Julian day with n between 1 and 365, leap
+ * years not counted), `"n"` (zero-based Julian day
+ * with n between 0 and 365) or `"Mm.w.d"` (day d
* (0 <= d <= 6) of week w (1 <= w <= 5) of month m (1 <= m <= 12), day
* 0 is a Sunday). Times are in local wall clock time, the default is
* 02:00:00.
* Coordinated Universal Time (UTC).
*
* g_time_zone_new_local() calls this function with the value of the
- * <varname>TZ</varname> environment variable. This function itself is
- * independent of the value of <varname>TZ</varname>, but if @identifier
- * is %NULL then <filename>/etc/localtime</filename> will be consulted
- * to discover the correct timezone.
+ * `TZ` environment variable. This function itself is independent of
+ * the value of `TZ`, but if @identifier is %NULL then `/etc/localtime`
+ * will be consulted to discover the correct time zone on UNIX and the
+ * registry will be consulted or GetTimeZoneInformation() will be used
+ * to get the local time zone on Windows.
*
- * If intervals are not available, only time zone rules from
- * <varname>TZ</varname> environment variable or other means, then they
- * will be computed from year 1900 to 2037. If the maximum year for the
- * rules is available and it is greater than 2037, then it will followed
+ * If intervals are not available, only time zone rules from `TZ`
+ * environment variable or other means, then they will be computed
+ * from year 1900 to 2037. If the maximum year for the rules is
+ * available and it is greater than 2037, then it will followed
* instead.
*
- * See <ulink
- * url='http://tools.ietf.org/html/rfc3339#section-5.6'>RFC3339
- * §5.6</ulink> for a precise definition of valid RFC3339 time offsets
- * (the <varname>time-offset</varname> expansion) and ISO 8601 for the
- * full list of valid time offsets. See <ulink
- * url='http://www.gnu.org/s/libc/manual/html_node/TZ-Variable.html'>The
- * GNU C Library manual</ulink> for an explanation of the possible
- * values of the <varname>TZ</varname> environment variable.
+ * See
+ * [RFC3339 §5.6](http://tools.ietf.org/html/rfc3339#section-5.6)
+ * for a precise definition of valid RFC3339 time offsets
+ * (the `time-offset` expansion) and ISO 8601 for the
+ * full list of valid time offsets. See
+ * [The GNU C Library manual](http://www.gnu.org/s/libc/manual/html_node/TZ-Variable.html)
+ * for an explanation of the possible
+ * values of the `TZ` environment variable. See
+ * [Microsoft Time Zone Index Values](http://msdn.microsoft.com/en-us/library/ms912391%28v=winembedded.11%29.aspx)
+ * for the list of time zones on Windows.
*
* You should release the return value by calling g_time_zone_unref()
* when you are done with it.
GTimeZone *tz = NULL;
TimeZoneRule *rules;
gint rules_num;
- gchar *std_name, *dlt_name;
G_LOCK (time_zones);
if (time_zones == NULL)
zone_for_constant_offset (tz, identifier);
if (tz->t_info == NULL &&
- rules_from_identifier (identifier,
- &rules, &rules_num,
- &std_name, &dlt_name))
+ (rules_num = rules_from_identifier (identifier, &rules)))
{
init_zone_from_rules (tz, rules, rules_num);
g_free (rules);
- g_free (std_name);
- g_free (dlt_name);
}
if (tz->t_info == NULL)
init_zone_from_iana_info (tz, zoneinfo);
g_bytes_unref (zoneinfo);
}
-#elif defined G_OS_WIN32
+#elif defined (G_OS_WIN32)
+ if ((rules_num = rules_from_windows_time_zone (identifier, &rules)))
+ {
+ init_zone_from_rules (tz, rules, rules_num);
+ g_free (rules);
+ }
+ }
+
+ if (tz->t_info == NULL)
+ {
+ if (identifier)
+ zone_for_constant_offset (tz, "UTC");
+ else
+ {
+ TIME_ZONE_INFORMATION tzi;
+
+ if (GetTimeZoneInformation (&tzi) != TIME_ZONE_ID_INVALID)
+ {
+ rules = g_new0 (TimeZoneRule, 2);
+
+ rule_from_windows_time_zone_info (&rules[0], &tzi);
+
+ memset (rules[0].std_name, 0, NAME_SIZE);
+ memset (rules[0].dlt_name, 0, NAME_SIZE);
+
+ rules[0].start_year = MIN_TZYEAR;
+ rules[1].start_year = MAX_TZYEAR;
+
+ init_zone_from_rules (tz, rules, 2);
+
+ g_free (rules);
+ }
+ }
#endif
}
* zone may change between invocations to this function; for example,
* if the system administrator changes it.
*
- * This is equivalent to calling g_time_zone_new() with the value of the
- * <varname>TZ</varname> environment variable (including the possibility
- * of %NULL).
+ * This is equivalent to calling g_time_zone_new() with the value of
+ * the `TZ` environment variable (including the possibility of %NULL).
*
* You should release the return value by calling g_time_zone_unref()
* when you are done with it.
#define TRANSITION_INFO(n) g_array_index (tz->t_info, TransitionInfo, n)
/* Internal helpers {{{1 */
-/* Note that interval 0 is *before* the first transition time, so
- * interval 1 gets transitions[0].
+/* NB: Interval 0 is before the first transition, so there's no
+ * transition structure to point to which TransitionInfo to
+ * use. Rule-based zones are set up so that TI 0 is always standard
+ * time (which is what's in effect before Daylight time got started
+ * in the early 20th century), but IANA tzfiles don't follow that
+ * convention. The tzfile documentation says to use the first
+ * standard-time (i.e., non-DST) tinfo, so that's what we do.
*/
inline static const TransitionInfo*
interval_info (GTimeZone *tz,
if (interval && tz->transitions && interval <= tz->transitions->len)
index = (TRANSITION(interval - 1)).info_index;
else
- index = 0;
+ {
+ for (index = 0; index < tz->t_info->len; index++)
+ {
+ TransitionInfo *tzinfo = &(TRANSITION_INFO(index));
+ if (!tzinfo->is_dst)
+ return tzinfo;
+ }
+ index = 0;
+ }
+
return &(TRANSITION_INFO(index));
}
}
-inline static gboolean
-interval_isgmt (GTimeZone *tz,
- guint interval)
-{
- g_return_val_if_fail (tz->t_info != NULL, 0);
- return interval_info (tz, interval)->is_gmt;
-}
-
-inline static gboolean
-interval_isstandard (GTimeZone *tz,
- guint interval)
-{
- return interval_info (tz, interval)->is_standard;
-}
-
inline static gchar*
interval_abbrev (GTimeZone *tz,
guint interval)
* If @type is %G_TIME_TYPE_UNIVERSAL then this function will always
* succeed (since universal time is monotonic and continuous).
*
- * Otherwise @time_ is treated is local time. The distinction between
+ * Otherwise @time_ is treated as local time. The distinction between
* %G_TIME_TYPE_STANDARD and %G_TIME_TYPE_DAYLIGHT is ignored except in
* the case that the given @time_ is ambiguous. In Toronto, for example,
* 01:30 on November 7th 2010 occurred twice (once inside of daylight