Coverage Report

Created: 2023-12-08 07:01

/src/cctz/src/time_zone_info.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2016 Google Inc. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//   https://www.apache.org/licenses/LICENSE-2.0
8
//
9
//   Unless required by applicable law or agreed to in writing, software
10
//   distributed under the License is distributed on an "AS IS" BASIS,
11
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//   See the License for the specific language governing permissions and
13
//   limitations under the License.
14
15
// This file implements the TimeZoneIf interface using the "zoneinfo"
16
// data provided by the IANA Time Zone Database (i.e., the only real game
17
// in town).
18
//
19
// TimeZoneInfo represents the history of UTC-offset changes within a time
20
// zone. Most changes are due to daylight-saving rules, but occasionally
21
// shifts are made to the time-zone's base offset. The database only attempts
22
// to be definitive for times since 1970, so be wary of local-time conversions
23
// before that. Also, rule and zone-boundary changes are made at the whim
24
// of governments, so the conversion of future times needs to be taken with
25
// a grain of salt.
26
//
27
// For more information see tzfile(5), http://www.iana.org/time-zones, or
28
// https://en.wikipedia.org/wiki/Zoneinfo.
29
//
30
// Note that we assume the proleptic Gregorian calendar and 60-second
31
// minutes throughout.
32
33
#include "time_zone_info.h"
34
35
#include <algorithm>
36
#include <cassert>
37
#include <chrono>
38
#include <cstdint>
39
#include <cstdio>
40
#include <cstdlib>
41
#include <cstring>
42
#include <fstream>
43
#include <functional>
44
#include <memory>
45
#include <sstream>
46
#include <string>
47
#include <utility>
48
#include <vector>
49
50
#include "cctz/civil_time.h"
51
#include "time_zone_fixed.h"
52
#include "time_zone_posix.h"
53
54
namespace cctz {
55
56
namespace {
57
58
17.3k
inline bool IsLeap(year_t year) {
59
17.3k
  return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
60
17.3k
}
61
62
// The number of days in non-leap and leap years respectively.
63
const std::int_least32_t kDaysPerYear[2] = {365, 366};
64
65
// The day offsets of the beginning of each (1-based) month in non-leap and
66
// leap years respectively (e.g., 335 days before December in a leap year).
67
const std::int_least16_t kMonthOffsets[2][1 + 12 + 1] = {
68
  {-1, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
69
  {-1, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366},
70
};
71
72
// We reject leap-second encoded zoneinfo and so assume 60-second minutes.
73
const std::int_least32_t kSecsPerDay = 24 * 60 * 60;
74
75
// 400-year chunks always have 146097 days (20871 weeks).
76
const std::int_least64_t kSecsPer400Years = 146097LL * kSecsPerDay;
77
78
// Like kDaysPerYear[] but scaled up by a factor of kSecsPerDay.
79
const std::int_least32_t kSecsPerYear[2] = {
80
  365 * kSecsPerDay,
81
  366 * kSecsPerDay,
82
};
83
84
// Convert a cctz::weekday to a POSIX TZ weekday number (0==Sun, ..., 6=Sat).
85
57
inline int ToPosixWeekday(weekday wd) {
86
57
  switch (wd) {
87
0
    case weekday::sunday:
88
0
      return 0;
89
0
    case weekday::monday:
90
0
      return 1;
91
0
    case weekday::tuesday:
92
0
      return 2;
93
0
    case weekday::wednesday:
94
0
      return 3;
95
57
    case weekday::thursday:
96
57
      return 4;
97
0
    case weekday::friday:
98
0
      return 5;
99
0
    case weekday::saturday:
100
0
      return 6;
101
57
  }
102
0
  return 0; /*NOTREACHED*/
103
57
}
104
105
// Single-byte, unsigned numeric values are encoded directly.
106
94.9k
inline std::uint_fast8_t Decode8(const char* cp) {
107
94.9k
  return static_cast<std::uint_fast8_t>(*cp) & 0xff;
108
94.9k
}
109
110
// Multi-byte, numeric values are encoded using a MSB first,
111
// twos-complement representation. These helpers decode, from
112
// the given address, 4-byte and 8-byte values respectively.
113
// Note: If int_fastXX_t == intXX_t and this machine is not
114
// twos complement, then there will be at least one input value
115
// we cannot represent.
116
1.84k
std::int_fast32_t Decode32(const char* cp) {
117
1.84k
  std::uint_fast32_t v = 0;
118
9.22k
  for (int i = 0; i != (32 / 8); ++i) v = (v << 8) | Decode8(cp++);
119
1.84k
  const std::int_fast32_t s32max = 0x7fffffff;
120
1.84k
  const auto s32maxU = static_cast<std::uint_fast32_t>(s32max);
121
1.84k
  if (v <= s32maxU) return static_cast<std::int_fast32_t>(v);
122
26
  return static_cast<std::int_fast32_t>(v - s32maxU - 1) - s32max - 1;
123
1.84k
}
124
125
9.63k
std::int_fast64_t Decode64(const char* cp) {
126
9.63k
  std::uint_fast64_t v = 0;
127
86.7k
  for (int i = 0; i != (64 / 8); ++i) v = (v << 8) | Decode8(cp++);
128
9.63k
  const std::int_fast64_t s64max = 0x7fffffffffffffff;
129
9.63k
  const auto s64maxU = static_cast<std::uint_fast64_t>(s64max);
130
9.63k
  if (v <= s64maxU) return static_cast<std::int_fast64_t>(v);
131
2.09k
  return static_cast<std::int_fast64_t>(v - s64maxU - 1) - s64max - 1;
132
9.63k
}
133
134
struct Header {            // counts of:
135
  std::size_t timecnt;     // transition times
136
  std::size_t typecnt;     // transition types
137
  std::size_t charcnt;     // zone abbreviation characters
138
  std::size_t leapcnt;     // leap seconds (we expect none)
139
  std::size_t ttisstdcnt;  // UTC/local indicators (unused)
140
  std::size_t ttisutcnt;   // standard/wall indicators (unused)
141
142
  bool Build(const tzhead& tzh);
143
  std::size_t DataLength(std::size_t time_len) const;
144
};
145
146
// Builds the in-memory header using the raw bytes from the file.
147
236
bool Header::Build(const tzhead& tzh) {
148
236
  std::int_fast32_t v;
149
236
  if ((v = Decode32(tzh.tzh_timecnt)) < 0) return false;
150
236
  timecnt = static_cast<std::size_t>(v);
151
236
  if ((v = Decode32(tzh.tzh_typecnt)) < 0) return false;
152
236
  typecnt = static_cast<std::size_t>(v);
153
236
  if ((v = Decode32(tzh.tzh_charcnt)) < 0) return false;
154
236
  charcnt = static_cast<std::size_t>(v);
155
236
  if ((v = Decode32(tzh.tzh_leapcnt)) < 0) return false;
156
236
  leapcnt = static_cast<std::size_t>(v);
157
236
  if ((v = Decode32(tzh.tzh_ttisstdcnt)) < 0) return false;
158
236
  ttisstdcnt = static_cast<std::size_t>(v);
159
236
  if ((v = Decode32(tzh.tzh_ttisutcnt)) < 0) return false;
160
236
  ttisutcnt = static_cast<std::size_t>(v);
161
236
  return true;
162
236
}
163
164
// How many bytes of data are associated with this header. The result
165
// depends upon whether this is a section with 4-byte or 8-byte times.
166
236
std::size_t Header::DataLength(std::size_t time_len) const {
167
236
  std::size_t len = 0;
168
236
  len += (time_len + 1) * timecnt;  // unix_time + type_index
169
236
  len += (4 + 1 + 1) * typecnt;     // utc_offset + is_dst + abbr_index
170
236
  len += 1 * charcnt;               // abbreviations
171
236
  len += (time_len + 4) * leapcnt;  // leap-time + TAI-UTC
172
236
  len += 1 * ttisstdcnt;            // UTC/local indicators
173
236
  len += 1 * ttisutcnt;             // standard/wall indicators
174
236
  return len;
175
236
}
176
177
// Does the rule for future transitions call for year-round daylight time?
178
// See tz/zic.c:stringzone() for the details on how such rules are encoded.
179
57
bool AllYearDST(const PosixTimeZone& posix) {
180
57
  if (posix.dst_start.date.fmt != PosixTransition::N) return false;
181
0
  if (posix.dst_start.date.n.day != 0) return false;
182
0
  if (posix.dst_start.time.offset != 0) return false;
183
184
0
  if (posix.dst_end.date.fmt != PosixTransition::J) return false;
185
0
  if (posix.dst_end.date.j.day != kDaysPerYear[0]) return false;
186
0
  const auto offset = posix.std_offset - posix.dst_offset;
187
0
  if (posix.dst_end.time.offset + offset != kSecsPerDay) return false;
188
189
0
  return true;
190
0
}
191
192
// Generate a year-relative offset for a PosixTransition.
193
std::int_fast64_t TransOffset(bool leap_year, int jan1_weekday,
194
45.8k
                              const PosixTransition& pt) {
195
45.8k
  std::int_fast64_t days = 0;
196
45.8k
  switch (pt.date.fmt) {
197
0
    case PosixTransition::J: {
198
0
      days = pt.date.j.day;
199
0
      if (!leap_year || days < kMonthOffsets[1][3]) days -= 1;
200
0
      break;
201
0
    }
202
0
    case PosixTransition::N: {
203
0
      days = pt.date.n.day;
204
0
      break;
205
0
    }
206
45.8k
    case PosixTransition::M: {
207
45.8k
      const bool last_week = (pt.date.m.week == 5);
208
45.8k
      days = kMonthOffsets[leap_year][pt.date.m.month + last_week];
209
45.8k
      const std::int_fast64_t weekday = (jan1_weekday + days) % 7;
210
45.8k
      if (last_week) {
211
36.5k
        days -= (weekday + 7 - 1 - pt.date.m.weekday) % 7 + 1;
212
36.5k
      } else {
213
9.24k
        days += (pt.date.m.weekday + 7 - weekday) % 7;
214
9.24k
        days += (pt.date.m.week - 1) * 7;
215
9.24k
      }
216
45.8k
      break;
217
0
    }
218
45.8k
  }
219
45.8k
  return (days * kSecsPerDay) + pt.time.offset;
220
45.8k
}
221
222
11.0k
inline time_zone::civil_lookup MakeUnique(const time_point<seconds>& tp) {
223
11.0k
  time_zone::civil_lookup cl;
224
11.0k
  cl.kind = time_zone::civil_lookup::UNIQUE;
225
11.0k
  cl.pre = cl.trans = cl.post = tp;
226
11.0k
  return cl;
227
11.0k
}
228
229
10.7k
inline time_zone::civil_lookup MakeUnique(std::int_fast64_t unix_time) {
230
10.7k
  return MakeUnique(FromUnixSeconds(unix_time));
231
10.7k
}
232
233
inline time_zone::civil_lookup MakeSkipped(const Transition& tr,
234
18
                                           const civil_second& cs) {
235
18
  time_zone::civil_lookup cl;
236
18
  cl.kind = time_zone::civil_lookup::SKIPPED;
237
18
  cl.pre = FromUnixSeconds(tr.unix_time - 1 + (cs - tr.prev_civil_sec));
238
18
  cl.trans = FromUnixSeconds(tr.unix_time);
239
18
  cl.post = FromUnixSeconds(tr.unix_time - (tr.civil_sec - cs));
240
18
  return cl;
241
18
}
242
243
inline time_zone::civil_lookup MakeRepeated(const Transition& tr,
244
41
                                            const civil_second& cs) {
245
41
  time_zone::civil_lookup cl;
246
41
  cl.kind = time_zone::civil_lookup::REPEATED;
247
41
  cl.pre = FromUnixSeconds(tr.unix_time - 1 - (tr.prev_civil_sec - cs));
248
41
  cl.trans = FromUnixSeconds(tr.unix_time);
249
41
  cl.post = FromUnixSeconds(tr.unix_time + (cs - tr.civil_sec));
250
41
  return cl;
251
41
}
252
253
4.21k
inline civil_second YearShift(const civil_second& cs, year_t shift) {
254
4.21k
  return civil_second(cs.year() + shift, cs.month(), cs.day(),
255
4.21k
                      cs.hour(), cs.minute(), cs.second());
256
4.21k
}
257
258
}  // namespace
259
260
// Find/make a transition type with these attributes.
261
bool TimeZoneInfo::GetTransitionType(std::int_fast32_t utc_offset, bool is_dst,
262
                                     const std::string& abbr,
263
175
                                     std::uint_least8_t* index) {
264
175
  std::size_t type_index = 0;
265
175
  std::size_t abbr_index = abbreviations_.size();
266
485
  for (; type_index != transition_types_.size(); ++type_index) {
267
485
    const TransitionType& tt(transition_types_[type_index]);
268
485
    const char* tt_abbr = &abbreviations_[tt.abbr_index];
269
485
    if (tt_abbr == abbr) abbr_index = tt.abbr_index;
270
485
    if (tt.utc_offset == utc_offset && tt.is_dst == is_dst) {
271
178
      if (abbr_index == tt.abbr_index) break;  // reuse
272
178
    }
273
485
  }
274
175
  if (type_index > 255 || abbr_index > 255) {
275
    // No index space (8 bits) available for a new type or abbreviation.
276
0
    return false;
277
0
  }
278
175
  if (type_index == transition_types_.size()) {
279
0
    TransitionType& tt(*transition_types_.emplace(transition_types_.end()));
280
0
    tt.utc_offset = static_cast<std::int_least32_t>(utc_offset);
281
0
    tt.is_dst = is_dst;
282
0
    if (abbr_index == abbreviations_.size()) {
283
0
      abbreviations_.append(abbr);
284
0
      abbreviations_.append(1, '\0');
285
0
    }
286
0
    tt.abbr_index = static_cast<std::uint_least8_t>(abbr_index);
287
0
  }
288
175
  *index = static_cast<std::uint_least8_t>(type_index);
289
175
  return true;
290
175
}
291
292
// zic(8) can generate no-op transitions when a zone changes rules at an
293
// instant when there is actually no discontinuity.  So we check whether
294
// two transitions have equivalent types (same offset/is_dst/abbr).
295
bool TimeZoneInfo::EquivTransitions(std::uint_fast8_t tt1_index,
296
131
                                    std::uint_fast8_t tt2_index) const {
297
131
  if (tt1_index == tt2_index) return true;
298
68
  const TransitionType& tt1(transition_types_[tt1_index]);
299
68
  const TransitionType& tt2(transition_types_[tt2_index]);
300
68
  if (tt1.utc_offset != tt2.utc_offset) return false;
301
2
  if (tt1.is_dst != tt2.is_dst) return false;
302
1
  if (tt1.abbr_index != tt2.abbr_index) return false;
303
1
  return true;
304
1
}
305
306
// Use the POSIX-TZ-environment-variable-style string to handle times
307
// in years after the last transition stored in the zoneinfo data.
308
118
bool TimeZoneInfo::ExtendTransitions() {
309
118
  extended_ = false;
310
118
  if (future_spec_.empty()) return true;  // last transition prevails
311
312
118
  PosixTimeZone posix;
313
118
  if (!ParsePosixSpec(future_spec_, &posix)) return false;
314
315
  // Find transition type for the future std specification.
316
118
  std::uint_least8_t std_ti;
317
118
  if (!GetTransitionType(posix.std_offset, false, posix.std_abbr, &std_ti))
318
0
    return false;
319
320
118
  if (posix.dst_abbr.empty()) {  // std only
321
    // The future specification should match the last transition, and
322
    // that means that handling the future will fall out naturally.
323
61
    return EquivTransitions(transitions_.back().type_index, std_ti);
324
61
  }
325
326
  // Find transition type for the future dst specification.
327
57
  std::uint_least8_t dst_ti;
328
57
  if (!GetTransitionType(posix.dst_offset, true, posix.dst_abbr, &dst_ti))
329
0
    return false;
330
331
57
  if (AllYearDST(posix)) {  // dst only
332
    // The future specification should match the last transition, and
333
    // that means that handling the future will fall out naturally.
334
0
    return EquivTransitions(transitions_.back().type_index, dst_ti);
335
0
  }
336
337
  // Extend the transitions for an additional 401 years using the future
338
  // specification. Years beyond those can be handled by mapping back to
339
  // a cycle-equivalent year within that range. Note that we need 401
340
  // (well, at least the first transition in the 401st year) so that the
341
  // end of the 400th year is mapped back to an extended year. And first
342
  // we may also need two additional transitions for the current year.
343
57
  transitions_.reserve(transitions_.size() + 2 + 401 * 2);
344
57
  extended_ = true;
345
346
57
  const Transition& last(transitions_.back());
347
57
  const std::int_fast64_t last_time = last.unix_time;
348
57
  const TransitionType& last_tt(transition_types_[last.type_index]);
349
57
  last_year_ = LocalTime(last_time, last_tt).cs.year();
350
57
  bool leap_year = IsLeap(last_year_);
351
57
  const civil_second jan1(last_year_);
352
57
  std::int_fast64_t jan1_time = jan1 - civil_second();
353
57
  int jan1_weekday = ToPosixWeekday(get_weekday(jan1));
354
355
57
  Transition dst = {0, dst_ti, civil_second(), civil_second()};
356
57
  Transition std = {0, std_ti, civil_second(), civil_second()};
357
22.9k
  for (const year_t limit = last_year_ + 401;; ++last_year_) {
358
22.9k
    auto dst_trans_off = TransOffset(leap_year, jan1_weekday, posix.dst_start);
359
22.9k
    auto std_trans_off = TransOffset(leap_year, jan1_weekday, posix.dst_end);
360
22.9k
    dst.unix_time = jan1_time + dst_trans_off - posix.std_offset;
361
22.9k
    std.unix_time = jan1_time + std_trans_off - posix.dst_offset;
362
22.9k
    const auto* ta = dst.unix_time < std.unix_time ? &dst : &std;
363
22.9k
    const auto* tb = dst.unix_time < std.unix_time ? &std : &dst;
364
22.9k
    if (last_time < tb->unix_time) {
365
22.8k
      if (last_time < ta->unix_time) transitions_.push_back(*ta);
366
22.8k
      transitions_.push_back(*tb);
367
22.8k
    }
368
22.9k
    if (last_year_ == limit) break;
369
22.8k
    jan1_time += kSecsPerYear[leap_year];
370
22.8k
    jan1_weekday = (jan1_weekday + kDaysPerYear[leap_year]) % 7;
371
22.8k
    leap_year = !leap_year && IsLeap(last_year_ + 1);
372
22.8k
  }
373
374
57
  return true;
375
57
}
376
377
namespace {
378
379
using FilePtr = std::unique_ptr<FILE, int(*)(FILE*)>;
380
381
// fopen(3) adaptor.
382
5.32k
inline FilePtr FOpen(const char* path, const char* mode) {
383
#if defined(_MSC_VER)
384
  FILE* fp;
385
  if (fopen_s(&fp, path, mode) != 0) fp = nullptr;
386
  return FilePtr(fp, fclose);
387
#else
388
  // TODO: Enable the close-on-exec flag.
389
5.32k
  return FilePtr(fopen(path, mode), fclose);
390
5.32k
#endif
391
5.32k
}
392
393
// A stdio(3)-backed implementation of ZoneInfoSource.
394
class FileZoneInfoSource : public ZoneInfoSource {
395
 public:
396
  static std::unique_ptr<ZoneInfoSource> Open(const std::string& name);
397
398
2.42k
  std::size_t Read(void* ptr, std::size_t size) override {
399
2.42k
    size = std::min(size, len_);
400
2.42k
    std::size_t nread = fread(ptr, 1, size, fp_.get());
401
2.42k
    len_ -= nread;
402
2.42k
    return nread;
403
2.42k
  }
404
118
  int Skip(std::size_t offset) override {
405
118
    offset = std::min(offset, len_);
406
118
    int rc = fseek(fp_.get(), static_cast<long>(offset), SEEK_CUR);
407
118
    if (rc == 0) len_ -= offset;
408
118
    return rc;
409
118
  }
410
118
  std::string Version() const override {
411
    // TODO: It would nice if the zoneinfo data included the tzdb version.
412
118
    return std::string();
413
118
  }
414
415
 protected:
416
  explicit FileZoneInfoSource(
417
      FilePtr fp, std::size_t len = std::numeric_limits<std::size_t>::max())
418
151
      : fp_(std::move(fp)), len_(len) {}
419
420
 private:
421
  FilePtr fp_;
422
  std::size_t len_;
423
};
424
425
std::unique_ptr<ZoneInfoSource> FileZoneInfoSource::Open(
426
801
    const std::string& name) {
427
  // Use of the "file:" prefix is intended for testing purposes only.
428
801
  const std::size_t pos = (name.compare(0, 5, "file:") == 0) ? 5 : 0;
429
430
  // Map the time-zone name to a path name.
431
801
  std::string path;
432
801
  if (pos == name.size() || name[pos] != '/') {
433
790
    const char* tzdir = "/usr/share/zoneinfo";
434
790
    char* tzdir_env = nullptr;
435
#if defined(_MSC_VER)
436
    _dupenv_s(&tzdir_env, nullptr, "TZDIR");
437
#else
438
790
    tzdir_env = std::getenv("TZDIR");
439
790
#endif
440
790
    if (tzdir_env && *tzdir_env) tzdir = tzdir_env;
441
790
    path += tzdir;
442
790
    path += '/';
443
#if defined(_MSC_VER)
444
    free(tzdir_env);
445
#endif
446
790
  }
447
801
  path.append(name, pos, std::string::npos);
448
449
  // Open the zoneinfo file.
450
801
  auto fp = FOpen(path.c_str(), "rb");
451
801
  if (fp == nullptr) return nullptr;
452
151
  return std::unique_ptr<ZoneInfoSource>(new FileZoneInfoSource(std::move(fp)));
453
801
}
454
455
class AndroidZoneInfoSource : public FileZoneInfoSource {
456
 public:
457
  static std::unique_ptr<ZoneInfoSource> Open(const std::string& name);
458
0
  std::string Version() const override { return version_; }
459
460
 private:
461
  explicit AndroidZoneInfoSource(FilePtr fp, std::size_t len,
462
                                 std::string version)
463
0
      : FileZoneInfoSource(std::move(fp), len), version_(std::move(version)) {}
464
  std::string version_;
465
};
466
467
std::unique_ptr<ZoneInfoSource> AndroidZoneInfoSource::Open(
468
650
    const std::string& name) {
469
  // Use of the "file:" prefix is intended for testing purposes only.
470
650
  const std::size_t pos = (name.compare(0, 5, "file:") == 0) ? 5 : 0;
471
472
  // See Android's libc/tzcode/bionic.cpp for additional information.
473
650
  for (const char* tzdata : {"/apex/com.android.tzdata/etc/tz/tzdata",
474
650
                             "/data/misc/zoneinfo/current/tzdata",
475
1.95k
                             "/system/usr/share/zoneinfo/tzdata"}) {
476
1.95k
    auto fp = FOpen(tzdata, "rb");
477
1.95k
    if (fp == nullptr) continue;
478
479
0
    char hbuf[24];  // covers header.zonetab_offset too
480
0
    if (fread(hbuf, 1, sizeof(hbuf), fp.get()) != sizeof(hbuf)) continue;
481
0
    if (strncmp(hbuf, "tzdata", 6) != 0) continue;
482
0
    const char* vers = (hbuf[11] == '\0') ? hbuf + 6 : "";
483
0
    const std::int_fast32_t index_offset = Decode32(hbuf + 12);
484
0
    const std::int_fast32_t data_offset = Decode32(hbuf + 16);
485
0
    if (index_offset < 0 || data_offset < index_offset) continue;
486
0
    if (fseek(fp.get(), static_cast<long>(index_offset), SEEK_SET) != 0)
487
0
      continue;
488
489
0
    char ebuf[52];  // covers entry.unused too
490
0
    const std::size_t index_size =
491
0
        static_cast<std::size_t>(data_offset - index_offset);
492
0
    const std::size_t zonecnt = index_size / sizeof(ebuf);
493
0
    if (zonecnt * sizeof(ebuf) != index_size) continue;
494
0
    for (std::size_t i = 0; i != zonecnt; ++i) {
495
0
      if (fread(ebuf, 1, sizeof(ebuf), fp.get()) != sizeof(ebuf)) break;
496
0
      const std::int_fast32_t start = data_offset + Decode32(ebuf + 40);
497
0
      const std::int_fast32_t length = Decode32(ebuf + 44);
498
0
      if (start < 0 || length < 0) break;
499
0
      ebuf[40] = '\0';  // ensure zone name is NUL terminated
500
0
      if (strcmp(name.c_str() + pos, ebuf) == 0) {
501
0
        if (fseek(fp.get(), static_cast<long>(start), SEEK_SET) != 0) break;
502
0
        return std::unique_ptr<ZoneInfoSource>(new AndroidZoneInfoSource(
503
0
            std::move(fp), static_cast<std::size_t>(length), vers));
504
0
      }
505
0
    }
506
0
  }
507
508
650
  return nullptr;
509
650
}
510
511
// A zoneinfo source for use inside Fuchsia components. This attempts to
512
// read zoneinfo files from one of several known paths in a component's
513
// incoming namespace. [Config data][1] is preferred, but package-specific
514
// resources are also supported.
515
//
516
// Fuchsia's implementation supports `FileZoneInfoSource::Version()`.
517
//
518
// [1]: https://fuchsia.dev/fuchsia-src/development/components/data#using_config_data_in_your_component
519
class FuchsiaZoneInfoSource : public FileZoneInfoSource {
520
 public:
521
  static std::unique_ptr<ZoneInfoSource> Open(const std::string& name);
522
0
  std::string Version() const override { return version_; }
523
524
 private:
525
  explicit FuchsiaZoneInfoSource(FilePtr fp, std::string version)
526
0
      : FileZoneInfoSource(std::move(fp)), version_(std::move(version)) {}
527
  std::string version_;
528
};
529
530
std::unique_ptr<ZoneInfoSource> FuchsiaZoneInfoSource::Open(
531
650
    const std::string& name) {
532
  // Use of the "file:" prefix is intended for testing purposes only.
533
650
  const std::size_t pos = (name.compare(0, 5, "file:") == 0) ? 5 : 0;
534
535
  // Prefixes where a Fuchsia component might find zoneinfo files,
536
  // in descending order of preference.
537
650
  const auto kTzdataPrefixes = {
538
      // The tzdata from `config-data`.
539
650
      "/config/data/tzdata/",
540
      // The tzdata bundled in the component's package.
541
650
      "/pkg/data/tzdata/",
542
      // General data storage.
543
650
      "/data/tzdata/",
544
      // The recommended path for routed-in tzdata files.
545
      // See for details:
546
      // https://fuchsia.dev/fuchsia-src/concepts/process/namespaces?hl=en#typical_directory_structure
547
650
      "/config/tzdata/",
548
650
  };
549
650
  const auto kEmptyPrefix = {""};
550
650
  const bool name_absolute = (pos != name.size() && name[pos] == '/');
551
650
  const auto prefixes = name_absolute ? kEmptyPrefix : kTzdataPrefixes;
552
553
  // Fuchsia builds place zoneinfo files at "<prefix><format><name>".
554
2.57k
  for (const std::string prefix : prefixes) {
555
2.57k
    std::string path = prefix;
556
2.57k
    if (!prefix.empty()) path += "zoneinfo/tzif2/";  // format
557
2.57k
    path.append(name, pos, std::string::npos);
558
559
2.57k
    auto fp = FOpen(path.c_str(), "rb");
560
2.57k
    if (fp == nullptr) continue;
561
562
0
    std::string version;
563
0
    if (!prefix.empty()) {
564
      // Fuchsia builds place the version in "<prefix>revision.txt".
565
0
      std::ifstream version_stream(prefix + "revision.txt");
566
0
      if (version_stream.is_open()) {
567
        // revision.txt should contain no newlines, but to be
568
        // defensive we read just the first line.
569
0
        std::getline(version_stream, version);
570
0
      }
571
0
    }
572
573
0
    return std::unique_ptr<ZoneInfoSource>(
574
0
        new FuchsiaZoneInfoSource(std::move(fp), std::move(version)));
575
2.57k
  }
576
577
650
  return nullptr;
578
650
}
579
580
}  // namespace
581
582
// What (no leap-seconds) UTC+seconds zoneinfo would look like.
583
167
bool TimeZoneInfo::ResetToBuiltinUTC(const seconds& offset) {
584
167
  transition_types_.resize(1);
585
167
  TransitionType& tt(transition_types_.back());
586
167
  tt.utc_offset = static_cast<std::int_least32_t>(offset.count());
587
167
  tt.is_dst = false;
588
167
  tt.abbr_index = 0;
589
590
  // We temporarily add some redundant, contemporary (2015 through 2025)
591
  // transitions for performance reasons.  See TimeZoneInfo::LocalTime().
592
  // TODO: Fix the performance issue and remove the extra transitions.
593
167
  transitions_.clear();
594
167
  transitions_.reserve(12);
595
167
  for (const std::int_fast64_t unix_time : {
596
167
           -(1LL << 59),  // a "first half" transition
597
167
           1420070400LL,  // 2015-01-01T00:00:00+00:00
598
167
           1451606400LL,  // 2016-01-01T00:00:00+00:00
599
167
           1483228800LL,  // 2017-01-01T00:00:00+00:00
600
167
           1514764800LL,  // 2018-01-01T00:00:00+00:00
601
167
           1546300800LL,  // 2019-01-01T00:00:00+00:00
602
167
           1577836800LL,  // 2020-01-01T00:00:00+00:00
603
167
           1609459200LL,  // 2021-01-01T00:00:00+00:00
604
167
           1640995200LL,  // 2022-01-01T00:00:00+00:00
605
167
           1672531200LL,  // 2023-01-01T00:00:00+00:00
606
167
           1704067200LL,  // 2024-01-01T00:00:00+00:00
607
167
           1735689600LL,  // 2025-01-01T00:00:00+00:00
608
2.00k
       }) {
609
2.00k
    Transition& tr(*transitions_.emplace(transitions_.end()));
610
2.00k
    tr.unix_time = unix_time;
611
2.00k
    tr.type_index = 0;
612
2.00k
    tr.civil_sec = LocalTime(tr.unix_time, tt).cs;
613
2.00k
    tr.prev_civil_sec = tr.civil_sec - 1;
614
2.00k
  }
615
616
167
  default_transition_type_ = 0;
617
167
  abbreviations_ = FixedOffsetToAbbr(offset);
618
167
  abbreviations_.append(1, '\0');
619
167
  future_spec_.clear();  // never needed for a fixed-offset zone
620
167
  extended_ = false;
621
622
167
  tt.civil_max = LocalTime(seconds::max().count(), tt).cs;
623
167
  tt.civil_min = LocalTime(seconds::min().count(), tt).cs;
624
625
167
  transitions_.shrink_to_fit();
626
167
  return true;
627
167
}
628
629
151
bool TimeZoneInfo::Load(ZoneInfoSource* zip) {
630
  // Read and validate the header.
631
151
  tzhead tzh;
632
151
  if (zip->Read(&tzh, sizeof(tzh)) != sizeof(tzh))
633
32
    return false;
634
119
  if (strncmp(tzh.tzh_magic, TZ_MAGIC, sizeof(tzh.tzh_magic)) != 0)
635
1
    return false;
636
118
  Header hdr;
637
118
  if (!hdr.Build(tzh))
638
0
    return false;
639
118
  std::size_t time_len = 4;
640
118
  if (tzh.tzh_version[0] != '\0') {
641
    // Skip the 4-byte data.
642
118
    if (zip->Skip(hdr.DataLength(time_len)) != 0)
643
0
      return false;
644
    // Read and validate the header for the 8-byte data.
645
118
    if (zip->Read(&tzh, sizeof(tzh)) != sizeof(tzh))
646
0
      return false;
647
118
    if (strncmp(tzh.tzh_magic, TZ_MAGIC, sizeof(tzh.tzh_magic)) != 0)
648
0
      return false;
649
118
    if (tzh.tzh_version[0] == '\0')
650
0
      return false;
651
118
    if (!hdr.Build(tzh))
652
0
      return false;
653
118
    time_len = 8;
654
118
  }
655
118
  if (hdr.typecnt == 0)
656
0
    return false;
657
118
  if (hdr.leapcnt != 0) {
658
    // This code assumes 60-second minutes so we do not want
659
    // the leap-second encoded zoneinfo. We could reverse the
660
    // compensation, but the "right" encoding is rarely used
661
    // so currently we simply reject such data.
662
0
    return false;
663
0
  }
664
118
  if (hdr.ttisstdcnt != 0 && hdr.ttisstdcnt != hdr.typecnt)
665
0
    return false;
666
118
  if (hdr.ttisutcnt != 0 && hdr.ttisutcnt != hdr.typecnt)
667
0
    return false;
668
669
  // Read the data into a local buffer.
670
118
  std::size_t len = hdr.DataLength(time_len);
671
118
  std::vector<char> tbuf(len);
672
118
  if (zip->Read(tbuf.data(), len) != len)
673
0
    return false;
674
118
  const char* bp = tbuf.data();
675
676
  // Decode and validate the transitions.
677
118
  transitions_.reserve(hdr.timecnt + 2);
678
118
  transitions_.resize(hdr.timecnt);
679
9.75k
  for (std::size_t i = 0; i != hdr.timecnt; ++i) {
680
9.63k
    transitions_[i].unix_time = (time_len == 4) ? Decode32(bp) : Decode64(bp);
681
9.63k
    bp += time_len;
682
9.63k
    if (i != 0) {
683
      // Check that the transitions are ordered by time (as zic guarantees).
684
9.57k
      if (!Transition::ByUnixTime()(transitions_[i - 1], transitions_[i]))
685
0
        return false;  // out of order
686
9.57k
    }
687
9.63k
  }
688
118
  bool seen_type_0 = false;
689
9.75k
  for (std::size_t i = 0; i != hdr.timecnt; ++i) {
690
9.63k
    transitions_[i].type_index = Decode8(bp++);
691
9.63k
    if (transitions_[i].type_index >= hdr.typecnt)
692
0
      return false;
693
9.63k
    if (transitions_[i].type_index == 0)
694
1.28k
      seen_type_0 = true;
695
9.63k
  }
696
697
  // Decode and validate the transition types.
698
118
  transition_types_.reserve(hdr.typecnt + 2);
699
118
  transition_types_.resize(hdr.typecnt);
700
546
  for (std::size_t i = 0; i != hdr.typecnt; ++i) {
701
428
    transition_types_[i].utc_offset =
702
428
        static_cast<std::int_least32_t>(Decode32(bp));
703
428
    if (transition_types_[i].utc_offset >= kSecsPerDay ||
704
428
        transition_types_[i].utc_offset <= -kSecsPerDay)
705
0
      return false;
706
428
    bp += 4;
707
428
    transition_types_[i].is_dst = (Decode8(bp++) != 0);
708
428
    transition_types_[i].abbr_index = Decode8(bp++);
709
428
    if (transition_types_[i].abbr_index >= hdr.charcnt)
710
0
      return false;
711
428
  }
712
713
  // Determine the before-first-transition type.
714
118
  default_transition_type_ = 0;
715
118
  if (seen_type_0 && hdr.timecnt != 0) {
716
23
    std::uint_fast8_t index = 0;
717
23
    if (transition_types_[0].is_dst) {
718
0
      index = transitions_[0].type_index;
719
0
      while (index != 0 && transition_types_[index].is_dst)
720
0
        --index;
721
0
    }
722
23
    while (index != hdr.typecnt && transition_types_[index].is_dst)
723
0
      ++index;
724
23
    if (index != hdr.typecnt)
725
23
      default_transition_type_ = index;
726
23
  }
727
728
  // Copy all the abbreviations.
729
118
  abbreviations_.reserve(hdr.charcnt + 10);
730
118
  abbreviations_.assign(bp, hdr.charcnt);
731
118
  bp += hdr.charcnt;
732
733
  // Skip the unused portions. We've already dispensed with leap-second
734
  // encoded zoneinfo. The ttisstd/ttisgmt indicators only apply when
735
  // interpreting a POSIX spec that does not include start/end rules, and
736
  // that isn't the case here (see "zic -p").
737
118
  bp += (time_len + 4) * hdr.leapcnt;  // leap-time + TAI-UTC
738
118
  bp += 1 * hdr.ttisstdcnt;            // UTC/local indicators
739
118
  bp += 1 * hdr.ttisutcnt;             // standard/wall indicators
740
118
  assert(bp == tbuf.data() + tbuf.size());
741
742
0
  future_spec_.clear();
743
118
  if (tzh.tzh_version[0] != '\0') {
744
    // Snarf up the NL-enclosed future POSIX spec. Note
745
    // that version '3' files utilize an extended format.
746
2.03k
    auto get_char = [](ZoneInfoSource* azip) -> int {
747
2.03k
      unsigned char ch;  // all non-EOF results are positive
748
2.03k
      return (azip->Read(&ch, 1) == 1) ? ch : EOF;
749
2.03k
    };
750
118
    if (get_char(zip) != '\n')
751
0
      return false;
752
1.91k
    for (int c = get_char(zip); c != '\n'; c = get_char(zip)) {
753
1.79k
      if (c == EOF)
754
0
        return false;
755
1.79k
      future_spec_.push_back(static_cast<char>(c));
756
1.79k
    }
757
118
  }
758
759
  // We don't check for EOF so that we're forwards compatible.
760
761
  // If we did not find version information during the standard loading
762
  // process (as of tzh_version '3' that is unsupported), then ask the
763
  // ZoneInfoSource for any out-of-bound version string it may be privy to.
764
118
  if (version_.empty()) {
765
118
    version_ = zip->Version();
766
118
  }
767
768
  // Trim redundant transitions. zic may have added these to work around
769
  // differences between the glibc and reference implementations (see
770
  // zic.c:dontmerge) or to avoid bugs in old readers. For us, they just
771
  // get in the way when we do future_spec_ extension.
772
121
  while (hdr.timecnt > 1) {
773
70
    if (!EquivTransitions(transitions_[hdr.timecnt - 1].type_index,
774
70
                          transitions_[hdr.timecnt - 2].type_index)) {
775
67
      break;
776
67
    }
777
3
    hdr.timecnt -= 1;
778
3
  }
779
118
  transitions_.resize(hdr.timecnt);
780
781
  // Ensure that there is always a transition in the first half of the
782
  // time line (the second half is handled below) so that the signed
783
  // difference between a civil_second and the civil_second of its
784
  // previous transition is always representable, without overflow.
785
118
  if (transitions_.empty() || transitions_.front().unix_time >= 0) {
786
72
    Transition& tr(*transitions_.emplace(transitions_.begin()));
787
72
    tr.unix_time = -(1LL << 59);  // -18267312070-10-26T17:01:52+00:00
788
72
    tr.type_index = default_transition_type_;
789
72
  }
790
791
  // Extend the transitions using the future specification.
792
118
  if (!ExtendTransitions()) return false;
793
794
  // Ensure that there is always a transition in the second half of the
795
  // time line (the first half is handled above) so that the signed
796
  // difference between a civil_second and the civil_second of its
797
  // previous transition is always representable, without overflow.
798
118
  const Transition& last(transitions_.back());
799
118
  if (last.unix_time < 0) {
800
51
    const std::uint_fast8_t type_index = last.type_index;
801
51
    Transition& tr(*transitions_.emplace(transitions_.end()));
802
51
    tr.unix_time = 2147483647;  // 2038-01-19T03:14:07+00:00
803
51
    tr.type_index = type_index;
804
51
  }
805
806
  // Compute the local civil time for each transition and the preceding
807
  // second. These will be used for reverse conversions in MakeTime().
808
118
  const TransitionType* ttp = &transition_types_[default_transition_type_];
809
55.5k
  for (std::size_t i = 0; i != transitions_.size(); ++i) {
810
55.4k
    Transition& tr(transitions_[i]);
811
55.4k
    tr.prev_civil_sec = LocalTime(tr.unix_time, *ttp).cs - 1;
812
55.4k
    ttp = &transition_types_[tr.type_index];
813
55.4k
    tr.civil_sec = LocalTime(tr.unix_time, *ttp).cs;
814
55.4k
    if (i != 0) {
815
      // Check that the transitions are ordered by civil time. Essentially
816
      // this means that an offset change cannot cross another such change.
817
      // No one does this in practice, and we depend on it in MakeTime().
818
55.3k
      if (!Transition::ByCivilTime()(transitions_[i - 1], tr))
819
0
        return false;  // out of order
820
55.3k
    }
821
55.4k
  }
822
823
  // Compute the maximum/minimum civil times that can be converted to a
824
  // time_point<seconds> for each of the zone's transition types.
825
428
  for (auto& tt : transition_types_) {
826
428
    tt.civil_max = LocalTime(seconds::max().count(), tt).cs;
827
428
    tt.civil_min = LocalTime(seconds::min().count(), tt).cs;
828
428
  }
829
830
118
  transitions_.shrink_to_fit();
831
118
  return true;
832
118
}
833
834
967
bool TimeZoneInfo::Load(const std::string& name) {
835
  // We can ensure that the loading of UTC or any other fixed-offset
836
  // zone never fails because the simple, fixed-offset state can be
837
  // internally generated. Note that this depends on our choice to not
838
  // accept leap-second encoded ("right") zoneinfo.
839
967
  auto offset = seconds::zero();
840
967
  if (FixedOffsetFromName(name, &offset)) {
841
166
    return ResetToBuiltinUTC(offset);
842
166
  }
843
844
  // Find and use a ZoneInfoSource to load the named zone.
845
801
  auto zip = cctz_extension::zone_info_source_factory(
846
801
      name, [](const std::string& n) -> std::unique_ptr<ZoneInfoSource> {
847
801
        if (auto z = FileZoneInfoSource::Open(n)) return z;
848
650
        if (auto z = AndroidZoneInfoSource::Open(n)) return z;
849
650
        if (auto z = FuchsiaZoneInfoSource::Open(n)) return z;
850
650
        return nullptr;
851
650
      });
852
801
  return zip != nullptr && Load(zip.get());
853
967
}
854
855
1
std::unique_ptr<TimeZoneInfo> TimeZoneInfo::UTC() {
856
1
  auto tz = std::unique_ptr<TimeZoneInfo>(new TimeZoneInfo);
857
1
  tz->ResetToBuiltinUTC(seconds::zero());
858
1
  return tz;
859
1
}
860
861
967
std::unique_ptr<TimeZoneInfo> TimeZoneInfo::Make(const std::string& name) {
862
967
  auto tz = std::unique_ptr<TimeZoneInfo>(new TimeZoneInfo);
863
967
  if (!tz->Load(name)) tz.reset();  // fallback to UTC
864
967
  return tz;
865
967
}
866
867
// BreakTime() translation for a particular transition type.
868
time_zone::absolute_lookup TimeZoneInfo::LocalTime(
869
118k
    std::int_fast64_t unix_time, const TransitionType& tt) const {
870
  // A civil time in "+offset" looks like (time+offset) in UTC.
871
  // Note: We perform two additions in the civil_second domain to
872
  // sidestep the chance of overflow in (unix_time + tt.utc_offset).
873
118k
  return {(civil_second() + unix_time) + tt.utc_offset,
874
118k
          tt.utc_offset, tt.is_dst, &abbreviations_[tt.abbr_index]};
875
118k
}
876
877
// BreakTime() translation for a particular transition.
878
time_zone::absolute_lookup TimeZoneInfo::LocalTime(
879
3.04k
    std::int_fast64_t unix_time, const Transition& tr) const {
880
3.04k
  const TransitionType& tt = transition_types_[tr.type_index];
881
  // Note: (unix_time - tr.unix_time) will never overflow as we
882
  // have ensured that there is always a "nearby" transition.
883
3.04k
  return {tr.civil_sec + (unix_time - tr.unix_time),  // TODO: Optimize.
884
3.04k
          tt.utc_offset, tt.is_dst, &abbreviations_[tt.abbr_index]};
885
3.04k
}
886
887
// MakeTime() translation with a conversion-preserving +N * 400-year shift.
888
time_zone::civil_lookup TimeZoneInfo::TimeLocal(const civil_second& cs,
889
2.19k
                                                year_t c4_shift) const {
890
2.19k
  assert(last_year_ - 400 < cs.year() && cs.year() <= last_year_);
891
0
  time_zone::civil_lookup cl = MakeTime(cs);
892
2.19k
  if (c4_shift > seconds::max().count() / kSecsPer400Years) {
893
182
    cl.pre = cl.trans = cl.post = time_point<seconds>::max();
894
2.01k
  } else {
895
2.01k
    const auto offset = seconds(c4_shift * kSecsPer400Years);
896
2.01k
    const auto limit = time_point<seconds>::max() - offset;
897
6.05k
    for (auto* tp : {&cl.pre, &cl.trans, &cl.post}) {
898
6.05k
      if (*tp > limit) {
899
12
        *tp = time_point<seconds>::max();
900
6.03k
      } else {
901
6.03k
        *tp += offset;
902
6.03k
      }
903
6.05k
    }
904
2.01k
  }
905
2.19k
  return cl;
906
2.19k
}
907
908
time_zone::absolute_lookup TimeZoneInfo::BreakTime(
909
9.12k
    const time_point<seconds>& tp) const {
910
9.12k
  std::int_fast64_t unix_time = ToUnixSeconds(tp);
911
9.12k
  const std::size_t timecnt = transitions_.size();
912
9.12k
  assert(timecnt != 0);  // We always add a transition.
913
914
9.12k
  if (unix_time < transitions_[0].unix_time) {
915
4.06k
    return LocalTime(unix_time, transition_types_[default_transition_type_]);
916
4.06k
  }
917
5.06k
  if (unix_time >= transitions_[timecnt - 1].unix_time) {
918
    // After the last transition. If we extended the transitions using
919
    // future_spec_, shift back to a supported year using the 400-year
920
    // cycle of calendaric equivalence and then compensate accordingly.
921
2.25k
    if (extended_) {
922
2.01k
      const std::int_fast64_t diff =
923
2.01k
          unix_time - transitions_[timecnt - 1].unix_time;
924
2.01k
      const year_t shift = diff / kSecsPer400Years + 1;
925
2.01k
      const auto d = seconds(shift * kSecsPer400Years);
926
2.01k
      time_zone::absolute_lookup al = BreakTime(tp - d);
927
2.01k
      al.cs = YearShift(al.cs, shift * 400);
928
2.01k
      return al;
929
2.01k
    }
930
238
    return LocalTime(unix_time, transitions_[timecnt - 1]);
931
2.25k
  }
932
933
2.80k
  const std::size_t hint = local_time_hint_.load(std::memory_order_relaxed);
934
2.80k
  if (0 < hint && hint < timecnt) {
935
2.59k
    if (transitions_[hint - 1].unix_time <= unix_time) {
936
1.56k
      if (unix_time < transitions_[hint].unix_time) {
937
551
        return LocalTime(unix_time, transitions_[hint - 1]);
938
551
      }
939
1.56k
    }
940
2.59k
  }
941
942
2.25k
  const Transition target = {unix_time, 0, civil_second(), civil_second()};
943
2.25k
  const Transition* begin = &transitions_[0];
944
2.25k
  const Transition* tr = std::upper_bound(begin, begin + timecnt, target,
945
2.25k
                                          Transition::ByUnixTime());
946
2.25k
  local_time_hint_.store(static_cast<std::size_t>(tr - begin),
947
2.25k
                         std::memory_order_relaxed);
948
2.25k
  return LocalTime(unix_time, *--tr);
949
2.80k
}
950
951
13.2k
time_zone::civil_lookup TimeZoneInfo::MakeTime(const civil_second& cs) const {
952
13.2k
  const std::size_t timecnt = transitions_.size();
953
13.2k
  assert(timecnt != 0);  // We always add a transition.
954
955
  // Find the first transition after our target civil time.
956
0
  const Transition* tr = nullptr;
957
13.2k
  const Transition* begin = &transitions_[0];
958
13.2k
  const Transition* end = begin + timecnt;
959
13.2k
  if (cs < begin->civil_sec) {
960
4.41k
    tr = begin;
961
8.85k
  } else if (cs >= transitions_[timecnt - 1].civil_sec) {
962
2.57k
    tr = end;
963
6.28k
  } else {
964
6.28k
    const std::size_t hint = time_local_hint_.load(std::memory_order_relaxed);
965
6.28k
    if (0 < hint && hint < timecnt) {
966
6.02k
      if (transitions_[hint - 1].civil_sec <= cs) {
967
3.84k
        if (cs < transitions_[hint].civil_sec) {
968
1.68k
          tr = begin + hint;
969
1.68k
        }
970
3.84k
      }
971
6.02k
    }
972
6.28k
    if (tr == nullptr) {
973
4.60k
      const Transition target = {0, 0, cs, civil_second()};
974
4.60k
      tr = std::upper_bound(begin, end, target, Transition::ByCivilTime());
975
4.60k
      time_local_hint_.store(static_cast<std::size_t>(tr - begin),
976
4.60k
                             std::memory_order_relaxed);
977
4.60k
    }
978
6.28k
  }
979
980
13.2k
  if (tr == begin) {
981
4.41k
    if (tr->prev_civil_sec >= cs) {
982
      // Before first transition, so use the default offset.
983
4.40k
      const TransitionType& tt(transition_types_[default_transition_type_]);
984
4.40k
      if (cs < tt.civil_min) return MakeUnique(time_point<seconds>::min());
985
4.21k
      return MakeUnique(cs - (civil_second() + tt.utc_offset));
986
4.40k
    }
987
    // tr->prev_civil_sec < cs < tr->civil_sec
988
7
    return MakeSkipped(*tr, cs);
989
4.41k
  }
990
991
8.85k
  if (tr == end) {
992
2.57k
    if (cs > (--tr)->prev_civil_sec) {
993
      // After the last transition. If we extended the transitions using
994
      // future_spec_, shift back to a supported year using the 400-year
995
      // cycle of calendaric equivalence and then compensate accordingly.
996
2.55k
      if (extended_ && cs.year() > last_year_) {
997
2.19k
        const year_t shift = (cs.year() - last_year_ - 1) / 400 + 1;
998
2.19k
        return TimeLocal(YearShift(cs, shift * -400), shift);
999
2.19k
      }
1000
359
      const TransitionType& tt(transition_types_[tr->type_index]);
1001
359
      if (cs > tt.civil_max) return MakeUnique(time_point<seconds>::max());
1002
325
      return MakeUnique(tr->unix_time + (cs - tr->civil_sec));
1003
359
    }
1004
    // tr->civil_sec <= cs <= tr->prev_civil_sec
1005
15
    return MakeRepeated(*tr, cs);
1006
2.57k
  }
1007
1008
6.28k
  if (tr->prev_civil_sec < cs) {
1009
    // tr->prev_civil_sec < cs < tr->civil_sec
1010
11
    return MakeSkipped(*tr, cs);
1011
11
  }
1012
1013
6.27k
  if (cs <= (--tr)->prev_civil_sec) {
1014
    // tr->civil_sec <= cs <= tr->prev_civil_sec
1015
26
    return MakeRepeated(*tr, cs);
1016
26
  }
1017
1018
  // In between transitions.
1019
6.24k
  return MakeUnique(tr->unix_time + (cs - tr->civil_sec));
1020
6.27k
}
1021
1022
0
std::string TimeZoneInfo::Version() const {
1023
0
  return version_;
1024
0
}
1025
1026
0
std::string TimeZoneInfo::Description() const {
1027
0
  std::ostringstream oss;
1028
0
  oss << "#trans=" << transitions_.size();
1029
0
  oss << " #types=" << transition_types_.size();
1030
0
  oss << " spec='" << future_spec_ << "'";
1031
0
  return oss.str();
1032
0
}
1033
1034
bool TimeZoneInfo::NextTransition(const time_point<seconds>& tp,
1035
0
                                  time_zone::civil_transition* trans) const {
1036
0
  if (transitions_.empty()) return false;
1037
0
  const Transition* begin = &transitions_[0];
1038
0
  const Transition* end = begin + transitions_.size();
1039
0
  if (begin->unix_time <= -(1LL << 59)) {
1040
    // Do not report the BIG_BANG found in some zoneinfo data as it is
1041
    // really a sentinel, not a transition.  See pre-2018f tz/zic.c.
1042
0
    ++begin;
1043
0
  }
1044
0
  std::int_fast64_t unix_time = ToUnixSeconds(tp);
1045
0
  const Transition target = {unix_time, 0, civil_second(), civil_second()};
1046
0
  const Transition* tr = std::upper_bound(begin, end, target,
1047
0
                                          Transition::ByUnixTime());
1048
0
  for (; tr != end; ++tr) {  // skip no-op transitions
1049
0
    std::uint_fast8_t prev_type_index =
1050
0
        (tr == begin) ? default_transition_type_ : tr[-1].type_index;
1051
0
    if (!EquivTransitions(prev_type_index, tr[0].type_index)) break;
1052
0
  }
1053
  // When tr == end we return false, ignoring future_spec_.
1054
0
  if (tr == end) return false;
1055
0
  trans->from = tr->prev_civil_sec + 1;
1056
0
  trans->to = tr->civil_sec;
1057
0
  return true;
1058
0
}
1059
1060
bool TimeZoneInfo::PrevTransition(const time_point<seconds>& tp,
1061
0
                                  time_zone::civil_transition* trans) const {
1062
0
  if (transitions_.empty()) return false;
1063
0
  const Transition* begin = &transitions_[0];
1064
0
  const Transition* end = begin + transitions_.size();
1065
0
  if (begin->unix_time <= -(1LL << 59)) {
1066
    // Do not report the BIG_BANG found in some zoneinfo data as it is
1067
    // really a sentinel, not a transition.  See pre-2018f tz/zic.c.
1068
0
    ++begin;
1069
0
  }
1070
0
  std::int_fast64_t unix_time = ToUnixSeconds(tp);
1071
0
  if (FromUnixSeconds(unix_time) != tp) {
1072
0
    if (unix_time == std::numeric_limits<std::int_fast64_t>::max()) {
1073
0
      if (end == begin) return false;  // Ignore future_spec_.
1074
0
      trans->from = (--end)->prev_civil_sec + 1;
1075
0
      trans->to = end->civil_sec;
1076
0
      return true;
1077
0
    }
1078
0
    unix_time += 1;  // ceils
1079
0
  }
1080
0
  const Transition target = {unix_time, 0, civil_second(), civil_second()};
1081
0
  const Transition* tr = std::lower_bound(begin, end, target,
1082
0
                                          Transition::ByUnixTime());
1083
0
  for (; tr != begin; --tr) {  // skip no-op transitions
1084
0
    std::uint_fast8_t prev_type_index =
1085
0
        (tr - 1 == begin) ? default_transition_type_ : tr[-2].type_index;
1086
0
    if (!EquivTransitions(prev_type_index, tr[-1].type_index)) break;
1087
0
  }
1088
  // When tr == end we return the "last" transition, ignoring future_spec_.
1089
0
  if (tr == begin) return false;
1090
0
  trans->from = (--tr)->prev_civil_sec + 1;
1091
0
  trans->to = tr->civil_sec;
1092
0
  return true;
1093
0
}
1094
1095
}  // namespace cctz