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