/src/abseil-cpp/absl/time/time.cc
Line | Count | Source |
1 | | // Copyright 2017 The Abseil Authors. |
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 | | // The implementation of the absl::Time class, which is declared in |
16 | | // //absl/time.h. |
17 | | // |
18 | | // The representation for an absl::Time is an absl::Duration offset from the |
19 | | // epoch. We use the traditional Unix epoch (1970-01-01 00:00:00 +0000) |
20 | | // for convenience, but this is not exposed in the API and could be changed. |
21 | | // |
22 | | // NOTE: To keep type verbosity to a minimum, the following variable naming |
23 | | // conventions are used throughout this file. |
24 | | // |
25 | | // tz: An absl::TimeZone |
26 | | // ci: An absl::TimeZone::CivilInfo |
27 | | // ti: An absl::TimeZone::TimeInfo |
28 | | // cd: An absl::CivilDay or a cctz::civil_day |
29 | | // cs: An absl::CivilSecond or a cctz::civil_second |
30 | | // bd: An absl::Time::Breakdown |
31 | | // cl: A cctz::time_zone::civil_lookup |
32 | | // al: A cctz::time_zone::absolute_lookup |
33 | | |
34 | | #include "absl/time/time.h" |
35 | | |
36 | | #include <chrono> // NOLINT(build/c++11) |
37 | | #include <cstdint> |
38 | | #include <cstring> |
39 | | #include <ctime> |
40 | | #include <limits> |
41 | | |
42 | | #include "absl/base/attributes.h" |
43 | | #include "absl/base/config.h" |
44 | | #include "absl/time/civil_time.h" |
45 | | #include "absl/time/internal/cctz/include/cctz/civil_time.h" |
46 | | #include "absl/time/internal/cctz/include/cctz/time_zone.h" |
47 | | |
48 | | #if defined(_MSC_VER) |
49 | | #include <winsock2.h> // for timeval |
50 | | #endif |
51 | | |
52 | | namespace cctz = absl::time_internal::cctz; |
53 | | |
54 | | namespace absl { |
55 | | ABSL_NAMESPACE_BEGIN |
56 | | |
57 | | namespace { |
58 | | |
59 | 0 | inline cctz::time_point<cctz::seconds> unix_epoch() { |
60 | 0 | return std::chrono::time_point_cast<cctz::seconds>( |
61 | 0 | std::chrono::system_clock::from_time_t(0)); |
62 | 0 | } |
63 | | |
64 | | // Floors d to the next unit boundary closer to negative infinity. |
65 | 0 | inline int64_t FloorToUnit(absl::Duration d, absl::Duration unit) { |
66 | 0 | absl::Duration rem; |
67 | 0 | int64_t q = absl::IDivDuration(d, unit, &rem); |
68 | 0 | return (q > 0 || rem >= ZeroDuration() || |
69 | 0 | q == std::numeric_limits<int64_t>::min()) |
70 | 0 | ? q |
71 | 0 | : q - 1; |
72 | 0 | } |
73 | | |
74 | | ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING |
75 | 0 | inline absl::Time::Breakdown InfiniteFutureBreakdown() { |
76 | 0 | absl::Time::Breakdown bd; |
77 | 0 | bd.year = std::numeric_limits<int64_t>::max(); |
78 | 0 | bd.month = 12; |
79 | 0 | bd.day = 31; |
80 | 0 | bd.hour = 23; |
81 | 0 | bd.minute = 59; |
82 | 0 | bd.second = 59; |
83 | 0 | bd.subsecond = absl::InfiniteDuration(); |
84 | 0 | bd.weekday = 4; |
85 | 0 | bd.yearday = 365; |
86 | 0 | bd.offset = 0; |
87 | 0 | bd.is_dst = false; |
88 | 0 | bd.zone_abbr = "-00"; |
89 | 0 | return bd; |
90 | 0 | } |
91 | | |
92 | 0 | inline absl::Time::Breakdown InfinitePastBreakdown() { |
93 | 0 | Time::Breakdown bd; |
94 | 0 | bd.year = std::numeric_limits<int64_t>::min(); |
95 | 0 | bd.month = 1; |
96 | 0 | bd.day = 1; |
97 | 0 | bd.hour = 0; |
98 | 0 | bd.minute = 0; |
99 | 0 | bd.second = 0; |
100 | 0 | bd.subsecond = -absl::InfiniteDuration(); |
101 | 0 | bd.weekday = 7; |
102 | 0 | bd.yearday = 1; |
103 | 0 | bd.offset = 0; |
104 | 0 | bd.is_dst = false; |
105 | 0 | bd.zone_abbr = "-00"; |
106 | 0 | return bd; |
107 | 0 | } |
108 | | ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING |
109 | | |
110 | 0 | inline absl::TimeZone::CivilInfo InfiniteFutureCivilInfo() { |
111 | 0 | TimeZone::CivilInfo ci; |
112 | 0 | ci.cs = CivilSecond::max(); |
113 | 0 | ci.subsecond = InfiniteDuration(); |
114 | 0 | ci.offset = 0; |
115 | 0 | ci.is_dst = false; |
116 | 0 | ci.zone_abbr = "-00"; |
117 | 0 | return ci; |
118 | 0 | } |
119 | | |
120 | 0 | inline absl::TimeZone::CivilInfo InfinitePastCivilInfo() { |
121 | 0 | TimeZone::CivilInfo ci; |
122 | 0 | ci.cs = CivilSecond::min(); |
123 | 0 | ci.subsecond = -InfiniteDuration(); |
124 | 0 | ci.offset = 0; |
125 | 0 | ci.is_dst = false; |
126 | 0 | ci.zone_abbr = "-00"; |
127 | 0 | return ci; |
128 | 0 | } |
129 | | |
130 | | ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING |
131 | 0 | inline absl::TimeConversion InfiniteFutureTimeConversion() { |
132 | 0 | absl::TimeConversion tc; |
133 | 0 | tc.pre = tc.trans = tc.post = absl::InfiniteFuture(); |
134 | 0 | tc.kind = absl::TimeConversion::UNIQUE; |
135 | 0 | tc.normalized = true; |
136 | 0 | return tc; |
137 | 0 | } |
138 | | |
139 | 0 | inline TimeConversion InfinitePastTimeConversion() { |
140 | 0 | absl::TimeConversion tc; |
141 | 0 | tc.pre = tc.trans = tc.post = absl::InfinitePast(); |
142 | 0 | tc.kind = absl::TimeConversion::UNIQUE; |
143 | 0 | tc.normalized = true; |
144 | 0 | return tc; |
145 | 0 | } |
146 | | ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING |
147 | | |
148 | | // Makes a Time from sec, overflowing to InfiniteFuture/InfinitePast as |
149 | | // necessary. If sec is min/max, then consult cs+tz to check for overflow. |
150 | | Time MakeTimeWithOverflow(const cctz::time_point<cctz::seconds>& sec, |
151 | | const cctz::civil_second& cs, |
152 | | const cctz::time_zone& tz, |
153 | 0 | bool* normalized = nullptr) { |
154 | 0 | const auto max = cctz::time_point<cctz::seconds>::max(); |
155 | 0 | const auto min = cctz::time_point<cctz::seconds>::min(); |
156 | 0 | if (sec == max) { |
157 | 0 | const auto al = tz.lookup(max); |
158 | 0 | if (cs > al.cs) { |
159 | 0 | if (normalized) *normalized = true; |
160 | 0 | return absl::InfiniteFuture(); |
161 | 0 | } |
162 | 0 | } |
163 | 0 | if (sec == min) { |
164 | 0 | const auto al = tz.lookup(min); |
165 | 0 | if (cs < al.cs) { |
166 | 0 | if (normalized) *normalized = true; |
167 | 0 | return absl::InfinitePast(); |
168 | 0 | } |
169 | 0 | } |
170 | 0 | const auto hi = (sec - unix_epoch()).count(); |
171 | 0 | return time_internal::FromUnixDuration(time_internal::MakeDuration(hi)); |
172 | 0 | } |
173 | | |
174 | | // Returns Mon=1..Sun=7. |
175 | 0 | inline int MapWeekday(const cctz::weekday& wd) { |
176 | 0 | switch (wd) { |
177 | 0 | case cctz::weekday::monday: |
178 | 0 | return 1; |
179 | 0 | case cctz::weekday::tuesday: |
180 | 0 | return 2; |
181 | 0 | case cctz::weekday::wednesday: |
182 | 0 | return 3; |
183 | 0 | case cctz::weekday::thursday: |
184 | 0 | return 4; |
185 | 0 | case cctz::weekday::friday: |
186 | 0 | return 5; |
187 | 0 | case cctz::weekday::saturday: |
188 | 0 | return 6; |
189 | 0 | case cctz::weekday::sunday: |
190 | 0 | return 7; |
191 | 0 | } |
192 | 0 | return 1; |
193 | 0 | } |
194 | | |
195 | | bool FindTransition(const cctz::time_zone& tz, |
196 | | bool (cctz::time_zone::*find_transition)( |
197 | | const cctz::time_point<cctz::seconds>& tp, |
198 | | cctz::time_zone::civil_transition* trans) const, |
199 | 0 | Time t, TimeZone::CivilTransition* trans) { |
200 | | // Transitions are second-aligned, so we can discard any fractional part. |
201 | 0 | const auto tp = unix_epoch() + cctz::seconds(ToUnixSeconds(t)); |
202 | 0 | cctz::time_zone::civil_transition tr; |
203 | 0 | if (!(tz.*find_transition)(tp, &tr)) return false; |
204 | 0 | trans->from = CivilSecond(tr.from); |
205 | 0 | trans->to = CivilSecond(tr.to); |
206 | 0 | return true; |
207 | 0 | } |
208 | | |
209 | | } // namespace |
210 | | |
211 | | // |
212 | | // Time |
213 | | // |
214 | | |
215 | | ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING |
216 | 0 | absl::Time::Breakdown Time::In(absl::TimeZone tz) const { |
217 | 0 | if (*this == absl::InfiniteFuture()) return InfiniteFutureBreakdown(); |
218 | 0 | if (*this == absl::InfinitePast()) return InfinitePastBreakdown(); |
219 | | |
220 | 0 | const auto tp = unix_epoch() + cctz::seconds(time_internal::GetRepHi(rep_)); |
221 | 0 | const auto al = cctz::time_zone(tz).lookup(tp); |
222 | 0 | const auto cs = al.cs; |
223 | 0 | const auto cd = cctz::civil_day(cs); |
224 | |
|
225 | 0 | absl::Time::Breakdown bd; |
226 | 0 | bd.year = cs.year(); |
227 | 0 | bd.month = cs.month(); |
228 | 0 | bd.day = cs.day(); |
229 | 0 | bd.hour = cs.hour(); |
230 | 0 | bd.minute = cs.minute(); |
231 | 0 | bd.second = cs.second(); |
232 | 0 | bd.subsecond = time_internal::MakeDuration(0, time_internal::GetRepLo(rep_)); |
233 | 0 | bd.weekday = MapWeekday(cctz::get_weekday(cd)); |
234 | 0 | bd.yearday = cctz::get_yearday(cd); |
235 | 0 | bd.offset = al.offset; |
236 | 0 | bd.is_dst = al.is_dst; |
237 | 0 | bd.zone_abbr = al.abbr; |
238 | 0 | return bd; |
239 | 0 | } |
240 | | ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING |
241 | | |
242 | | // |
243 | | // Conversions from/to other time types. |
244 | | // |
245 | | |
246 | 0 | absl::Time FromUDate(double udate) { |
247 | 0 | return time_internal::FromUnixDuration(absl::Milliseconds(udate)); |
248 | 0 | } |
249 | | |
250 | 0 | absl::Time FromUniversal(int64_t universal) { |
251 | 0 | return absl::UniversalEpoch() + 100 * absl::Nanoseconds(universal); |
252 | 0 | } |
253 | | |
254 | 0 | int64_t ToUnixNanos(Time t) { |
255 | 0 | if (time_internal::GetRepHi(time_internal::ToUnixDuration(t)) >= 0 && |
256 | 0 | time_internal::GetRepHi(time_internal::ToUnixDuration(t)) >> 33 == 0) { |
257 | 0 | return (time_internal::GetRepHi(time_internal::ToUnixDuration(t)) * 1000 * |
258 | 0 | 1000 * 1000) + |
259 | 0 | (time_internal::GetRepLo(time_internal::ToUnixDuration(t)) / 4); |
260 | 0 | } |
261 | 0 | return FloorToUnit(time_internal::ToUnixDuration(t), absl::Nanoseconds(1)); |
262 | 0 | } |
263 | | |
264 | 0 | int64_t ToUnixMicros(Time t) { |
265 | 0 | if (time_internal::GetRepHi(time_internal::ToUnixDuration(t)) >= 0 && |
266 | 0 | time_internal::GetRepHi(time_internal::ToUnixDuration(t)) >> 43 == 0) { |
267 | 0 | return (time_internal::GetRepHi(time_internal::ToUnixDuration(t)) * 1000 * |
268 | 0 | 1000) + |
269 | 0 | (time_internal::GetRepLo(time_internal::ToUnixDuration(t)) / 4000); |
270 | 0 | } |
271 | 0 | return FloorToUnit(time_internal::ToUnixDuration(t), absl::Microseconds(1)); |
272 | 0 | } |
273 | | |
274 | 0 | int64_t ToUnixMillis(Time t) { |
275 | 0 | if (time_internal::GetRepHi(time_internal::ToUnixDuration(t)) >= 0 && |
276 | 0 | time_internal::GetRepHi(time_internal::ToUnixDuration(t)) >> 53 == 0) { |
277 | 0 | return (time_internal::GetRepHi(time_internal::ToUnixDuration(t)) * 1000) + |
278 | 0 | (time_internal::GetRepLo(time_internal::ToUnixDuration(t)) / |
279 | 0 | (4000 * 1000)); |
280 | 0 | } |
281 | 0 | return FloorToUnit(time_internal::ToUnixDuration(t), absl::Milliseconds(1)); |
282 | 0 | } |
283 | | |
284 | 0 | int64_t ToUnixSeconds(Time t) { |
285 | 0 | return time_internal::GetRepHi(time_internal::ToUnixDuration(t)); |
286 | 0 | } |
287 | | |
288 | 0 | time_t ToTimeT(Time t) { return absl::ToTimespec(t).tv_sec; } |
289 | | |
290 | 0 | double ToUDate(Time t) { |
291 | 0 | return absl::FDivDuration(time_internal::ToUnixDuration(t), |
292 | 0 | absl::Milliseconds(1)); |
293 | 0 | } |
294 | | |
295 | 0 | int64_t ToUniversal(absl::Time t) { |
296 | 0 | return absl::FloorToUnit(t - absl::UniversalEpoch(), absl::Nanoseconds(100)); |
297 | 0 | } |
298 | | |
299 | 0 | absl::Time TimeFromTimespec(timespec ts) { |
300 | 0 | return time_internal::FromUnixDuration(absl::DurationFromTimespec(ts)); |
301 | 0 | } |
302 | | |
303 | 0 | absl::Time TimeFromTimeval(timeval tv) { |
304 | 0 | return time_internal::FromUnixDuration(absl::DurationFromTimeval(tv)); |
305 | 0 | } |
306 | | |
307 | 0 | timespec ToTimespec(Time t) { |
308 | 0 | timespec ts; |
309 | 0 | absl::Duration d = time_internal::ToUnixDuration(t); |
310 | 0 | if (!time_internal::IsInfiniteDuration(d)) { |
311 | 0 | ts.tv_sec = static_cast<decltype(ts.tv_sec)>(time_internal::GetRepHi(d)); |
312 | 0 | if (ts.tv_sec == time_internal::GetRepHi(d)) { // no time_t narrowing |
313 | 0 | ts.tv_nsec = time_internal::GetRepLo(d) / 4; // floor |
314 | 0 | return ts; |
315 | 0 | } |
316 | 0 | } |
317 | 0 | if (d >= absl::ZeroDuration()) { |
318 | 0 | ts.tv_sec = std::numeric_limits<time_t>::max(); |
319 | 0 | ts.tv_nsec = 1000 * 1000 * 1000 - 1; |
320 | 0 | } else { |
321 | 0 | ts.tv_sec = std::numeric_limits<time_t>::min(); |
322 | 0 | ts.tv_nsec = 0; |
323 | 0 | } |
324 | 0 | return ts; |
325 | 0 | } |
326 | | |
327 | 0 | timeval ToTimeval(Time t) { |
328 | 0 | timeval tv; |
329 | 0 | timespec ts = absl::ToTimespec(t); |
330 | 0 | tv.tv_sec = static_cast<decltype(tv.tv_sec)>(ts.tv_sec); |
331 | 0 | if (tv.tv_sec != ts.tv_sec) { // narrowing |
332 | 0 | if (ts.tv_sec < 0) { |
333 | 0 | tv.tv_sec = std::numeric_limits<decltype(tv.tv_sec)>::min(); |
334 | 0 | tv.tv_usec = 0; |
335 | 0 | } else { |
336 | 0 | tv.tv_sec = std::numeric_limits<decltype(tv.tv_sec)>::max(); |
337 | 0 | tv.tv_usec = 1000 * 1000 - 1; |
338 | 0 | } |
339 | 0 | return tv; |
340 | 0 | } |
341 | 0 | tv.tv_usec = static_cast<int>(ts.tv_nsec / 1000); // suseconds_t |
342 | 0 | return tv; |
343 | 0 | } |
344 | | |
345 | 0 | Time FromChrono(const std::chrono::system_clock::time_point& tp) { |
346 | 0 | return time_internal::FromUnixDuration(time_internal::FromChrono( |
347 | 0 | tp - std::chrono::system_clock::from_time_t(0))); |
348 | 0 | } |
349 | | |
350 | 0 | std::chrono::system_clock::time_point ToChronoTime(absl::Time t) { |
351 | 0 | using D = std::chrono::system_clock::duration; |
352 | 0 | auto d = time_internal::ToUnixDuration(t); |
353 | 0 | if (d < ZeroDuration()) d = Floor(d, FromChrono(D{1})); |
354 | 0 | return std::chrono::system_clock::from_time_t(0) + |
355 | 0 | time_internal::ToChronoDuration<D>(d); |
356 | 0 | } |
357 | | |
358 | | // |
359 | | // TimeZone |
360 | | // |
361 | | |
362 | 0 | absl::TimeZone::CivilInfo TimeZone::At(Time t) const { |
363 | 0 | if (t == absl::InfiniteFuture()) return InfiniteFutureCivilInfo(); |
364 | 0 | if (t == absl::InfinitePast()) return InfinitePastCivilInfo(); |
365 | | |
366 | 0 | const auto ud = time_internal::ToUnixDuration(t); |
367 | 0 | const auto tp = unix_epoch() + cctz::seconds(time_internal::GetRepHi(ud)); |
368 | 0 | const auto al = cz_.lookup(tp); |
369 | |
|
370 | 0 | TimeZone::CivilInfo ci; |
371 | 0 | ci.cs = CivilSecond(al.cs); |
372 | 0 | ci.subsecond = time_internal::MakeDuration(0, time_internal::GetRepLo(ud)); |
373 | 0 | ci.offset = al.offset; |
374 | 0 | ci.is_dst = al.is_dst; |
375 | 0 | ci.zone_abbr = al.abbr; |
376 | 0 | return ci; |
377 | 0 | } |
378 | | |
379 | 0 | absl::TimeZone::TimeInfo TimeZone::At(CivilSecond ct) const { |
380 | 0 | const cctz::civil_second cs(ct); |
381 | 0 | const auto cl = cz_.lookup(cs); |
382 | |
|
383 | 0 | TimeZone::TimeInfo ti; |
384 | 0 | switch (cl.kind) { |
385 | 0 | case cctz::time_zone::civil_lookup::UNIQUE: |
386 | 0 | ti.kind = TimeZone::TimeInfo::UNIQUE; |
387 | 0 | break; |
388 | 0 | case cctz::time_zone::civil_lookup::SKIPPED: |
389 | 0 | ti.kind = TimeZone::TimeInfo::SKIPPED; |
390 | 0 | break; |
391 | 0 | case cctz::time_zone::civil_lookup::REPEATED: |
392 | 0 | ti.kind = TimeZone::TimeInfo::REPEATED; |
393 | 0 | break; |
394 | 0 | } |
395 | 0 | ti.pre = MakeTimeWithOverflow(cl.pre, cs, cz_); |
396 | 0 | ti.trans = MakeTimeWithOverflow(cl.trans, cs, cz_); |
397 | 0 | ti.post = MakeTimeWithOverflow(cl.post, cs, cz_); |
398 | 0 | return ti; |
399 | 0 | } |
400 | | |
401 | 0 | bool TimeZone::NextTransition(Time t, CivilTransition* trans) const { |
402 | 0 | return FindTransition(cz_, &cctz::time_zone::next_transition, t, trans); |
403 | 0 | } |
404 | | |
405 | 0 | bool TimeZone::PrevTransition(Time t, CivilTransition* trans) const { |
406 | 0 | return FindTransition(cz_, &cctz::time_zone::prev_transition, t, trans); |
407 | 0 | } |
408 | | |
409 | | // |
410 | | // Conversions involving time zones. |
411 | | // |
412 | | ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING |
413 | | absl::TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour, |
414 | 0 | int min, int sec, TimeZone tz) { |
415 | | // Avoids years that are too extreme for CivilSecond to normalize. |
416 | 0 | if (year > 300000000000) return InfiniteFutureTimeConversion(); |
417 | 0 | if (year < -300000000000) return InfinitePastTimeConversion(); |
418 | | |
419 | 0 | const CivilSecond cs(year, mon, day, hour, min, sec); |
420 | 0 | const auto ti = tz.At(cs); |
421 | |
|
422 | 0 | TimeConversion tc; |
423 | 0 | tc.pre = ti.pre; |
424 | 0 | tc.trans = ti.trans; |
425 | 0 | tc.post = ti.post; |
426 | 0 | switch (ti.kind) { |
427 | 0 | case TimeZone::TimeInfo::UNIQUE: |
428 | 0 | tc.kind = TimeConversion::UNIQUE; |
429 | 0 | break; |
430 | 0 | case TimeZone::TimeInfo::SKIPPED: |
431 | 0 | tc.kind = TimeConversion::SKIPPED; |
432 | 0 | break; |
433 | 0 | case TimeZone::TimeInfo::REPEATED: |
434 | 0 | tc.kind = TimeConversion::REPEATED; |
435 | 0 | break; |
436 | 0 | } |
437 | 0 | tc.normalized = false; |
438 | 0 | if (year != cs.year() || mon != cs.month() || day != cs.day() || |
439 | 0 | hour != cs.hour() || min != cs.minute() || sec != cs.second()) { |
440 | 0 | tc.normalized = true; |
441 | 0 | } |
442 | 0 | return tc; |
443 | 0 | } |
444 | | ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING |
445 | | |
446 | 0 | absl::Time FromTM(const struct tm& tm, absl::TimeZone tz) { |
447 | 0 | civil_year_t tm_year = tm.tm_year; |
448 | | // Avoids years that are too extreme for CivilSecond to normalize. |
449 | 0 | if (tm_year > 300000000000ll) return InfiniteFuture(); |
450 | 0 | if (tm_year < -300000000000ll) return InfinitePast(); |
451 | 0 | int tm_mon = tm.tm_mon; |
452 | 0 | if (tm_mon == std::numeric_limits<int>::max()) { |
453 | 0 | tm_mon -= 12; |
454 | 0 | tm_year += 1; |
455 | 0 | } |
456 | 0 | const auto ti = tz.At(CivilSecond(tm_year + 1900, tm_mon + 1, tm.tm_mday, |
457 | 0 | tm.tm_hour, tm.tm_min, tm.tm_sec)); |
458 | 0 | return tm.tm_isdst == 0 ? ti.post : ti.pre; |
459 | 0 | } |
460 | | |
461 | 0 | struct tm ToTM(absl::Time t, absl::TimeZone tz) { |
462 | 0 | struct tm tm = {}; |
463 | |
|
464 | 0 | const auto ci = tz.At(t); |
465 | 0 | const auto& cs = ci.cs; |
466 | 0 | tm.tm_sec = cs.second(); |
467 | 0 | tm.tm_min = cs.minute(); |
468 | 0 | tm.tm_hour = cs.hour(); |
469 | 0 | tm.tm_mday = cs.day(); |
470 | 0 | tm.tm_mon = cs.month() - 1; |
471 | | |
472 | | // Saturates tm.tm_year in cases of over/underflow, accounting for the fact |
473 | | // that tm.tm_year is years since 1900. |
474 | 0 | if (cs.year() < std::numeric_limits<int>::min() + 1900) { |
475 | 0 | tm.tm_year = std::numeric_limits<int>::min(); |
476 | 0 | } else if (cs.year() > std::numeric_limits<int>::max()) { |
477 | 0 | tm.tm_year = std::numeric_limits<int>::max() - 1900; |
478 | 0 | } else { |
479 | 0 | tm.tm_year = static_cast<int>(cs.year() - 1900); |
480 | 0 | } |
481 | |
|
482 | 0 | switch (GetWeekday(cs)) { |
483 | 0 | case Weekday::sunday: |
484 | 0 | tm.tm_wday = 0; |
485 | 0 | break; |
486 | 0 | case Weekday::monday: |
487 | 0 | tm.tm_wday = 1; |
488 | 0 | break; |
489 | 0 | case Weekday::tuesday: |
490 | 0 | tm.tm_wday = 2; |
491 | 0 | break; |
492 | 0 | case Weekday::wednesday: |
493 | 0 | tm.tm_wday = 3; |
494 | 0 | break; |
495 | 0 | case Weekday::thursday: |
496 | 0 | tm.tm_wday = 4; |
497 | 0 | break; |
498 | 0 | case Weekday::friday: |
499 | 0 | tm.tm_wday = 5; |
500 | 0 | break; |
501 | 0 | case Weekday::saturday: |
502 | 0 | tm.tm_wday = 6; |
503 | 0 | break; |
504 | 0 | } |
505 | 0 | tm.tm_yday = GetYearDay(cs) - 1; |
506 | 0 | tm.tm_isdst = ci.is_dst ? 1 : 0; |
507 | |
|
508 | 0 | return tm; |
509 | 0 | } |
510 | | |
511 | | ABSL_NAMESPACE_END |
512 | | } // namespace absl |