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