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