Coverage Report

Created: 2023-09-25 06:17

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