/src/glib/glib/gtimezone.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2010 Codethink Limited |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | * Author: Ryan Lortie <desrt@desrt.ca> |
20 | | */ |
21 | | |
22 | | /* Prologue {{{1 */ |
23 | | |
24 | | #include "config.h" |
25 | | |
26 | | #include "gtimezone.h" |
27 | | |
28 | | #include <string.h> |
29 | | #include <stdlib.h> |
30 | | #include <signal.h> |
31 | | |
32 | | #include "gmappedfile.h" |
33 | | #include "gtestutils.h" |
34 | | #include "gfileutils.h" |
35 | | #include "gstrfuncs.h" |
36 | | #include "ghash.h" |
37 | | #include "gthread.h" |
38 | | #include "gbytes.h" |
39 | | #include "gslice.h" |
40 | | #include "gdatetime.h" |
41 | | #include "gdate.h" |
42 | | #include "genviron.h" |
43 | | |
44 | | #ifdef G_OS_UNIX |
45 | | #include "gstdio.h" |
46 | | #endif |
47 | | |
48 | | #ifdef G_OS_WIN32 |
49 | | #include <windows.h> |
50 | | #include <wchar.h> |
51 | | #endif |
52 | | |
53 | | /** |
54 | | * GTimeZone: |
55 | | * |
56 | | * A `GTimeZone` represents a time zone, at no particular point in time. |
57 | | * |
58 | | * The `GTimeZone` struct is refcounted and immutable. |
59 | | * |
60 | | * Each time zone has an identifier (for example, ‘Europe/London’) which is |
61 | | * platform dependent. See [ctor@GLib.TimeZone.new] for information on the |
62 | | * identifier formats. The identifier of a time zone can be retrieved using |
63 | | * [method@GLib.TimeZone.get_identifier]. |
64 | | * |
65 | | * A time zone contains a number of intervals. Each interval has an abbreviation |
66 | | * to describe it (for example, ‘PDT’), an offset to UTC and a flag indicating |
67 | | * if the daylight savings time is in effect during that interval. A time zone |
68 | | * always has at least one interval — interval 0. Note that interval abbreviations |
69 | | * are not the same as time zone identifiers (apart from ‘UTC’), and cannot be |
70 | | * passed to [ctor@GLib.TimeZone.new]. |
71 | | * |
72 | | * Every UTC time is contained within exactly one interval, but a given |
73 | | * local time may be contained within zero, one or two intervals (due to |
74 | | * incontinuities associated with daylight savings time). |
75 | | * |
76 | | * An interval may refer to a specific period of time (eg: the duration |
77 | | * of daylight savings time during 2010) or it may refer to many periods |
78 | | * of time that share the same properties (eg: all periods of daylight |
79 | | * savings time). It is also possible (usually for political reasons) |
80 | | * that some properties (like the abbreviation) change between intervals |
81 | | * without other properties changing. |
82 | | * |
83 | | * Since: 2.26 |
84 | | */ |
85 | | |
86 | | /* IANA zoneinfo file format {{{1 */ |
87 | | |
88 | | /* unaligned */ |
89 | | typedef struct { gchar bytes[8]; } gint64_be; |
90 | | typedef struct { gchar bytes[4]; } gint32_be; |
91 | | typedef struct { gchar bytes[4]; } guint32_be; |
92 | | |
93 | | #ifdef G_OS_UNIX |
94 | | |
95 | 0 | static inline gint64 gint64_from_be (const gint64_be be) { |
96 | 0 | gint64 tmp; memcpy (&tmp, &be, sizeof tmp); return GINT64_FROM_BE (tmp); |
97 | 0 | } |
98 | | |
99 | 0 | static inline gint32 gint32_from_be (const gint32_be be) { |
100 | 0 | gint32 tmp; memcpy (&tmp, &be, sizeof tmp); return GINT32_FROM_BE (tmp); |
101 | 0 | } |
102 | | |
103 | 0 | static inline guint32 guint32_from_be (const guint32_be be) { |
104 | 0 | guint32 tmp; memcpy (&tmp, &be, sizeof tmp); return GUINT32_FROM_BE (tmp); |
105 | 0 | } |
106 | | |
107 | | #endif |
108 | | |
109 | | /* The layout of an IANA timezone file header */ |
110 | | struct tzhead |
111 | | { |
112 | | gchar tzh_magic[4]; |
113 | | gchar tzh_version; |
114 | | guchar tzh_reserved[15]; |
115 | | |
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; |
122 | | }; |
123 | | |
124 | | struct ttinfo |
125 | | { |
126 | | gint32_be tt_gmtoff; |
127 | | guint8 tt_isdst; |
128 | | guint8 tt_abbrind; |
129 | | }; |
130 | | |
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. |
134 | | */ |
135 | | typedef struct |
136 | | { |
137 | | gint year; |
138 | | gint mon; |
139 | | gint mday; |
140 | | gint wday; |
141 | | gint week; |
142 | | gint32 offset; /* hour*3600 + min*60 + sec; can be negative. */ |
143 | | } TimeZoneDate; |
144 | | |
145 | | /* POSIX Timezone abbreviations are typically 3 or 4 characters, but |
146 | | Microsoft uses 32-character names. We'll use one larger to ensure |
147 | | we have room for the terminating \0. |
148 | | */ |
149 | 0 | #define NAME_SIZE 33 |
150 | | |
151 | | /* A MSWindows-style time zone transition rule. Generalizes the |
152 | | MSWindows TIME_ZONE_INFORMATION struct. Also used to compose time |
153 | | zones from tzset-style identifiers. |
154 | | */ |
155 | | typedef struct |
156 | | { |
157 | | guint start_year; |
158 | | gint32 std_offset; |
159 | | gint32 dlt_offset; |
160 | | TimeZoneDate dlt_start; |
161 | | TimeZoneDate dlt_end; |
162 | | gchar std_name[NAME_SIZE]; |
163 | | gchar dlt_name[NAME_SIZE]; |
164 | | } TimeZoneRule; |
165 | | |
166 | | /* GTimeZone's internal representation of a Daylight Savings (Summer) |
167 | | time interval. |
168 | | */ |
169 | | typedef struct |
170 | | { |
171 | | gint32 gmt_offset; |
172 | | gboolean is_dst; |
173 | | gchar *abbrev; |
174 | | } TransitionInfo; |
175 | | |
176 | | /* GTimeZone's representation of a transition time to or from Daylight |
177 | | Savings (Summer) time and Standard time for the zone. */ |
178 | | typedef struct |
179 | | { |
180 | | gint64 time; |
181 | | gint info_index; |
182 | | } Transition; |
183 | | |
184 | | /* GTimeZone structure */ |
185 | | struct _GTimeZone |
186 | | { |
187 | | gchar *name; |
188 | | GArray *t_info; /* Array of TransitionInfo */ |
189 | | GArray *transitions; /* Array of Transition */ |
190 | | gint ref_count; |
191 | | }; |
192 | | |
193 | | G_LOCK_DEFINE_STATIC (time_zones); |
194 | | static GHashTable/*<string?, GTimeZone>*/ *time_zones; |
195 | | G_LOCK_DEFINE_STATIC (tz_default); |
196 | | static GTimeZone *tz_default = NULL; |
197 | | G_LOCK_DEFINE_STATIC (tz_local); |
198 | | static GTimeZone *tz_local = NULL; |
199 | | |
200 | 0 | #define MIN_TZYEAR 1916 /* Daylight Savings started in WWI */ |
201 | 0 | #define MAX_TZYEAR 2999 /* And it's not likely ever to go away, but |
202 | | there's no point in getting carried |
203 | | away. */ |
204 | | |
205 | | #ifdef G_OS_UNIX |
206 | | static GTimeZone *parse_footertz (const gchar *, size_t); |
207 | | #endif |
208 | | |
209 | | /** |
210 | | * g_time_zone_unref: |
211 | | * @tz: a #GTimeZone |
212 | | * |
213 | | * Decreases the reference count on @tz. |
214 | | * |
215 | | * Since: 2.26 |
216 | | **/ |
217 | | void |
218 | | g_time_zone_unref (GTimeZone *tz) |
219 | 0 | { |
220 | 0 | int ref_count; |
221 | |
|
222 | 0 | again: |
223 | 0 | ref_count = g_atomic_int_get (&tz->ref_count); |
224 | |
|
225 | 0 | g_assert (ref_count > 0); |
226 | | |
227 | 0 | if (ref_count == 1) |
228 | 0 | { |
229 | 0 | if (tz->name != NULL) |
230 | 0 | { |
231 | 0 | G_LOCK(time_zones); |
232 | | |
233 | | /* someone else might have grabbed a ref in the meantime */ |
234 | 0 | if G_UNLIKELY (g_atomic_int_get (&tz->ref_count) != 1) |
235 | 0 | { |
236 | 0 | G_UNLOCK(time_zones); |
237 | 0 | goto again; |
238 | 0 | } |
239 | | |
240 | 0 | if (time_zones != NULL) |
241 | 0 | g_hash_table_remove (time_zones, tz->name); |
242 | 0 | G_UNLOCK(time_zones); |
243 | 0 | } |
244 | | |
245 | 0 | if (tz->t_info != NULL) |
246 | 0 | { |
247 | 0 | guint idx; |
248 | 0 | for (idx = 0; idx < tz->t_info->len; idx++) |
249 | 0 | { |
250 | 0 | TransitionInfo *info = &g_array_index (tz->t_info, TransitionInfo, idx); |
251 | 0 | g_free (info->abbrev); |
252 | 0 | } |
253 | 0 | g_array_free (tz->t_info, TRUE); |
254 | 0 | } |
255 | 0 | if (tz->transitions != NULL) |
256 | 0 | g_array_free (tz->transitions, TRUE); |
257 | 0 | g_free (tz->name); |
258 | |
|
259 | 0 | g_slice_free (GTimeZone, tz); |
260 | 0 | } |
261 | | |
262 | 0 | else if G_UNLIKELY (!g_atomic_int_compare_and_exchange (&tz->ref_count, |
263 | 0 | ref_count, |
264 | 0 | ref_count - 1)) |
265 | 0 | goto again; |
266 | 0 | } |
267 | | |
268 | | /** |
269 | | * g_time_zone_ref: |
270 | | * @tz: a #GTimeZone |
271 | | * |
272 | | * Increases the reference count on @tz. |
273 | | * |
274 | | * Returns: a new reference to @tz. |
275 | | * |
276 | | * Since: 2.26 |
277 | | **/ |
278 | | GTimeZone * |
279 | | g_time_zone_ref (GTimeZone *tz) |
280 | 0 | { |
281 | 0 | g_return_val_if_fail (tz != NULL, NULL); |
282 | 0 | g_assert (tz->ref_count > 0); |
283 | | |
284 | 0 | g_atomic_int_inc (&tz->ref_count); |
285 | |
|
286 | 0 | return tz; |
287 | 0 | } |
288 | | |
289 | | /* fake zoneinfo creation (for RFC3339/ISO 8601 timezones) {{{1 */ |
290 | | /* |
291 | | * parses strings of the form h or hh[[:]mm[[[:]ss]]] where: |
292 | | * - h[h] is 0 to 24 |
293 | | * - mm is 00 to 59 |
294 | | * - ss is 00 to 59 |
295 | | * If RFC8536, TIME_ is a transition time sans sign, |
296 | | * so colons are required before mm and ss, and hh can be up to 167. |
297 | | * See Internet RFC 8536 section 3.3.1: |
298 | | * https://tools.ietf.org/html/rfc8536#section-3.3.1 |
299 | | * and POSIX Base Definitions 8.3 TZ rule time: |
300 | | * https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03 |
301 | | */ |
302 | | static gboolean |
303 | | parse_time (const gchar *time_, |
304 | | gint32 *offset, |
305 | | gboolean rfc8536) |
306 | 0 | { |
307 | 0 | if (*time_ < '0' || '9' < *time_) |
308 | 0 | return FALSE; |
309 | | |
310 | 0 | *offset = 60 * 60 * (*time_++ - '0'); |
311 | |
|
312 | 0 | if (*time_ == '\0') |
313 | 0 | return TRUE; |
314 | | |
315 | 0 | if (*time_ != ':') |
316 | 0 | { |
317 | 0 | if (*time_ < '0' || '9' < *time_) |
318 | 0 | return FALSE; |
319 | | |
320 | 0 | *offset *= 10; |
321 | 0 | *offset += 60 * 60 * (*time_++ - '0'); |
322 | |
|
323 | 0 | if (rfc8536) |
324 | 0 | { |
325 | | /* Internet RFC 8536 section 3.3.1 and POSIX 8.3 TZ together say |
326 | | that a transition time must be of the form [+-]hh[:mm[:ss]] where |
327 | | the hours part can range from -167 to 167. */ |
328 | 0 | if ('0' <= *time_ && *time_ <= '9') |
329 | 0 | { |
330 | 0 | *offset *= 10; |
331 | 0 | *offset += 60 * 60 * (*time_++ - '0'); |
332 | 0 | } |
333 | 0 | if (*offset > 167 * 60 * 60) |
334 | 0 | return FALSE; |
335 | 0 | } |
336 | 0 | else if (*offset > 24 * 60 * 60) |
337 | 0 | return FALSE; |
338 | | |
339 | 0 | if (*time_ == '\0') |
340 | 0 | return TRUE; |
341 | 0 | } |
342 | | |
343 | 0 | if (*time_ == ':') |
344 | 0 | time_++; |
345 | 0 | else if (rfc8536) |
346 | 0 | return FALSE; |
347 | | |
348 | 0 | if (*time_ < '0' || '5' < *time_) |
349 | 0 | return FALSE; |
350 | | |
351 | 0 | *offset += 10 * 60 * (*time_++ - '0'); |
352 | |
|
353 | 0 | if (*time_ < '0' || '9' < *time_) |
354 | 0 | return FALSE; |
355 | | |
356 | 0 | *offset += 60 * (*time_++ - '0'); |
357 | |
|
358 | 0 | if (*time_ == '\0') |
359 | 0 | return TRUE; |
360 | | |
361 | 0 | if (*time_ == ':') |
362 | 0 | time_++; |
363 | 0 | else if (rfc8536) |
364 | 0 | return FALSE; |
365 | | |
366 | 0 | if (*time_ < '0' || '5' < *time_) |
367 | 0 | return FALSE; |
368 | | |
369 | 0 | *offset += 10 * (*time_++ - '0'); |
370 | |
|
371 | 0 | if (*time_ < '0' || '9' < *time_) |
372 | 0 | return FALSE; |
373 | | |
374 | 0 | *offset += *time_++ - '0'; |
375 | |
|
376 | 0 | return *time_ == '\0'; |
377 | 0 | } |
378 | | |
379 | | static gboolean |
380 | | parse_constant_offset (const gchar *name, |
381 | | gint32 *offset, |
382 | | gboolean rfc8536) |
383 | 0 | { |
384 | | /* Internet RFC 8536 section 3.3.1 and POSIX 8.3 TZ together say |
385 | | that a transition time must be numeric. */ |
386 | 0 | if (!rfc8536 && g_strcmp0 (name, "UTC") == 0) |
387 | 0 | { |
388 | 0 | *offset = 0; |
389 | 0 | return TRUE; |
390 | 0 | } |
391 | | |
392 | 0 | if (*name >= '0' && '9' >= *name) |
393 | 0 | return parse_time (name, offset, rfc8536); |
394 | | |
395 | 0 | switch (*name++) |
396 | 0 | { |
397 | 0 | case 'Z': |
398 | 0 | *offset = 0; |
399 | | /* Internet RFC 8536 section 3.3.1 requires a numeric zone. */ |
400 | 0 | return !rfc8536 && !*name; |
401 | | |
402 | 0 | case '+': |
403 | 0 | return parse_time (name, offset, rfc8536); |
404 | | |
405 | 0 | case '-': |
406 | 0 | if (parse_time (name, offset, rfc8536)) |
407 | 0 | { |
408 | 0 | *offset = -*offset; |
409 | 0 | return TRUE; |
410 | 0 | } |
411 | 0 | else |
412 | 0 | return FALSE; |
413 | | |
414 | 0 | default: |
415 | 0 | return FALSE; |
416 | 0 | } |
417 | 0 | } |
418 | | |
419 | | static void |
420 | | zone_for_constant_offset (GTimeZone *gtz, const gchar *name) |
421 | 0 | { |
422 | 0 | gint32 offset; |
423 | 0 | TransitionInfo info; |
424 | |
|
425 | 0 | if (name == NULL || !parse_constant_offset (name, &offset, FALSE)) |
426 | 0 | return; |
427 | | |
428 | 0 | info.gmt_offset = offset; |
429 | 0 | info.is_dst = FALSE; |
430 | 0 | info.abbrev = g_strdup (name); |
431 | |
|
432 | 0 | gtz->name = g_strdup (name); |
433 | 0 | gtz->t_info = g_array_sized_new (FALSE, TRUE, sizeof (TransitionInfo), 1); |
434 | 0 | g_array_append_val (gtz->t_info, info); |
435 | | |
436 | | /* Constant offset, no transitions */ |
437 | 0 | gtz->transitions = NULL; |
438 | 0 | } |
439 | | |
440 | | #if defined(G_OS_UNIX) && defined(__sun) && defined(__SVR4) |
441 | | /* |
442 | | * only used by Illumos distros or Solaris < 11: parse the /etc/default/init |
443 | | * text file looking for TZ= followed by the timezone, possibly quoted |
444 | | * |
445 | | */ |
446 | | static gchar * |
447 | | zone_identifier_illumos (void) |
448 | | { |
449 | | gchar *resolved_identifier = NULL; |
450 | | gchar *contents = NULL; |
451 | | const gchar *line_start = NULL; |
452 | | gsize tz_len = 0; |
453 | | |
454 | | if (!g_file_get_contents ("/etc/default/init", &contents, NULL, NULL) ) |
455 | | return NULL; |
456 | | |
457 | | /* is TZ= the first/only line in the file? */ |
458 | | if (strncmp (contents, "TZ=", 3) == 0) |
459 | | { |
460 | | /* found TZ= on the first line, skip over the TZ= */ |
461 | | line_start = contents + 3; |
462 | | } |
463 | | else |
464 | | { |
465 | | /* find a newline followed by TZ= */ |
466 | | line_start = strstr (contents, "\nTZ="); |
467 | | if (line_start != NULL) |
468 | | line_start = line_start + 4; /* skip past the \nTZ= */ |
469 | | } |
470 | | |
471 | | /* |
472 | | * line_start is NULL if we didn't find TZ= at the start of any line, |
473 | | * otherwise it points to what is after the '=' (possibly '\0') |
474 | | */ |
475 | | if (line_start == NULL || *line_start == '\0') |
476 | | return NULL; |
477 | | |
478 | | /* skip past a possible opening " or ' */ |
479 | | if (*line_start == '"' || *line_start == '\'') |
480 | | line_start++; |
481 | | |
482 | | /* |
483 | | * loop over the next few characters, building up the length of |
484 | | * the timezone identifier, ending with end of string, newline or |
485 | | * a " or ' character |
486 | | */ |
487 | | while (*(line_start + tz_len) != '\0' && |
488 | | *(line_start + tz_len) != '\n' && |
489 | | *(line_start + tz_len) != '"' && |
490 | | *(line_start + tz_len) != '\'') |
491 | | tz_len++; |
492 | | |
493 | | if (tz_len > 0) |
494 | | { |
495 | | /* found it */ |
496 | | resolved_identifier = g_strndup (line_start, tz_len); |
497 | | g_strchomp (resolved_identifier); |
498 | | g_free (contents); |
499 | | return g_steal_pointer (&resolved_identifier); |
500 | | } |
501 | | else |
502 | | return NULL; |
503 | | } |
504 | | #endif /* defined(__sun) && defined(__SRVR) */ |
505 | | |
506 | | #ifdef G_OS_UNIX |
507 | | /* |
508 | | * returns the path to the top of the Olson zoneinfo timezone hierarchy. |
509 | | */ |
510 | | static const gchar * |
511 | | zone_info_base_dir (void) |
512 | 0 | { |
513 | 0 | if (g_file_test ("/usr/share/zoneinfo", G_FILE_TEST_IS_DIR)) |
514 | 0 | return "/usr/share/zoneinfo"; /* Most distros */ |
515 | 0 | else if (g_file_test ("/usr/share/lib/zoneinfo", G_FILE_TEST_IS_DIR)) |
516 | 0 | return "/usr/share/lib/zoneinfo"; /* Illumos distros */ |
517 | | |
518 | | /* need a better fallback case */ |
519 | 0 | return "/usr/share/zoneinfo"; |
520 | 0 | } |
521 | | |
522 | | static gchar * |
523 | | zone_identifier_unix (void) |
524 | 0 | { |
525 | 0 | gchar *resolved_identifier = NULL; |
526 | 0 | gsize prefix_len = 0; |
527 | 0 | gchar *canonical_path = NULL; |
528 | 0 | GError *read_link_err = NULL; |
529 | 0 | const gchar *tzdir; |
530 | 0 | gboolean not_a_symlink_to_zoneinfo = FALSE; |
531 | 0 | struct stat file_status; |
532 | | |
533 | | /* Resolve the actual timezone pointed to by /etc/localtime. */ |
534 | 0 | resolved_identifier = g_file_read_link ("/etc/localtime", &read_link_err); |
535 | |
|
536 | 0 | if (resolved_identifier != NULL) |
537 | 0 | { |
538 | 0 | if (!g_path_is_absolute (resolved_identifier)) |
539 | 0 | { |
540 | 0 | gchar *absolute_resolved_identifier = g_build_filename ("/etc", resolved_identifier, NULL); |
541 | 0 | g_free (resolved_identifier); |
542 | 0 | resolved_identifier = g_steal_pointer (&absolute_resolved_identifier); |
543 | 0 | } |
544 | |
|
545 | 0 | if (g_lstat (resolved_identifier, &file_status) == 0) |
546 | 0 | { |
547 | 0 | if ((file_status.st_mode & S_IFMT) != S_IFREG) |
548 | 0 | { |
549 | | /* Some systems (e.g. toolbox containers) make /etc/localtime be a symlink |
550 | | * to a symlink. |
551 | | * |
552 | | * Rather than try to cope with that, just ignore /etc/localtime and use |
553 | | * the fallback code to read timezone from /etc/timezone |
554 | | */ |
555 | 0 | g_clear_pointer (&resolved_identifier, g_free); |
556 | 0 | not_a_symlink_to_zoneinfo = TRUE; |
557 | 0 | } |
558 | 0 | } |
559 | 0 | else |
560 | 0 | { |
561 | 0 | g_clear_pointer (&resolved_identifier, g_free); |
562 | 0 | } |
563 | 0 | } |
564 | 0 | else |
565 | 0 | { |
566 | 0 | not_a_symlink_to_zoneinfo = g_error_matches (read_link_err, |
567 | 0 | G_FILE_ERROR, |
568 | 0 | G_FILE_ERROR_INVAL); |
569 | 0 | g_clear_error (&read_link_err); |
570 | 0 | } |
571 | |
|
572 | 0 | if (resolved_identifier == NULL) |
573 | 0 | { |
574 | | /* if /etc/localtime is not a symlink, try: |
575 | | * - /var/db/zoneinfo : 'tzsetup' program on FreeBSD and |
576 | | * DragonflyBSD stores the timezone chosen by the user there. |
577 | | * - /etc/timezone : Gentoo, OpenRC, and others store |
578 | | * the user choice there. |
579 | | * - call zone_identifier_illumos iff __sun and __SVR4 are defined, |
580 | | * as a last-ditch effort to parse the TZ= setting from within |
581 | | * /etc/default/init |
582 | | */ |
583 | 0 | if (not_a_symlink_to_zoneinfo && (g_file_get_contents ("/var/db/zoneinfo", |
584 | 0 | &resolved_identifier, |
585 | 0 | NULL, NULL) || |
586 | 0 | g_file_get_contents ("/etc/timezone", |
587 | 0 | &resolved_identifier, |
588 | 0 | NULL, NULL) |
589 | | #if defined(__sun) && defined(__SVR4) |
590 | | || |
591 | | (resolved_identifier = zone_identifier_illumos ()) |
592 | | #endif |
593 | 0 | )) |
594 | 0 | g_strchomp (resolved_identifier); |
595 | 0 | else |
596 | 0 | { |
597 | | /* Error */ |
598 | 0 | g_assert (resolved_identifier == NULL); |
599 | 0 | goto out; |
600 | 0 | } |
601 | 0 | } |
602 | 0 | else |
603 | 0 | { |
604 | | /* Resolve relative path */ |
605 | 0 | canonical_path = g_canonicalize_filename (resolved_identifier, "/etc"); |
606 | 0 | g_free (resolved_identifier); |
607 | 0 | resolved_identifier = g_steal_pointer (&canonical_path); |
608 | 0 | } |
609 | | |
610 | 0 | tzdir = g_getenv ("TZDIR"); |
611 | 0 | if (tzdir == NULL) |
612 | 0 | tzdir = zone_info_base_dir (); |
613 | | |
614 | | /* Strip the prefix and slashes if possible. */ |
615 | 0 | if (g_str_has_prefix (resolved_identifier, tzdir)) |
616 | 0 | { |
617 | 0 | prefix_len = strlen (tzdir); |
618 | 0 | while (*(resolved_identifier + prefix_len) == '/') |
619 | 0 | prefix_len++; |
620 | 0 | } |
621 | |
|
622 | 0 | if (prefix_len > 0) |
623 | 0 | memmove (resolved_identifier, resolved_identifier + prefix_len, |
624 | 0 | strlen (resolved_identifier) - prefix_len + 1 /* nul terminator */); |
625 | |
|
626 | 0 | g_assert (resolved_identifier != NULL); |
627 | | |
628 | 0 | out: |
629 | 0 | g_free (canonical_path); |
630 | |
|
631 | 0 | return resolved_identifier; |
632 | 0 | } |
633 | | |
634 | | static GBytes* |
635 | | zone_info_unix (const gchar *identifier, |
636 | | const gchar *resolved_identifier) |
637 | 0 | { |
638 | 0 | gchar *filename = NULL; |
639 | 0 | GMappedFile *file = NULL; |
640 | 0 | GBytes *zoneinfo = NULL; |
641 | 0 | const gchar *tzdir; |
642 | |
|
643 | 0 | tzdir = g_getenv ("TZDIR"); |
644 | 0 | if (tzdir == NULL) |
645 | 0 | tzdir = zone_info_base_dir (); |
646 | | |
647 | | /* identifier can be a relative or absolute path name; |
648 | | if relative, it is interpreted starting from /usr/share/zoneinfo |
649 | | while the POSIX standard says it should start with :, |
650 | | glibc allows both syntaxes, so we should too */ |
651 | 0 | if (identifier != NULL) |
652 | 0 | { |
653 | 0 | if (*identifier == ':') |
654 | 0 | identifier ++; |
655 | |
|
656 | 0 | if (g_path_is_absolute (identifier)) |
657 | 0 | filename = g_strdup (identifier); |
658 | 0 | else |
659 | 0 | filename = g_build_filename (tzdir, identifier, NULL); |
660 | 0 | } |
661 | 0 | else |
662 | 0 | { |
663 | 0 | if (resolved_identifier == NULL) |
664 | 0 | goto out; |
665 | | |
666 | 0 | filename = g_strdup ("/etc/localtime"); |
667 | 0 | } |
668 | | |
669 | 0 | file = g_mapped_file_new (filename, FALSE, NULL); |
670 | 0 | if (file != NULL) |
671 | 0 | { |
672 | 0 | zoneinfo = g_bytes_new_with_free_func (g_mapped_file_get_contents (file), |
673 | 0 | g_mapped_file_get_length (file), |
674 | 0 | (GDestroyNotify)g_mapped_file_unref, |
675 | 0 | g_mapped_file_ref (file)); |
676 | 0 | g_mapped_file_unref (file); |
677 | 0 | } |
678 | |
|
679 | 0 | g_assert (resolved_identifier != NULL); |
680 | | |
681 | 0 | out: |
682 | 0 | g_free (filename); |
683 | |
|
684 | 0 | return zoneinfo; |
685 | 0 | } |
686 | | |
687 | | static void |
688 | | init_zone_from_iana_info (GTimeZone *gtz, |
689 | | GBytes *zoneinfo, |
690 | | gchar *identifier /* (transfer full) */) |
691 | 0 | { |
692 | 0 | gsize size; |
693 | 0 | guint index; |
694 | 0 | guint32 time_count, type_count; |
695 | 0 | guint8 *tz_transitions, *tz_type_index, *tz_ttinfo; |
696 | 0 | guint8 *tz_abbrs; |
697 | 0 | gsize timesize = sizeof (gint32); |
698 | 0 | gconstpointer header_data = g_bytes_get_data (zoneinfo, &size); |
699 | 0 | const gchar *data = header_data; |
700 | 0 | const struct tzhead *header = header_data; |
701 | 0 | GTimeZone *footertz = NULL; |
702 | 0 | guint extra_time_count = 0, extra_type_count = 0; |
703 | 0 | gint64 last_explicit_transition_time = 0; |
704 | |
|
705 | 0 | g_return_if_fail (size >= sizeof (struct tzhead) && |
706 | 0 | memcmp (header, "TZif", 4) == 0); |
707 | | |
708 | | /* FIXME: Handle invalid TZif files better (Issue#1088). */ |
709 | | |
710 | 0 | if (header->tzh_version >= '2') |
711 | 0 | { |
712 | | /* Skip ahead to the newer 64-bit data if it's available. */ |
713 | 0 | header = (const struct tzhead *) |
714 | 0 | (((const gchar *) (header + 1)) + |
715 | 0 | guint32_from_be(header->tzh_ttisgmtcnt) + |
716 | 0 | guint32_from_be(header->tzh_ttisstdcnt) + |
717 | 0 | 8 * guint32_from_be(header->tzh_leapcnt) + |
718 | 0 | 5 * guint32_from_be(header->tzh_timecnt) + |
719 | 0 | 6 * guint32_from_be(header->tzh_typecnt) + |
720 | 0 | guint32_from_be(header->tzh_charcnt)); |
721 | 0 | timesize = sizeof (gint64); |
722 | 0 | } |
723 | 0 | time_count = guint32_from_be(header->tzh_timecnt); |
724 | 0 | type_count = guint32_from_be(header->tzh_typecnt); |
725 | |
|
726 | 0 | if (header->tzh_version >= '2') |
727 | 0 | { |
728 | 0 | const gchar *footer = (((const gchar *) (header + 1)) |
729 | 0 | + guint32_from_be(header->tzh_ttisgmtcnt) |
730 | 0 | + guint32_from_be(header->tzh_ttisstdcnt) |
731 | 0 | + 12 * guint32_from_be(header->tzh_leapcnt) |
732 | 0 | + 9 * time_count |
733 | 0 | + 6 * type_count |
734 | 0 | + guint32_from_be(header->tzh_charcnt)); |
735 | 0 | const gchar *footerlast; |
736 | 0 | size_t footerlen; |
737 | 0 | g_return_if_fail (footer <= data + size - 2 && footer[0] == '\n'); |
738 | 0 | footerlast = memchr (footer + 1, '\n', data + size - (footer + 1)); |
739 | 0 | g_return_if_fail (footerlast); |
740 | 0 | footerlen = footerlast + 1 - footer; |
741 | 0 | if (footerlen != 2) |
742 | 0 | { |
743 | 0 | footertz = parse_footertz (footer, footerlen); |
744 | 0 | g_return_if_fail (footertz); |
745 | 0 | extra_type_count = footertz->t_info->len; |
746 | 0 | extra_time_count = footertz->transitions->len; |
747 | 0 | } |
748 | 0 | } |
749 | | |
750 | 0 | tz_transitions = ((guint8 *) (header) + sizeof (*header)); |
751 | 0 | tz_type_index = tz_transitions + timesize * time_count; |
752 | 0 | tz_ttinfo = tz_type_index + time_count; |
753 | 0 | tz_abbrs = tz_ttinfo + sizeof (struct ttinfo) * type_count; |
754 | |
|
755 | 0 | gtz->name = g_steal_pointer (&identifier); |
756 | 0 | gtz->t_info = g_array_sized_new (FALSE, TRUE, sizeof (TransitionInfo), |
757 | 0 | type_count + extra_type_count); |
758 | 0 | gtz->transitions = g_array_sized_new (FALSE, TRUE, sizeof (Transition), |
759 | 0 | time_count + extra_time_count); |
760 | |
|
761 | 0 | for (index = 0; index < type_count; index++) |
762 | 0 | { |
763 | 0 | TransitionInfo t_info; |
764 | 0 | struct ttinfo info = ((struct ttinfo*)tz_ttinfo)[index]; |
765 | 0 | t_info.gmt_offset = gint32_from_be (info.tt_gmtoff); |
766 | 0 | t_info.is_dst = info.tt_isdst ? TRUE : FALSE; |
767 | 0 | t_info.abbrev = g_strdup ((gchar *) &tz_abbrs[info.tt_abbrind]); |
768 | 0 | g_array_append_val (gtz->t_info, t_info); |
769 | 0 | } |
770 | |
|
771 | 0 | for (index = 0; index < time_count; index++) |
772 | 0 | { |
773 | 0 | Transition trans; |
774 | 0 | if (header->tzh_version >= '2') |
775 | 0 | trans.time = gint64_from_be (((gint64_be*)tz_transitions)[index]); |
776 | 0 | else |
777 | 0 | trans.time = gint32_from_be (((gint32_be*)tz_transitions)[index]); |
778 | 0 | last_explicit_transition_time = trans.time; |
779 | 0 | trans.info_index = tz_type_index[index]; |
780 | 0 | g_assert (trans.info_index >= 0); |
781 | 0 | g_assert ((guint) trans.info_index < gtz->t_info->len); |
782 | 0 | g_array_append_val (gtz->transitions, trans); |
783 | 0 | } |
784 | | |
785 | 0 | if (footertz) |
786 | 0 | { |
787 | | /* Append footer time types. Don't bother to coalesce |
788 | | duplicates with existing time types. */ |
789 | 0 | for (index = 0; index < extra_type_count; index++) |
790 | 0 | { |
791 | 0 | TransitionInfo t_info; |
792 | 0 | TransitionInfo *footer_t_info |
793 | 0 | = &g_array_index (footertz->t_info, TransitionInfo, index); |
794 | 0 | t_info.gmt_offset = footer_t_info->gmt_offset; |
795 | 0 | t_info.is_dst = footer_t_info->is_dst; |
796 | 0 | t_info.abbrev = g_steal_pointer (&footer_t_info->abbrev); |
797 | 0 | g_array_append_val (gtz->t_info, t_info); |
798 | 0 | } |
799 | | |
800 | | /* Append footer transitions that follow the last explicit |
801 | | transition. */ |
802 | 0 | for (index = 0; index < extra_time_count; index++) |
803 | 0 | { |
804 | 0 | Transition *footer_transition |
805 | 0 | = &g_array_index (footertz->transitions, Transition, index); |
806 | 0 | if (time_count <= 0 |
807 | 0 | || last_explicit_transition_time < footer_transition->time) |
808 | 0 | { |
809 | 0 | Transition trans; |
810 | 0 | trans.time = footer_transition->time; |
811 | 0 | trans.info_index = type_count + footer_transition->info_index; |
812 | 0 | g_array_append_val (gtz->transitions, trans); |
813 | 0 | } |
814 | 0 | } |
815 | |
|
816 | 0 | g_time_zone_unref (footertz); |
817 | 0 | } |
818 | 0 | } |
819 | | |
820 | | #elif defined (G_OS_WIN32) |
821 | | |
822 | | static void |
823 | | copy_windows_systemtime (SYSTEMTIME *s_time, TimeZoneDate *tzdate) |
824 | | { |
825 | | tzdate->offset |
826 | | = s_time->wHour * 3600 + s_time->wMinute * 60 + s_time->wSecond; |
827 | | tzdate->mon = s_time->wMonth; |
828 | | tzdate->year = s_time->wYear; |
829 | | tzdate->wday = s_time->wDayOfWeek ? s_time->wDayOfWeek : 7; |
830 | | |
831 | | if (s_time->wYear) |
832 | | { |
833 | | tzdate->mday = s_time->wDay; |
834 | | tzdate->wday = 0; |
835 | | } |
836 | | else |
837 | | tzdate->week = s_time->wDay; |
838 | | } |
839 | | |
840 | | /* UTC = local time + bias while local time = UTC + offset */ |
841 | | static gboolean |
842 | | rule_from_windows_time_zone_info (TimeZoneRule *rule, |
843 | | TIME_ZONE_INFORMATION *tzi) |
844 | | { |
845 | | gchar *std_name, *dlt_name; |
846 | | |
847 | | std_name = g_utf16_to_utf8 ((gunichar2 *)tzi->StandardName, -1, NULL, NULL, NULL); |
848 | | if (std_name == NULL) |
849 | | return FALSE; |
850 | | |
851 | | dlt_name = g_utf16_to_utf8 ((gunichar2 *)tzi->DaylightName, -1, NULL, NULL, NULL); |
852 | | if (dlt_name == NULL) |
853 | | { |
854 | | g_free (std_name); |
855 | | return FALSE; |
856 | | } |
857 | | |
858 | | /* Set offset */ |
859 | | if (tzi->StandardDate.wMonth) |
860 | | { |
861 | | rule->std_offset = -(tzi->Bias + tzi->StandardBias) * 60; |
862 | | rule->dlt_offset = -(tzi->Bias + tzi->DaylightBias) * 60; |
863 | | copy_windows_systemtime (&(tzi->DaylightDate), &(rule->dlt_start)); |
864 | | |
865 | | copy_windows_systemtime (&(tzi->StandardDate), &(rule->dlt_end)); |
866 | | } |
867 | | |
868 | | else |
869 | | { |
870 | | rule->std_offset = -tzi->Bias * 60; |
871 | | rule->dlt_start.mon = 0; |
872 | | } |
873 | | strncpy (rule->std_name, std_name, NAME_SIZE - 1); |
874 | | strncpy (rule->dlt_name, dlt_name, NAME_SIZE - 1); |
875 | | |
876 | | g_free (std_name); |
877 | | g_free (dlt_name); |
878 | | |
879 | | return TRUE; |
880 | | } |
881 | | |
882 | | static gchar* |
883 | | windows_default_tzname (void) |
884 | | { |
885 | | const gunichar2 *subkey = |
886 | | L"SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation"; |
887 | | HKEY key; |
888 | | gchar *key_name = NULL; |
889 | | gunichar2 *key_name_w = NULL; |
890 | | if (RegOpenKeyExW (HKEY_LOCAL_MACHINE, subkey, 0, |
891 | | KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) |
892 | | { |
893 | | DWORD size = 0; |
894 | | if (RegQueryValueExW (key, L"TimeZoneKeyName", NULL, NULL, |
895 | | NULL, &size) == ERROR_SUCCESS) |
896 | | { |
897 | | key_name_w = g_malloc ((gint)size); |
898 | | |
899 | | if (key_name_w == NULL || |
900 | | RegQueryValueExW (key, L"TimeZoneKeyName", NULL, NULL, |
901 | | (LPBYTE)key_name_w, &size) != ERROR_SUCCESS) |
902 | | { |
903 | | g_free (key_name_w); |
904 | | key_name = NULL; |
905 | | } |
906 | | else |
907 | | key_name = g_utf16_to_utf8 (key_name_w, -1, NULL, NULL, NULL); |
908 | | } |
909 | | RegCloseKey (key); |
910 | | } |
911 | | return key_name; |
912 | | } |
913 | | |
914 | | typedef struct |
915 | | { |
916 | | LONG Bias; |
917 | | LONG StandardBias; |
918 | | LONG DaylightBias; |
919 | | SYSTEMTIME StandardDate; |
920 | | SYSTEMTIME DaylightDate; |
921 | | } RegTZI; |
922 | | |
923 | | static void |
924 | | system_time_copy (SYSTEMTIME *orig, SYSTEMTIME *target) |
925 | | { |
926 | | g_return_if_fail (orig != NULL); |
927 | | g_return_if_fail (target != NULL); |
928 | | |
929 | | target->wYear = orig->wYear; |
930 | | target->wMonth = orig->wMonth; |
931 | | target->wDayOfWeek = orig->wDayOfWeek; |
932 | | target->wDay = orig->wDay; |
933 | | target->wHour = orig->wHour; |
934 | | target->wMinute = orig->wMinute; |
935 | | target->wSecond = orig->wSecond; |
936 | | target->wMilliseconds = orig->wMilliseconds; |
937 | | } |
938 | | |
939 | | static void |
940 | | register_tzi_to_tzi (RegTZI *reg, TIME_ZONE_INFORMATION *tzi) |
941 | | { |
942 | | g_return_if_fail (reg != NULL); |
943 | | g_return_if_fail (tzi != NULL); |
944 | | tzi->Bias = reg->Bias; |
945 | | system_time_copy (&(reg->StandardDate), &(tzi->StandardDate)); |
946 | | tzi->StandardBias = reg->StandardBias; |
947 | | system_time_copy (&(reg->DaylightDate), &(tzi->DaylightDate)); |
948 | | tzi->DaylightBias = reg->DaylightBias; |
949 | | } |
950 | | |
951 | | static guint |
952 | | rules_from_windows_time_zone (const gchar *identifier, |
953 | | const gchar *resolved_identifier, |
954 | | TimeZoneRule **rules) |
955 | | { |
956 | | HKEY key; |
957 | | gchar *subkey = NULL; |
958 | | gchar *subkey_dynamic = NULL; |
959 | | const gchar *key_name; |
960 | | const gchar *reg_key = |
961 | | "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\"; |
962 | | TIME_ZONE_INFORMATION tzi; |
963 | | DWORD size; |
964 | | guint rules_num = 0; |
965 | | RegTZI regtzi = { 0 }, regtzi_prev; |
966 | | WCHAR winsyspath[MAX_PATH]; |
967 | | gunichar2 *subkey_w, *subkey_dynamic_w; |
968 | | |
969 | | subkey_dynamic_w = NULL; |
970 | | |
971 | | if (GetSystemDirectoryW (winsyspath, MAX_PATH) == 0) |
972 | | return 0; |
973 | | |
974 | | g_assert (rules != NULL); |
975 | | |
976 | | *rules = NULL; |
977 | | key_name = NULL; |
978 | | |
979 | | if (!identifier) |
980 | | key_name = resolved_identifier; |
981 | | else |
982 | | key_name = identifier; |
983 | | |
984 | | if (!key_name) |
985 | | return 0; |
986 | | |
987 | | subkey = g_strconcat (reg_key, key_name, NULL); |
988 | | subkey_w = g_utf8_to_utf16 (subkey, -1, NULL, NULL, NULL); |
989 | | if (subkey_w == NULL) |
990 | | goto utf16_conv_failed; |
991 | | |
992 | | subkey_dynamic = g_strconcat (subkey, "\\Dynamic DST", NULL); |
993 | | subkey_dynamic_w = g_utf8_to_utf16 (subkey_dynamic, -1, NULL, NULL, NULL); |
994 | | if (subkey_dynamic_w == NULL) |
995 | | goto utf16_conv_failed; |
996 | | |
997 | | if (RegOpenKeyExW (HKEY_LOCAL_MACHINE, subkey_w, 0, |
998 | | KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) |
999 | | goto utf16_conv_failed; |
1000 | | |
1001 | | size = sizeof tzi.StandardName; |
1002 | | |
1003 | | /* use RegLoadMUIStringW() to query MUI_Std from the registry if possible, otherwise |
1004 | | fallback to querying Std */ |
1005 | | if (RegLoadMUIStringW (key, L"MUI_Std", tzi.StandardName, |
1006 | | size, &size, 0, winsyspath) != ERROR_SUCCESS) |
1007 | | { |
1008 | | size = sizeof tzi.StandardName; |
1009 | | if (RegQueryValueExW (key, L"Std", NULL, NULL, |
1010 | | (LPBYTE)&(tzi.StandardName), &size) != ERROR_SUCCESS) |
1011 | | goto registry_failed; |
1012 | | } |
1013 | | |
1014 | | size = sizeof tzi.DaylightName; |
1015 | | |
1016 | | /* use RegLoadMUIStringW() to query MUI_Dlt from the registry if possible, otherwise |
1017 | | fallback to querying Dlt */ |
1018 | | if (RegLoadMUIStringW (key, L"MUI_Dlt", tzi.DaylightName, |
1019 | | size, &size, 0, winsyspath) != ERROR_SUCCESS) |
1020 | | { |
1021 | | size = sizeof tzi.DaylightName; |
1022 | | if (RegQueryValueExW (key, L"Dlt", NULL, NULL, |
1023 | | (LPBYTE)&(tzi.DaylightName), &size) != ERROR_SUCCESS) |
1024 | | goto registry_failed; |
1025 | | } |
1026 | | |
1027 | | RegCloseKey (key); |
1028 | | if (RegOpenKeyExW (HKEY_LOCAL_MACHINE, subkey_dynamic_w, 0, |
1029 | | KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) |
1030 | | { |
1031 | | DWORD i, first, last, year; |
1032 | | wchar_t s[12]; |
1033 | | |
1034 | | size = sizeof first; |
1035 | | if (RegQueryValueExW (key, L"FirstEntry", NULL, NULL, |
1036 | | (LPBYTE) &first, &size) != ERROR_SUCCESS) |
1037 | | goto registry_failed; |
1038 | | |
1039 | | size = sizeof last; |
1040 | | if (RegQueryValueExW (key, L"LastEntry", NULL, NULL, |
1041 | | (LPBYTE) &last, &size) != ERROR_SUCCESS) |
1042 | | goto registry_failed; |
1043 | | |
1044 | | rules_num = last - first + 2; |
1045 | | *rules = g_new0 (TimeZoneRule, rules_num); |
1046 | | |
1047 | | for (year = first, i = 0; *rules != NULL && year <= last; year++) |
1048 | | { |
1049 | | gboolean failed = FALSE; |
1050 | | swprintf_s (s, 11, L"%d", year); |
1051 | | |
1052 | | if (!failed) |
1053 | | { |
1054 | | size = sizeof regtzi; |
1055 | | if (RegQueryValueExW (key, s, NULL, NULL, |
1056 | | (LPBYTE) ®tzi, &size) != ERROR_SUCCESS) |
1057 | | failed = TRUE; |
1058 | | } |
1059 | | |
1060 | | if (failed) |
1061 | | { |
1062 | | g_free (*rules); |
1063 | | *rules = NULL; |
1064 | | break; |
1065 | | } |
1066 | | |
1067 | | if (year > first && memcmp (®tzi_prev, ®tzi, sizeof regtzi) == 0) |
1068 | | continue; |
1069 | | else |
1070 | | memcpy (®tzi_prev, ®tzi, sizeof regtzi); |
1071 | | |
1072 | | register_tzi_to_tzi (®tzi, &tzi); |
1073 | | |
1074 | | if (!rule_from_windows_time_zone_info (&(*rules)[i], &tzi)) |
1075 | | { |
1076 | | g_free (*rules); |
1077 | | *rules = NULL; |
1078 | | break; |
1079 | | } |
1080 | | |
1081 | | (*rules)[i++].start_year = year; |
1082 | | } |
1083 | | |
1084 | | rules_num = i + 1; |
1085 | | |
1086 | | registry_failed: |
1087 | | RegCloseKey (key); |
1088 | | } |
1089 | | else if (RegOpenKeyExW (HKEY_LOCAL_MACHINE, subkey_w, 0, |
1090 | | KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) |
1091 | | { |
1092 | | size = sizeof regtzi; |
1093 | | if (RegQueryValueExW (key, L"TZI", NULL, NULL, |
1094 | | (LPBYTE) ®tzi, &size) == ERROR_SUCCESS) |
1095 | | { |
1096 | | rules_num = 2; |
1097 | | *rules = g_new0 (TimeZoneRule, 2); |
1098 | | register_tzi_to_tzi (®tzi, &tzi); |
1099 | | |
1100 | | if (!rule_from_windows_time_zone_info (&(*rules)[0], &tzi)) |
1101 | | { |
1102 | | g_free (*rules); |
1103 | | *rules = NULL; |
1104 | | } |
1105 | | } |
1106 | | |
1107 | | RegCloseKey (key); |
1108 | | } |
1109 | | |
1110 | | utf16_conv_failed: |
1111 | | g_free (subkey_dynamic_w); |
1112 | | g_free (subkey_dynamic); |
1113 | | g_free (subkey_w); |
1114 | | g_free (subkey); |
1115 | | |
1116 | | if (*rules) |
1117 | | { |
1118 | | (*rules)[0].start_year = MIN_TZYEAR; |
1119 | | if ((*rules)[rules_num - 2].start_year < MAX_TZYEAR) |
1120 | | (*rules)[rules_num - 1].start_year = MAX_TZYEAR; |
1121 | | else |
1122 | | (*rules)[rules_num - 1].start_year = (*rules)[rules_num - 2].start_year + 1; |
1123 | | |
1124 | | return rules_num; |
1125 | | } |
1126 | | |
1127 | | return 0; |
1128 | | } |
1129 | | |
1130 | | #endif |
1131 | | |
1132 | | static void |
1133 | | find_relative_date (TimeZoneDate *buffer) |
1134 | 0 | { |
1135 | 0 | guint wday; |
1136 | 0 | GDate date; |
1137 | 0 | g_date_clear (&date, 1); |
1138 | 0 | wday = buffer->wday; |
1139 | | |
1140 | | /* Get last day if last is needed, first day otherwise */ |
1141 | 0 | if (buffer->mon == 13 || buffer->mon == 14) /* Julian Date */ |
1142 | 0 | { |
1143 | 0 | g_date_set_dmy (&date, 1, 1, buffer->year); |
1144 | 0 | if (wday >= 59 && buffer->mon == 13 && g_date_is_leap_year (buffer->year)) |
1145 | 0 | g_date_add_days (&date, wday); |
1146 | 0 | else |
1147 | 0 | g_date_add_days (&date, wday - 1); |
1148 | 0 | buffer->mon = (int) g_date_get_month (&date); |
1149 | 0 | buffer->mday = (int) g_date_get_day (&date); |
1150 | 0 | buffer->wday = 0; |
1151 | 0 | } |
1152 | 0 | else /* M.W.D */ |
1153 | 0 | { |
1154 | 0 | guint days; |
1155 | 0 | guint days_in_month = g_date_get_days_in_month (buffer->mon, buffer->year); |
1156 | 0 | GDateWeekday first_wday; |
1157 | |
|
1158 | 0 | g_date_set_dmy (&date, 1, buffer->mon, buffer->year); |
1159 | 0 | first_wday = g_date_get_weekday (&date); |
1160 | |
|
1161 | 0 | if ((guint) first_wday > wday) |
1162 | 0 | ++(buffer->week); |
1163 | | /* week is 1 <= w <= 5, we need 0-based */ |
1164 | 0 | days = 7 * (buffer->week - 1) + wday - first_wday; |
1165 | | |
1166 | | /* "days" is a 0-based offset from the 1st of the month. |
1167 | | * Adding days == days_in_month would bring us into the next month, |
1168 | | * hence the ">=" instead of just ">". |
1169 | | */ |
1170 | 0 | while (days >= days_in_month) |
1171 | 0 | days -= 7; |
1172 | |
|
1173 | 0 | g_date_add_days (&date, days); |
1174 | |
|
1175 | 0 | buffer->mday = g_date_get_day (&date); |
1176 | 0 | } |
1177 | 0 | } |
1178 | | |
1179 | | /* Offset is previous offset of local time. Returns 0 if month is 0 */ |
1180 | | static gint64 |
1181 | | boundary_for_year (TimeZoneDate *boundary, |
1182 | | gint year, |
1183 | | gint32 offset) |
1184 | 0 | { |
1185 | 0 | TimeZoneDate buffer; |
1186 | 0 | GDate date; |
1187 | 0 | const guint64 unix_epoch_start = 719163L; |
1188 | 0 | const guint64 seconds_per_day = 86400L; |
1189 | |
|
1190 | 0 | if (!boundary->mon) |
1191 | 0 | return 0; |
1192 | 0 | buffer = *boundary; |
1193 | |
|
1194 | 0 | if (boundary->year == 0) |
1195 | 0 | { |
1196 | 0 | buffer.year = year; |
1197 | |
|
1198 | 0 | if (buffer.wday) |
1199 | 0 | find_relative_date (&buffer); |
1200 | 0 | } |
1201 | |
|
1202 | 0 | g_assert (buffer.year == year); |
1203 | 0 | g_date_clear (&date, 1); |
1204 | 0 | g_date_set_dmy (&date, buffer.mday, buffer.mon, buffer.year); |
1205 | 0 | return ((g_date_get_julian (&date) - unix_epoch_start) * seconds_per_day + |
1206 | 0 | buffer.offset - offset); |
1207 | 0 | } |
1208 | | |
1209 | | static void |
1210 | | fill_transition_info_from_rule (TransitionInfo *info, |
1211 | | TimeZoneRule *rule, |
1212 | | gboolean is_dst) |
1213 | 0 | { |
1214 | 0 | gint offset = is_dst ? rule->dlt_offset : rule->std_offset; |
1215 | 0 | gchar *name = is_dst ? rule->dlt_name : rule->std_name; |
1216 | |
|
1217 | 0 | info->gmt_offset = offset; |
1218 | 0 | info->is_dst = is_dst; |
1219 | |
|
1220 | 0 | if (name) |
1221 | 0 | info->abbrev = g_strdup (name); |
1222 | | |
1223 | 0 | else |
1224 | 0 | info->abbrev = g_strdup_printf ("%+03d%02d", |
1225 | 0 | (int) offset / 3600, |
1226 | 0 | (int) abs (offset / 60) % 60); |
1227 | 0 | } |
1228 | | |
1229 | | static void |
1230 | | init_zone_from_rules (GTimeZone *gtz, |
1231 | | TimeZoneRule *rules, |
1232 | | guint rules_num, |
1233 | | gchar *identifier /* (transfer full) */) |
1234 | 0 | { |
1235 | 0 | guint type_count = 0, trans_count = 0, info_index = 0; |
1236 | 0 | guint ri; /* rule index */ |
1237 | 0 | gboolean skip_first_std_trans = TRUE; |
1238 | 0 | gint32 last_offset; |
1239 | |
|
1240 | 0 | type_count = 0; |
1241 | 0 | trans_count = 0; |
1242 | | |
1243 | | /* Last rule only contains max year */ |
1244 | 0 | for (ri = 0; ri < rules_num - 1; ri++) |
1245 | 0 | { |
1246 | 0 | if (rules[ri].dlt_start.mon || rules[ri].dlt_end.mon) |
1247 | 0 | { |
1248 | 0 | guint rulespan = (rules[ri + 1].start_year - rules[ri].start_year); |
1249 | 0 | guint transitions = rules[ri].dlt_start.mon > 0 ? 1 : 0; |
1250 | 0 | transitions += rules[ri].dlt_end.mon > 0 ? 1 : 0; |
1251 | 0 | type_count += rules[ri].dlt_start.mon > 0 ? 2 : 1; |
1252 | 0 | trans_count += transitions * rulespan; |
1253 | 0 | } |
1254 | 0 | else |
1255 | 0 | type_count++; |
1256 | 0 | } |
1257 | |
|
1258 | 0 | gtz->name = g_steal_pointer (&identifier); |
1259 | 0 | gtz->t_info = g_array_sized_new (FALSE, TRUE, sizeof (TransitionInfo), type_count); |
1260 | 0 | gtz->transitions = g_array_sized_new (FALSE, TRUE, sizeof (Transition), trans_count); |
1261 | |
|
1262 | 0 | last_offset = rules[0].std_offset; |
1263 | |
|
1264 | 0 | for (ri = 0; ri < rules_num - 1; ri++) |
1265 | 0 | { |
1266 | 0 | if ((rules[ri].std_offset || rules[ri].dlt_offset) && |
1267 | 0 | rules[ri].dlt_start.mon == 0 && rules[ri].dlt_end.mon == 0) |
1268 | 0 | { |
1269 | 0 | TransitionInfo std_info; |
1270 | | /* Standard */ |
1271 | 0 | fill_transition_info_from_rule (&std_info, &(rules[ri]), FALSE); |
1272 | 0 | g_array_append_val (gtz->t_info, std_info); |
1273 | |
|
1274 | 0 | if (ri > 0 && |
1275 | 0 | ((rules[ri - 1].dlt_start.mon > 12 && |
1276 | 0 | rules[ri - 1].dlt_start.wday > rules[ri - 1].dlt_end.wday) || |
1277 | 0 | rules[ri - 1].dlt_start.mon > rules[ri - 1].dlt_end.mon)) |
1278 | 0 | { |
1279 | | /* The previous rule was a southern hemisphere rule that |
1280 | | starts the year with DST, so we need to add a |
1281 | | transition to return to standard time */ |
1282 | 0 | guint year = rules[ri].start_year; |
1283 | 0 | gint64 std_time = boundary_for_year (&rules[ri].dlt_end, |
1284 | 0 | year, last_offset); |
1285 | 0 | Transition std_trans = {std_time, info_index}; |
1286 | 0 | g_array_append_val (gtz->transitions, std_trans); |
1287 | |
|
1288 | 0 | } |
1289 | 0 | last_offset = rules[ri].std_offset; |
1290 | 0 | ++info_index; |
1291 | 0 | skip_first_std_trans = TRUE; |
1292 | 0 | } |
1293 | 0 | else |
1294 | 0 | { |
1295 | 0 | const guint start_year = rules[ri].start_year; |
1296 | 0 | const guint end_year = rules[ri + 1].start_year; |
1297 | 0 | gboolean dlt_first; |
1298 | 0 | guint year; |
1299 | 0 | TransitionInfo std_info, dlt_info; |
1300 | 0 | if (rules[ri].dlt_start.mon > 12) |
1301 | 0 | dlt_first = rules[ri].dlt_start.wday > rules[ri].dlt_end.wday; |
1302 | 0 | else |
1303 | 0 | dlt_first = rules[ri].dlt_start.mon > rules[ri].dlt_end.mon; |
1304 | | /* Standard rules are always even, because before the first |
1305 | | transition is always standard time, and 0 is even. */ |
1306 | 0 | fill_transition_info_from_rule (&std_info, &(rules[ri]), FALSE); |
1307 | 0 | fill_transition_info_from_rule (&dlt_info, &(rules[ri]), TRUE); |
1308 | |
|
1309 | 0 | g_array_append_val (gtz->t_info, std_info); |
1310 | 0 | g_array_append_val (gtz->t_info, dlt_info); |
1311 | | |
1312 | | /* Transition dates. We hope that a year which ends daylight |
1313 | | time in a southern-hemisphere country (i.e., one that |
1314 | | begins the year in daylight time) will include a rule |
1315 | | which has only a dlt_end. */ |
1316 | 0 | for (year = start_year; year < end_year; year++) |
1317 | 0 | { |
1318 | 0 | gint32 dlt_offset = (dlt_first ? last_offset : |
1319 | 0 | rules[ri].dlt_offset); |
1320 | 0 | gint32 std_offset = (dlt_first ? rules[ri].std_offset : |
1321 | 0 | last_offset); |
1322 | | /* NB: boundary_for_year returns 0 if mon == 0 */ |
1323 | 0 | gint64 std_time = boundary_for_year (&rules[ri].dlt_end, |
1324 | 0 | year, dlt_offset); |
1325 | 0 | gint64 dlt_time = boundary_for_year (&rules[ri].dlt_start, |
1326 | 0 | year, std_offset); |
1327 | 0 | Transition std_trans = {std_time, info_index}; |
1328 | 0 | Transition dlt_trans = {dlt_time, info_index + 1}; |
1329 | 0 | last_offset = (dlt_first ? rules[ri].dlt_offset : |
1330 | 0 | rules[ri].std_offset); |
1331 | 0 | if (dlt_first) |
1332 | 0 | { |
1333 | 0 | if (skip_first_std_trans) |
1334 | 0 | skip_first_std_trans = FALSE; |
1335 | 0 | else if (std_time) |
1336 | 0 | g_array_append_val (gtz->transitions, std_trans); |
1337 | 0 | if (dlt_time) |
1338 | 0 | g_array_append_val (gtz->transitions, dlt_trans); |
1339 | 0 | } |
1340 | 0 | else |
1341 | 0 | { |
1342 | 0 | if (dlt_time) |
1343 | 0 | g_array_append_val (gtz->transitions, dlt_trans); |
1344 | 0 | if (std_time) |
1345 | 0 | g_array_append_val (gtz->transitions, std_trans); |
1346 | 0 | } |
1347 | 0 | } |
1348 | |
|
1349 | 0 | info_index += 2; |
1350 | 0 | } |
1351 | 0 | } |
1352 | 0 | if (ri > 0 && |
1353 | 0 | ((rules[ri - 1].dlt_start.mon > 12 && |
1354 | 0 | rules[ri - 1].dlt_start.wday > rules[ri - 1].dlt_end.wday) || |
1355 | 0 | rules[ri - 1].dlt_start.mon > rules[ri - 1].dlt_end.mon)) |
1356 | 0 | { |
1357 | | /* The previous rule was a southern hemisphere rule that |
1358 | | starts the year with DST, so we need to add a |
1359 | | transition to return to standard time */ |
1360 | 0 | TransitionInfo info; |
1361 | 0 | guint year = rules[ri].start_year; |
1362 | 0 | Transition trans; |
1363 | 0 | fill_transition_info_from_rule (&info, &(rules[ri - 1]), FALSE); |
1364 | 0 | g_array_append_val (gtz->t_info, info); |
1365 | 0 | trans.time = boundary_for_year (&rules[ri - 1].dlt_end, |
1366 | 0 | year, last_offset); |
1367 | 0 | trans.info_index = info_index; |
1368 | 0 | g_array_append_val (gtz->transitions, trans); |
1369 | 0 | } |
1370 | 0 | } |
1371 | | |
1372 | | /* |
1373 | | * parses date[/time] for parsing TZ environment variable |
1374 | | * |
1375 | | * date is either Mm.w.d, Jn or N |
1376 | | * - m is 1 to 12 |
1377 | | * - w is 1 to 5 |
1378 | | * - d is 0 to 6 |
1379 | | * - n is 1 to 365 |
1380 | | * - N is 0 to 365 |
1381 | | * |
1382 | | * time is either h or hh[[:]mm[[[:]ss]]] |
1383 | | * - h[h] is 0 to 24 |
1384 | | * - mm is 00 to 59 |
1385 | | * - ss is 00 to 59 |
1386 | | */ |
1387 | | static gboolean |
1388 | | parse_mwd_boundary (gchar **pos, TimeZoneDate *boundary) |
1389 | 0 | { |
1390 | 0 | gint month, week, day; |
1391 | |
|
1392 | 0 | if (**pos == '\0' || **pos < '0' || '9' < **pos) |
1393 | 0 | return FALSE; |
1394 | | |
1395 | 0 | month = *(*pos)++ - '0'; |
1396 | |
|
1397 | 0 | if ((month == 1 && **pos >= '0' && '2' >= **pos) || |
1398 | 0 | (month == 0 && **pos >= '0' && '9' >= **pos)) |
1399 | 0 | { |
1400 | 0 | month *= 10; |
1401 | 0 | month += *(*pos)++ - '0'; |
1402 | 0 | } |
1403 | |
|
1404 | 0 | if (*(*pos)++ != '.' || month == 0) |
1405 | 0 | return FALSE; |
1406 | | |
1407 | 0 | if (**pos == '\0' || **pos < '1' || '5' < **pos) |
1408 | 0 | return FALSE; |
1409 | | |
1410 | 0 | week = *(*pos)++ - '0'; |
1411 | |
|
1412 | 0 | if (*(*pos)++ != '.') |
1413 | 0 | return FALSE; |
1414 | | |
1415 | 0 | if (**pos == '\0' || **pos < '0' || '6' < **pos) |
1416 | 0 | return FALSE; |
1417 | | |
1418 | 0 | day = *(*pos)++ - '0'; |
1419 | |
|
1420 | 0 | if (!day) |
1421 | 0 | day += 7; |
1422 | |
|
1423 | 0 | boundary->year = 0; |
1424 | 0 | boundary->mon = month; |
1425 | 0 | boundary->week = week; |
1426 | 0 | boundary->wday = day; |
1427 | 0 | return TRUE; |
1428 | 0 | } |
1429 | | |
1430 | | /* |
1431 | | * This parses two slightly different ways of specifying |
1432 | | * the Julian day: |
1433 | | * |
1434 | | * - ignore_leap == TRUE |
1435 | | * |
1436 | | * Jn This specifies the Julian day with n between 1 and 365. Leap days |
1437 | | * are not counted. In this format, February 29 can't be represented; |
1438 | | * February 28 is day 59, and March 1 is always day 60. |
1439 | | * |
1440 | | * - ignore_leap == FALSE |
1441 | | * |
1442 | | * n This specifies the zero-based Julian day with n between 0 and 365. |
1443 | | * February 29 is counted in leap years. |
1444 | | */ |
1445 | | static gboolean |
1446 | | parse_julian_boundary (gchar** pos, TimeZoneDate *boundary, |
1447 | | gboolean ignore_leap) |
1448 | 0 | { |
1449 | 0 | gint day = 0; |
1450 | 0 | GDate date; |
1451 | |
|
1452 | 0 | while (**pos >= '0' && '9' >= **pos) |
1453 | 0 | { |
1454 | 0 | day *= 10; |
1455 | 0 | day += *(*pos)++ - '0'; |
1456 | 0 | } |
1457 | |
|
1458 | 0 | if (ignore_leap) |
1459 | 0 | { |
1460 | 0 | if (day < 1 || 365 < day) |
1461 | 0 | return FALSE; |
1462 | 0 | if (day >= 59) |
1463 | 0 | day++; |
1464 | 0 | } |
1465 | 0 | else |
1466 | 0 | { |
1467 | 0 | if (day < 0 || 365 < day) |
1468 | 0 | return FALSE; |
1469 | | /* GDate wants day in range 1->366 */ |
1470 | 0 | day++; |
1471 | 0 | } |
1472 | | |
1473 | 0 | g_date_clear (&date, 1); |
1474 | 0 | g_date_set_julian (&date, day); |
1475 | 0 | boundary->year = 0; |
1476 | 0 | boundary->mon = (int) g_date_get_month (&date); |
1477 | 0 | boundary->mday = (int) g_date_get_day (&date); |
1478 | 0 | boundary->wday = 0; |
1479 | |
|
1480 | 0 | return TRUE; |
1481 | 0 | } |
1482 | | |
1483 | | static gboolean |
1484 | | parse_tz_boundary (const gchar *identifier, |
1485 | | TimeZoneDate *boundary) |
1486 | 0 | { |
1487 | 0 | gchar *pos; |
1488 | |
|
1489 | 0 | pos = (gchar*)identifier; |
1490 | | /* Month-week-weekday */ |
1491 | 0 | if (*pos == 'M') |
1492 | 0 | { |
1493 | 0 | ++pos; |
1494 | 0 | if (!parse_mwd_boundary (&pos, boundary)) |
1495 | 0 | return FALSE; |
1496 | 0 | } |
1497 | | /* Julian date which ignores Feb 29 in leap years */ |
1498 | 0 | else if (*pos == 'J') |
1499 | 0 | { |
1500 | 0 | ++pos; |
1501 | 0 | if (!parse_julian_boundary (&pos, boundary, TRUE)) |
1502 | 0 | return FALSE ; |
1503 | 0 | } |
1504 | | /* Julian date which counts Feb 29 in leap years */ |
1505 | 0 | else if (*pos >= '0' && '9' >= *pos) |
1506 | 0 | { |
1507 | 0 | if (!parse_julian_boundary (&pos, boundary, FALSE)) |
1508 | 0 | return FALSE; |
1509 | 0 | } |
1510 | 0 | else |
1511 | 0 | return FALSE; |
1512 | | |
1513 | | /* Time */ |
1514 | | |
1515 | 0 | if (*pos == '/') |
1516 | 0 | return parse_constant_offset (pos + 1, &boundary->offset, TRUE); |
1517 | 0 | else |
1518 | 0 | { |
1519 | 0 | boundary->offset = 2 * 60 * 60; |
1520 | 0 | return *pos == '\0'; |
1521 | 0 | } |
1522 | 0 | } |
1523 | | |
1524 | | static guint |
1525 | | create_ruleset_from_rule (TimeZoneRule **rules, TimeZoneRule *rule) |
1526 | 0 | { |
1527 | 0 | *rules = g_new0 (TimeZoneRule, 2); |
1528 | |
|
1529 | 0 | (*rules)[0].start_year = MIN_TZYEAR; |
1530 | 0 | (*rules)[1].start_year = MAX_TZYEAR; |
1531 | |
|
1532 | 0 | (*rules)[0].std_offset = -rule->std_offset; |
1533 | 0 | (*rules)[0].dlt_offset = -rule->dlt_offset; |
1534 | 0 | (*rules)[0].dlt_start = rule->dlt_start; |
1535 | 0 | (*rules)[0].dlt_end = rule->dlt_end; |
1536 | 0 | strcpy ((*rules)[0].std_name, rule->std_name); |
1537 | 0 | strcpy ((*rules)[0].dlt_name, rule->dlt_name); |
1538 | 0 | return 2; |
1539 | 0 | } |
1540 | | |
1541 | | static gboolean |
1542 | | parse_offset (gchar **pos, gint32 *target) |
1543 | 0 | { |
1544 | 0 | gchar *buffer; |
1545 | 0 | gchar *target_pos = *pos; |
1546 | 0 | gboolean ret; |
1547 | |
|
1548 | 0 | while (**pos == '+' || **pos == '-' || **pos == ':' || |
1549 | 0 | (**pos >= '0' && '9' >= **pos)) |
1550 | 0 | ++(*pos); |
1551 | |
|
1552 | 0 | buffer = g_strndup (target_pos, *pos - target_pos); |
1553 | 0 | ret = parse_constant_offset (buffer, target, FALSE); |
1554 | 0 | g_free (buffer); |
1555 | |
|
1556 | 0 | return ret; |
1557 | 0 | } |
1558 | | |
1559 | | static gboolean |
1560 | | parse_identifier_boundary (gchar **pos, TimeZoneDate *target) |
1561 | 0 | { |
1562 | 0 | gchar *buffer; |
1563 | 0 | gchar *target_pos = *pos; |
1564 | 0 | gboolean ret; |
1565 | |
|
1566 | 0 | while (**pos != ',' && **pos != '\0') |
1567 | 0 | ++(*pos); |
1568 | 0 | buffer = g_strndup (target_pos, *pos - target_pos); |
1569 | 0 | ret = parse_tz_boundary (buffer, target); |
1570 | 0 | g_free (buffer); |
1571 | |
|
1572 | 0 | return ret; |
1573 | 0 | } |
1574 | | |
1575 | | static gboolean |
1576 | | set_tz_name (gchar **pos, gchar *buffer, guint size) |
1577 | 0 | { |
1578 | 0 | gboolean quoted = **pos == '<'; |
1579 | 0 | gchar *name_pos = *pos; |
1580 | 0 | guint len; |
1581 | |
|
1582 | 0 | g_assert (size != 0); |
1583 | | |
1584 | 0 | if (quoted) |
1585 | 0 | { |
1586 | 0 | name_pos++; |
1587 | 0 | do |
1588 | 0 | ++(*pos); |
1589 | 0 | while (g_ascii_isalnum (**pos) || **pos == '-' || **pos == '+'); |
1590 | 0 | if (**pos != '>') |
1591 | 0 | return FALSE; |
1592 | 0 | } |
1593 | 0 | else |
1594 | 0 | while (g_ascii_isalpha (**pos)) |
1595 | 0 | ++(*pos); |
1596 | | |
1597 | | /* Name should be three or more characters */ |
1598 | | /* FIXME: Should return FALSE if the name is too long. |
1599 | | This should simplify code later in this function. */ |
1600 | 0 | if (*pos - name_pos < 3) |
1601 | 0 | return FALSE; |
1602 | | |
1603 | 0 | memset (buffer, 0, size); |
1604 | | /* name_pos isn't 0-terminated, so we have to limit the length expressly */ |
1605 | 0 | len = (guint) (*pos - name_pos) > size - 1 ? size - 1 : (guint) (*pos - name_pos); |
1606 | 0 | strncpy (buffer, name_pos, len); |
1607 | 0 | *pos += quoted; |
1608 | 0 | return TRUE; |
1609 | 0 | } |
1610 | | |
1611 | | static gboolean |
1612 | | parse_identifier_boundaries (gchar **pos, TimeZoneRule *tzr) |
1613 | 0 | { |
1614 | 0 | if (*(*pos)++ != ',') |
1615 | 0 | return FALSE; |
1616 | | |
1617 | | /* Start date */ |
1618 | 0 | if (!parse_identifier_boundary (pos, &(tzr->dlt_start)) || *(*pos)++ != ',') |
1619 | 0 | return FALSE; |
1620 | | |
1621 | | /* End date */ |
1622 | 0 | if (!parse_identifier_boundary (pos, &(tzr->dlt_end))) |
1623 | 0 | return FALSE; |
1624 | 0 | return TRUE; |
1625 | 0 | } |
1626 | | |
1627 | | /* |
1628 | | * Creates an array of TimeZoneRule from a TZ environment variable |
1629 | | * type of identifier. Should free rules afterwards |
1630 | | */ |
1631 | | static guint |
1632 | | rules_from_identifier (const gchar *identifier, |
1633 | | TimeZoneRule **rules) |
1634 | 0 | { |
1635 | 0 | gchar *pos; |
1636 | 0 | TimeZoneRule tzr; |
1637 | |
|
1638 | 0 | g_assert (rules != NULL); |
1639 | | |
1640 | 0 | *rules = NULL; |
1641 | |
|
1642 | 0 | if (!identifier) |
1643 | 0 | return 0; |
1644 | | |
1645 | 0 | pos = (gchar*)identifier; |
1646 | 0 | memset (&tzr, 0, sizeof (tzr)); |
1647 | | /* Standard offset */ |
1648 | 0 | if (!(set_tz_name (&pos, tzr.std_name, NAME_SIZE)) || |
1649 | 0 | !parse_offset (&pos, &(tzr.std_offset))) |
1650 | 0 | return 0; |
1651 | | |
1652 | 0 | if (*pos == 0) |
1653 | 0 | { |
1654 | 0 | return create_ruleset_from_rule (rules, &tzr); |
1655 | 0 | } |
1656 | | |
1657 | | /* Format 2 */ |
1658 | 0 | if (!(set_tz_name (&pos, tzr.dlt_name, NAME_SIZE))) |
1659 | 0 | return 0; |
1660 | 0 | parse_offset (&pos, &(tzr.dlt_offset)); |
1661 | 0 | if (tzr.dlt_offset == 0) /* No daylight offset given, assume it's 1 |
1662 | | hour earlier that standard */ |
1663 | 0 | tzr.dlt_offset = tzr.std_offset - 3600; |
1664 | 0 | if (*pos == '\0') |
1665 | | #ifdef G_OS_WIN32 |
1666 | | /* Windows allows us to use the US DST boundaries if they're not given */ |
1667 | | { |
1668 | | guint i, rules_num = 0; |
1669 | | |
1670 | | /* Use US rules, Windows' default is Pacific Standard Time */ |
1671 | | if ((rules_num = rules_from_windows_time_zone ("Pacific Standard Time", |
1672 | | NULL, |
1673 | | rules))) |
1674 | | { |
1675 | | for (i = 0; i < rules_num - 1; i++) |
1676 | | { |
1677 | | (*rules)[i].std_offset = - tzr.std_offset; |
1678 | | (*rules)[i].dlt_offset = - tzr.dlt_offset; |
1679 | | strcpy ((*rules)[i].std_name, tzr.std_name); |
1680 | | strcpy ((*rules)[i].dlt_name, tzr.dlt_name); |
1681 | | } |
1682 | | |
1683 | | return rules_num; |
1684 | | } |
1685 | | else |
1686 | | return 0; |
1687 | | } |
1688 | | #else |
1689 | 0 | return 0; |
1690 | 0 | #endif |
1691 | | /* Start and end required (format 2) */ |
1692 | 0 | if (!parse_identifier_boundaries (&pos, &tzr)) |
1693 | 0 | return 0; |
1694 | | |
1695 | 0 | return create_ruleset_from_rule (rules, &tzr); |
1696 | 0 | } |
1697 | | |
1698 | | #ifdef G_OS_UNIX |
1699 | | static GTimeZone * |
1700 | | parse_footertz (const gchar *footer, size_t footerlen) |
1701 | 0 | { |
1702 | 0 | gchar *tzstring = g_strndup (footer + 1, footerlen - 2); |
1703 | 0 | GTimeZone *footertz = NULL; |
1704 | | |
1705 | | /* FIXME: The allocation for tzstring could be avoided by |
1706 | | passing a gsize identifier_len argument to rules_from_identifier |
1707 | | and changing the code in that function to stop assuming that |
1708 | | identifier is nul-terminated. */ |
1709 | 0 | TimeZoneRule *rules; |
1710 | 0 | guint rules_num = rules_from_identifier (tzstring, &rules); |
1711 | |
|
1712 | 0 | g_free (tzstring); |
1713 | 0 | if (rules_num > 1) |
1714 | 0 | { |
1715 | 0 | footertz = g_slice_new0 (GTimeZone); |
1716 | 0 | init_zone_from_rules (footertz, rules, rules_num, NULL); |
1717 | 0 | footertz->ref_count++; |
1718 | 0 | } |
1719 | 0 | g_free (rules); |
1720 | 0 | return footertz; |
1721 | 0 | } |
1722 | | #endif |
1723 | | |
1724 | | /* Construction {{{1 */ |
1725 | | /** |
1726 | | * g_time_zone_new: |
1727 | | * @identifier: (nullable): a timezone identifier |
1728 | | * |
1729 | | * A version of g_time_zone_new_identifier() which returns the UTC time zone |
1730 | | * if @identifier could not be parsed or loaded. |
1731 | | * |
1732 | | * If you need to check whether @identifier was loaded successfully, use |
1733 | | * g_time_zone_new_identifier(). |
1734 | | * |
1735 | | * Returns: (transfer full) (not nullable): the requested timezone |
1736 | | * Deprecated: 2.68: Use g_time_zone_new_identifier() instead, as it provides |
1737 | | * error reporting. Change your code to handle a potentially %NULL return |
1738 | | * value. |
1739 | | * |
1740 | | * Since: 2.26 |
1741 | | **/ |
1742 | | GTimeZone * |
1743 | | g_time_zone_new (const gchar *identifier) |
1744 | 0 | { |
1745 | 0 | GTimeZone *tz = g_time_zone_new_identifier (identifier); |
1746 | | |
1747 | | /* Always fall back to UTC. */ |
1748 | 0 | if (tz == NULL) |
1749 | 0 | tz = g_time_zone_new_utc (); |
1750 | |
|
1751 | 0 | g_assert (tz != NULL); |
1752 | | |
1753 | 0 | return g_steal_pointer (&tz); |
1754 | 0 | } |
1755 | | |
1756 | | /** |
1757 | | * g_time_zone_new_identifier: |
1758 | | * @identifier: (nullable): a timezone identifier |
1759 | | * |
1760 | | * Creates a #GTimeZone corresponding to @identifier. If @identifier cannot be |
1761 | | * parsed or loaded, %NULL is returned. |
1762 | | * |
1763 | | * @identifier can either be an RFC3339/ISO 8601 time offset or |
1764 | | * something that would pass as a valid value for the `TZ` environment |
1765 | | * variable (including %NULL). |
1766 | | * |
1767 | | * In Windows, @identifier can also be the unlocalized name of a time |
1768 | | * zone for standard time, for example "Pacific Standard Time". |
1769 | | * |
1770 | | * Valid RFC3339 time offsets are `"Z"` (for UTC) or |
1771 | | * `"±hh:mm"`. ISO 8601 additionally specifies |
1772 | | * `"±hhmm"` and `"±hh"`. Offsets are |
1773 | | * time values to be added to Coordinated Universal Time (UTC) to get |
1774 | | * the local time. |
1775 | | * |
1776 | | * In UNIX, the `TZ` environment variable typically corresponds |
1777 | | * to the name of a file in the zoneinfo database, an absolute path to a file |
1778 | | * somewhere else, or a string in |
1779 | | * "std offset [dst [offset],start[/time],end[/time]]" (POSIX) format. |
1780 | | * There are no spaces in the specification. The name of standard |
1781 | | * and daylight savings time zone must be three or more alphabetic |
1782 | | * characters. Offsets are time values to be added to local time to |
1783 | | * get Coordinated Universal Time (UTC) and should be |
1784 | | * `"[±]hh[[:]mm[:ss]]"`. Dates are either |
1785 | | * `"Jn"` (Julian day with n between 1 and 365, leap |
1786 | | * years not counted), `"n"` (zero-based Julian day |
1787 | | * with n between 0 and 365) or `"Mm.w.d"` (day d |
1788 | | * (0 <= d <= 6) of week w (1 <= w <= 5) of month m (1 <= m <= 12), day |
1789 | | * 0 is a Sunday). Times are in local wall clock time, the default is |
1790 | | * 02:00:00. |
1791 | | * |
1792 | | * In Windows, the "tzn[+|–]hh[:mm[:ss]][dzn]" format is used, but also |
1793 | | * accepts POSIX format. The Windows format uses US rules for all time |
1794 | | * zones; daylight savings time is 60 minutes behind the standard time |
1795 | | * with date and time of change taken from Pacific Standard Time. |
1796 | | * Offsets are time values to be added to the local time to get |
1797 | | * Coordinated Universal Time (UTC). |
1798 | | * |
1799 | | * g_time_zone_new_local() calls this function with the value of the |
1800 | | * `TZ` environment variable. This function itself is independent of |
1801 | | * the value of `TZ`, but if @identifier is %NULL then `/etc/localtime` |
1802 | | * will be consulted to discover the correct time zone on UNIX and the |
1803 | | * registry will be consulted or GetTimeZoneInformation() will be used |
1804 | | * to get the local time zone on Windows. |
1805 | | * |
1806 | | * If intervals are not available, only time zone rules from `TZ` |
1807 | | * environment variable or other means, then they will be computed |
1808 | | * from year 1900 to 2037. If the maximum year for the rules is |
1809 | | * available and it is greater than 2037, then it will followed |
1810 | | * instead. |
1811 | | * |
1812 | | * See |
1813 | | * [RFC3339 §5.6](http://tools.ietf.org/html/rfc3339#section-5.6) |
1814 | | * for a precise definition of valid RFC3339 time offsets |
1815 | | * (the `time-offset` expansion) and ISO 8601 for the |
1816 | | * full list of valid time offsets. See |
1817 | | * [The GNU C Library manual](http://www.gnu.org/s/libc/manual/html_node/TZ-Variable.html) |
1818 | | * for an explanation of the possible |
1819 | | * values of the `TZ` environment variable. See |
1820 | | * [Microsoft Time Zone Index Values](http://msdn.microsoft.com/en-us/library/ms912391%28v=winembedded.11%29.aspx) |
1821 | | * for the list of time zones on Windows. |
1822 | | * |
1823 | | * You should release the return value by calling g_time_zone_unref() |
1824 | | * when you are done with it. |
1825 | | * |
1826 | | * Returns: (transfer full) (nullable): the requested timezone, or %NULL on |
1827 | | * failure |
1828 | | * Since: 2.68 |
1829 | | */ |
1830 | | GTimeZone * |
1831 | | g_time_zone_new_identifier (const gchar *identifier) |
1832 | 0 | { |
1833 | 0 | GTimeZone *tz = NULL; |
1834 | 0 | TimeZoneRule *rules; |
1835 | 0 | gint rules_num; |
1836 | 0 | gchar *resolved_identifier = NULL; |
1837 | |
|
1838 | 0 | if (identifier) |
1839 | 0 | { |
1840 | 0 | G_LOCK (time_zones); |
1841 | 0 | if (time_zones == NULL) |
1842 | 0 | time_zones = g_hash_table_new (g_str_hash, g_str_equal); |
1843 | |
|
1844 | 0 | tz = g_hash_table_lookup (time_zones, identifier); |
1845 | 0 | if (tz) |
1846 | 0 | { |
1847 | 0 | g_atomic_int_inc (&tz->ref_count); |
1848 | 0 | G_UNLOCK (time_zones); |
1849 | 0 | return tz; |
1850 | 0 | } |
1851 | 0 | else |
1852 | 0 | resolved_identifier = g_strdup (identifier); |
1853 | 0 | } |
1854 | 0 | else |
1855 | 0 | { |
1856 | 0 | G_LOCK (tz_default); |
1857 | 0 | #ifdef G_OS_UNIX |
1858 | 0 | resolved_identifier = zone_identifier_unix (); |
1859 | | #elif defined (G_OS_WIN32) |
1860 | | resolved_identifier = windows_default_tzname (); |
1861 | | #endif |
1862 | 0 | if (tz_default) |
1863 | 0 | { |
1864 | | /* Flush default if changed. If the identifier couldn’t be resolved, |
1865 | | * we’re going to fall back to UTC eventually, so don’t clear out the |
1866 | | * cache if it’s already UTC. */ |
1867 | 0 | if (!(resolved_identifier == NULL && g_str_equal (tz_default->name, "UTC")) && |
1868 | 0 | g_strcmp0 (tz_default->name, resolved_identifier) != 0) |
1869 | 0 | { |
1870 | 0 | g_clear_pointer (&tz_default, g_time_zone_unref); |
1871 | 0 | } |
1872 | 0 | else |
1873 | 0 | { |
1874 | 0 | tz = g_time_zone_ref (tz_default); |
1875 | 0 | G_UNLOCK (tz_default); |
1876 | |
|
1877 | 0 | g_free (resolved_identifier); |
1878 | 0 | return tz; |
1879 | 0 | } |
1880 | 0 | } |
1881 | 0 | } |
1882 | | |
1883 | 0 | tz = g_slice_new0 (GTimeZone); |
1884 | 0 | tz->ref_count = 0; |
1885 | |
|
1886 | 0 | zone_for_constant_offset (tz, identifier); |
1887 | |
|
1888 | 0 | if (tz->t_info == NULL && |
1889 | 0 | (rules_num = rules_from_identifier (identifier, &rules))) |
1890 | 0 | { |
1891 | 0 | init_zone_from_rules (tz, rules, rules_num, g_steal_pointer (&resolved_identifier)); |
1892 | 0 | g_free (rules); |
1893 | 0 | } |
1894 | |
|
1895 | 0 | if (tz->t_info == NULL) |
1896 | 0 | { |
1897 | 0 | #ifdef G_OS_UNIX |
1898 | 0 | GBytes *zoneinfo = zone_info_unix (identifier, resolved_identifier); |
1899 | 0 | if (zoneinfo != NULL) |
1900 | 0 | { |
1901 | 0 | init_zone_from_iana_info (tz, zoneinfo, g_steal_pointer (&resolved_identifier)); |
1902 | 0 | g_bytes_unref (zoneinfo); |
1903 | 0 | } |
1904 | | #elif defined (G_OS_WIN32) |
1905 | | if ((rules_num = rules_from_windows_time_zone (identifier, |
1906 | | resolved_identifier, |
1907 | | &rules))) |
1908 | | { |
1909 | | init_zone_from_rules (tz, rules, rules_num, g_steal_pointer (&resolved_identifier)); |
1910 | | g_free (rules); |
1911 | | } |
1912 | | #endif |
1913 | 0 | } |
1914 | |
|
1915 | | #if defined (G_OS_WIN32) |
1916 | | if (tz->t_info == NULL) |
1917 | | { |
1918 | | if (identifier == NULL) |
1919 | | { |
1920 | | TIME_ZONE_INFORMATION tzi; |
1921 | | |
1922 | | if (GetTimeZoneInformation (&tzi) != TIME_ZONE_ID_INVALID) |
1923 | | { |
1924 | | rules = g_new0 (TimeZoneRule, 2); |
1925 | | |
1926 | | if (rule_from_windows_time_zone_info (&rules[0], &tzi)) |
1927 | | { |
1928 | | memset (rules[0].std_name, 0, NAME_SIZE); |
1929 | | memset (rules[0].dlt_name, 0, NAME_SIZE); |
1930 | | |
1931 | | rules[0].start_year = MIN_TZYEAR; |
1932 | | rules[1].start_year = MAX_TZYEAR; |
1933 | | |
1934 | | init_zone_from_rules (tz, rules, 2, g_steal_pointer (&resolved_identifier)); |
1935 | | } |
1936 | | |
1937 | | g_free (rules); |
1938 | | } |
1939 | | } |
1940 | | } |
1941 | | #endif |
1942 | |
|
1943 | 0 | g_free (resolved_identifier); |
1944 | | |
1945 | | /* Failed to load the timezone. */ |
1946 | 0 | if (tz->t_info == NULL) |
1947 | 0 | { |
1948 | 0 | g_slice_free (GTimeZone, tz); |
1949 | |
|
1950 | 0 | if (identifier) |
1951 | 0 | G_UNLOCK (time_zones); |
1952 | 0 | else |
1953 | 0 | G_UNLOCK (tz_default); |
1954 | |
|
1955 | 0 | return NULL; |
1956 | 0 | } |
1957 | | |
1958 | 0 | g_assert (tz->name != NULL); |
1959 | 0 | g_assert (tz->t_info != NULL); |
1960 | | |
1961 | 0 | if (identifier) |
1962 | 0 | g_hash_table_insert (time_zones, tz->name, tz); |
1963 | 0 | else if (tz->name) |
1964 | 0 | { |
1965 | | /* Caching reference */ |
1966 | 0 | g_atomic_int_inc (&tz->ref_count); |
1967 | 0 | tz_default = tz; |
1968 | 0 | } |
1969 | |
|
1970 | 0 | g_atomic_int_inc (&tz->ref_count); |
1971 | |
|
1972 | 0 | if (identifier) |
1973 | 0 | G_UNLOCK (time_zones); |
1974 | 0 | else |
1975 | 0 | G_UNLOCK (tz_default); |
1976 | |
|
1977 | 0 | return tz; |
1978 | 0 | } |
1979 | | |
1980 | | /** |
1981 | | * g_time_zone_new_utc: |
1982 | | * |
1983 | | * Creates a #GTimeZone corresponding to UTC. |
1984 | | * |
1985 | | * This is equivalent to calling g_time_zone_new() with a value like |
1986 | | * "Z", "UTC", "+00", etc. |
1987 | | * |
1988 | | * You should release the return value by calling g_time_zone_unref() |
1989 | | * when you are done with it. |
1990 | | * |
1991 | | * Returns: the universal timezone |
1992 | | * |
1993 | | * Since: 2.26 |
1994 | | **/ |
1995 | | GTimeZone * |
1996 | | g_time_zone_new_utc (void) |
1997 | 0 | { |
1998 | 0 | static GTimeZone *utc = NULL; |
1999 | 0 | static gsize initialised; |
2000 | |
|
2001 | 0 | if (g_once_init_enter (&initialised)) |
2002 | 0 | { |
2003 | 0 | utc = g_time_zone_new_identifier ("UTC"); |
2004 | 0 | g_assert (utc != NULL); |
2005 | 0 | g_once_init_leave (&initialised, TRUE); |
2006 | 0 | } |
2007 | | |
2008 | 0 | return g_time_zone_ref (utc); |
2009 | 0 | } |
2010 | | |
2011 | | /** |
2012 | | * g_time_zone_new_local: |
2013 | | * |
2014 | | * Creates a #GTimeZone corresponding to local time. The local time |
2015 | | * zone may change between invocations to this function; for example, |
2016 | | * if the system administrator changes it. |
2017 | | * |
2018 | | * This is equivalent to calling g_time_zone_new() with the value of |
2019 | | * the `TZ` environment variable (including the possibility of %NULL). |
2020 | | * |
2021 | | * You should release the return value by calling g_time_zone_unref() |
2022 | | * when you are done with it. |
2023 | | * |
2024 | | * Returns: the local timezone |
2025 | | * |
2026 | | * Since: 2.26 |
2027 | | **/ |
2028 | | GTimeZone * |
2029 | | g_time_zone_new_local (void) |
2030 | 0 | { |
2031 | 0 | const gchar *tzenv = g_getenv ("TZ"); |
2032 | 0 | GTimeZone *tz; |
2033 | |
|
2034 | 0 | G_LOCK (tz_local); |
2035 | | |
2036 | | /* Is time zone changed and must be flushed? */ |
2037 | 0 | if (tz_local && g_strcmp0 (g_time_zone_get_identifier (tz_local), tzenv)) |
2038 | 0 | g_clear_pointer (&tz_local, g_time_zone_unref); |
2039 | |
|
2040 | 0 | if (tz_local == NULL) |
2041 | 0 | tz_local = g_time_zone_new_identifier (tzenv); |
2042 | 0 | if (tz_local == NULL) |
2043 | 0 | tz_local = g_time_zone_new_utc (); |
2044 | |
|
2045 | 0 | tz = g_time_zone_ref (tz_local); |
2046 | |
|
2047 | 0 | G_UNLOCK (tz_local); |
2048 | |
|
2049 | 0 | return tz; |
2050 | 0 | } |
2051 | | |
2052 | | /** |
2053 | | * g_time_zone_new_offset: |
2054 | | * @seconds: offset to UTC, in seconds |
2055 | | * |
2056 | | * Creates a #GTimeZone corresponding to the given constant offset from UTC, |
2057 | | * in seconds. |
2058 | | * |
2059 | | * This is equivalent to calling g_time_zone_new() with a string in the form |
2060 | | * `[+|-]hh[:mm[:ss]]`. |
2061 | | * |
2062 | | * It is possible for this function to fail if @seconds is too big (greater than |
2063 | | * 24 hours), in which case this function will return the UTC timezone for |
2064 | | * backwards compatibility. To detect failures like this, use |
2065 | | * g_time_zone_new_identifier() directly. |
2066 | | * |
2067 | | * Returns: (transfer full): a timezone at the given offset from UTC, or UTC on |
2068 | | * failure |
2069 | | * Since: 2.58 |
2070 | | */ |
2071 | | GTimeZone * |
2072 | | g_time_zone_new_offset (gint32 seconds) |
2073 | 0 | { |
2074 | 0 | GTimeZone *tz = NULL; |
2075 | 0 | gchar *identifier = NULL; |
2076 | | |
2077 | | /* Seemingly, we should be using @seconds directly to set the |
2078 | | * #TransitionInfo.gmt_offset to avoid all this string building and parsing. |
2079 | | * However, we always need to set the #GTimeZone.name to a constructed |
2080 | | * string anyway, so we might as well reuse its code. |
2081 | | * g_time_zone_new_identifier() should never fail in this situation. */ |
2082 | 0 | identifier = g_strdup_printf ("%c%02u:%02u:%02u", |
2083 | 0 | (seconds >= 0) ? '+' : '-', |
2084 | 0 | (ABS (seconds) / 60) / 60, |
2085 | 0 | (ABS (seconds) / 60) % 60, |
2086 | 0 | ABS (seconds) % 60); |
2087 | 0 | tz = g_time_zone_new_identifier (identifier); |
2088 | |
|
2089 | 0 | if (tz == NULL) |
2090 | 0 | tz = g_time_zone_new_utc (); |
2091 | 0 | else |
2092 | 0 | g_assert (g_time_zone_get_offset (tz, 0) == seconds); |
2093 | | |
2094 | 0 | g_assert (tz != NULL); |
2095 | 0 | g_free (identifier); |
2096 | |
|
2097 | 0 | return tz; |
2098 | 0 | } |
2099 | | |
2100 | 0 | #define TRANSITION(n) g_array_index (tz->transitions, Transition, n) |
2101 | 0 | #define TRANSITION_INFO(n) g_array_index (tz->t_info, TransitionInfo, n) |
2102 | | |
2103 | | /* Internal helpers {{{1 */ |
2104 | | /* NB: Interval 0 is before the first transition, so there's no |
2105 | | * transition structure to point to which TransitionInfo to |
2106 | | * use. Rule-based zones are set up so that TI 0 is always standard |
2107 | | * time (which is what's in effect before Daylight time got started |
2108 | | * in the early 20th century), but IANA tzfiles don't follow that |
2109 | | * convention. The tzfile documentation says to use the first |
2110 | | * standard-time (i.e., non-DST) tinfo, so that's what we do. |
2111 | | */ |
2112 | | inline static const TransitionInfo* |
2113 | | interval_info (GTimeZone *tz, |
2114 | | guint interval) |
2115 | 0 | { |
2116 | 0 | guint index; |
2117 | 0 | g_return_val_if_fail (tz->t_info != NULL, NULL); |
2118 | 0 | if (interval && tz->transitions && interval <= tz->transitions->len) |
2119 | 0 | index = (TRANSITION(interval - 1)).info_index; |
2120 | 0 | else |
2121 | 0 | { |
2122 | 0 | for (index = 0; index < tz->t_info->len; index++) |
2123 | 0 | { |
2124 | 0 | TransitionInfo *tzinfo = &(TRANSITION_INFO(index)); |
2125 | 0 | if (!tzinfo->is_dst) |
2126 | 0 | return tzinfo; |
2127 | 0 | } |
2128 | 0 | index = 0; |
2129 | 0 | } |
2130 | | |
2131 | 0 | return &(TRANSITION_INFO(index)); |
2132 | 0 | } |
2133 | | |
2134 | | inline static gint64 |
2135 | | interval_start (GTimeZone *tz, |
2136 | | guint interval) |
2137 | 0 | { |
2138 | 0 | if (!interval || tz->transitions == NULL || tz->transitions->len == 0) |
2139 | 0 | return G_MININT64; |
2140 | 0 | if (interval > tz->transitions->len) |
2141 | 0 | interval = tz->transitions->len; |
2142 | 0 | return (TRANSITION(interval - 1)).time; |
2143 | 0 | } |
2144 | | |
2145 | | inline static gint64 |
2146 | | interval_end (GTimeZone *tz, |
2147 | | guint interval) |
2148 | 0 | { |
2149 | 0 | if (tz->transitions && interval < tz->transitions->len) |
2150 | 0 | { |
2151 | 0 | gint64 lim = (TRANSITION(interval)).time; |
2152 | 0 | return lim - (lim != G_MININT64); |
2153 | 0 | } |
2154 | 0 | return G_MAXINT64; |
2155 | 0 | } |
2156 | | |
2157 | | inline static gint32 |
2158 | | interval_offset (GTimeZone *tz, |
2159 | | guint interval) |
2160 | 0 | { |
2161 | 0 | g_return_val_if_fail (tz->t_info != NULL, 0); |
2162 | 0 | return interval_info (tz, interval)->gmt_offset; |
2163 | 0 | } |
2164 | | |
2165 | | inline static gboolean |
2166 | | interval_isdst (GTimeZone *tz, |
2167 | | guint interval) |
2168 | 0 | { |
2169 | 0 | g_return_val_if_fail (tz->t_info != NULL, 0); |
2170 | 0 | return interval_info (tz, interval)->is_dst; |
2171 | 0 | } |
2172 | | |
2173 | | |
2174 | | inline static gchar* |
2175 | | interval_abbrev (GTimeZone *tz, |
2176 | | guint interval) |
2177 | 0 | { |
2178 | 0 | g_return_val_if_fail (tz->t_info != NULL, 0); |
2179 | 0 | return interval_info (tz, interval)->abbrev; |
2180 | 0 | } |
2181 | | |
2182 | | inline static gint64 |
2183 | | interval_local_start (GTimeZone *tz, |
2184 | | guint interval) |
2185 | 0 | { |
2186 | 0 | if (interval) |
2187 | 0 | return interval_start (tz, interval) + interval_offset (tz, interval); |
2188 | | |
2189 | 0 | return G_MININT64; |
2190 | 0 | } |
2191 | | |
2192 | | inline static gint64 |
2193 | | interval_local_end (GTimeZone *tz, |
2194 | | guint interval) |
2195 | 0 | { |
2196 | 0 | if (tz->transitions && interval < tz->transitions->len) |
2197 | 0 | return interval_end (tz, interval) + interval_offset (tz, interval); |
2198 | | |
2199 | 0 | return G_MAXINT64; |
2200 | 0 | } |
2201 | | |
2202 | | static gboolean |
2203 | | interval_valid (GTimeZone *tz, |
2204 | | guint interval) |
2205 | 0 | { |
2206 | 0 | if ( tz->transitions == NULL) |
2207 | 0 | return interval == 0; |
2208 | 0 | return interval <= tz->transitions->len; |
2209 | 0 | } |
2210 | | |
2211 | | /* g_time_zone_find_interval() {{{1 */ |
2212 | | |
2213 | | /** |
2214 | | * g_time_zone_adjust_time: |
2215 | | * @tz: a #GTimeZone |
2216 | | * @type: the #GTimeType of @time_ |
2217 | | * @time_: (inout): a pointer to a number of seconds since January 1, 1970 |
2218 | | * |
2219 | | * Finds an interval within @tz that corresponds to the given @time_, |
2220 | | * possibly adjusting @time_ if required to fit into an interval. |
2221 | | * The meaning of @time_ depends on @type. |
2222 | | * |
2223 | | * This function is similar to g_time_zone_find_interval(), with the |
2224 | | * difference that it always succeeds (by making the adjustments |
2225 | | * described below). |
2226 | | * |
2227 | | * In any of the cases where g_time_zone_find_interval() succeeds then |
2228 | | * this function returns the same value, without modifying @time_. |
2229 | | * |
2230 | | * This function may, however, modify @time_ in order to deal with |
2231 | | * non-existent times. If the non-existent local @time_ of 02:30 were |
2232 | | * requested on March 14th 2010 in Toronto then this function would |
2233 | | * adjust @time_ to be 03:00 and return the interval containing the |
2234 | | * adjusted time. |
2235 | | * |
2236 | | * Returns: the interval containing @time_, never -1 |
2237 | | * |
2238 | | * Since: 2.26 |
2239 | | **/ |
2240 | | gint |
2241 | | g_time_zone_adjust_time (GTimeZone *tz, |
2242 | | GTimeType type, |
2243 | | gint64 *time_) |
2244 | 0 | { |
2245 | 0 | guint i, intervals; |
2246 | 0 | gboolean interval_is_dst; |
2247 | |
|
2248 | 0 | if (tz->transitions == NULL) |
2249 | 0 | return 0; |
2250 | | |
2251 | 0 | intervals = tz->transitions->len; |
2252 | | |
2253 | | /* find the interval containing *time UTC |
2254 | | * TODO: this could be binary searched (or better) */ |
2255 | 0 | for (i = 0; i <= intervals; i++) |
2256 | 0 | if (*time_ <= interval_end (tz, i)) |
2257 | 0 | break; |
2258 | |
|
2259 | 0 | g_assert (interval_start (tz, i) <= *time_ && *time_ <= interval_end (tz, i)); |
2260 | | |
2261 | 0 | if (type != G_TIME_TYPE_UNIVERSAL) |
2262 | 0 | { |
2263 | 0 | if (*time_ < interval_local_start (tz, i)) |
2264 | | /* if time came before the start of this interval... */ |
2265 | 0 | { |
2266 | 0 | i--; |
2267 | | |
2268 | | /* if it's not in the previous interval... */ |
2269 | 0 | if (*time_ > interval_local_end (tz, i)) |
2270 | 0 | { |
2271 | | /* it doesn't exist. fast-forward it. */ |
2272 | 0 | i++; |
2273 | 0 | *time_ = interval_local_start (tz, i); |
2274 | 0 | } |
2275 | 0 | } |
2276 | | |
2277 | 0 | else if (*time_ > interval_local_end (tz, i)) |
2278 | | /* if time came after the end of this interval... */ |
2279 | 0 | { |
2280 | 0 | i++; |
2281 | | |
2282 | | /* if it's not in the next interval... */ |
2283 | 0 | if (*time_ < interval_local_start (tz, i)) |
2284 | | /* it doesn't exist. fast-forward it. */ |
2285 | 0 | *time_ = interval_local_start (tz, i); |
2286 | 0 | } |
2287 | | |
2288 | 0 | else |
2289 | 0 | { |
2290 | 0 | interval_is_dst = interval_isdst (tz, i); |
2291 | 0 | if ((interval_is_dst && type != G_TIME_TYPE_DAYLIGHT) || |
2292 | 0 | (!interval_is_dst && type == G_TIME_TYPE_DAYLIGHT)) |
2293 | 0 | { |
2294 | | /* it's in this interval, but dst flag doesn't match. |
2295 | | * check neighbours for a better fit. */ |
2296 | 0 | if (i && *time_ <= interval_local_end (tz, i - 1)) |
2297 | 0 | i--; |
2298 | | |
2299 | 0 | else if (i < intervals && |
2300 | 0 | *time_ >= interval_local_start (tz, i + 1)) |
2301 | 0 | i++; |
2302 | 0 | } |
2303 | 0 | } |
2304 | 0 | } |
2305 | |
|
2306 | 0 | return i; |
2307 | 0 | } |
2308 | | |
2309 | | /** |
2310 | | * g_time_zone_find_interval: |
2311 | | * @tz: a #GTimeZone |
2312 | | * @type: the #GTimeType of @time_ |
2313 | | * @time_: a number of seconds since January 1, 1970 |
2314 | | * |
2315 | | * Finds an interval within @tz that corresponds to the given @time_. |
2316 | | * The meaning of @time_ depends on @type. |
2317 | | * |
2318 | | * If @type is %G_TIME_TYPE_UNIVERSAL then this function will always |
2319 | | * succeed (since universal time is monotonic and continuous). |
2320 | | * |
2321 | | * Otherwise @time_ is treated as local time. The distinction between |
2322 | | * %G_TIME_TYPE_STANDARD and %G_TIME_TYPE_DAYLIGHT is ignored except in |
2323 | | * the case that the given @time_ is ambiguous. In Toronto, for example, |
2324 | | * 01:30 on November 7th 2010 occurred twice (once inside of daylight |
2325 | | * savings time and the next, an hour later, outside of daylight savings |
2326 | | * time). In this case, the different value of @type would result in a |
2327 | | * different interval being returned. |
2328 | | * |
2329 | | * It is still possible for this function to fail. In Toronto, for |
2330 | | * example, 02:00 on March 14th 2010 does not exist (due to the leap |
2331 | | * forward to begin daylight savings time). -1 is returned in that |
2332 | | * case. |
2333 | | * |
2334 | | * Returns: the interval containing @time_, or -1 in case of failure |
2335 | | * |
2336 | | * Since: 2.26 |
2337 | | */ |
2338 | | gint |
2339 | | g_time_zone_find_interval (GTimeZone *tz, |
2340 | | GTimeType type, |
2341 | | gint64 time_) |
2342 | 0 | { |
2343 | 0 | guint i, intervals; |
2344 | 0 | gboolean interval_is_dst; |
2345 | |
|
2346 | 0 | if (tz->transitions == NULL) |
2347 | 0 | return 0; |
2348 | 0 | intervals = tz->transitions->len; |
2349 | 0 | for (i = 0; i <= intervals; i++) |
2350 | 0 | if (time_ <= interval_end (tz, i)) |
2351 | 0 | break; |
2352 | |
|
2353 | 0 | if (type == G_TIME_TYPE_UNIVERSAL) |
2354 | 0 | return i; |
2355 | | |
2356 | 0 | if (time_ < interval_local_start (tz, i)) |
2357 | 0 | { |
2358 | 0 | if (time_ > interval_local_end (tz, --i)) |
2359 | 0 | return -1; |
2360 | 0 | } |
2361 | | |
2362 | 0 | else if (time_ > interval_local_end (tz, i)) |
2363 | 0 | { |
2364 | 0 | if (time_ < interval_local_start (tz, ++i)) |
2365 | 0 | return -1; |
2366 | 0 | } |
2367 | | |
2368 | 0 | else |
2369 | 0 | { |
2370 | 0 | interval_is_dst = interval_isdst (tz, i); |
2371 | 0 | if ((interval_is_dst && type != G_TIME_TYPE_DAYLIGHT) || |
2372 | 0 | (!interval_is_dst && type == G_TIME_TYPE_DAYLIGHT)) |
2373 | 0 | { |
2374 | 0 | if (i && time_ <= interval_local_end (tz, i - 1)) |
2375 | 0 | i--; |
2376 | | |
2377 | 0 | else if (i < intervals && time_ >= interval_local_start (tz, i + 1)) |
2378 | 0 | i++; |
2379 | 0 | } |
2380 | 0 | } |
2381 | | |
2382 | 0 | return i; |
2383 | 0 | } |
2384 | | |
2385 | | /* Public API accessors {{{1 */ |
2386 | | |
2387 | | /** |
2388 | | * g_time_zone_get_abbreviation: |
2389 | | * @tz: a #GTimeZone |
2390 | | * @interval: an interval within the timezone |
2391 | | * |
2392 | | * Determines the time zone abbreviation to be used during a particular |
2393 | | * @interval of time in the time zone @tz. |
2394 | | * |
2395 | | * For example, in Toronto this is currently "EST" during the winter |
2396 | | * months and "EDT" during the summer months when daylight savings time |
2397 | | * is in effect. |
2398 | | * |
2399 | | * Returns: the time zone abbreviation, which belongs to @tz |
2400 | | * |
2401 | | * Since: 2.26 |
2402 | | **/ |
2403 | | const gchar * |
2404 | | g_time_zone_get_abbreviation (GTimeZone *tz, |
2405 | | gint interval) |
2406 | 0 | { |
2407 | 0 | g_return_val_if_fail (interval_valid (tz, (guint)interval), NULL); |
2408 | | |
2409 | 0 | return interval_abbrev (tz, (guint)interval); |
2410 | 0 | } |
2411 | | |
2412 | | /** |
2413 | | * g_time_zone_get_offset: |
2414 | | * @tz: a #GTimeZone |
2415 | | * @interval: an interval within the timezone |
2416 | | * |
2417 | | * Determines the offset to UTC in effect during a particular @interval |
2418 | | * of time in the time zone @tz. |
2419 | | * |
2420 | | * The offset is the number of seconds that you add to UTC time to |
2421 | | * arrive at local time for @tz (ie: negative numbers for time zones |
2422 | | * west of GMT, positive numbers for east). |
2423 | | * |
2424 | | * Returns: the number of seconds that should be added to UTC to get the |
2425 | | * local time in @tz |
2426 | | * |
2427 | | * Since: 2.26 |
2428 | | **/ |
2429 | | gint32 |
2430 | | g_time_zone_get_offset (GTimeZone *tz, |
2431 | | gint interval) |
2432 | 0 | { |
2433 | 0 | g_return_val_if_fail (interval_valid (tz, (guint)interval), 0); |
2434 | | |
2435 | 0 | return interval_offset (tz, (guint)interval); |
2436 | 0 | } |
2437 | | |
2438 | | /** |
2439 | | * g_time_zone_is_dst: |
2440 | | * @tz: a #GTimeZone |
2441 | | * @interval: an interval within the timezone |
2442 | | * |
2443 | | * Determines if daylight savings time is in effect during a particular |
2444 | | * @interval of time in the time zone @tz. |
2445 | | * |
2446 | | * Returns: %TRUE if daylight savings time is in effect |
2447 | | * |
2448 | | * Since: 2.26 |
2449 | | **/ |
2450 | | gboolean |
2451 | | g_time_zone_is_dst (GTimeZone *tz, |
2452 | | gint interval) |
2453 | 0 | { |
2454 | 0 | g_return_val_if_fail (interval_valid (tz, interval), FALSE); |
2455 | | |
2456 | 0 | if (tz->transitions == NULL) |
2457 | 0 | return FALSE; |
2458 | | |
2459 | 0 | return interval_isdst (tz, (guint)interval); |
2460 | 0 | } |
2461 | | |
2462 | | /** |
2463 | | * g_time_zone_get_identifier: |
2464 | | * @tz: a #GTimeZone |
2465 | | * |
2466 | | * Get the identifier of this #GTimeZone, as passed to g_time_zone_new(). |
2467 | | * If the identifier passed at construction time was not recognised, `UTC` will |
2468 | | * be returned. If it was %NULL, the identifier of the local timezone at |
2469 | | * construction time will be returned. |
2470 | | * |
2471 | | * The identifier will be returned in the same format as provided at |
2472 | | * construction time: if provided as a time offset, that will be returned by |
2473 | | * this function. |
2474 | | * |
2475 | | * Returns: identifier for this timezone |
2476 | | * Since: 2.58 |
2477 | | */ |
2478 | | const gchar * |
2479 | | g_time_zone_get_identifier (GTimeZone *tz) |
2480 | 0 | { |
2481 | 0 | g_return_val_if_fail (tz != NULL, NULL); |
2482 | | |
2483 | 0 | return tz->name; |
2484 | 0 | } |
2485 | | |
2486 | | /* Epilogue {{{1 */ |
2487 | | /* vim:set foldmethod=marker: */ |