Coverage Report

Created: 2025-07-11 06:47

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