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