Export of internal Abseil changes.
-- 906c47420646d510edd2479d5542c56f5fa31b65 by CJ Johnson <johnsoncj@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 216573923 -- 74560d4afd2b605909e677c6fc3076049fb3010a by Eric Fiselier <ericwf@google.com>: Avoid -Wformat-pedantic in benchmark. PiperOrigin-RevId: 216523769 -- 9bcc9da8b03e6d1ea43ee78931256c5541cb9686 by Eric Fiselier <ericwf@google.com>: Delete unused CityHash functions. PiperOrigin-RevId: 216464492 -- a42563b394c89fbb4c55cb5a6a5edbf96d271eea by Abseil Team <absl-team@google.com>: Introduce new Abseil interfaces for converting between civil times and absolute times.s Deprecates absl::ConvertDateTime() and absl::FromDateTime(). PiperOrigin-RevId: 216424948 -- 088e11235124267517d7f137854fa5554679c24f by Eric Fiselier <ericwf@google.com>: Remove unneeded break statements in test. PiperOrigin-RevId: 216403321 GitOrigin-RevId: 906c47420646d510edd2479d5542c56f5fa31b65 Change-Id: Idb44420be623e369c66f5a9c92bdc9ab46d3ec92
This commit is contained in:
		
							parent
							
								
									445998d7ac
								
							
						
					
					
						commit
						f340f773ed
					
				
					 24 changed files with 2675 additions and 2592 deletions
				
			
		|  | @ -1029,7 +1029,6 @@ ExpectedStats XorSeedExpectedStats() { | |||
|           {{0.95, 0.1}}, | ||||
|           {{0.95, 0}, {0.99, 2}, {0.999, 4}, {0.9999, 10}}}; | ||||
|       } | ||||
|       break; | ||||
|     case 16: | ||||
|       if (kRandomizesInserts) { | ||||
|         return {0.1, | ||||
|  | @ -1042,10 +1041,8 @@ ExpectedStats XorSeedExpectedStats() { | |||
|                 {{0.95, 0.05}}, | ||||
|                 {{0.95, 0}, {0.99, 1}, {0.999, 4}, {0.9999, 10}}}; | ||||
|       } | ||||
|       break; | ||||
|     default: | ||||
|       ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); | ||||
|   } | ||||
|   ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); | ||||
|   return {}; | ||||
| } | ||||
| TEST(Table, DISABLED_EnsureNonQuadraticTopNXorSeedByProbeSeqLength) { | ||||
|  | @ -1125,7 +1122,6 @@ ExpectedStats LinearTransformExpectedStats() { | |||
|                 {{0.95, 0.3}}, | ||||
|                 {{0.95, 0}, {0.99, 3}, {0.999, 15}, {0.9999, 25}}}; | ||||
|       } | ||||
|       break; | ||||
|     case 16: | ||||
|       if (kRandomizesInserts) { | ||||
|         return {0.1, | ||||
|  | @ -1138,10 +1134,8 @@ ExpectedStats LinearTransformExpectedStats() { | |||
|                 {{0.95, 0.1}}, | ||||
|                 {{0.95, 0}, {0.99, 1}, {0.999, 6}, {0.9999, 10}}}; | ||||
|       } | ||||
|       break; | ||||
|     default: | ||||
|       ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); | ||||
|   } | ||||
|   ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); | ||||
|   return {}; | ||||
| } | ||||
| TEST(Table, DISABLED_EnsureNonQuadraticTopNLinearTransformByProbeSeqLength) { | ||||
|  | @ -1834,18 +1828,15 @@ std::vector<std::pair<double, double>> StringTablePefectRatios() { | |||
|       } else { | ||||
|         return {{0.995, 0.01}, {0.97, 0.01}, {0.89, 0.02}}; | ||||
|       } | ||||
|       break; | ||||
|     case 16: | ||||
|       if (kRandomizesInserts) { | ||||
|         return {{0.973, 0.01}, {0.965, 0.01}, {0.92, 0.02}}; | ||||
|       } else { | ||||
|         return {{0.995, 0.005}, {0.99, 0.005}, {0.94, 0.01}}; | ||||
|       } | ||||
|       break; | ||||
|     default: | ||||
|       // Ignore anything else.
 | ||||
|       return {}; | ||||
|   } | ||||
|   ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); | ||||
|   return {}; | ||||
| } | ||||
| 
 | ||||
| // This is almost a change detector, but it allows us to know how we are
 | ||||
|  | @ -1884,18 +1875,15 @@ std::vector<std::pair<double, double>> IntTablePefectRatios() { | |||
|       } else { | ||||
|         return {{0.99, 0.01}, {0.99, 0.01}, {0.95, 0.02}}; | ||||
|       } | ||||
|       break; | ||||
|     case 16: | ||||
|       if (kRandomizesInserts) { | ||||
|         return {{0.98, 0.02}, {0.978, 0.02}, {0.96, 0.02}}; | ||||
|       } else { | ||||
|         return {{0.998, 0.003}, {0.995, 0.01}, {0.975, 0.02}}; | ||||
|       } | ||||
|       break; | ||||
|     default: | ||||
|       // Ignore anything else.
 | ||||
|       return {}; | ||||
|   } | ||||
|   ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width"); | ||||
|   return {}; | ||||
| } | ||||
| 
 | ||||
| // This is almost a change detector, but it allows us to know how we are
 | ||||
|  |  | |||
|  | @ -93,7 +93,6 @@ cc_library( | |||
|     srcs = ["internal/city.cc"], | ||||
|     hdrs = [ | ||||
|         "internal/city.h", | ||||
|         "internal/city_crc.h", | ||||
|     ], | ||||
|     copts = ABSL_DEFAULT_COPTS, | ||||
|     deps = [ | ||||
|  |  | |||
|  | @ -20,7 +20,6 @@ list(APPEND HASH_PUBLIC_HEADERS | |||
| 
 | ||||
| list(APPEND HASH_INTERNAL_HEADERS | ||||
|   "internal/city.h" | ||||
|   "internal/city_crc.h" | ||||
|   "internal/hash.h" | ||||
| ) | ||||
| 
 | ||||
|  |  | |||
|  | @ -340,251 +340,5 @@ uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0, | |||
|   return HashLen16(CityHash64(s, len) - seed0, seed1); | ||||
| } | ||||
| 
 | ||||
| // A subroutine for CityHash128().  Returns a decent 128-bit hash for strings
 | ||||
| // of any length representable in signed long.  Based on City and Murmur.
 | ||||
| static uint128 CityMurmur(const char *s, size_t len, uint128 seed) { | ||||
|   uint64_t a = Uint128Low64(seed); | ||||
|   uint64_t b = Uint128High64(seed); | ||||
|   uint64_t c = 0; | ||||
|   uint64_t d = 0; | ||||
|   int64_t l = len - 16; | ||||
|   if (l <= 0) {  // len <= 16
 | ||||
|     a = ShiftMix(a * k1) * k1; | ||||
|     c = b * k1 + HashLen0to16(s, len); | ||||
|     d = ShiftMix(a + (len >= 8 ? Fetch64(s) : c)); | ||||
|   } else {  // len > 16
 | ||||
|     c = HashLen16(Fetch64(s + len - 8) + k1, a); | ||||
|     d = HashLen16(b + len, c + Fetch64(s + len - 16)); | ||||
|     a += d; | ||||
|     do { | ||||
|       a ^= ShiftMix(Fetch64(s) * k1) * k1; | ||||
|       a *= k1; | ||||
|       b ^= a; | ||||
|       c ^= ShiftMix(Fetch64(s + 8) * k1) * k1; | ||||
|       c *= k1; | ||||
|       d ^= c; | ||||
|       s += 16; | ||||
|       l -= 16; | ||||
|     } while (l > 0); | ||||
|   } | ||||
|   a = HashLen16(a, c); | ||||
|   b = HashLen16(d, b); | ||||
|   return uint128(a ^ b, HashLen16(b, a)); | ||||
| } | ||||
| 
 | ||||
| uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) { | ||||
|   if (len < 128) { | ||||
|     return CityMurmur(s, len, seed); | ||||
|   } | ||||
| 
 | ||||
|   // We expect len >= 128 to be the common case.  Keep 56 bytes of state:
 | ||||
|   // v, w, x, y, and z.
 | ||||
|   std::pair<uint64_t, uint64_t> v, w; | ||||
|   uint64_t x = Uint128Low64(seed); | ||||
|   uint64_t y = Uint128High64(seed); | ||||
|   uint64_t z = len * k1; | ||||
|   v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s); | ||||
|   v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8); | ||||
|   w.first = Rotate(y + z, 35) * k1 + x; | ||||
|   w.second = Rotate(x + Fetch64(s + 88), 53) * k1; | ||||
| 
 | ||||
|   // This is the same inner loop as CityHash64(), manually unrolled.
 | ||||
|   do { | ||||
|     x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; | ||||
|     y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; | ||||
|     x ^= w.second; | ||||
|     y += v.first + Fetch64(s + 40); | ||||
|     z = Rotate(z + w.first, 33) * k1; | ||||
|     v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); | ||||
|     w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16)); | ||||
|     std::swap(z, x); | ||||
|     s += 64; | ||||
|     x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; | ||||
|     y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; | ||||
|     x ^= w.second; | ||||
|     y += v.first + Fetch64(s + 40); | ||||
|     z = Rotate(z + w.first, 33) * k1; | ||||
|     v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); | ||||
|     w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16)); | ||||
|     std::swap(z, x); | ||||
|     s += 64; | ||||
|     len -= 128; | ||||
|   } while (ABSL_PREDICT_TRUE(len >= 128)); | ||||
|   x += Rotate(v.first + z, 49) * k0; | ||||
|   y = y * k0 + Rotate(w.second, 37); | ||||
|   z = z * k0 + Rotate(w.first, 27); | ||||
|   w.first *= 9; | ||||
|   v.first *= k0; | ||||
|   // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
 | ||||
|   for (size_t tail_done = 0; tail_done < len;) { | ||||
|     tail_done += 32; | ||||
|     y = Rotate(x + y, 42) * k0 + v.second; | ||||
|     w.first += Fetch64(s + len - tail_done + 16); | ||||
|     x = x * k0 + w.first; | ||||
|     z += w.second + Fetch64(s + len - tail_done); | ||||
|     w.second += v.first; | ||||
|     v = WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second); | ||||
|     v.first *= k0; | ||||
|   } | ||||
|   // At this point our 56 bytes of state should contain more than
 | ||||
|   // enough information for a strong 128-bit hash.  We use two
 | ||||
|   // different 56-byte-to-8-byte hashes to get a 16-byte final result.
 | ||||
|   x = HashLen16(x, v.first); | ||||
|   y = HashLen16(y + z, w.first); | ||||
|   return uint128(HashLen16(x + v.second, w.second) + y, | ||||
|                  HashLen16(x + w.second, y + v.second)); | ||||
| } | ||||
| 
 | ||||
| uint128 CityHash128(const char *s, size_t len) { | ||||
|   return len >= 16 | ||||
|              ? CityHash128WithSeed(s + 16, len - 16, | ||||
|                                    uint128(Fetch64(s), Fetch64(s + 8) + k0)) | ||||
|              : CityHash128WithSeed(s, len, uint128(k0, k1)); | ||||
| } | ||||
| }  // namespace hash_internal
 | ||||
| }  // namespace absl
 | ||||
| 
 | ||||
| #ifdef __SSE4_2__ | ||||
| #include <nmmintrin.h> | ||||
| #include "absl/hash/internal/city_crc.h" | ||||
| 
 | ||||
| namespace absl { | ||||
| namespace hash_internal { | ||||
| 
 | ||||
| // Requires len >= 240.
 | ||||
| static void CityHashCrc256Long(const char *s, size_t len, uint32_t seed, | ||||
|                                uint64_t *result) { | ||||
|   uint64_t a = Fetch64(s + 56) + k0; | ||||
|   uint64_t b = Fetch64(s + 96) + k0; | ||||
|   uint64_t c = result[0] = HashLen16(b, len); | ||||
|   uint64_t d = result[1] = Fetch64(s + 120) * k0 + len; | ||||
|   uint64_t e = Fetch64(s + 184) + seed; | ||||
|   uint64_t f = 0; | ||||
|   uint64_t g = 0; | ||||
|   uint64_t h = c + d; | ||||
|   uint64_t x = seed; | ||||
|   uint64_t y = 0; | ||||
|   uint64_t z = 0; | ||||
| 
 | ||||
|   // 240 bytes of input per iter.
 | ||||
|   size_t iters = len / 240; | ||||
|   len -= iters * 240; | ||||
|   do { | ||||
| #undef CHUNK | ||||
| #define CHUNK(r)               \ | ||||
|   PERMUTE3(x, z, y);           \ | ||||
|   b += Fetch64(s);             \ | ||||
|   c += Fetch64(s + 8);         \ | ||||
|   d += Fetch64(s + 16);        \ | ||||
|   e += Fetch64(s + 24);        \ | ||||
|   f += Fetch64(s + 32);        \ | ||||
|   a += b;                      \ | ||||
|   h += f;                      \ | ||||
|   b += c;                      \ | ||||
|   f += d;                      \ | ||||
|   g += e;                      \ | ||||
|   e += z;                      \ | ||||
|   g += x;                      \ | ||||
|   z = _mm_crc32_u64(z, b + g); \ | ||||
|   y = _mm_crc32_u64(y, e + h); \ | ||||
|   x = _mm_crc32_u64(x, f + a); \ | ||||
|   e = Rotate(e, r);            \ | ||||
|   c += e;                      \ | ||||
|   s += 40 | ||||
| 
 | ||||
|     CHUNK(0); | ||||
|     PERMUTE3(a, h, c); | ||||
|     CHUNK(33); | ||||
|     PERMUTE3(a, h, f); | ||||
|     CHUNK(0); | ||||
|     PERMUTE3(b, h, f); | ||||
|     CHUNK(42); | ||||
|     PERMUTE3(b, h, d); | ||||
|     CHUNK(0); | ||||
|     PERMUTE3(b, h, e); | ||||
|     CHUNK(33); | ||||
|     PERMUTE3(a, h, e); | ||||
|   } while (--iters > 0); | ||||
| 
 | ||||
|   while (len >= 40) { | ||||
|     CHUNK(29); | ||||
|     e ^= Rotate(a, 20); | ||||
|     h += Rotate(b, 30); | ||||
|     g ^= Rotate(c, 40); | ||||
|     f += Rotate(d, 34); | ||||
|     PERMUTE3(c, h, g); | ||||
|     len -= 40; | ||||
|   } | ||||
|   if (len > 0) { | ||||
|     s = s + len - 40; | ||||
|     CHUNK(33); | ||||
|     e ^= Rotate(a, 43); | ||||
|     h += Rotate(b, 42); | ||||
|     g ^= Rotate(c, 41); | ||||
|     f += Rotate(d, 40); | ||||
|   } | ||||
|   result[0] ^= h; | ||||
|   result[1] ^= g; | ||||
|   g += h; | ||||
|   a = HashLen16(a, g + z); | ||||
|   x += y << 32; | ||||
|   b += x; | ||||
|   c = HashLen16(c, z) + h; | ||||
|   d = HashLen16(d, e + result[0]); | ||||
|   g += e; | ||||
|   h += HashLen16(x, f); | ||||
|   e = HashLen16(a, d) + g; | ||||
|   z = HashLen16(b, c) + a; | ||||
|   y = HashLen16(g, h) + c; | ||||
|   result[0] = e + z + y + x; | ||||
|   a = ShiftMix((a + y) * k0) * k0 + b; | ||||
|   result[1] += a + result[0]; | ||||
|   a = ShiftMix(a * k0) * k0 + c; | ||||
|   result[2] = a + result[1]; | ||||
|   a = ShiftMix((a + e) * k0) * k0; | ||||
|   result[3] = a + result[2]; | ||||
| } | ||||
| 
 | ||||
| // Requires len < 240.
 | ||||
| static void CityHashCrc256Short(const char *s, size_t len, uint64_t *result) { | ||||
|   char buf[240]; | ||||
|   memcpy(buf, s, len); | ||||
|   memset(buf + len, 0, 240 - len); | ||||
|   CityHashCrc256Long(buf, 240, ~static_cast<uint32_t>(len), result); | ||||
| } | ||||
| 
 | ||||
| void CityHashCrc256(const char *s, size_t len, uint64_t *result) { | ||||
|   if (ABSL_PREDICT_TRUE(len >= 240)) { | ||||
|     CityHashCrc256Long(s, len, 0, result); | ||||
|   } else { | ||||
|     CityHashCrc256Short(s, len, result); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) { | ||||
|   if (len <= 900) { | ||||
|     return CityHash128WithSeed(s, len, seed); | ||||
|   } else { | ||||
|     uint64_t result[4]; | ||||
|     CityHashCrc256(s, len, result); | ||||
|     uint64_t u = Uint128High64(seed) + result[0]; | ||||
|     uint64_t v = Uint128Low64(seed) + result[1]; | ||||
|     return uint128(HashLen16(u, v + result[2]), | ||||
|                    HashLen16(Rotate(v, 32), u * k0 + result[3])); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| uint128 CityHashCrc128(const char *s, size_t len) { | ||||
|   if (len <= 900) { | ||||
|     return CityHash128(s, len); | ||||
|   } else { | ||||
|     uint64_t result[4]; | ||||
|     CityHashCrc256(s, len, result); | ||||
|     return uint128(result[2], result[3]); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| }  // namespace hash_internal
 | ||||
| }  // namespace absl
 | ||||
| 
 | ||||
| #endif | ||||
|  |  | |||
|  | @ -23,15 +23,6 @@ | |||
| // is Murmur3.  For 64-bit x86 code, CityHash64 is an excellent choice for hash
 | ||||
| // tables and most other hashing (excluding cryptography).
 | ||||
| //
 | ||||
| // For 64-bit x86 code, on long strings, the picture is more complicated.
 | ||||
| // On many recent Intel CPUs, such as Nehalem, Westmere, Sandy Bridge, etc.,
 | ||||
| // CityHashCrc128 appears to be faster than all competitors of comparable
 | ||||
| // quality.  CityHash128 is also good but not quite as fast.  We believe our
 | ||||
| // nearest competitor is Bob Jenkins' Spooky.  We don't have great data for
 | ||||
| // other 64-bit CPUs, but for long strings we know that Spooky is slightly
 | ||||
| // faster than CityHash on some relatively recent AMD x86-64 CPUs, for example.
 | ||||
| // Note that CityHashCrc128 is declared in citycrc.h.
 | ||||
| //
 | ||||
| // For 32-bit x86 code, we don't know of anything faster than CityHash32 that
 | ||||
| // is of comparable quality.  We believe our nearest competitor is Murmur3A.
 | ||||
| // (On 64-bit CPUs, it is typically faster to use the other CityHash variants.)
 | ||||
|  | @ -79,13 +70,6 @@ uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed); | |||
| uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0, | ||||
|                            uint64_t seed1); | ||||
| 
 | ||||
| // Hash function for a byte array.
 | ||||
| uint128 CityHash128(const char *s, size_t len); | ||||
| 
 | ||||
| // Hash function for a byte array.  For convenience, a 128-bit seed is also
 | ||||
| // hashed into the result.
 | ||||
| uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed); | ||||
| 
 | ||||
| // Hash function for a byte array.  Most useful in 32-bit binaries.
 | ||||
| uint32_t CityHash32(const char *s, size_t len); | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,41 +0,0 @@ | |||
| // Copyright 2018 The Abseil Authors.
 | ||||
| //
 | ||||
| // Licensed under the Apache License, Version 2.0 (the "License");
 | ||||
| // you may not use this file except in compliance with the License.
 | ||||
| // You may obtain a copy of the License at
 | ||||
| //
 | ||||
| //      http://www.apache.org/licenses/LICENSE-2.0
 | ||||
| //
 | ||||
| // Unless required by applicable law or agreed to in writing, software
 | ||||
| // distributed under the License is distributed on an "AS IS" BASIS,
 | ||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | ||||
| // See the License for the specific language governing permissions and
 | ||||
| // limitations under the License.
 | ||||
| //
 | ||||
| // This file declares the subset of the CityHash functions that require
 | ||||
| // _mm_crc32_u64().  See the CityHash README for details.
 | ||||
| //
 | ||||
| // Functions in the CityHash family are not suitable for cryptography.
 | ||||
| 
 | ||||
| #ifndef ABSL_HASH_INTERNAL_CITY_CRC_H_ | ||||
| #define ABSL_HASH_INTERNAL_CITY_CRC_H_ | ||||
| 
 | ||||
| #include "absl/hash/internal/city.h" | ||||
| 
 | ||||
| namespace absl { | ||||
| namespace hash_internal { | ||||
| 
 | ||||
| // Hash function for a byte array.
 | ||||
| uint128 CityHashCrc128(const char *s, size_t len); | ||||
| 
 | ||||
| // Hash function for a byte array.  For convenience, a 128-bit seed is also
 | ||||
| // hashed into the result.
 | ||||
| uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed); | ||||
| 
 | ||||
| // Hash function for a byte array.  Sets result[0] ... result[3].
 | ||||
| void CityHashCrc256(const char *s, size_t len, uint64_t *result); | ||||
| 
 | ||||
| }  // namespace hash_internal
 | ||||
| }  // namespace absl
 | ||||
| 
 | ||||
| #endif  // ABSL_HASH_INTERNAL_CITY_CRC_H_
 | ||||
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							|  | @ -605,35 +605,21 @@ TEST_F(ParsedFormatTest, RegressionMixPositional) { | |||
| // Some codegen thunks that we can use to easily dump the generated assembly for
 | ||||
| // different StrFormat calls.
 | ||||
| 
 | ||||
| inline std::string CodegenAbslStrFormatInt(int i) { | ||||
| std::string CodegenAbslStrFormatInt(int i) { // NOLINT
 | ||||
|   return absl::StrFormat("%d", i); | ||||
| } | ||||
| 
 | ||||
| inline std::string CodegenAbslStrFormatIntStringInt64(int i, const std::string& s, | ||||
|                                                  int64_t i64) { | ||||
| std::string CodegenAbslStrFormatIntStringInt64(int i, const std::string& s, | ||||
|                                                  int64_t i64) { // NOLINT
 | ||||
|   return absl::StrFormat("%d %s %d", i, s, i64); | ||||
| } | ||||
| 
 | ||||
| inline void CodegenAbslStrAppendFormatInt(std::string* out, int i) { | ||||
| void CodegenAbslStrAppendFormatInt(std::string* out, int i) { // NOLINT
 | ||||
|   absl::StrAppendFormat(out, "%d", i); | ||||
| } | ||||
| 
 | ||||
| inline void CodegenAbslStrAppendFormatIntStringInt64(std::string* out, int i, | ||||
| void CodegenAbslStrAppendFormatIntStringInt64(std::string* out, int i, | ||||
|                                                      const std::string& s, | ||||
|                                                      int64_t i64) { | ||||
|                                                      int64_t i64) { // NOLINT
 | ||||
|   absl::StrAppendFormat(out, "%d %s %d", i, s, i64); | ||||
| } | ||||
| 
 | ||||
| auto absl_internal_str_format_force_codegen_funcs = std::make_tuple( | ||||
|     CodegenAbslStrFormatInt, CodegenAbslStrFormatIntStringInt64, | ||||
|     CodegenAbslStrAppendFormatInt, CodegenAbslStrAppendFormatIntStringInt64); | ||||
| 
 | ||||
| bool absl_internal_str_format_force_codegen_always_false; | ||||
| // Force the compiler to generate the functions by making it look like we
 | ||||
| // escape the function pointers.
 | ||||
| // It can't statically know that
 | ||||
| // absl_internal_str_format_force_codegen_always_false is not changed by someone
 | ||||
| // else.
 | ||||
| bool absl_internal_str_format_force_codegen = | ||||
|     absl_internal_str_format_force_codegen_always_false && | ||||
|     printf("%p", &absl_internal_str_format_force_codegen_funcs) == 0; | ||||
|  |  | |||
|  | @ -27,6 +27,7 @@ licenses(["notice"])  # Apache 2.0 | |||
| cc_library( | ||||
|     name = "time", | ||||
|     srcs = [ | ||||
|         "civil_time.cc", | ||||
|         "clock.cc", | ||||
|         "duration.cc", | ||||
|         "format.cc", | ||||
|  | @ -35,6 +36,7 @@ cc_library( | |||
|         "time.cc", | ||||
|     ], | ||||
|     hdrs = [ | ||||
|         "civil_time.h", | ||||
|         "clock.h", | ||||
|         "time.h", | ||||
|     ], | ||||
|  | @ -72,10 +74,10 @@ cc_library( | |||
| cc_test( | ||||
|     name = "time_test", | ||||
|     srcs = [ | ||||
|         "civil_time_test.cc", | ||||
|         "clock_test.cc", | ||||
|         "duration_test.cc", | ||||
|         "format_test.cc", | ||||
|         "time_norm_test.cc", | ||||
|         "time_test.cc", | ||||
|         "time_zone_test.cc", | ||||
|     ], | ||||
|  | @ -94,6 +96,7 @@ cc_test( | |||
| cc_test( | ||||
|     name = "time_benchmark", | ||||
|     srcs = [ | ||||
|         "civil_time_benchmark.cc", | ||||
|         "clock_benchmark.cc", | ||||
|         "duration_benchmark.cc", | ||||
|         "format_benchmark.cc", | ||||
|  |  | |||
|  | @ -15,6 +15,7 @@ | |||
| # | ||||
| 
 | ||||
| list(APPEND TIME_PUBLIC_HEADERS | ||||
|   "civil_time.h" | ||||
|   "clock.h" | ||||
|   "time.h" | ||||
| ) | ||||
|  | @ -29,6 +30,7 @@ list(APPEND TIME_INTERNAL_HEADERS | |||
| ) | ||||
| 
 | ||||
| list(APPEND TIME_SRC | ||||
|   "civil_time.cc" | ||||
|   "time.cc" | ||||
|   "clock.cc" | ||||
|   "duration.cc" | ||||
|  | @ -74,11 +76,11 @@ absl_library( | |||
| 
 | ||||
| # test time_test | ||||
| list(APPEND TIME_TEST_SRC | ||||
|   "civil_time_test.cc" | ||||
|   "time_test.cc" | ||||
|   "clock_test.cc" | ||||
|   "duration_test.cc" | ||||
|   "format_test.cc" | ||||
|   "time_norm_test.cc" | ||||
|   "time_test.cc" | ||||
|   "time_zone_test.cc" | ||||
|   "internal/test_util.cc" | ||||
|  |  | |||
							
								
								
									
										88
									
								
								absl/time/civil_time.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								absl/time/civil_time.cc
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,88 @@ | |||
| // Copyright 2018 The Abseil Authors.
 | ||||
| //
 | ||||
| // Licensed under the Apache License, Version 2.0 (the "License");
 | ||||
| // you may not use this file except in compliance with the License.
 | ||||
| // You may obtain a copy of the License at
 | ||||
| //
 | ||||
| //      http://www.apache.org/licenses/LICENSE-2.0
 | ||||
| //
 | ||||
| // Unless required by applicable law or agreed to in writing, software
 | ||||
| // distributed under the License is distributed on an "AS IS" BASIS,
 | ||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | ||||
| // See the License for the specific language governing permissions and
 | ||||
| // limitations under the License.
 | ||||
| 
 | ||||
| #include "absl/time/civil_time.h" | ||||
| 
 | ||||
| #include <cstdlib> | ||||
| #include <string> | ||||
| 
 | ||||
| #include "absl/strings/str_cat.h" | ||||
| #include "absl/time/time.h" | ||||
| 
 | ||||
| namespace absl { | ||||
| 
 | ||||
| namespace { | ||||
| 
 | ||||
| // Since a civil time has a larger year range than absl::Time (64-bit years vs
 | ||||
| // 64-bit seconds, respectively) we normalize years to roughly +/- 400 years
 | ||||
| // around the year 2400, which will produce an equivalent year in a range that
 | ||||
| // absl::Time can handle.
 | ||||
| inline civil_year_t NormalizeYear(civil_year_t year) { | ||||
|   return 2400 + year % 400; | ||||
| } | ||||
| 
 | ||||
| // Formats the given CivilSecond according to the given format.
 | ||||
| std::string FormatYearAnd(string_view fmt, CivilSecond cs) { | ||||
|   const CivilSecond ncs(NormalizeYear(cs.year()), cs.month(), cs.day(), | ||||
|                         cs.hour(), cs.minute(), cs.second()); | ||||
|   const TimeZone utc = UTCTimeZone(); | ||||
|   // TODO(absl-team): Avoid conversion of fmt std::string.
 | ||||
|   return StrCat(cs.year(), FormatTime(std::string(fmt), FromCivil(ncs, utc), utc)); | ||||
| } | ||||
| 
 | ||||
| }  // namespace
 | ||||
| 
 | ||||
| std::string FormatCivilTime(CivilSecond c) { | ||||
|   return FormatYearAnd("-%m-%dT%H:%M:%S", c); | ||||
| } | ||||
| std::string FormatCivilTime(CivilMinute c) { | ||||
|   return FormatYearAnd("-%m-%dT%H:%M", c); | ||||
| } | ||||
| std::string FormatCivilTime(CivilHour c) { | ||||
|   return FormatYearAnd("-%m-%dT%H", c); | ||||
| } | ||||
| std::string FormatCivilTime(CivilDay c) { | ||||
|   return FormatYearAnd("-%m-%d", c); | ||||
| } | ||||
| std::string FormatCivilTime(CivilMonth c) { | ||||
|   return FormatYearAnd("-%m", c); | ||||
| } | ||||
| std::string FormatCivilTime(CivilYear c) { | ||||
|   return FormatYearAnd("", c); | ||||
| } | ||||
| 
 | ||||
| namespace time_internal { | ||||
| 
 | ||||
| std::ostream& operator<<(std::ostream& os, CivilYear y) { | ||||
|   return os << FormatCivilTime(y); | ||||
| } | ||||
| std::ostream& operator<<(std::ostream& os, CivilMonth m) { | ||||
|   return os << FormatCivilTime(m); | ||||
| } | ||||
| std::ostream& operator<<(std::ostream& os, CivilDay d) { | ||||
|   return os << FormatCivilTime(d); | ||||
| } | ||||
| std::ostream& operator<<(std::ostream& os, CivilHour h) { | ||||
|   return os << FormatCivilTime(h); | ||||
| } | ||||
| std::ostream& operator<<(std::ostream& os, CivilMinute m) { | ||||
|   return os << FormatCivilTime(m); | ||||
| } | ||||
| std::ostream& operator<<(std::ostream& os, CivilSecond s) { | ||||
|   return os << FormatCivilTime(s); | ||||
| } | ||||
| 
 | ||||
| }  // namespace time_internal
 | ||||
| 
 | ||||
| }  // namespace absl
 | ||||
							
								
								
									
										487
									
								
								absl/time/civil_time.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										487
									
								
								absl/time/civil_time.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,487 @@ | |||
| // Copyright 2018 The Abseil Authors.
 | ||||
| //
 | ||||
| // Licensed under the Apache License, Version 2.0 (the "License");
 | ||||
| // you may not use this file except in compliance with the License.
 | ||||
| // You may obtain a copy of the License at
 | ||||
| //
 | ||||
| //      http://www.apache.org/licenses/LICENSE-2.0
 | ||||
| //
 | ||||
| // Unless required by applicable law or agreed to in writing, software
 | ||||
| // distributed under the License is distributed on an "AS IS" BASIS,
 | ||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | ||||
| // See the License for the specific language governing permissions and
 | ||||
| // limitations under the License.
 | ||||
| //
 | ||||
| // -----------------------------------------------------------------------------
 | ||||
| // File: civil_time.h
 | ||||
| // -----------------------------------------------------------------------------
 | ||||
| //
 | ||||
| // This header file defines abstractions for computing with "civil time".
 | ||||
| // The term "civil time" refers to the legally recognized human-scale time
 | ||||
| // that is represented by the six fields `YYYY-MM-DD hh:mm:ss`. A "date"
 | ||||
| // is perhaps the most common example of a civil time (represented here as
 | ||||
| // an `absl::CivilDay`).
 | ||||
| //
 | ||||
| // Modern-day civil time follows the Gregorian Calendar and is a
 | ||||
| // time-zone-independent concept: a civil time of "2015-06-01 12:00:00", for
 | ||||
| // example, is not tied to a time zone. Put another way, a civil time does not
 | ||||
| // map to a unique point in time; a civil time must be mapped to an absolute
 | ||||
| // time *through* a time zone.
 | ||||
| //
 | ||||
| // Because a civil time is what most people think of as "time," it is common to
 | ||||
| // map absolute times to civil times to present to users.
 | ||||
| //
 | ||||
| // Time zones define the relationship between absolute and civil times. Given an
 | ||||
| // absolute or civil time and a time zone, you can compute the other time:
 | ||||
| //
 | ||||
| //   Civil Time = F(Absolute Time, Time Zone)
 | ||||
| //   Absolute Time = G(Civil Time, Time Zone)
 | ||||
| //
 | ||||
| // The Abseil time library allows you to construct such civil times from
 | ||||
| // absolute times; consult time.h for such functionality.
 | ||||
| //
 | ||||
| // This library provides six classes for constructing civil-time objects, and
 | ||||
| // provides several helper functions for rounding, iterating, and performing
 | ||||
| // arithmetic on civil-time objects, while avoiding complications like
 | ||||
| // daylight-saving time (DST):
 | ||||
| //
 | ||||
| //   * `absl::CivilSecond`
 | ||||
| //   * `absl::CivilMinute`
 | ||||
| //   * `absl::CivilHour`
 | ||||
| //   * `absl::CivilDay`
 | ||||
| //   * `absl::CivilMonth`
 | ||||
| //   * `absl::CivilYear`
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   // Construct a civil-time object for a specific day
 | ||||
| //   const absl::CivilDay cd(1969, 07, 20);
 | ||||
| //
 | ||||
| //   // Construct a civil-time object for a specific second
 | ||||
| //   const absl::CivilSecond cd(2018, 8, 1, 12, 0, 1);
 | ||||
| //
 | ||||
| // Note: In C++14 and later, this library is usable in a constexpr context.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   // Valid in C++14
 | ||||
| //   constexpr absl::CivilDay cd(1969, 07, 20);
 | ||||
| //
 | ||||
| 
 | ||||
| #ifndef ABSL_TIME_CIVIL_TIME_H_ | ||||
| #define ABSL_TIME_CIVIL_TIME_H_ | ||||
| 
 | ||||
| #include <string> | ||||
| 
 | ||||
| #include "absl/base/port.h"  // Needed for string vs std::string | ||||
| #include "absl/strings/string_view.h" | ||||
| #include "absl/time/internal/cctz/include/cctz/civil_time.h" | ||||
| 
 | ||||
| namespace absl { | ||||
| 
 | ||||
| namespace time_internal { | ||||
| struct second_tag : cctz::detail::second_tag {}; | ||||
| struct minute_tag : second_tag, cctz::detail::minute_tag {}; | ||||
| struct hour_tag : minute_tag, cctz::detail::hour_tag {}; | ||||
| struct day_tag : hour_tag, cctz::detail::day_tag {}; | ||||
| struct month_tag : day_tag, cctz::detail::month_tag {}; | ||||
| struct year_tag : month_tag, cctz::detail::year_tag {}; | ||||
| }  // namespace time_internal
 | ||||
| 
 | ||||
| // -----------------------------------------------------------------------------
 | ||||
| // CivilSecond, CivilMinute, CivilHour, CivilDay, CivilMonth, CivilYear
 | ||||
| // -----------------------------------------------------------------------------
 | ||||
| //
 | ||||
| // Each of these civil-time types is a simple value type with the same
 | ||||
| // interface for construction and the same six accessors for each of the civil
 | ||||
| // time fields (year, month, day, hour, minute, and second, aka YMDHMS). These
 | ||||
| // classes differ only in their alignment, which is indicated by the type name
 | ||||
| // and specifies the field on which arithmetic operates.
 | ||||
| //
 | ||||
| // CONSTRUCTION
 | ||||
| //
 | ||||
| // Each of the civil-time types can be constructed in two ways: by directly
 | ||||
| // passing to the constructor up to six integers representing the YMDHMS fields,
 | ||||
| // or by copying the YMDHMS fields from a differently aligned civil-time type.
 | ||||
| // Omitted fields are assigned their minimum valid value. Hours, minutes, and
 | ||||
| // seconds will be set to 0, month and day will be set to 1. Since there is no
 | ||||
| // minimum year, the default is 1970.
 | ||||
| //
 | ||||
| // Examples:
 | ||||
| //
 | ||||
| //   absl::CivilDay default_value;               // 1970-01-01 00:00:00
 | ||||
| //
 | ||||
| //   absl::CivilDay a(2015, 2, 3);               // 2015-02-03 00:00:00
 | ||||
| //   absl::CivilDay b(2015, 2, 3, 4, 5, 6);      // 2015-02-03 00:00:00
 | ||||
| //   absl::CivilDay c(2015);                     // 2015-01-01 00:00:00
 | ||||
| //
 | ||||
| //   absl::CivilSecond ss(2015, 2, 3, 4, 5, 6);  // 2015-02-03 04:05:06
 | ||||
| //   absl::CivilMinute mm(ss);                   // 2015-02-03 04:05:00
 | ||||
| //   absl::CivilHour hh(mm);                     // 2015-02-03 04:00:00
 | ||||
| //   absl::CivilDay d(hh);                       // 2015-02-03 00:00:00
 | ||||
| //   absl::CivilMonth m(d);                      // 2015-02-01 00:00:00
 | ||||
| //   absl::CivilYear y(m);                       // 2015-01-01 00:00:00
 | ||||
| //
 | ||||
| //   m = absl::CivilMonth(y);                    // 2015-01-01 00:00:00
 | ||||
| //   d = absl::CivilDay(m);                      // 2015-01-01 00:00:00
 | ||||
| //   hh = absl::CivilHour(d);                    // 2015-01-01 00:00:00
 | ||||
| //   mm = absl::CivilMinute(hh);                 // 2015-01-01 00:00:00
 | ||||
| //   ss = absl::CivilSecond(mm);                 // 2015-01-01 00:00:00
 | ||||
| //
 | ||||
| // Each civil-time class is aligned to the civil-time field indicated in the
 | ||||
| // class's name after normalization. Alignment is performed by setting all the
 | ||||
| // inferior fields to their minimum valid value (as described above). The
 | ||||
| // following are examples of how each of the six types would align the fields
 | ||||
| // representing November 22, 2015 at 12:34:56 in the afternoon. (Note: the
 | ||||
| // string format used here is not important; it's just a shorthand way of
 | ||||
| // showing the six YMDHMS fields.)
 | ||||
| //
 | ||||
| //   absl::CivilSecond   : 2015-11-22 12:34:56
 | ||||
| //   absl::CivilMinute   : 2015-11-22 12:34:00
 | ||||
| //   absl::CivilHour     : 2015-11-22 12:00:00
 | ||||
| //   absl::CivilDay      : 2015-11-22 00:00:00
 | ||||
| //   absl::CivilMonth    : 2015-11-01 00:00:00
 | ||||
| //   absl::CivilYear     : 2015-01-01 00:00:00
 | ||||
| //
 | ||||
| // Each civil-time type performs arithmetic on the field to which it is
 | ||||
| // aligned. This means that adding 1 to an absl::CivilDay increments the day
 | ||||
| // field (normalizing as necessary), and subtracting 7 from an absl::CivilMonth
 | ||||
| // operates on the month field (normalizing as necessary). All arithmetic
 | ||||
| // produces a valid civil time. Difference requires two similarly aligned
 | ||||
| // civil-time objects and returns the scalar answer in units of the objects'
 | ||||
| // alignment. For example, the difference between two absl::CivilHour objects
 | ||||
| // will give an answer in units of civil hours.
 | ||||
| //
 | ||||
| // ALIGNMENT CONVERSION
 | ||||
| //
 | ||||
| // The alignment of a civil-time object cannot change, but the object may be
 | ||||
| // used to construct a new object with a different alignment. This is referred
 | ||||
| // to as "realigning". When realigning to a type with the same or more
 | ||||
| // precision (e.g., absl::CivilDay -> absl::CivilSecond), the conversion may be
 | ||||
| // performed implicitly since no information is lost. However, if information
 | ||||
| // could be discarded (e.g., CivilSecond -> CivilDay), the conversion must
 | ||||
| // be explicit at the call site.
 | ||||
| //
 | ||||
| // Examples:
 | ||||
| //
 | ||||
| //   void UseDay(absl::CivilDay day);
 | ||||
| //
 | ||||
| //   absl::CivilSecond cs;
 | ||||
| //   UseDay(cs);                  // Won't compile because data may be discarded
 | ||||
| //   UseDay(absl::CivilDay(cs));  // OK: explicit conversion
 | ||||
| //
 | ||||
| //   absl::CivilDay cd;
 | ||||
| //   UseDay(cd);                  // OK: no conversion needed
 | ||||
| //
 | ||||
| //   absl::CivilMonth cm;
 | ||||
| //   UseDay(cm);                  // OK: implicit conversion to absl::CivilDay
 | ||||
| //
 | ||||
| // NORMALIZATION
 | ||||
| //
 | ||||
| // Normalization takes invalid values and adjusts them to produce valid values.
 | ||||
| // Within the civil-time library, integer arguments passed to the Civil*
 | ||||
| // constructors may be out-of-range, in which case they are normalized by
 | ||||
| // carrying overflow into a field of courser granularity to produce valid
 | ||||
| // civil-time objects. This normalization enables natural arithmetic on
 | ||||
| // constructor arguments without worrying about the field's range.
 | ||||
| //
 | ||||
| // Examples:
 | ||||
| //
 | ||||
| //   // Out-of-range; normalized to 2016-11-01
 | ||||
| //   absl::CivilDay d(2016, 10, 32);
 | ||||
| //   // Out-of-range, negative: normalized to 2016-10-30T23
 | ||||
| //   absl::CivilHour h1(2016, 10, 31, -1);
 | ||||
| //   // Normalization is cumulative: normalized to 2016-10-30T23
 | ||||
| //   absl::CivilHour h2(2016, 10, 32, -25);
 | ||||
| //
 | ||||
| // Note: If normalization is undesired, you can signal an error by comparing
 | ||||
| // the constructor arguments to the normalized values returned by the YMDHMS
 | ||||
| // properties.
 | ||||
| //
 | ||||
| // COMPARISON
 | ||||
| //
 | ||||
| // Comparison between civil-time objects considers all six YMDHMS fields,
 | ||||
| // regardless of the type's alignment. Comparison between differently aligned
 | ||||
| // civil-time types is allowed.
 | ||||
| //
 | ||||
| // Examples:
 | ||||
| //
 | ||||
| //   absl::CivilDay feb_3(2015, 2, 3);  // 2015-02-03 00:00:00
 | ||||
| //   absl::CivilDay mar_4(2015, 3, 4);  // 2015-03-04 00:00:00
 | ||||
| //   // feb_3 < mar_4
 | ||||
| //   // absl::CivilYear(feb_3) == absl::CivilYear(mar_4)
 | ||||
| //
 | ||||
| //   absl::CivilSecond feb_3_noon(2015, 2, 3, 12, 0, 0);  // 2015-02-03 12:00:00
 | ||||
| //   // feb_3 < feb_3_noon
 | ||||
| //   // feb_3 == absl::CivilDay(feb_3_noon)
 | ||||
| //
 | ||||
| //   // Iterates all the days of February 2015.
 | ||||
| //   for (absl::CivilDay d(2015, 2, 1); d < absl::CivilMonth(2015, 3); ++d) {
 | ||||
| //     // ...
 | ||||
| //   }
 | ||||
| //
 | ||||
| // ARITHMETIC
 | ||||
| //
 | ||||
| // Civil-time types support natural arithmetic operators such as addition,
 | ||||
| // subtraction, and difference. Arithmetic operates on the civil-time field
 | ||||
| // indicated in the type's name. Difference operators require arguments with
 | ||||
| // the same alignment and return the answer in units of the alignment.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::CivilDay a(2015, 2, 3);
 | ||||
| //   ++a;                              // 2015-02-04 00:00:00
 | ||||
| //   --a;                              // 2015-02-03 00:00:00
 | ||||
| //   absl::CivilDay b = a + 1;         // 2015-02-04 00:00:00
 | ||||
| //   absl::CivilDay c = 1 + b;         // 2015-02-05 00:00:00
 | ||||
| //   int n = c - a;                    // n = 2 (civil days)
 | ||||
| //   int m = c - absl::CivilMonth(c);  // Won't compile: different types.
 | ||||
| //
 | ||||
| // ACCESSORS
 | ||||
| //
 | ||||
| // Each civil-time type has accessors for all six of the civil-time fields:
 | ||||
| // year, month, day, hour, minute, and second.
 | ||||
| //
 | ||||
| // civil_year_t year()
 | ||||
| // int          month()
 | ||||
| // int          day()
 | ||||
| // int          hour()
 | ||||
| // int          minute()
 | ||||
| // int          second()
 | ||||
| //
 | ||||
| // Recall that fields inferior to the type's aligment will be set to their
 | ||||
| // minimum valid value.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::CivilDay d(2015, 6, 28);
 | ||||
| //   // d.year() == 2015
 | ||||
| //   // d.month() == 6
 | ||||
| //   // d.day() == 28
 | ||||
| //   // d.hour() == 0
 | ||||
| //   // d.minute() == 0
 | ||||
| //   // d.second() == 0
 | ||||
| //
 | ||||
| // CASE STUDY: Adding a month to January 31.
 | ||||
| //
 | ||||
| // One of the classic questions that arises when considering a civil time
 | ||||
| // library (or a date library or a date/time library) is this:
 | ||||
| //   "What is the result of adding a month to January 31?"
 | ||||
| // This is an interesting question because it is unclear what is meant by a
 | ||||
| // "month", and several different answers are possible, depending on context:
 | ||||
| //
 | ||||
| //   1. March 3 (or 2 if a leap year), if "add a month" means to add a month to
 | ||||
| //      the current month, and adjust the date to overflow the extra days into
 | ||||
| //      March. In this case the result of "February 31" would be normalized as
 | ||||
| //      within the civil-time library.
 | ||||
| //   2. February 28 (or 29 if a leap year), if "add a month" means to add a
 | ||||
| //      month, and adjust the date while holding the resulting month constant.
 | ||||
| //      In this case, the result of "February 31" would be truncated to the last
 | ||||
| //      day in February.
 | ||||
| //   3. An error. The caller may get some error, an exception, an invalid date
 | ||||
| //      object, or perhaps return `false`. This may make sense because there is
 | ||||
| //      no single unambiguously correct answer to the question.
 | ||||
| //
 | ||||
| // Practically speaking, any answer that is not what the programmer intended
 | ||||
| // is the wrong answer.
 | ||||
| //
 | ||||
| // The Abseil time library avoids this problem by making it impossible to
 | ||||
| // ask ambiguous questions. All civil-time objects are aligned to a particular
 | ||||
| // civil-field boundary (such as aligned to a year, month, day, hour, minute,
 | ||||
| // or second), and arithmetic operates on the field to which the object is
 | ||||
| // aligned. This means that in order to "add a month" the object must first be
 | ||||
| // aligned to a month boundary, which is equivalent to the first day of that
 | ||||
| // month.
 | ||||
| //
 | ||||
| // Of course, there are ways to compute an answer the question at hand using
 | ||||
| // this Abseil time library, but they require the programmer to be explicit
 | ||||
| // about the answer they expect. To illustrate, let's see how to compute all
 | ||||
| // three of the above possible answers to the question of "Jan 31 plus 1
 | ||||
| // month":
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   const absl::CivilDay d(2015, 1, 31);
 | ||||
| //
 | ||||
| //   // Answer 1:
 | ||||
| //   // Add 1 to the month field in the constructor, and rely on normalization.
 | ||||
| //   const auto normalized = absl::CivilDay(d.year(), d.month() + 1, d.day());
 | ||||
| //   // normalized == 2015-03-03 (aka Feb 31)
 | ||||
| //
 | ||||
| //   // Answer 2:
 | ||||
| //   // Add 1 to month field, capping to the end of next month.
 | ||||
| //   const auto next_month = absl::CivilMonth(d) + 1;
 | ||||
| //   const auto last_day_of_next_month = absl::CivilDay(next_month + 1) - 1;
 | ||||
| //   const auto capped = std::min(normalized, last_day_of_next_month);
 | ||||
| //   // capped == 2015-02-28
 | ||||
| //
 | ||||
| //   // Answer 3:
 | ||||
| //   // Signal an error if the normalized answer is not in next month.
 | ||||
| //   if (absl::CivilMonth(normalized) != next_month) {
 | ||||
| //     // error, month overflow
 | ||||
| //   }
 | ||||
| //
 | ||||
| using CivilSecond = | ||||
|     time_internal::cctz::detail::civil_time<time_internal::second_tag>; | ||||
| using CivilMinute = | ||||
|     time_internal::cctz::detail::civil_time<time_internal::minute_tag>; | ||||
| using CivilHour = | ||||
|     time_internal::cctz::detail::civil_time<time_internal::hour_tag>; | ||||
| using CivilDay = | ||||
|     time_internal::cctz::detail::civil_time<time_internal::day_tag>; | ||||
| using CivilMonth = | ||||
|     time_internal::cctz::detail::civil_time<time_internal::month_tag>; | ||||
| using CivilYear = | ||||
|     time_internal::cctz::detail::civil_time<time_internal::year_tag>; | ||||
| 
 | ||||
| // civil_year_t
 | ||||
| //
 | ||||
| // Type alias of a civil-time year value. This type is guaranteed to (at least)
 | ||||
| // support any year value supported by `time_t`.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::CivilSecond cs = ...;
 | ||||
| //   absl::civil_year_t y = cs.year();
 | ||||
| //   cs = absl::CivilSecond(y, 1, 1, 0, 0 0);  // CivilSecond(CivilYear(cs))
 | ||||
| //
 | ||||
| using civil_year_t = time_internal::cctz::year_t; | ||||
| 
 | ||||
| // civil_diff_t
 | ||||
| //
 | ||||
| // Type alias of the difference between two civil-time values.
 | ||||
| // This type is used to indicate arguments that are not
 | ||||
| // normalized (such as parameters to the civil-time constructors), the results
 | ||||
| // of civil-time subtraction, or the operand to civil-time addition.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::civil_diff_t n_sec = cs1 - cs2;             // cs1 == cs2 + n_sec;
 | ||||
| //
 | ||||
| using civil_diff_t = time_internal::cctz::diff_t; | ||||
| 
 | ||||
| // Weekday::monday, Weekday::tuesday, Weekday::wednesday, Weekday::thursday,
 | ||||
| // Weekday::friday, Weekday::saturday, Weekday::sunday
 | ||||
| //
 | ||||
| // The Weekday enum class represents the civil-time concept of a "weekday" with
 | ||||
| // members for all days of the week.
 | ||||
| //
 | ||||
| //   absl::Weekday wd = absl::Weekday::thursday;
 | ||||
| //
 | ||||
| using Weekday = time_internal::cctz::weekday; | ||||
| 
 | ||||
| // GetWeekday()
 | ||||
| //
 | ||||
| // Returns the absl::Weekday for the given absl::CivilDay.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::CivilDay a(2015, 8, 13);
 | ||||
| //   absl::Weekday wd = absl::GetWeekday(a);  // wd == absl::Weekday::thursday
 | ||||
| //
 | ||||
| inline Weekday GetWeekday(CivilDay cd) { | ||||
|   return time_internal::cctz::get_weekday(cd); | ||||
| } | ||||
| 
 | ||||
| // NextWeekday()
 | ||||
| // PrevWeekday()
 | ||||
| //
 | ||||
| // Returns the absl::CivilDay that strictly follows or precedes a given
 | ||||
| // absl::CivilDay, and that falls on the given absl::Weekday.
 | ||||
| //
 | ||||
| // Example, given the following month:
 | ||||
| //
 | ||||
| //       August 2015
 | ||||
| //   Su Mo Tu We Th Fr Sa
 | ||||
| //                      1
 | ||||
| //    2  3  4  5  6  7  8
 | ||||
| //    9 10 11 12 13 14 15
 | ||||
| //   16 17 18 19 20 21 22
 | ||||
| //   23 24 25 26 27 28 29
 | ||||
| //   30 31
 | ||||
| //
 | ||||
| //   absl::CivilDay a(2015, 8, 13);
 | ||||
| //   // absl::GetWeekday(a) == absl::Weekday::thursday
 | ||||
| //   absl::CivilDay b = absl::NextWeekday(a, absl::Weekday::thursday);
 | ||||
| //   // b = 2015-08-20
 | ||||
| //   absl::CivilDay c = absl::PrevWeekday(a, absl::Weekday::thursday);
 | ||||
| //   // c = 2015-08-06
 | ||||
| //
 | ||||
| //   absl::CivilDay d = ...
 | ||||
| //   // Gets the following Thursday if d is not already Thursday
 | ||||
| //   absl::CivilDay thurs1 = absl::PrevWeekday(d, absl::Weekday::thursday) + 7;
 | ||||
| //   // Gets the previous Thursday if d is not already Thursday
 | ||||
| //   absl::CivilDay thurs2 = absl::NextWeekday(d, absl::Weekday::thursday) - 7;
 | ||||
| //
 | ||||
| inline CivilDay NextWeekday(CivilDay cd, Weekday wd) { | ||||
|   return CivilDay(time_internal::cctz::next_weekday(cd, wd)); | ||||
| } | ||||
| inline CivilDay PrevWeekday(CivilDay cd, Weekday wd) { | ||||
|   return CivilDay(time_internal::cctz::prev_weekday(cd, wd)); | ||||
| } | ||||
| 
 | ||||
| // GetYearDay()
 | ||||
| //
 | ||||
| // Returns the day-of-year for the given absl::CivilDay.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::CivilDay a(2015, 1, 1);
 | ||||
| //   int yd_jan_1 = absl::GetYearDay(a);   // yd_jan_1 = 1
 | ||||
| //   absl::CivilDay b(2015, 12, 31);
 | ||||
| //   int yd_dec_31 = absl::GetYearDay(b);  // yd_dec_31 = 365
 | ||||
| //
 | ||||
| inline int GetYearDay(CivilDay cd) { | ||||
|   return time_internal::cctz::get_yearday(cd); | ||||
| } | ||||
| 
 | ||||
| // FormatCivilTime()
 | ||||
| //
 | ||||
| // Formats the given civil-time value into a string value of the following
 | ||||
| // format:
 | ||||
| //
 | ||||
| //  Type        | Format
 | ||||
| //  ---------------------------------
 | ||||
| //  CivilSecond | YYYY-MM-DDTHH:MM:SS
 | ||||
| //  CivilMinute | YYYY-MM-DDTHH:MM
 | ||||
| //  CivilHour   | YYYY-MM-DDTHH
 | ||||
| //  CivilDay    | YYYY-MM-DD
 | ||||
| //  CivilMonth  | YYYY-MM
 | ||||
| //  CivilYear   | YYYY
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::CivilDay d = absl::CivilDay(1969, 7, 20);
 | ||||
| //   string day_string = absl::FormatCivilTime(d);          // "1969-07-20"
 | ||||
| //
 | ||||
| std::string FormatCivilTime(CivilSecond c); | ||||
| std::string FormatCivilTime(CivilMinute c); | ||||
| std::string FormatCivilTime(CivilHour c); | ||||
| std::string FormatCivilTime(CivilDay c); | ||||
| std::string FormatCivilTime(CivilMonth c); | ||||
| std::string FormatCivilTime(CivilYear c); | ||||
| 
 | ||||
| namespace time_internal {  // For functions found via ADL on civil-time tags.
 | ||||
| 
 | ||||
| // Streaming Operators
 | ||||
| //
 | ||||
| // Each civil-time type may be sent to an output stream using operator<<().
 | ||||
| // The result matches the string produced by `FormatCivilTime()`.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::CivilDay d = absl::CivilDay("1969-07-20");
 | ||||
| //   std::cout << "Date is: " << d << "\n";
 | ||||
| //
 | ||||
| std::ostream& operator<<(std::ostream& os, CivilYear y); | ||||
| std::ostream& operator<<(std::ostream& os, CivilMonth m); | ||||
| std::ostream& operator<<(std::ostream& os, CivilDay d); | ||||
| std::ostream& operator<<(std::ostream& os, CivilHour h); | ||||
| std::ostream& operator<<(std::ostream& os, CivilMinute m); | ||||
| std::ostream& operator<<(std::ostream& os, CivilSecond s); | ||||
| 
 | ||||
| }  // namespace time_internal
 | ||||
| 
 | ||||
| }  // namespace absl
 | ||||
| 
 | ||||
| #endif  // ABSL_TIME_CIVIL_TIME_H_
 | ||||
							
								
								
									
										57
									
								
								absl/time/civil_time_benchmark.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								absl/time/civil_time_benchmark.cc
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,57 @@ | |||
| // Copyright 2018 The Abseil Authors.
 | ||||
| //
 | ||||
| // Licensed under the Apache License, Version 2.0 (the "License");
 | ||||
| // you may not use this file except in compliance with the License.
 | ||||
| // You may obtain a copy of the License at
 | ||||
| //
 | ||||
| //      http://www.apache.org/licenses/LICENSE-2.0
 | ||||
| //
 | ||||
| // Unless required by applicable law or agreed to in writing, software
 | ||||
| // distributed under the License is distributed on an "AS IS" BASIS,
 | ||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | ||||
| // See the License for the specific language governing permissions and
 | ||||
| // limitations under the License.
 | ||||
| 
 | ||||
| #include "absl/time/civil_time.h" | ||||
| 
 | ||||
| #include "benchmark/benchmark.h" | ||||
| 
 | ||||
| namespace { | ||||
| 
 | ||||
| // Benchmark                            Time(ns)       CPU(ns)    Iterations
 | ||||
| // -------------------------------------------------------------------------
 | ||||
| // BM_Difference_Days                         20            20      34542508
 | ||||
| // BM_Step_Days                               15            15      48098146
 | ||||
| // BM_Format                                 688           687       1019803
 | ||||
| // BM_Parse                                  921           920        762788
 | ||||
| // BM_RoundTripFormatParse                  1766          1764        396092
 | ||||
| 
 | ||||
| void BM_Difference_Days(benchmark::State& state) { | ||||
|   const absl::CivilDay c(2014, 8, 22); | ||||
|   const absl::CivilDay epoch(1970, 1, 1); | ||||
|   while (state.KeepRunning()) { | ||||
|     const absl::civil_diff_t n = c - epoch; | ||||
|     benchmark::DoNotOptimize(n); | ||||
|   } | ||||
| } | ||||
| BENCHMARK(BM_Difference_Days); | ||||
| 
 | ||||
| void BM_Step_Days(benchmark::State& state) { | ||||
|   const absl::CivilDay kStart(2014, 8, 22); | ||||
|   absl::CivilDay c = kStart; | ||||
|   while (state.KeepRunning()) { | ||||
|     benchmark::DoNotOptimize(++c); | ||||
|   } | ||||
| } | ||||
| BENCHMARK(BM_Step_Days); | ||||
| 
 | ||||
| void BM_Format(benchmark::State& state) { | ||||
|   const absl::CivilSecond c(2014, 1, 2, 3, 4, 5); | ||||
|   while (state.KeepRunning()) { | ||||
|     const std::string s = absl::FormatCivilTime(c); | ||||
|     benchmark::DoNotOptimize(s); | ||||
|   } | ||||
| } | ||||
| BENCHMARK(BM_Format); | ||||
| 
 | ||||
| }  // namespace
 | ||||
							
								
								
									
										1073
									
								
								absl/time/civil_time_test.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1073
									
								
								absl/time/civil_time_test.cc
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							|  | @ -38,7 +38,8 @@ void BM_Format_FormatTime(benchmark::State& state) { | |||
|   const absl::TimeZone lax = | ||||
|       absl::time_internal::LoadTimeZone("America/Los_Angeles"); | ||||
|   const absl::Time t = | ||||
|       absl::FromDateTime(1977, 6, 28, 9, 8, 7, lax) + absl::Nanoseconds(1); | ||||
|       absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax) + | ||||
|       absl::Nanoseconds(1); | ||||
|   while (state.KeepRunning()) { | ||||
|     benchmark::DoNotOptimize(absl::FormatTime(fmt, t, lax).length()); | ||||
|   } | ||||
|  | @ -50,8 +51,8 @@ void BM_Format_ParseTime(benchmark::State& state) { | |||
|   state.SetLabel(fmt); | ||||
|   const absl::TimeZone lax = | ||||
|       absl::time_internal::LoadTimeZone("America/Los_Angeles"); | ||||
|   absl::Time t = | ||||
|       absl::FromDateTime(1977, 6, 28, 9, 8, 7, lax) + absl::Nanoseconds(1); | ||||
|   absl::Time t = absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax) + | ||||
|                  absl::Nanoseconds(1); | ||||
|   const std::string when = absl::FormatTime(fmt, t, lax); | ||||
|   std::string err; | ||||
|   while (state.KeepRunning()) { | ||||
|  |  | |||
|  | @ -118,7 +118,7 @@ TEST(FormatTime, RFC1123FormatPadsYear) {  // locale specific | |||
|   absl::TimeZone tz = absl::UTCTimeZone(); | ||||
| 
 | ||||
|   // A year of 77 should be padded to 0077.
 | ||||
|   absl::Time t = absl::FromDateTime(77, 6, 28, 9, 8, 7, tz); | ||||
|   absl::Time t = absl::FromCivil(absl::CivilSecond(77, 6, 28, 9, 8, 7), tz); | ||||
|   EXPECT_EQ("Mon, 28 Jun 0077 09:08:07 +0000", | ||||
|             absl::FormatTime(absl::RFC1123_full, t, tz)); | ||||
|   EXPECT_EQ("28 Jun 0077 09:08:07 +0000", | ||||
|  | @ -154,9 +154,9 @@ TEST(ParseTime, Basics) { | |||
|   EXPECT_TRUE(absl::ParseTime("%Y-%m-%d %H:%M:%S %z", | ||||
|                               "2013-06-28 19:08:09 -0800", &t, &err)) | ||||
|       << err; | ||||
|   absl::Time::Breakdown bd = t.In(absl::FixedTimeZone(-8 * 60 * 60)); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 6, 28, 19, 8, 9, -8 * 60 * 60, false); | ||||
|   EXPECT_EQ(absl::ZeroDuration(), bd.subsecond); | ||||
|   const auto ci = absl::FixedTimeZone(-8 * 60 * 60).At(t); | ||||
|   EXPECT_EQ(absl::CivilSecond(2013, 6, 28, 19, 8, 9), ci.cs); | ||||
|   EXPECT_EQ(absl::ZeroDuration(), ci.subsecond); | ||||
| } | ||||
| 
 | ||||
| TEST(ParseTime, NullErrorString) { | ||||
|  | @ -177,17 +177,17 @@ TEST(ParseTime, WithTimeZone) { | |||
|   EXPECT_TRUE( | ||||
|       absl::ParseTime("%Y-%m-%d %H:%M:%S", "2013-06-28 19:08:09", tz, &t, &e)) | ||||
|       << e; | ||||
|   absl::Time::Breakdown bd = t.In(tz); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 6, 28, 19, 8, 9, -7 * 60 * 60, true); | ||||
|   EXPECT_EQ(absl::ZeroDuration(), bd.subsecond); | ||||
|   auto ci = tz.At(t); | ||||
|   EXPECT_EQ(absl::CivilSecond(2013, 6, 28, 19, 8, 9), ci.cs); | ||||
|   EXPECT_EQ(absl::ZeroDuration(), ci.subsecond); | ||||
| 
 | ||||
|   // But the timezone is ignored when a UTC offset is present.
 | ||||
|   EXPECT_TRUE(absl::ParseTime("%Y-%m-%d %H:%M:%S %z", | ||||
|                               "2013-06-28 19:08:09 +0800", tz, &t, &e)) | ||||
|       << e; | ||||
|   bd = t.In(absl::FixedTimeZone(8 * 60 * 60)); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 6, 28, 19, 8, 9, 8 * 60 * 60, false); | ||||
|   EXPECT_EQ(absl::ZeroDuration(), bd.subsecond); | ||||
|   ci = absl::FixedTimeZone(8 * 60 * 60).At(t); | ||||
|   EXPECT_EQ(absl::CivilSecond(2013, 6, 28, 19, 8, 9), ci.cs); | ||||
|   EXPECT_EQ(absl::ZeroDuration(), ci.subsecond); | ||||
| } | ||||
| 
 | ||||
| TEST(ParseTime, ErrorCases) { | ||||
|  | @ -332,15 +332,15 @@ TEST(ParseTime, InfiniteTime) { | |||
|   EXPECT_TRUE(absl::ParseTime("infinite-future %H:%M", "infinite-future 03:04", | ||||
|                               &t, &err)); | ||||
|   EXPECT_NE(absl::InfiniteFuture(), t); | ||||
|   EXPECT_EQ(3, t.In(tz).hour); | ||||
|   EXPECT_EQ(4, t.In(tz).minute); | ||||
|   EXPECT_EQ(3, tz.At(t).cs.hour()); | ||||
|   EXPECT_EQ(4, tz.At(t).cs.minute()); | ||||
| 
 | ||||
|   // "infinite-past" as literal std::string
 | ||||
|   EXPECT_TRUE( | ||||
|       absl::ParseTime("infinite-past %H:%M", "infinite-past 03:04", &t, &err)); | ||||
|   EXPECT_NE(absl::InfinitePast(), t); | ||||
|   EXPECT_EQ(3, t.In(tz).hour); | ||||
|   EXPECT_EQ(4, t.In(tz).minute); | ||||
|   EXPECT_EQ(3, tz.At(t).cs.hour()); | ||||
|   EXPECT_EQ(4, tz.At(t).cs.minute()); | ||||
| 
 | ||||
|   // The input doesn't match the format.
 | ||||
|   EXPECT_FALSE(absl::ParseTime("infinite-future %H:%M", "03:04", &t, &err)); | ||||
|  | @ -365,16 +365,17 @@ TEST(ParseTime, FailsOnUnrepresentableTime) { | |||
| //
 | ||||
| 
 | ||||
| TEST(FormatParse, RoundTrip) { | ||||
|   const absl::TimeZone gst = | ||||
|   const absl::TimeZone lax = | ||||
|       absl::time_internal::LoadTimeZone("America/Los_Angeles"); | ||||
|   const absl::Time in = absl::FromDateTime(1977, 6, 28, 9, 8, 7, gst); | ||||
|   const absl::Time in = | ||||
|       absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax); | ||||
|   const absl::Duration subseconds = absl::Nanoseconds(654321); | ||||
|   std::string err; | ||||
| 
 | ||||
|   // RFC3339, which renders subseconds.
 | ||||
|   { | ||||
|     absl::Time out; | ||||
|     const std::string s = absl::FormatTime(absl::RFC3339_full, in + subseconds, gst); | ||||
|     const std::string s = absl::FormatTime(absl::RFC3339_full, in + subseconds, lax); | ||||
|     EXPECT_TRUE(absl::ParseTime(absl::RFC3339_full, s, &out, &err)) | ||||
|         << s << ": " << err; | ||||
|     EXPECT_EQ(in + subseconds, out);  // RFC3339_full includes %Ez
 | ||||
|  | @ -383,7 +384,7 @@ TEST(FormatParse, RoundTrip) { | |||
|   // RFC1123, which only does whole seconds.
 | ||||
|   { | ||||
|     absl::Time out; | ||||
|     const std::string s = absl::FormatTime(absl::RFC1123_full, in, gst); | ||||
|     const std::string s = absl::FormatTime(absl::RFC1123_full, in, lax); | ||||
|     EXPECT_TRUE(absl::ParseTime(absl::RFC1123_full, s, &out, &err)) | ||||
|         << s << ": " << err; | ||||
|     EXPECT_EQ(in, out);  // RFC1123_full includes %z
 | ||||
|  |  | |||
|  | @ -68,25 +68,35 @@ namespace cctz { | |||
| // it would take us to another day, and perhaps week, or even month.
 | ||||
| struct PosixTransition { | ||||
|   enum DateFormat { J, N, M }; | ||||
|   struct { | ||||
|     DateFormat fmt; | ||||
|     union { | ||||
|       struct { | ||||
|         std::int_fast16_t day;  // day of non-leap year [1:365]
 | ||||
|       } j; | ||||
|       struct { | ||||
|         std::int_fast16_t day;  // day of year [0:365]
 | ||||
|       } n; | ||||
|       struct { | ||||
|         std::int_fast8_t month;    // month of year [1:12]
 | ||||
|         std::int_fast8_t week;     // week of month [1:5] (5==last)
 | ||||
|         std::int_fast8_t weekday;  // 0==Sun, ..., 6=Sat
 | ||||
|       } m; | ||||
| 
 | ||||
|   struct Date { | ||||
|     struct NonLeapDay { | ||||
|       std::int_fast16_t day;  // day of non-leap year [1:365]
 | ||||
|     }; | ||||
|   } date; | ||||
|   struct { | ||||
|     struct Day { | ||||
|       std::int_fast16_t day;  // day of year [0:365]
 | ||||
|     }; | ||||
|     struct MonthWeekWeekday { | ||||
|       std::int_fast8_t month;    // month of year [1:12]
 | ||||
|       std::int_fast8_t week;     // week of month [1:5] (5==last)
 | ||||
|       std::int_fast8_t weekday;  // 0==Sun, ..., 6=Sat
 | ||||
|     }; | ||||
| 
 | ||||
|     DateFormat fmt; | ||||
| 
 | ||||
|     union { | ||||
|       NonLeapDay j; | ||||
|       Day n; | ||||
|       MonthWeekWeekday m; | ||||
|     }; | ||||
|   }; | ||||
| 
 | ||||
|   struct Time { | ||||
|     std::int_fast32_t offset;  // seconds before/after 00:00:00
 | ||||
|   } time; | ||||
|   }; | ||||
| 
 | ||||
|   Date date; | ||||
|   Time time; | ||||
| }; | ||||
| 
 | ||||
| // The entirety of a POSIX-string specified time-zone rule. The standard
 | ||||
|  |  | |||
|  | @ -26,12 +26,6 @@ namespace cctz = absl::time_internal::cctz; | |||
| namespace absl { | ||||
| namespace time_internal { | ||||
| 
 | ||||
| #if GTEST_USES_SIMPLE_RE | ||||
| extern const char kZoneAbbrRE[] = ".*";  // just punt
 | ||||
| #else | ||||
| extern const char kZoneAbbrRE[] = "[A-Za-z]{3,4}|[-+][0-9]{2}([0-9]{2})?"; | ||||
| #endif | ||||
| 
 | ||||
| TimeZone LoadTimeZone(const std::string& name) { | ||||
|   TimeZone tz; | ||||
|   ABSL_RAW_CHECK(LoadTimeZone(name, &tz), name.c_str()); | ||||
|  |  | |||
|  | @ -17,35 +17,11 @@ | |||
| 
 | ||||
| #include <string> | ||||
| 
 | ||||
| #include "gmock/gmock.h" | ||||
| #include "gtest/gtest.h" | ||||
| #include "absl/time/time.h" | ||||
| 
 | ||||
| // This helper is a macro so that failed expectations show up with the
 | ||||
| // correct line numbers.
 | ||||
| //
 | ||||
| // This is for internal testing of the Base Time library itself. This is not
 | ||||
| // part of a public API.
 | ||||
| #define ABSL_INTERNAL_EXPECT_TIME(bd, y, m, d, h, min, s, off, isdst)     \ | ||||
|   do {                                                                    \ | ||||
|     EXPECT_EQ(y, bd.year);                                                \ | ||||
|     EXPECT_EQ(m, bd.month);                                               \ | ||||
|     EXPECT_EQ(d, bd.day);                                                 \ | ||||
|     EXPECT_EQ(h, bd.hour);                                                \ | ||||
|     EXPECT_EQ(min, bd.minute);                                            \ | ||||
|     EXPECT_EQ(s, bd.second);                                              \ | ||||
|     EXPECT_EQ(off, bd.offset);                                            \ | ||||
|     EXPECT_EQ(isdst, bd.is_dst);                                          \ | ||||
|     EXPECT_THAT(bd.zone_abbr,                                             \ | ||||
|                 testing::MatchesRegex(absl::time_internal::kZoneAbbrRE)); \ | ||||
|   } while (0) | ||||
| 
 | ||||
| namespace absl { | ||||
| namespace time_internal { | ||||
| 
 | ||||
| // A regular expression that matches all zone abbreviations (%Z).
 | ||||
| extern const char kZoneAbbrRE[]; | ||||
| 
 | ||||
| // Loads the named timezone, but dies on any failure.
 | ||||
| absl::TimeZone LoadTimeZone(const std::string& name); | ||||
| 
 | ||||
|  |  | |||
|  | @ -22,13 +22,14 @@ | |||
| // NOTE: To keep type verbosity to a minimum, the following variable naming
 | ||||
| // conventions are used throughout this file.
 | ||||
| //
 | ||||
| // cz: A cctz::time_zone
 | ||||
| // tz: An absl::TimeZone
 | ||||
| // ci: An absl::TimeZone::CivilInfo
 | ||||
| // ti: An absl::TimeZone::TimeInfo
 | ||||
| // cd: An absl::CivilDay or a cctz::civil_day
 | ||||
| // cs: An absl::CivilSecond or a cctz::civil_second
 | ||||
| // bd: An absl::Time::Breakdown
 | ||||
| // cl: A cctz::time_zone::civil_lookup
 | ||||
| // al: A cctz::time_zone::absolute_lookup
 | ||||
| // cd: A cctz::civil_day
 | ||||
| // cs: A cctz::civil_second
 | ||||
| // bd: An absl::Time::Breakdown
 | ||||
| 
 | ||||
| #include "absl/time/time.h" | ||||
| 
 | ||||
|  | @ -75,7 +76,7 @@ inline absl::Time::Breakdown InfiniteFutureBreakdown() { | |||
|   return bd; | ||||
| } | ||||
| 
 | ||||
| inline Time::Breakdown InfinitePastBreakdown() { | ||||
| inline absl::Time::Breakdown InfinitePastBreakdown() { | ||||
|   Time::Breakdown bd; | ||||
|   bd.year = std::numeric_limits<int64_t>::min(); | ||||
|   bd.month = 1; | ||||
|  | @ -92,6 +93,26 @@ inline Time::Breakdown InfinitePastBreakdown() { | |||
|   return bd; | ||||
| } | ||||
| 
 | ||||
| inline absl::TimeZone::CivilInfo InfiniteFutureCivilInfo() { | ||||
|   TimeZone::CivilInfo ci; | ||||
|   ci.cs = CivilSecond::max(); | ||||
|   ci.subsecond = InfiniteDuration(); | ||||
|   ci.offset = 0; | ||||
|   ci.is_dst = false; | ||||
|   ci.zone_abbr = "-00"; | ||||
|   return ci; | ||||
| } | ||||
| 
 | ||||
| inline absl::TimeZone::CivilInfo InfinitePastCivilInfo() { | ||||
|   TimeZone::CivilInfo ci; | ||||
|   ci.cs = CivilSecond::min(); | ||||
|   ci.subsecond = -InfiniteDuration(); | ||||
|   ci.offset = 0; | ||||
|   ci.is_dst = false; | ||||
|   ci.zone_abbr = "-00"; | ||||
|   return ci; | ||||
| } | ||||
| 
 | ||||
| inline absl::TimeConversion InfiniteFutureTimeConversion() { | ||||
|   absl::TimeConversion tc; | ||||
|   tc.pre = tc.trans = tc.post = absl::InfiniteFuture(); | ||||
|  | @ -134,19 +155,6 @@ Time MakeTimeWithOverflow(const cctz::time_point<cctz::seconds>& sec, | |||
|   return time_internal::FromUnixDuration(time_internal::MakeDuration(hi)); | ||||
| } | ||||
| 
 | ||||
| inline absl::TimeConversion::Kind MapKind( | ||||
|     const cctz::time_zone::civil_lookup::civil_kind& kind) { | ||||
|   switch (kind) { | ||||
|     case cctz::time_zone::civil_lookup::UNIQUE: | ||||
|       return absl::TimeConversion::UNIQUE; | ||||
|     case cctz::time_zone::civil_lookup::SKIPPED: | ||||
|       return absl::TimeConversion::SKIPPED; | ||||
|     case cctz::time_zone::civil_lookup::REPEATED: | ||||
|       return absl::TimeConversion::REPEATED; | ||||
|   } | ||||
|   return absl::TimeConversion::UNIQUE; | ||||
| } | ||||
| 
 | ||||
| // Returns Mon=1..Sun=7.
 | ||||
| inline int MapWeekday(const cctz::weekday& wd) { | ||||
|   switch (wd) { | ||||
|  | @ -170,9 +178,13 @@ inline int MapWeekday(const cctz::weekday& wd) { | |||
| 
 | ||||
| }  // namespace
 | ||||
| 
 | ||||
| //
 | ||||
| // Time
 | ||||
| //
 | ||||
| 
 | ||||
| absl::Time::Breakdown Time::In(absl::TimeZone tz) const { | ||||
|   if (*this == absl::InfiniteFuture()) return absl::InfiniteFutureBreakdown(); | ||||
|   if (*this == absl::InfinitePast()) return absl::InfinitePastBreakdown(); | ||||
|   if (*this == absl::InfiniteFuture()) return InfiniteFutureBreakdown(); | ||||
|   if (*this == absl::InfinitePast()) return InfinitePastBreakdown(); | ||||
| 
 | ||||
|   const auto tp = unix_epoch() + cctz::seconds(time_internal::GetRepHi(rep_)); | ||||
|   const auto al = cctz::time_zone(tz).lookup(tp); | ||||
|  | @ -187,92 +199,18 @@ absl::Time::Breakdown Time::In(absl::TimeZone tz) const { | |||
|   bd.minute = cs.minute(); | ||||
|   bd.second = cs.second(); | ||||
|   bd.subsecond = time_internal::MakeDuration(0, time_internal::GetRepLo(rep_)); | ||||
|   bd.weekday = MapWeekday(get_weekday(cd)); | ||||
|   bd.yearday = get_yearday(cd); | ||||
|   bd.weekday = MapWeekday(cctz::get_weekday(cd)); | ||||
|   bd.yearday = cctz::get_yearday(cd); | ||||
|   bd.offset = al.offset; | ||||
|   bd.is_dst = al.is_dst; | ||||
|   bd.zone_abbr = al.abbr; | ||||
|   return bd; | ||||
| } | ||||
| 
 | ||||
| absl::Time FromTM(const struct tm& tm, absl::TimeZone tz) { | ||||
|   const auto cz = cctz::time_zone(tz); | ||||
|   const auto cs = | ||||
|       cctz::civil_second(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, | ||||
|                          tm.tm_hour, tm.tm_min, tm.tm_sec); | ||||
|   const auto cl = cz.lookup(cs); | ||||
|   const auto tp = tm.tm_isdst == 0 ? cl.post : cl.pre; | ||||
|   return MakeTimeWithOverflow(tp, cs, cz); | ||||
| } | ||||
| 
 | ||||
| struct tm ToTM(absl::Time t, absl::TimeZone tz) { | ||||
|   const absl::Time::Breakdown bd = t.In(tz); | ||||
|   struct tm tm; | ||||
|   std::memset(&tm, 0, sizeof(tm)); | ||||
|   tm.tm_sec = bd.second; | ||||
|   tm.tm_min = bd.minute; | ||||
|   tm.tm_hour = bd.hour; | ||||
|   tm.tm_mday = bd.day; | ||||
|   tm.tm_mon = bd.month - 1; | ||||
| 
 | ||||
|   // Saturates tm.tm_year in cases of over/underflow, accounting for the fact
 | ||||
|   // that tm.tm_year is years since 1900.
 | ||||
|   if (bd.year < std::numeric_limits<int>::min() + 1900) { | ||||
|     tm.tm_year = std::numeric_limits<int>::min(); | ||||
|   } else if (bd.year > std::numeric_limits<int>::max()) { | ||||
|     tm.tm_year = std::numeric_limits<int>::max() - 1900; | ||||
|   } else { | ||||
|     tm.tm_year = static_cast<int>(bd.year - 1900); | ||||
|   } | ||||
| 
 | ||||
|   tm.tm_wday = bd.weekday % 7; | ||||
|   tm.tm_yday = bd.yearday - 1; | ||||
|   tm.tm_isdst = bd.is_dst ? 1 : 0; | ||||
| 
 | ||||
|   return tm; | ||||
| } | ||||
| 
 | ||||
| //
 | ||||
| // Factory functions.
 | ||||
| // Conversions from/to other time types.
 | ||||
| //
 | ||||
| 
 | ||||
| absl::TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour, | ||||
|                                      int min, int sec, TimeZone tz) { | ||||
|   // Avoids years that are too extreme for civil_second to normalize.
 | ||||
|   if (year > 300000000000) return InfiniteFutureTimeConversion(); | ||||
|   if (year < -300000000000) return InfinitePastTimeConversion(); | ||||
|   const auto cz = cctz::time_zone(tz); | ||||
|   const auto cs = cctz::civil_second(year, mon, day, hour, min, sec); | ||||
|   absl::TimeConversion tc; | ||||
|   tc.normalized = year != cs.year() || mon != cs.month() || day != cs.day() || | ||||
|                   hour != cs.hour() || min != cs.minute() || sec != cs.second(); | ||||
|   const auto cl = cz.lookup(cs); | ||||
|   // Converts the civil_lookup struct to a TimeConversion.
 | ||||
|   tc.pre = MakeTimeWithOverflow(cl.pre, cs, cz, &tc.normalized); | ||||
|   tc.trans = MakeTimeWithOverflow(cl.trans, cs, cz, &tc.normalized); | ||||
|   tc.post = MakeTimeWithOverflow(cl.post, cs, cz, &tc.normalized); | ||||
|   tc.kind = MapKind(cl.kind); | ||||
|   return tc; | ||||
| } | ||||
| 
 | ||||
| absl::Time FromDateTime(int64_t year, int mon, int day, int hour, int min, | ||||
|                         int sec, TimeZone tz) { | ||||
|   if (year > 300000000000) return InfiniteFuture(); | ||||
|   if (year < -300000000000) return InfinitePast(); | ||||
|   const auto cz = cctz::time_zone(tz); | ||||
|   const auto cs = cctz::civil_second(year, mon, day, hour, min, sec); | ||||
|   const auto cl = cz.lookup(cs); | ||||
|   return MakeTimeWithOverflow(cl.pre, cs, cz); | ||||
| } | ||||
| 
 | ||||
| absl::Time TimeFromTimespec(timespec ts) { | ||||
|   return time_internal::FromUnixDuration(absl::DurationFromTimespec(ts)); | ||||
| } | ||||
| 
 | ||||
| absl::Time TimeFromTimeval(timeval tv) { | ||||
|   return time_internal::FromUnixDuration(absl::DurationFromTimeval(tv)); | ||||
| } | ||||
| 
 | ||||
| absl::Time FromUDate(double udate) { | ||||
|   return time_internal::FromUnixDuration(absl::Milliseconds(udate)); | ||||
| } | ||||
|  | @ -281,10 +219,6 @@ absl::Time FromUniversal(int64_t universal) { | |||
|   return absl::UniversalEpoch() + 100 * absl::Nanoseconds(universal); | ||||
| } | ||||
| 
 | ||||
| //
 | ||||
| // Conversion to other time types.
 | ||||
| //
 | ||||
| 
 | ||||
| int64_t ToUnixNanos(Time t) { | ||||
|   if (time_internal::GetRepHi(time_internal::ToUnixDuration(t)) >= 0 && | ||||
|       time_internal::GetRepHi(time_internal::ToUnixDuration(t)) >> 33 == 0) { | ||||
|  | @ -321,6 +255,23 @@ int64_t ToUnixSeconds(Time t) { | |||
| 
 | ||||
| time_t ToTimeT(Time t) { return absl::ToTimespec(t).tv_sec; } | ||||
| 
 | ||||
| double ToUDate(Time t) { | ||||
|   return absl::FDivDuration(time_internal::ToUnixDuration(t), | ||||
|                             absl::Milliseconds(1)); | ||||
| } | ||||
| 
 | ||||
| int64_t ToUniversal(absl::Time t) { | ||||
|   return absl::FloorToUnit(t - absl::UniversalEpoch(), absl::Nanoseconds(100)); | ||||
| } | ||||
| 
 | ||||
| absl::Time TimeFromTimespec(timespec ts) { | ||||
|   return time_internal::FromUnixDuration(absl::DurationFromTimespec(ts)); | ||||
| } | ||||
| 
 | ||||
| absl::Time TimeFromTimeval(timeval tv) { | ||||
|   return time_internal::FromUnixDuration(absl::DurationFromTimeval(tv)); | ||||
| } | ||||
| 
 | ||||
| timespec ToTimespec(Time t) { | ||||
|   timespec ts; | ||||
|   absl::Duration d = time_internal::ToUnixDuration(t); | ||||
|  | @ -359,15 +310,6 @@ timeval ToTimeval(Time t) { | |||
|   return tv; | ||||
| } | ||||
| 
 | ||||
| double ToUDate(Time t) { | ||||
|   return absl::FDivDuration(time_internal::ToUnixDuration(t), | ||||
|                             absl::Milliseconds(1)); | ||||
| } | ||||
| 
 | ||||
| int64_t ToUniversal(absl::Time t) { | ||||
|   return absl::FloorToUnit(t - absl::UniversalEpoch(), absl::Nanoseconds(100)); | ||||
| } | ||||
| 
 | ||||
| Time FromChrono(const std::chrono::system_clock::time_point& tp) { | ||||
|   return time_internal::FromUnixDuration(time_internal::FromChrono( | ||||
|       tp - std::chrono::system_clock::from_time_t(0))); | ||||
|  | @ -381,4 +323,141 @@ std::chrono::system_clock::time_point ToChronoTime(absl::Time t) { | |||
|          time_internal::ToChronoDuration<D>(d); | ||||
| } | ||||
| 
 | ||||
| //
 | ||||
| // TimeZone
 | ||||
| //
 | ||||
| 
 | ||||
| absl::TimeZone::CivilInfo TimeZone::At(Time t) const { | ||||
|   if (t == absl::InfiniteFuture()) return InfiniteFutureCivilInfo(); | ||||
|   if (t == absl::InfinitePast()) return InfinitePastCivilInfo(); | ||||
| 
 | ||||
|   const auto ud = time_internal::ToUnixDuration(t); | ||||
|   const auto tp = unix_epoch() + cctz::seconds(time_internal::GetRepHi(ud)); | ||||
|   const auto al = cz_.lookup(tp); | ||||
| 
 | ||||
|   TimeZone::CivilInfo ci; | ||||
|   ci.cs = CivilSecond(al.cs); | ||||
|   ci.subsecond = time_internal::MakeDuration(0, time_internal::GetRepLo(ud)); | ||||
|   ci.offset = al.offset; | ||||
|   ci.is_dst = al.is_dst; | ||||
|   ci.zone_abbr = al.abbr; | ||||
|   return ci; | ||||
| } | ||||
| 
 | ||||
| absl::TimeZone::TimeInfo TimeZone::At(CivilSecond ct) const { | ||||
|   const cctz::civil_second cs(ct); | ||||
|   const auto cl = cz_.lookup(cs); | ||||
| 
 | ||||
|   TimeZone::TimeInfo ti; | ||||
|   switch (cl.kind) { | ||||
|     case cctz::time_zone::civil_lookup::UNIQUE: | ||||
|       ti.kind = TimeZone::TimeInfo::UNIQUE; | ||||
|       break; | ||||
|     case cctz::time_zone::civil_lookup::SKIPPED: | ||||
|       ti.kind = TimeZone::TimeInfo::SKIPPED; | ||||
|       break; | ||||
|     case cctz::time_zone::civil_lookup::REPEATED: | ||||
|       ti.kind = TimeZone::TimeInfo::REPEATED; | ||||
|       break; | ||||
|   } | ||||
|   ti.pre = MakeTimeWithOverflow(cl.pre, cs, cz_); | ||||
|   ti.trans = MakeTimeWithOverflow(cl.trans, cs, cz_); | ||||
|   ti.post = MakeTimeWithOverflow(cl.post, cs, cz_); | ||||
|   return ti; | ||||
| } | ||||
| 
 | ||||
| //
 | ||||
| // Conversions involving time zones.
 | ||||
| //
 | ||||
| 
 | ||||
| absl::TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour, | ||||
|                                      int min, int sec, TimeZone tz) { | ||||
|   // Avoids years that are too extreme for CivilSecond to normalize.
 | ||||
|   if (year > 300000000000) return InfiniteFutureTimeConversion(); | ||||
|   if (year < -300000000000) return InfinitePastTimeConversion(); | ||||
| 
 | ||||
|   const CivilSecond cs(year, mon, day, hour, min, sec); | ||||
|   const auto ti = tz.At(cs); | ||||
| 
 | ||||
|   TimeConversion tc; | ||||
|   tc.pre = ti.pre; | ||||
|   tc.trans = ti.trans; | ||||
|   tc.post = ti.post; | ||||
|   switch (ti.kind) { | ||||
|     case TimeZone::TimeInfo::UNIQUE: | ||||
|       tc.kind = TimeConversion::UNIQUE; | ||||
|       break; | ||||
|     case TimeZone::TimeInfo::SKIPPED: | ||||
|       tc.kind = TimeConversion::SKIPPED; | ||||
|       break; | ||||
|     case TimeZone::TimeInfo::REPEATED: | ||||
|       tc.kind = TimeConversion::REPEATED; | ||||
|       break; | ||||
|   } | ||||
|   tc.normalized = false; | ||||
|   if (year != cs.year() || mon != cs.month() || day != cs.day() || | ||||
|       hour != cs.hour() || min != cs.minute() || sec != cs.second()) { | ||||
|     tc.normalized = true; | ||||
|   } | ||||
|   return tc; | ||||
| } | ||||
| 
 | ||||
| absl::Time FromTM(const struct tm& tm, absl::TimeZone tz) { | ||||
|   const CivilSecond cs(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, | ||||
|                        tm.tm_hour, tm.tm_min, tm.tm_sec); | ||||
|   const auto ti = tz.At(cs); | ||||
|   return tm.tm_isdst == 0 ? ti.post : ti.pre; | ||||
| } | ||||
| 
 | ||||
| struct tm ToTM(absl::Time t, absl::TimeZone tz) { | ||||
|   struct tm tm = {}; | ||||
| 
 | ||||
|   const auto ci = tz.At(t); | ||||
|   const auto& cs = ci.cs; | ||||
|   tm.tm_sec = cs.second(); | ||||
|   tm.tm_min = cs.minute(); | ||||
|   tm.tm_hour = cs.hour(); | ||||
|   tm.tm_mday = cs.day(); | ||||
|   tm.tm_mon = cs.month() - 1; | ||||
| 
 | ||||
|   // Saturates tm.tm_year in cases of over/underflow, accounting for the fact
 | ||||
|   // that tm.tm_year is years since 1900.
 | ||||
|   if (cs.year() < std::numeric_limits<int>::min() + 1900) { | ||||
|     tm.tm_year = std::numeric_limits<int>::min(); | ||||
|   } else if (cs.year() > std::numeric_limits<int>::max()) { | ||||
|     tm.tm_year = std::numeric_limits<int>::max() - 1900; | ||||
|   } else { | ||||
|     tm.tm_year = static_cast<int>(cs.year() - 1900); | ||||
|   } | ||||
| 
 | ||||
|   const CivilDay cd(cs); | ||||
|   switch (GetWeekday(cd)) { | ||||
|     case Weekday::sunday: | ||||
|       tm.tm_wday = 0; | ||||
|       break; | ||||
|     case Weekday::monday: | ||||
|       tm.tm_wday = 1; | ||||
|       break; | ||||
|     case Weekday::tuesday: | ||||
|       tm.tm_wday = 2; | ||||
|       break; | ||||
|     case Weekday::wednesday: | ||||
|       tm.tm_wday = 3; | ||||
|       break; | ||||
|     case Weekday::thursday: | ||||
|       tm.tm_wday = 4; | ||||
|       break; | ||||
|     case Weekday::friday: | ||||
|       tm.tm_wday = 5; | ||||
|       break; | ||||
|     case Weekday::saturday: | ||||
|       tm.tm_wday = 6; | ||||
|       break; | ||||
|   } | ||||
|   tm.tm_yday = GetYearDay(cd) - 1; | ||||
|   tm.tm_isdst = ci.is_dst ? 1 : 0; | ||||
| 
 | ||||
|   return tm; | ||||
| } | ||||
| 
 | ||||
| }  // namespace absl
 | ||||
|  |  | |||
							
								
								
									
										646
									
								
								absl/time/time.h
									
										
									
									
									
								
							
							
						
						
									
										646
									
								
								absl/time/time.h
									
										
									
									
									
								
							|  | @ -25,18 +25,29 @@ | |||
| //  * `absl::TimeZone` defines geopolitical time zone regions (as collected
 | ||||
| //     within the IANA Time Zone database (https://www.iana.org/time-zones)).
 | ||||
| //
 | ||||
| // Note: Absolute times are distinct from civil times, which refer to the
 | ||||
| // human-scale time commonly represented by `YYYY-MM-DD hh:mm:ss`. The mapping
 | ||||
| // between absolute and civil times can be specified by use of time zones
 | ||||
| // (`absl::TimeZone` within this API). That is:
 | ||||
| //
 | ||||
| //   Civil Time = F(Absolute Time, Time Zone)
 | ||||
| //   Absolute Time = G(Civil Time, Time Zone)
 | ||||
| //
 | ||||
| // See civil_time.h for abstractions related to constructing and manipulating
 | ||||
| // civil time.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::TimeZone nyc;
 | ||||
| //
 | ||||
| //   // LoadTimeZone() may fail so it's always better to check for success.
 | ||||
| //   if (!absl::LoadTimeZone("America/New_York", &nyc)) {
 | ||||
| //      // handle error case
 | ||||
| //   }
 | ||||
| //
 | ||||
| //   // My flight leaves NYC on Jan 2, 2017 at 03:04:05
 | ||||
| //   absl::Time takeoff = absl::FromDateTime(2017, 1, 2, 3, 4, 5, nyc);
 | ||||
| //   absl::CivilSecond cs(2017, 1, 2, 3, 4, 5);
 | ||||
| //   absl::Time takeoff = absl::FromCivil(cs, nyc);
 | ||||
| //
 | ||||
| //   absl::Duration flight_duration = absl::Hours(21) + absl::Minutes(35);
 | ||||
| //   absl::Time landing = takeoff + flight_duration;
 | ||||
| //
 | ||||
|  | @ -48,6 +59,7 @@ | |||
| //       "My flight will land in Sydney on %Y-%m-%d at %H:%M:%S",
 | ||||
| //       landing, syd);
 | ||||
| //
 | ||||
| 
 | ||||
| #ifndef ABSL_TIME_TIME_H_ | ||||
| #define ABSL_TIME_TIME_H_ | ||||
| 
 | ||||
|  | @ -66,6 +78,7 @@ | |||
| 
 | ||||
| #include "absl/base/port.h"  // Needed for string vs std::string | ||||
| #include "absl/strings/string_view.h" | ||||
| #include "absl/time/civil_time.h" | ||||
| #include "absl/time/internal/cctz/include/cctz/time_zone.h" | ||||
| 
 | ||||
| namespace absl { | ||||
|  | @ -348,11 +361,11 @@ constexpr Duration InfiniteDuration(); | |||
| // Factory functions for constructing `Duration` values from an integral number
 | ||||
| // of the unit indicated by the factory function's name.
 | ||||
| //
 | ||||
| // Note: no "Days()" factory function exists because "a day" is ambiguous. Civil
 | ||||
| // days are not always 24 hours long, and a 24-hour duration often does not
 | ||||
| // correspond with a civil day. If a 24-hour duration is needed, use
 | ||||
| // `absl::Hours(24)`.
 | ||||
| //
 | ||||
| // Note: no "Days()" factory function exists because "a day" is ambiguous.
 | ||||
| // Civil days are not always 24 hours long, and a 24-hour duration often does
 | ||||
| // not correspond with a civil day. If a 24-hour duration is needed, use
 | ||||
| // `absl::Hours(24)`. (If you actually want a civil day, use absl::CivilDay
 | ||||
| // from civil_time.h.)
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
|  | @ -371,6 +384,7 @@ constexpr Duration Hours(int64_t n); | |||
| // factories, which should be preferred.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   auto a = absl::Seconds(1.5);        // OK
 | ||||
| //   auto b = absl::Milliseconds(1500);  // BETTER
 | ||||
| template <typename T, time_internal::EnableIfFloat<T> = 0> | ||||
|  | @ -546,7 +560,7 @@ std::string UnparseFlag(Duration d); | |||
| //
 | ||||
| // `absl::Time` uses a resolution that is high enough to avoid loss in
 | ||||
| // precision, and a range that is wide enough to avoid overflow, when
 | ||||
| // converting between tick counts in most Google time scales (i.e., precision
 | ||||
| // converting between tick counts in most Google time scales (i.e., resolution
 | ||||
| // of at least one nanosecond, and range +/-100 billion years).  Conversions
 | ||||
| // between the time scales are performed by truncating (towards negative
 | ||||
| // infinity) to the nearest representable point.
 | ||||
|  | @ -556,7 +570,6 @@ std::string UnparseFlag(Duration d); | |||
| //   absl::Time t1 = ...;
 | ||||
| //   absl::Time t2 = t1 + absl::Minutes(2);
 | ||||
| //   absl::Duration d = t2 - t1;  // == absl::Minutes(2)
 | ||||
| //   absl::Time::Breakdown bd = t1.In(absl::LocalTimeZone());
 | ||||
| //
 | ||||
| class Time { | ||||
|  public: | ||||
|  | @ -590,7 +603,10 @@ class Time { | |||
|   // intended to represent an instant in time. So, rather than passing
 | ||||
|   // a `Time::Breakdown` to a function, pass an `absl::Time` and an
 | ||||
|   // `absl::TimeZone`.
 | ||||
|   struct Breakdown { | ||||
|   //
 | ||||
|   // Deprecated. Use `absl::TimeZone::CivilInfo`.
 | ||||
|   struct | ||||
|       Breakdown { | ||||
|     int64_t year;          // year (e.g., 2013)
 | ||||
|     int month;           // month of year [1:12]
 | ||||
|     int day;             // day of month [1:31]
 | ||||
|  | @ -614,6 +630,8 @@ class Time { | |||
|   // Time::In()
 | ||||
|   //
 | ||||
|   // Returns the breakdown of this instant in the given TimeZone.
 | ||||
|   //
 | ||||
|   // Deprecated. Use `absl::TimeZone::At(Time)`.
 | ||||
|   Breakdown In(TimeZone tz) const; | ||||
| 
 | ||||
|   template <typename H> | ||||
|  | @ -679,126 +697,6 @@ constexpr Time InfinitePast() { | |||
|       time_internal::MakeDuration(std::numeric_limits<int64_t>::min(), ~0U)); | ||||
| } | ||||
| 
 | ||||
| // TimeConversion
 | ||||
| //
 | ||||
| // An `absl::TimeConversion` represents the conversion of year, month, day,
 | ||||
| // hour, minute, and second values (i.e., a civil time), in a particular
 | ||||
| // `absl::TimeZone`, to a time instant (an absolute time), as returned by
 | ||||
| // `absl::ConvertDateTime()`. (Subseconds must be handled separately.)
 | ||||
| //
 | ||||
| // It is possible, though, for a caller to try to convert values that
 | ||||
| // do not represent an actual or unique instant in time (due to a shift
 | ||||
| // in UTC offset in the `absl::TimeZone`, which results in a discontinuity in
 | ||||
| // the civil-time components). For example, a daylight-saving-time
 | ||||
| // transition skips or repeats civil times---in the United States, March
 | ||||
| // 13, 2011 02:15 never occurred, while November 6, 2011 01:15 occurred
 | ||||
| // twice---so requests for such times are not well-defined.
 | ||||
| //
 | ||||
| // To account for these possibilities, `absl::TimeConversion` is richer
 | ||||
| // than just a single `absl::Time`. When the civil time is skipped or
 | ||||
| // repeated, `absl::ConvertDateTime()` returns times calculated using the
 | ||||
| // pre-transition and post-transition UTC offsets, plus the transition
 | ||||
| // time itself.
 | ||||
| //
 | ||||
| // Examples:
 | ||||
| //
 | ||||
| //   absl::TimeZone lax;
 | ||||
| //   if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) {
 | ||||
| //     // handle error case
 | ||||
| //   }
 | ||||
| //
 | ||||
| //   // A unique civil time
 | ||||
| //   absl::TimeConversion jan01 =
 | ||||
| //       absl::ConvertDateTime(2011, 1, 1, 0, 0, 0, lax);
 | ||||
| //   // jan01.kind == TimeConversion::UNIQUE
 | ||||
| //   // jan01.pre    is 2011/01/01 00:00:00 -0800
 | ||||
| //   // jan01.trans  is 2011/01/01 00:00:00 -0800
 | ||||
| //   // jan01.post   is 2011/01/01 00:00:00 -0800
 | ||||
| //
 | ||||
| //   // A Spring DST transition, when there is a gap in civil time
 | ||||
| //   absl::TimeConversion mar13 =
 | ||||
| //       absl::ConvertDateTime(2011, 3, 13, 2, 15, 0, lax);
 | ||||
| //   // mar13.kind == TimeConversion::SKIPPED
 | ||||
| //   // mar13.pre   is 2011/03/13 03:15:00 -0700
 | ||||
| //   // mar13.trans is 2011/03/13 03:00:00 -0700
 | ||||
| //   // mar13.post  is 2011/03/13 01:15:00 -0800
 | ||||
| //
 | ||||
| //   // A Fall DST transition, when civil times are repeated
 | ||||
| //   absl::TimeConversion nov06 =
 | ||||
| //       absl::ConvertDateTime(2011, 11, 6, 1, 15, 0, lax);
 | ||||
| //   // nov06.kind == TimeConversion::REPEATED
 | ||||
| //   // nov06.pre   is 2011/11/06 01:15:00 -0700
 | ||||
| //   // nov06.trans is 2011/11/06 01:00:00 -0800
 | ||||
| //   // nov06.post  is 2011/11/06 01:15:00 -0800
 | ||||
| //
 | ||||
| // The input month, day, hour, minute, and second values can also be
 | ||||
| // outside of their valid ranges, in which case they will be "normalized"
 | ||||
| // during the conversion.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   // "October 32" normalizes to "November 1".
 | ||||
| //   absl::TimeZone tz = absl::LocalTimeZone();
 | ||||
| //   absl::TimeConversion tc =
 | ||||
| //       absl::ConvertDateTime(2013, 10, 32, 8, 30, 0, tz);
 | ||||
| //   // tc.kind == TimeConversion::UNIQUE && tc.normalized == true
 | ||||
| //   // tc.pre.In(tz).month == 11 && tc.pre.In(tz).day == 1
 | ||||
| struct TimeConversion { | ||||
|   Time pre;    // time calculated using the pre-transition offset
 | ||||
|   Time trans;  // when the civil-time discontinuity occurred
 | ||||
|   Time post;   // time calculated using the post-transition offset
 | ||||
| 
 | ||||
|   enum Kind { | ||||
|     UNIQUE,    // the civil time was singular (pre == trans == post)
 | ||||
|     SKIPPED,   // the civil time did not exist
 | ||||
|     REPEATED,  // the civil time was ambiguous
 | ||||
|   }; | ||||
|   Kind kind; | ||||
| 
 | ||||
|   bool normalized;  // input values were outside their valid ranges
 | ||||
| }; | ||||
| 
 | ||||
| // ConvertDateTime()
 | ||||
| //
 | ||||
| // The full generality of a civil time to absl::Time conversion.
 | ||||
| TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour, | ||||
|                                int min, int sec, TimeZone tz); | ||||
| 
 | ||||
| // FromDateTime()
 | ||||
| //
 | ||||
| // A convenience wrapper for `absl::ConvertDateTime()` that simply returns the
 | ||||
| // "pre" `absl::Time`.  That is, the unique result, or the instant that
 | ||||
| // is correct using the pre-transition offset (as if the transition
 | ||||
| // never happened). This is typically the answer that humans expected when
 | ||||
| // faced with non-unique times, such as near daylight-saving time transitions.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::TimeZone seattle;
 | ||||
| //   if (!absl::LoadTimeZone("America/Los_Angeles", &seattle)) {
 | ||||
| //     // handle error case
 | ||||
| //   }
 | ||||
| //   absl::Time t =  absl::FromDateTime(2017, 9, 26, 9, 30, 0, seattle);
 | ||||
| Time FromDateTime(int64_t year, int mon, int day, int hour, int min, int sec, | ||||
|                   TimeZone tz); | ||||
| 
 | ||||
| // FromTM()
 | ||||
| //
 | ||||
| // Converts the `tm_year`, `tm_mon`, `tm_mday`, `tm_hour`, `tm_min`, and
 | ||||
| // `tm_sec` fields to an `absl::Time` using the given time zone. See ctime(3)
 | ||||
| // for a description of the expected values of the tm fields. IFF the indicated
 | ||||
| // time instant is not unique (see `absl::ConvertDateTime()` above), the
 | ||||
| // `tm_isdst` field is consulted to select the desired instant (`tm_isdst` > 0
 | ||||
| // means DST, `tm_isdst` == 0 means no DST, `tm_isdst` < 0 means use the default
 | ||||
| // like `absl::FromDateTime()`).
 | ||||
| Time FromTM(const struct tm& tm, TimeZone tz); | ||||
| 
 | ||||
| // ToTM()
 | ||||
| //
 | ||||
| // Converts the given `absl::Time` to a struct tm using the given time zone.
 | ||||
| // See ctime(3) for a description of the values of the tm fields.
 | ||||
| struct tm ToTM(Time t, TimeZone tz); | ||||
| 
 | ||||
| // FromUnixNanos()
 | ||||
| // FromUnixMicros()
 | ||||
| // FromUnixMillis()
 | ||||
|  | @ -883,140 +781,6 @@ Time FromChrono(const std::chrono::system_clock::time_point& tp); | |||
| //   // tp == std::chrono::system_clock::from_time_t(123);
 | ||||
| std::chrono::system_clock::time_point ToChronoTime(Time); | ||||
| 
 | ||||
| // RFC3339_full
 | ||||
| // RFC3339_sec
 | ||||
| //
 | ||||
| // FormatTime()/ParseTime() format specifiers for RFC3339 date/time strings,
 | ||||
| // with trailing zeros trimmed or with fractional seconds omitted altogether.
 | ||||
| //
 | ||||
| // Note that RFC3339_sec[] matches an ISO 8601 extended format for date and
 | ||||
| // time with UTC offset.  Also note the use of "%Y": RFC3339 mandates that
 | ||||
| // years have exactly four digits, but we allow them to take their natural
 | ||||
| // width.
 | ||||
| extern const char RFC3339_full[];  // %Y-%m-%dT%H:%M:%E*S%Ez
 | ||||
| extern const char RFC3339_sec[];   // %Y-%m-%dT%H:%M:%S%Ez
 | ||||
| 
 | ||||
| // RFC1123_full
 | ||||
| // RFC1123_no_wday
 | ||||
| //
 | ||||
| // FormatTime()/ParseTime() format specifiers for RFC1123 date/time strings.
 | ||||
| extern const char RFC1123_full[];     // %a, %d %b %E4Y %H:%M:%S %z
 | ||||
| extern const char RFC1123_no_wday[];  // %d %b %E4Y %H:%M:%S %z
 | ||||
| 
 | ||||
| // FormatTime()
 | ||||
| //
 | ||||
| // Formats the given `absl::Time` in the `absl::TimeZone` according to the
 | ||||
| // provided format string. Uses strftime()-like formatting options, with
 | ||||
| // the following extensions:
 | ||||
| //
 | ||||
| //   - %Ez  - RFC3339-compatible numeric UTC offset (+hh:mm or -hh:mm)
 | ||||
| //   - %E*z - Full-resolution numeric UTC offset (+hh:mm:ss or -hh:mm:ss)
 | ||||
| //   - %E#S - Seconds with # digits of fractional precision
 | ||||
| //   - %E*S - Seconds with full fractional precision (a literal '*')
 | ||||
| //   - %E#f - Fractional seconds with # digits of precision
 | ||||
| //   - %E*f - Fractional seconds with full precision (a literal '*')
 | ||||
| //   - %E4Y - Four-character years (-999 ... -001, 0000, 0001 ... 9999)
 | ||||
| //
 | ||||
| // Note that %E0S behaves like %S, and %E0f produces no characters.  In
 | ||||
| // contrast %E*f always produces at least one digit, which may be '0'.
 | ||||
| //
 | ||||
| // Note that %Y produces as many characters as it takes to fully render the
 | ||||
| // year.  A year outside of [-999:9999] when formatted with %E4Y will produce
 | ||||
| // more than four characters, just like %Y.
 | ||||
| //
 | ||||
| // We recommend that format strings include the UTC offset (%z, %Ez, or %E*z)
 | ||||
| // so that the result uniquely identifies a time instant.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::TimeZone lax;
 | ||||
| //   if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) {
 | ||||
| //     // handle error case
 | ||||
| //   }
 | ||||
| //   absl::Time t = absl::FromDateTime(2013, 1, 2, 3, 4, 5, lax);
 | ||||
| //
 | ||||
| //   string f = absl::FormatTime("%H:%M:%S", t, lax);  // "03:04:05"
 | ||||
| //   f = absl::FormatTime("%H:%M:%E3S", t, lax);  // "03:04:05.000"
 | ||||
| //
 | ||||
| // Note: If the given `absl::Time` is `absl::InfiniteFuture()`, the returned
 | ||||
| // string will be exactly "infinite-future". If the given `absl::Time` is
 | ||||
| // `absl::InfinitePast()`, the returned string will be exactly "infinite-past".
 | ||||
| // In both cases the given format string and `absl::TimeZone` are ignored.
 | ||||
| //
 | ||||
| std::string FormatTime(const std::string& format, Time t, TimeZone tz); | ||||
| 
 | ||||
| // Convenience functions that format the given time using the RFC3339_full
 | ||||
| // format.  The first overload uses the provided TimeZone, while the second
 | ||||
| // uses LocalTimeZone().
 | ||||
| std::string FormatTime(Time t, TimeZone tz); | ||||
| std::string FormatTime(Time t); | ||||
| 
 | ||||
| // Output stream operator.
 | ||||
| inline std::ostream& operator<<(std::ostream& os, Time t) { | ||||
|   return os << FormatTime(t); | ||||
| } | ||||
| 
 | ||||
| // ParseTime()
 | ||||
| //
 | ||||
| // Parses an input string according to the provided format string and
 | ||||
| // returns the corresponding `absl::Time`. Uses strftime()-like formatting
 | ||||
| // options, with the same extensions as FormatTime(), but with the
 | ||||
| // exceptions that %E#S is interpreted as %E*S, and %E#f as %E*f.  %Ez
 | ||||
| // and %E*z also accept the same inputs.
 | ||||
| //
 | ||||
| // %Y consumes as many numeric characters as it can, so the matching data
 | ||||
| // should always be terminated with a non-numeric.  %E4Y always consumes
 | ||||
| // exactly four characters, including any sign.
 | ||||
| //
 | ||||
| // Unspecified fields are taken from the default date and time of ...
 | ||||
| //
 | ||||
| //   "1970-01-01 00:00:00.0 +0000"
 | ||||
| //
 | ||||
| // For example, parsing a string of "15:45" (%H:%M) will return an absl::Time
 | ||||
| // that represents "1970-01-01 15:45:00.0 +0000".
 | ||||
| //
 | ||||
| // Note that since ParseTime() returns time instants, it makes the most sense
 | ||||
| // to parse fully-specified date/time strings that include a UTC offset (%z,
 | ||||
| // %Ez, or %E*z).
 | ||||
| //
 | ||||
| // Note also that `absl::ParseTime()` only heeds the fields year, month, day,
 | ||||
| // hour, minute, (fractional) second, and UTC offset.  Other fields, like
 | ||||
| // weekday (%a or %A), while parsed for syntactic validity, are ignored
 | ||||
| // in the conversion.
 | ||||
| //
 | ||||
| // Date and time fields that are out-of-range will be treated as errors
 | ||||
| // rather than normalizing them like `absl::FromDateTime()` does.  For example,
 | ||||
| // it is an error to parse the date "Oct 32, 2013" because 32 is out of range.
 | ||||
| //
 | ||||
| // A leap second of ":60" is normalized to ":00" of the following minute
 | ||||
| // with fractional seconds discarded.  The following table shows how the
 | ||||
| // given seconds and subseconds will be parsed:
 | ||||
| //
 | ||||
| //   "59.x" -> 59.x  // exact
 | ||||
| //   "60.x" -> 00.0  // normalized
 | ||||
| //   "00.x" -> 00.x  // exact
 | ||||
| //
 | ||||
| // Errors are indicated by returning false and assigning an error message
 | ||||
| // to the "err" out param if it is non-null.
 | ||||
| //
 | ||||
| // Note: If the input string is exactly "infinite-future", the returned
 | ||||
| // `absl::Time` will be `absl::InfiniteFuture()` and `true` will be returned.
 | ||||
| // If the input string is "infinite-past", the returned `absl::Time` will be
 | ||||
| // `absl::InfinitePast()` and `true` will be returned.
 | ||||
| //
 | ||||
| bool ParseTime(const std::string& format, const std::string& input, Time* time, | ||||
|                std::string* err); | ||||
| 
 | ||||
| // Like ParseTime() above, but if the format string does not contain a UTC
 | ||||
| // offset specification (%z/%Ez/%E*z) then the input is interpreted in the
 | ||||
| // given TimeZone.  This means that the input, by itself, does not identify a
 | ||||
| // unique instant.  Being time-zone dependent, it also admits the possibility
 | ||||
| // of ambiguity or non-existence, in which case the "pre" time (as defined
 | ||||
| // for ConvertDateTime()) is returned.  For these reasons we recommend that
 | ||||
| // all date/time strings include a UTC offset so they're context independent.
 | ||||
| bool ParseTime(const std::string& format, const std::string& input, TimeZone tz, | ||||
|                Time* time, std::string* err); | ||||
| 
 | ||||
| // Support for flag values of type Time. Time flags must be specified in a
 | ||||
| // format that matches absl::RFC3339_full. For example:
 | ||||
| //
 | ||||
|  | @ -1069,6 +833,98 @@ class TimeZone { | |||
| 
 | ||||
|   std::string name() const { return cz_.name(); } | ||||
| 
 | ||||
|   // TimeZone::CivilInfo
 | ||||
|   //
 | ||||
|   // Information about the civil time corresponding to an absolute time.
 | ||||
|   // This struct is not intended to represent an instant in time. So, rather
 | ||||
|   // than passing a `TimeZone::CivilInfo` to a function, pass an `absl::Time`
 | ||||
|   // and an `absl::TimeZone`.
 | ||||
|   struct CivilInfo { | ||||
|     CivilSecond cs; | ||||
|     Duration subsecond; | ||||
| 
 | ||||
|     // Note: The following fields exist for backward compatibility
 | ||||
|     // with older APIs.  Accessing these fields directly is a sign of
 | ||||
|     // imprudent logic in the calling code.  Modern time-related code
 | ||||
|     // should only access this data indirectly by way of FormatTime().
 | ||||
|     // These fields are undefined for InfiniteFuture() and InfinitePast().
 | ||||
|     int offset;             // seconds east of UTC
 | ||||
|     bool is_dst;            // is offset non-standard?
 | ||||
|     const char* zone_abbr;  // time-zone abbreviation (e.g., "PST")
 | ||||
|   }; | ||||
| 
 | ||||
|   // TimeZone::At(Time)
 | ||||
|   //
 | ||||
|   // Returns the civil time for this TimeZone at a certain `absl::Time`.
 | ||||
|   // If the input time is infinite, the output civil second will be set to
 | ||||
|   // CivilSecond::max() or min(), and the subsecond will be infinite.
 | ||||
|   //
 | ||||
|   // Example:
 | ||||
|   //
 | ||||
|   //   const auto epoch = lax.At(absl::UnixEpoch());
 | ||||
|   //   // epoch.cs == 1969-12-31 16:00:00
 | ||||
|   //   // epoch.subsecond == absl::ZeroDuration()
 | ||||
|   //   // epoch.offset == -28800
 | ||||
|   //   // epoch.is_dst == false
 | ||||
|   //   // epoch.abbr == "PST"
 | ||||
|   CivilInfo At(Time t) const; | ||||
| 
 | ||||
|   // TimeZone::TimeInfo
 | ||||
|   //
 | ||||
|   // Information about the absolute times corresponding to a civil time.
 | ||||
|   // (Subseconds must be handled separately.)
 | ||||
|   //
 | ||||
|   // It is possible for a caller to pass a civil-time value that does
 | ||||
|   // not represent an actual or unique instant in time (due to a shift
 | ||||
|   // in UTC offset in the TimeZone, which results in a discontinuity in
 | ||||
|   // the civil-time components). For example, a daylight-saving-time
 | ||||
|   // transition skips or repeats civil times---in the United States,
 | ||||
|   // March 13, 2011 02:15 never occurred, while November 6, 2011 01:15
 | ||||
|   // occurred twice---so requests for such times are not well-defined.
 | ||||
|   // To account for these possibilities, `absl::TimeZone::TimeInfo` is
 | ||||
|   // richer than just a single `absl::Time`.
 | ||||
|   struct TimeInfo { | ||||
|     enum CivilKind { | ||||
|       UNIQUE,    // the civil time was singular (pre == trans == post)
 | ||||
|       SKIPPED,   // the civil time did not exist (pre => trans > post)
 | ||||
|       REPEATED,  // the civil time was ambiguous (pre < trans <= post)
 | ||||
|     } kind; | ||||
|     Time pre;    // time calculated using the pre-transition offset
 | ||||
|     Time trans;  // when the civil-time discontinuity occurred
 | ||||
|     Time post;   // time calculated using the post-transition offset
 | ||||
|   }; | ||||
| 
 | ||||
|   // TimeZone::At(CivilSecond)
 | ||||
|   //
 | ||||
|   // Returns an `absl::TimeInfo` containing the absolute time(s) for this
 | ||||
|   // TimeZone at an `absl::CivilSecond`. When the civil time is skipped or
 | ||||
|   // repeated, returns times calculated using the pre-transition and post-
 | ||||
|   // transition UTC offsets, plus the transition time itself.
 | ||||
|   //
 | ||||
|   // Examples:
 | ||||
|   //
 | ||||
|   //   // A unique civil time
 | ||||
|   //   const auto jan01 = lax.At(absl::CivilSecond(2011, 1, 1, 0, 0, 0));
 | ||||
|   //   // jan01.kind == TimeZone::TimeInfo::UNIQUE
 | ||||
|   //   // jan01.pre    is 2011-01-01 00:00:00 -0800
 | ||||
|   //   // jan01.trans  is 2011-01-01 00:00:00 -0800
 | ||||
|   //   // jan01.post   is 2011-01-01 00:00:00 -0800
 | ||||
|   //
 | ||||
|   //   // A Spring DST transition, when there is a gap in civil time
 | ||||
|   //   const auto mar13 = lax.At(absl::CivilSecond(2011, 3, 13, 2, 15, 0));
 | ||||
|   //   // mar13.kind == TimeZone::TimeInfo::SKIPPED
 | ||||
|   //   // mar13.pre   is 2011-03-13 03:15:00 -0700
 | ||||
|   //   // mar13.trans is 2011-03-13 03:00:00 -0700
 | ||||
|   //   // mar13.post  is 2011-03-13 01:15:00 -0800
 | ||||
|   //
 | ||||
|   //   // A Fall DST transition, when civil times are repeated
 | ||||
|   //   const auto nov06 = lax.At(absl::CivilSecond(2011, 11, 6, 1, 15, 0));
 | ||||
|   //   // nov06.kind == TimeZone::TimeInfo::REPEATED
 | ||||
|   //   // nov06.pre   is 2011-11-06 01:15:00 -0700
 | ||||
|   //   // nov06.trans is 2011-11-06 01:00:00 -0800
 | ||||
|   //   // nov06.post  is 2011-11-06 01:15:00 -0800
 | ||||
|   TimeInfo At(CivilSecond ct) const; | ||||
| 
 | ||||
|   template <typename H> | ||||
|   friend H AbslHashValue(H h, TimeZone tz) { | ||||
|     return H::combine(std::move(h), tz.cz_); | ||||
|  | @ -1127,6 +983,268 @@ inline TimeZone LocalTimeZone() { | |||
|   return TimeZone(time_internal::cctz::local_time_zone()); | ||||
| } | ||||
| 
 | ||||
| // ToCivilSecond()
 | ||||
| // ToCivilMinute()
 | ||||
| // ToCivilHour()
 | ||||
| // ToCivilDay()
 | ||||
| // ToCivilMonth()
 | ||||
| // ToCivilYear()
 | ||||
| //
 | ||||
| // Helpers for TimeZone::At(Time) to return particularly aligned civil times.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::Time t = ...;
 | ||||
| //   absl::TimeZone tz = ...;
 | ||||
| //   const auto cd = absl::ToCivilDay(t, tz);
 | ||||
| inline CivilSecond ToCivilSecond(Time t, TimeZone tz) { | ||||
|   return tz.At(t).cs;  // already a CivilSecond
 | ||||
| } | ||||
| inline CivilMinute ToCivilMinute(Time t, TimeZone tz) { | ||||
|   return CivilMinute(tz.At(t).cs); | ||||
| } | ||||
| inline CivilHour ToCivilHour(Time t, TimeZone tz) { | ||||
|   return CivilHour(tz.At(t).cs); | ||||
| } | ||||
| inline CivilDay ToCivilDay(Time t, TimeZone tz) { | ||||
|   return CivilDay(tz.At(t).cs); | ||||
| } | ||||
| inline CivilMonth ToCivilMonth(Time t, TimeZone tz) { | ||||
|   return CivilMonth(tz.At(t).cs); | ||||
| } | ||||
| inline CivilYear ToCivilYear(Time t, TimeZone tz) { | ||||
|   return CivilYear(tz.At(t).cs); | ||||
| } | ||||
| 
 | ||||
| // FromCivil()
 | ||||
| //
 | ||||
| // Helper for TimeZone::At(CivilSecond) that provides "order-preserving
 | ||||
| // semantics." If the civil time maps to a unique time, that time is
 | ||||
| // returned. If the civil time is repeated in the given time zone, the
 | ||||
| // time using the pre-transition offset is returned. Otherwise, the
 | ||||
| // civil time is skipped in the given time zone, and the transition time
 | ||||
| // is returned. This means that for any two civil times, ct1 and ct2,
 | ||||
| // (ct1 < ct2) => (FromCivil(ct1) <= FromCivil(ct2)), the equal case
 | ||||
| // being when two non-existent civil times map to the same transition time.
 | ||||
| //
 | ||||
| // Note: Accepts civil times of any alignment.
 | ||||
| inline Time FromCivil(CivilSecond ct, TimeZone tz) { | ||||
|   const auto ti = tz.At(ct); | ||||
|   if (ti.kind == TimeZone::TimeInfo::SKIPPED) return ti.trans; | ||||
|   return ti.pre; | ||||
| } | ||||
| 
 | ||||
| // TimeConversion
 | ||||
| //
 | ||||
| // An `absl::TimeConversion` represents the conversion of year, month, day,
 | ||||
| // hour, minute, and second values (i.e., a civil time), in a particular
 | ||||
| // `absl::TimeZone`, to a time instant (an absolute time), as returned by
 | ||||
| // `absl::ConvertDateTime()`. Lecacy version of `absl::TimeZone::TimeInfo`.
 | ||||
| //
 | ||||
| // Deprecated. Use `absl::TimeZone::TimeInfo`.
 | ||||
| struct | ||||
|     TimeConversion { | ||||
|   Time pre;    // time calculated using the pre-transition offset
 | ||||
|   Time trans;  // when the civil-time discontinuity occurred
 | ||||
|   Time post;   // time calculated using the post-transition offset
 | ||||
| 
 | ||||
|   enum Kind { | ||||
|     UNIQUE,    // the civil time was singular (pre == trans == post)
 | ||||
|     SKIPPED,   // the civil time did not exist
 | ||||
|     REPEATED,  // the civil time was ambiguous
 | ||||
|   }; | ||||
|   Kind kind; | ||||
| 
 | ||||
|   bool normalized;  // input values were outside their valid ranges
 | ||||
| }; | ||||
| 
 | ||||
| // ConvertDateTime()
 | ||||
| //
 | ||||
| // Legacy version of `absl::TimeZone::At(absl::CivilSecond)` that takes
 | ||||
| // the civil time as six, separate values (YMDHMS).
 | ||||
| //
 | ||||
| // The input month, day, hour, minute, and second values can be outside
 | ||||
| // of their valid ranges, in which case they will be "normalized" during
 | ||||
| // the conversion.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   // "October 32" normalizes to "November 1".
 | ||||
| //   absl::TimeConversion tc =
 | ||||
| //       absl::ConvertDateTime(2013, 10, 32, 8, 30, 0, lax);
 | ||||
| //   // tc.kind == TimeConversion::UNIQUE && tc.normalized == true
 | ||||
| //   // absl::ToCivilDay(tc.pre, tz).month() == 11
 | ||||
| //   // absl::ToCivilDay(tc.pre, tz).day() == 1
 | ||||
| //
 | ||||
| // Deprecated. Use `absl::TimeZone::At(CivilSecond)`.
 | ||||
| TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour, | ||||
|                                int min, int sec, TimeZone tz); | ||||
| 
 | ||||
| // FromDateTime()
 | ||||
| //
 | ||||
| // A convenience wrapper for `absl::ConvertDateTime()` that simply returns
 | ||||
| // the "pre" `absl::Time`.  That is, the unique result, or the instant that
 | ||||
| // is correct using the pre-transition offset (as if the transition never
 | ||||
| // happened).
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::Time t = absl::FromDateTime(2017, 9, 26, 9, 30, 0, lax);
 | ||||
| //   // t = 2017-09-26 09:30:00 -0700
 | ||||
| //
 | ||||
| // Deprecated. Use `absl::TimeZone::At(CivilSecond).pre`.
 | ||||
| inline Time FromDateTime(int64_t year, int mon, int day, int hour, | ||||
|                          int min, int sec, TimeZone tz) { | ||||
|   return ConvertDateTime(year, mon, day, hour, min, sec, tz).pre; | ||||
| } | ||||
| 
 | ||||
| // FromTM()
 | ||||
| //
 | ||||
| // Converts the `tm_year`, `tm_mon`, `tm_mday`, `tm_hour`, `tm_min`, and
 | ||||
| // `tm_sec` fields to an `absl::Time` using the given time zone. See ctime(3)
 | ||||
| // for a description of the expected values of the tm fields. If the indicated
 | ||||
| // time instant is not unique (see `absl::TimeZone::At(absl::CivilSecond)`
 | ||||
| // above), the `tm_isdst` field is consulted to select the desired instant
 | ||||
| // (`tm_isdst` > 0 means DST, `tm_isdst` == 0 means no DST, `tm_isdst` < 0
 | ||||
| // means use the post-transition offset).
 | ||||
| Time FromTM(const struct tm& tm, TimeZone tz); | ||||
| 
 | ||||
| // ToTM()
 | ||||
| //
 | ||||
| // Converts the given `absl::Time` to a struct tm using the given time zone.
 | ||||
| // See ctime(3) for a description of the values of the tm fields.
 | ||||
| struct tm ToTM(Time t, TimeZone tz); | ||||
| 
 | ||||
| // RFC3339_full
 | ||||
| // RFC3339_sec
 | ||||
| //
 | ||||
| // FormatTime()/ParseTime() format specifiers for RFC3339 date/time strings,
 | ||||
| // with trailing zeros trimmed or with fractional seconds omitted altogether.
 | ||||
| //
 | ||||
| // Note that RFC3339_sec[] matches an ISO 8601 extended format for date and
 | ||||
| // time with UTC offset.  Also note the use of "%Y": RFC3339 mandates that
 | ||||
| // years have exactly four digits, but we allow them to take their natural
 | ||||
| // width.
 | ||||
| extern const char RFC3339_full[];  // %Y-%m-%dT%H:%M:%E*S%Ez
 | ||||
| extern const char RFC3339_sec[];   // %Y-%m-%dT%H:%M:%S%Ez
 | ||||
| 
 | ||||
| // RFC1123_full
 | ||||
| // RFC1123_no_wday
 | ||||
| //
 | ||||
| // FormatTime()/ParseTime() format specifiers for RFC1123 date/time strings.
 | ||||
| extern const char RFC1123_full[];     // %a, %d %b %E4Y %H:%M:%S %z
 | ||||
| extern const char RFC1123_no_wday[];  // %d %b %E4Y %H:%M:%S %z
 | ||||
| 
 | ||||
| // FormatTime()
 | ||||
| //
 | ||||
| // Formats the given `absl::Time` in the `absl::TimeZone` according to the
 | ||||
| // provided format string. Uses strftime()-like formatting options, with
 | ||||
| // the following extensions:
 | ||||
| //
 | ||||
| //   - %Ez  - RFC3339-compatible numeric UTC offset (+hh:mm or -hh:mm)
 | ||||
| //   - %E*z - Full-resolution numeric UTC offset (+hh:mm:ss or -hh:mm:ss)
 | ||||
| //   - %E#S - Seconds with # digits of fractional precision
 | ||||
| //   - %E*S - Seconds with full fractional precision (a literal '*')
 | ||||
| //   - %E#f - Fractional seconds with # digits of precision
 | ||||
| //   - %E*f - Fractional seconds with full precision (a literal '*')
 | ||||
| //   - %E4Y - Four-character years (-999 ... -001, 0000, 0001 ... 9999)
 | ||||
| //
 | ||||
| // Note that %E0S behaves like %S, and %E0f produces no characters.  In
 | ||||
| // contrast %E*f always produces at least one digit, which may be '0'.
 | ||||
| //
 | ||||
| // Note that %Y produces as many characters as it takes to fully render the
 | ||||
| // year.  A year outside of [-999:9999] when formatted with %E4Y will produce
 | ||||
| // more than four characters, just like %Y.
 | ||||
| //
 | ||||
| // We recommend that format strings include the UTC offset (%z, %Ez, or %E*z)
 | ||||
| // so that the result uniquely identifies a time instant.
 | ||||
| //
 | ||||
| // Example:
 | ||||
| //
 | ||||
| //   absl::CivilSecond cs(2013, 1, 2, 3, 4, 5);
 | ||||
| //   absl::Time t = absl::FromCivil(cs, lax);
 | ||||
| //   string f = absl::FormatTime("%H:%M:%S", t, lax);  // "03:04:05"
 | ||||
| //   f = absl::FormatTime("%H:%M:%E3S", t, lax);  // "03:04:05.000"
 | ||||
| //
 | ||||
| // Note: If the given `absl::Time` is `absl::InfiniteFuture()`, the returned
 | ||||
| // string will be exactly "infinite-future". If the given `absl::Time` is
 | ||||
| // `absl::InfinitePast()`, the returned string will be exactly "infinite-past".
 | ||||
| // In both cases the given format string and `absl::TimeZone` are ignored.
 | ||||
| //
 | ||||
| std::string FormatTime(const std::string& format, Time t, TimeZone tz); | ||||
| 
 | ||||
| // Convenience functions that format the given time using the RFC3339_full
 | ||||
| // format.  The first overload uses the provided TimeZone, while the second
 | ||||
| // uses LocalTimeZone().
 | ||||
| std::string FormatTime(Time t, TimeZone tz); | ||||
| std::string FormatTime(Time t); | ||||
| 
 | ||||
| // Output stream operator.
 | ||||
| inline std::ostream& operator<<(std::ostream& os, Time t) { | ||||
|   return os << FormatTime(t); | ||||
| } | ||||
| 
 | ||||
| // ParseTime()
 | ||||
| //
 | ||||
| // Parses an input string according to the provided format string and
 | ||||
| // returns the corresponding `absl::Time`. Uses strftime()-like formatting
 | ||||
| // options, with the same extensions as FormatTime(), but with the
 | ||||
| // exceptions that %E#S is interpreted as %E*S, and %E#f as %E*f.  %Ez
 | ||||
| // and %E*z also accept the same inputs.
 | ||||
| //
 | ||||
| // %Y consumes as many numeric characters as it can, so the matching data
 | ||||
| // should always be terminated with a non-numeric.  %E4Y always consumes
 | ||||
| // exactly four characters, including any sign.
 | ||||
| //
 | ||||
| // Unspecified fields are taken from the default date and time of ...
 | ||||
| //
 | ||||
| //   "1970-01-01 00:00:00.0 +0000"
 | ||||
| //
 | ||||
| // For example, parsing a string of "15:45" (%H:%M) will return an absl::Time
 | ||||
| // that represents "1970-01-01 15:45:00.0 +0000".
 | ||||
| //
 | ||||
| // Note that since ParseTime() returns time instants, it makes the most sense
 | ||||
| // to parse fully-specified date/time strings that include a UTC offset (%z,
 | ||||
| // %Ez, or %E*z).
 | ||||
| //
 | ||||
| // Note also that `absl::ParseTime()` only heeds the fields year, month, day,
 | ||||
| // hour, minute, (fractional) second, and UTC offset.  Other fields, like
 | ||||
| // weekday (%a or %A), while parsed for syntactic validity, are ignored
 | ||||
| // in the conversion.
 | ||||
| //
 | ||||
| // Date and time fields that are out-of-range will be treated as errors
 | ||||
| // rather than normalizing them like `absl::CivilSecond` does.  For example,
 | ||||
| // it is an error to parse the date "Oct 32, 2013" because 32 is out of range.
 | ||||
| //
 | ||||
| // A leap second of ":60" is normalized to ":00" of the following minute
 | ||||
| // with fractional seconds discarded.  The following table shows how the
 | ||||
| // given seconds and subseconds will be parsed:
 | ||||
| //
 | ||||
| //   "59.x" -> 59.x  // exact
 | ||||
| //   "60.x" -> 00.0  // normalized
 | ||||
| //   "00.x" -> 00.x  // exact
 | ||||
| //
 | ||||
| // Errors are indicated by returning false and assigning an error message
 | ||||
| // to the "err" out param if it is non-null.
 | ||||
| //
 | ||||
| // Note: If the input string is exactly "infinite-future", the returned
 | ||||
| // `absl::Time` will be `absl::InfiniteFuture()` and `true` will be returned.
 | ||||
| // If the input string is "infinite-past", the returned `absl::Time` will be
 | ||||
| // `absl::InfinitePast()` and `true` will be returned.
 | ||||
| //
 | ||||
| bool ParseTime(const std::string& format, const std::string& input, Time* time, | ||||
|                std::string* err); | ||||
| 
 | ||||
| // Like ParseTime() above, but if the format string does not contain a UTC
 | ||||
| // offset specification (%z/%Ez/%E*z) then the input is interpreted in the
 | ||||
| // given TimeZone.  This means that the input, by itself, does not identify a
 | ||||
| // unique instant.  Being time-zone dependent, it also admits the possibility
 | ||||
| // of ambiguity or non-existence, in which case the "pre" time (as defined
 | ||||
| // by TimeZone::TimeInfo) is returned.  For these reasons we recommend that
 | ||||
| // all date/time strings include a UTC offset so they're context independent.
 | ||||
| bool ParseTime(const std::string& format, const std::string& input, TimeZone tz, | ||||
|                Time* time, std::string* err); | ||||
| 
 | ||||
| // ============================================================================
 | ||||
| // Implementation Details Follow
 | ||||
| // ============================================================================
 | ||||
|  |  | |||
|  | @ -169,32 +169,32 @@ void BM_Time_ToUnixSeconds(benchmark::State& state) { | |||
| BENCHMARK(BM_Time_ToUnixSeconds); | ||||
| 
 | ||||
| //
 | ||||
| // FromDateTime
 | ||||
| // FromCivil
 | ||||
| //
 | ||||
| // In each "FromDateTime" benchmark we switch between two YMDhms
 | ||||
| // values separated by at least one transition in order to defeat any
 | ||||
| // internal caching of previous results (e.g., see time_local_hint_).
 | ||||
| // In each "FromCivil" benchmark we switch between two YMDhms values
 | ||||
| // separated by at least one transition in order to defeat any internal
 | ||||
| // caching of previous results (e.g., see time_local_hint_).
 | ||||
| //
 | ||||
| // The "UTC" variants use UTC instead of the Google/local time zone.
 | ||||
| // The "Day0" variants require normalization of the day of month.
 | ||||
| //
 | ||||
| 
 | ||||
| void BM_Time_FromDateTime_Absl(benchmark::State& state) { | ||||
| void BM_Time_FromCivil_Absl(benchmark::State& state) { | ||||
|   const absl::TimeZone tz = | ||||
|       absl::time_internal::LoadTimeZone("America/Los_Angeles"); | ||||
|   int i = 0; | ||||
|   while (state.KeepRunning()) { | ||||
|     if ((i & 1) == 0) { | ||||
|       absl::FromDateTime(2014, 12, 18, 20, 16, 18, tz); | ||||
|       absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz); | ||||
|     } else { | ||||
|       absl::FromDateTime(2013, 11, 15, 18, 30, 27, tz); | ||||
|       absl::FromCivil(absl::CivilSecond(2013, 11, 15, 18, 30, 27), tz); | ||||
|     } | ||||
|     ++i; | ||||
|   } | ||||
| } | ||||
| BENCHMARK(BM_Time_FromDateTime_Absl); | ||||
| BENCHMARK(BM_Time_FromCivil_Absl); | ||||
| 
 | ||||
| void BM_Time_FromDateTime_Libc(benchmark::State& state) { | ||||
| void BM_Time_FromCivil_Libc(benchmark::State& state) { | ||||
|   // No timezone support, so just use localtime.
 | ||||
|   int i = 0; | ||||
|   while (state.KeepRunning()) { | ||||
|  | @ -219,32 +219,32 @@ void BM_Time_FromDateTime_Libc(benchmark::State& state) { | |||
|     ++i; | ||||
|   } | ||||
| } | ||||
| BENCHMARK(BM_Time_FromDateTime_Libc); | ||||
| BENCHMARK(BM_Time_FromCivil_Libc); | ||||
| 
 | ||||
| void BM_Time_FromDateTimeUTC_Absl(benchmark::State& state) { | ||||
| void BM_Time_FromCivilUTC_Absl(benchmark::State& state) { | ||||
|   const absl::TimeZone tz = absl::UTCTimeZone(); | ||||
|   while (state.KeepRunning()) { | ||||
|     FromDateTime(2014, 12, 18, 20, 16, 18, tz); | ||||
|     absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz); | ||||
|   } | ||||
| } | ||||
| BENCHMARK(BM_Time_FromDateTimeUTC_Absl); | ||||
| BENCHMARK(BM_Time_FromCivilUTC_Absl); | ||||
| 
 | ||||
| void BM_Time_FromDateTimeDay0_Absl(benchmark::State& state) { | ||||
| void BM_Time_FromCivilDay0_Absl(benchmark::State& state) { | ||||
|   const absl::TimeZone tz = | ||||
|       absl::time_internal::LoadTimeZone("America/Los_Angeles"); | ||||
|   int i = 0; | ||||
|   while (state.KeepRunning()) { | ||||
|     if ((i & 1) == 0) { | ||||
|       absl::FromDateTime(2014, 12, 0, 20, 16, 18, tz); | ||||
|       absl::FromCivil(absl::CivilSecond(2014, 12, 0, 20, 16, 18), tz); | ||||
|     } else { | ||||
|       absl::FromDateTime(2013, 11, 0, 18, 30, 27, tz); | ||||
|       absl::FromCivil(absl::CivilSecond(2013, 11, 0, 18, 30, 27), tz); | ||||
|     } | ||||
|     ++i; | ||||
|   } | ||||
| } | ||||
| BENCHMARK(BM_Time_FromDateTimeDay0_Absl); | ||||
| BENCHMARK(BM_Time_FromCivilDay0_Absl); | ||||
| 
 | ||||
| void BM_Time_FromDateTimeDay0_Libc(benchmark::State& state) { | ||||
| void BM_Time_FromCivilDay0_Libc(benchmark::State& state) { | ||||
|   // No timezone support, so just use localtime.
 | ||||
|   int i = 0; | ||||
|   while (state.KeepRunning()) { | ||||
|  | @ -269,7 +269,7 @@ void BM_Time_FromDateTimeDay0_Libc(benchmark::State& state) { | |||
|     ++i; | ||||
|   } | ||||
| } | ||||
| BENCHMARK(BM_Time_FromDateTimeDay0_Libc); | ||||
| BENCHMARK(BM_Time_FromCivilDay0_Libc); | ||||
| 
 | ||||
| //
 | ||||
| // To/FromTimespec
 | ||||
|  |  | |||
|  | @ -1,306 +0,0 @@ | |||
| // Copyright 2017 The Abseil Authors.
 | ||||
| //
 | ||||
| // Licensed under the Apache License, Version 2.0 (the "License");
 | ||||
| // you may not use this file except in compliance with the License.
 | ||||
| // You may obtain a copy of the License at
 | ||||
| //
 | ||||
| //      http://www.apache.org/licenses/LICENSE-2.0
 | ||||
| //
 | ||||
| // Unless required by applicable law or agreed to in writing, software
 | ||||
| // distributed under the License is distributed on an "AS IS" BASIS,
 | ||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | ||||
| // See the License for the specific language governing permissions and
 | ||||
| // limitations under the License.
 | ||||
| 
 | ||||
| // This file contains tests for FromDateTime() normalization, which is
 | ||||
| // time-zone independent so we just use UTC throughout.
 | ||||
| 
 | ||||
| #include <cstdint> | ||||
| #include <limits> | ||||
| 
 | ||||
| #include "gmock/gmock.h" | ||||
| #include "gtest/gtest.h" | ||||
| #include "absl/time/internal/test_util.h" | ||||
| #include "absl/time/time.h" | ||||
| 
 | ||||
| namespace { | ||||
| 
 | ||||
| TEST(TimeNormCase, SimpleOverflow) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
| 
 | ||||
|   absl::TimeConversion tc = | ||||
|       absl::ConvertDateTime(2013, 11, 15, 16, 32, 59 + 1, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   absl::Time::Breakdown bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 15, 16, 33, 0, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 11, 15, 16, 59 + 1, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 15, 17, 0, 14, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 11, 15, 23 + 1, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 16, 0, 32, 14, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 11, 30 + 1, 16, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 12, 1, 16, 32, 14, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 12 + 1, 15, 16, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2014, 1, 15, 16, 32, 14, 0, false); | ||||
| } | ||||
| 
 | ||||
| TEST(TimeNormCase, SimpleUnderflow) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
| 
 | ||||
|   absl::TimeConversion tc = ConvertDateTime(2013, 11, 15, 16, 32, 0 - 1, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   absl::Time::Breakdown bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 15, 16, 31, 59, 0, false); | ||||
| 
 | ||||
|   tc = ConvertDateTime(2013, 11, 15, 16, 0 - 1, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 15, 15, 59, 14, 0, false); | ||||
| 
 | ||||
|   tc = ConvertDateTime(2013, 11, 15, 0 - 1, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 14, 23, 32, 14, 0, false); | ||||
| 
 | ||||
|   tc = ConvertDateTime(2013, 11, 1 - 1, 16, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 10, 31, 16, 32, 14, 0, false); | ||||
| 
 | ||||
|   tc = ConvertDateTime(2013, 1 - 1, 15, 16, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2012, 12, 15, 16, 32, 14, 0, false); | ||||
| } | ||||
| 
 | ||||
| TEST(TimeNormCase, MultipleOverflow) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
|   absl::TimeConversion tc = ConvertDateTime(2013, 12, 31, 23, 59, 59 + 1, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   absl::Time::Breakdown bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2014, 1, 1, 0, 0, 0, 0, false); | ||||
| } | ||||
| 
 | ||||
| TEST(TimeNormCase, MultipleUnderflow) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
|   absl::TimeConversion tc = absl::ConvertDateTime(2014, 1, 1, 0, 0, 0 - 1, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   absl::Time::Breakdown bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 12, 31, 23, 59, 59, 0, false); | ||||
| } | ||||
| 
 | ||||
| TEST(TimeNormCase, OverflowLimits) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
|   absl::TimeConversion tc; | ||||
|   absl::Time::Breakdown bd; | ||||
| 
 | ||||
|   const int kintmax = std::numeric_limits<int>::max(); | ||||
|   tc = absl::ConvertDateTime(0, kintmax, kintmax, kintmax, kintmax, kintmax, | ||||
|                              utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 185085715, 11, 27, 12, 21, 7, 0, false); | ||||
| 
 | ||||
|   const int kintmin = std::numeric_limits<int>::min(); | ||||
|   tc = absl::ConvertDateTime(0, kintmin, kintmin, kintmin, kintmin, kintmin, | ||||
|                              utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, -185085717, 10, 31, 10, 37, 52, 0, false); | ||||
| 
 | ||||
|   const int64_t max_year = std::numeric_limits<int64_t>::max(); | ||||
|   tc = absl::ConvertDateTime(max_year, 12, 31, 23, 59, 59, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   EXPECT_EQ(absl::InfiniteFuture(), tc.pre); | ||||
| 
 | ||||
|   const int64_t min_year = std::numeric_limits<int64_t>::min(); | ||||
|   tc = absl::ConvertDateTime(min_year, 1, 1, 0, 0, 0, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   EXPECT_EQ(absl::InfinitePast(), tc.pre); | ||||
| } | ||||
| 
 | ||||
| TEST(TimeNormCase, ComplexOverflow) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
| 
 | ||||
|   absl::TimeConversion tc = | ||||
|       ConvertDateTime(2013, 11, 15, 16, 32, 14 + 123456789, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   absl::Time::Breakdown bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2017, 10, 14, 14, 5, 23, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 11, 15, 16, 32 + 1234567, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2016, 3, 22, 0, 39, 14, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 11, 15, 16 + 123456, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2027, 12, 16, 16, 32, 14, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 11, 15 + 1234, 16, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2017, 4, 2, 16, 32, 14, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 11 + 123, 15, 16, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2024, 2, 15, 16, 32, 14, 0, false); | ||||
| } | ||||
| 
 | ||||
| TEST(TimeNormCase, ComplexUnderflow) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
| 
 | ||||
|   absl::TimeConversion tc = | ||||
|       absl::ConvertDateTime(1999, 3, 0, 0, 0, 0, utc);  // year 400
 | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   absl::Time::Breakdown bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 1999, 2, 28, 0, 0, 0, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 11, 15, 16, 32, 14 - 123456789, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2009, 12, 17, 18, 59, 5, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 11, 15, 16, 32 - 1234567, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2011, 7, 12, 8, 25, 14, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 11, 15, 16 - 123456, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 1999, 10, 16, 16, 32, 14, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 11, 15 - 1234, 16, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2010, 6, 30, 16, 32, 14, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 11 - 123, 15, 16, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2003, 8, 15, 16, 32, 14, 0, false); | ||||
| } | ||||
| 
 | ||||
| TEST(TimeNormCase, Mishmash) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
| 
 | ||||
|   absl::TimeConversion tc = | ||||
|       absl::ConvertDateTime(2013, 11 - 123, 15 + 1234, 16 - 123456, | ||||
|                             32 + 1234567, 14 - 123456789, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   absl::Time::Breakdown bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 1991, 5, 9, 3, 6, 5, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2013, 11 + 123, 15 - 1234, 16 + 123456, | ||||
|                              32 - 1234567, 14 + 123456789, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2036, 5, 24, 5, 58, 23, 0, false); | ||||
| 
 | ||||
|   // Here is a normalization case we got wrong for a while.  Because the
 | ||||
|   // day is converted to "1" within a 400-year (146097-day) period, we
 | ||||
|   // didn't need to roll the month and so we didn't mark it as normalized.
 | ||||
|   tc = absl::ConvertDateTime(2013, 11, -146097 + 1, 16, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 1613, 11, 1, 16, 32, 14, 0, false); | ||||
| 
 | ||||
|   // Even though the month overflow compensates for the day underflow,
 | ||||
|   // this should still be marked as normalized.
 | ||||
|   tc = absl::ConvertDateTime(2013, 11 + 400 * 12, -146097 + 1, 16, 32, 14, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 1, 16, 32, 14, 0, false); | ||||
| } | ||||
| 
 | ||||
| TEST(TimeNormCase, LeapYears) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
| 
 | ||||
|   absl::TimeConversion tc = | ||||
|       absl::ConvertDateTime(2013, 2, 28 + 1, 0, 0, 0, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   absl::Time::Breakdown bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 3, 1, 0, 0, 0, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2012, 2, 28 + 1, 0, 0, 0, utc); | ||||
|   EXPECT_FALSE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2012, 2, 29, 0, 0, 0, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(2000, 2, 28 + 1, 0, 0, 0, utc); | ||||
|   EXPECT_FALSE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 2000, 2, 29, 0, 0, 0, 0, false); | ||||
| 
 | ||||
|   tc = absl::ConvertDateTime(1900, 2, 28 + 1, 0, 0, 0, utc); | ||||
|   EXPECT_TRUE(tc.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|   bd = tc.pre.In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 1900, 3, 1, 0, 0, 0, 0, false); | ||||
| } | ||||
| 
 | ||||
| // Convert all the days from 1970-1-1 to 1970-1-146097 (aka 2369-12-31)
 | ||||
| // and check that they normalize to the expected time.  146097 days span
 | ||||
| // the 400-year Gregorian cycle used during normalization.
 | ||||
| TEST(TimeNormCase, AllTheDays) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
|   absl::Time exp_time = absl::UnixEpoch(); | ||||
| 
 | ||||
|   for (int day = 1; day <= 146097; ++day) { | ||||
|     absl::TimeConversion tc = absl::ConvertDateTime(1970, 1, day, 0, 0, 0, utc); | ||||
|     EXPECT_EQ(day > 31, tc.normalized); | ||||
|     EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind); | ||||
|     EXPECT_EQ(exp_time, tc.pre); | ||||
|     exp_time += absl::Hours(24); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| }  // namespace
 | ||||
|  | @ -28,6 +28,27 @@ | |||
| 
 | ||||
| namespace { | ||||
| 
 | ||||
| #if GTEST_USES_SIMPLE_RE | ||||
| const char kZoneAbbrRE[] = ".*";  // just punt
 | ||||
| #else | ||||
| const char kZoneAbbrRE[] = "[A-Za-z]{3,4}|[-+][0-9]{2}([0-9]{2})?"; | ||||
| #endif | ||||
| 
 | ||||
| // This helper is a macro so that failed expectations show up with the
 | ||||
| // correct line numbers.
 | ||||
| #define EXPECT_CIVIL_INFO(ci, y, m, d, h, min, s, off, isdst)      \ | ||||
|   do {                                                             \ | ||||
|     EXPECT_EQ(y, ci.cs.year());                                    \ | ||||
|     EXPECT_EQ(m, ci.cs.month());                                   \ | ||||
|     EXPECT_EQ(d, ci.cs.day());                                     \ | ||||
|     EXPECT_EQ(h, ci.cs.hour());                                    \ | ||||
|     EXPECT_EQ(min, ci.cs.minute());                                \ | ||||
|     EXPECT_EQ(s, ci.cs.second());                                  \ | ||||
|     EXPECT_EQ(off, ci.offset);                                     \ | ||||
|     EXPECT_EQ(isdst, ci.is_dst);                                   \ | ||||
|     EXPECT_THAT(ci.zone_abbr, testing::MatchesRegex(kZoneAbbrRE)); \ | ||||
|   } while (0) | ||||
| 
 | ||||
| // A gMock matcher to match timespec values. Use this matcher like:
 | ||||
| // timespec ts1, ts2;
 | ||||
| // EXPECT_THAT(ts1, TimespecMatcher(ts2));
 | ||||
|  | @ -84,10 +105,10 @@ TEST(Time, ValueSemantics) { | |||
| } | ||||
| 
 | ||||
| TEST(Time, UnixEpoch) { | ||||
|   absl::Time::Breakdown bd = absl::UnixEpoch().In(absl::UTCTimeZone()); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 1970, 1, 1, 0, 0, 0, 0, false); | ||||
|   EXPECT_EQ(absl::ZeroDuration(), bd.subsecond); | ||||
|   EXPECT_EQ(4, bd.weekday);  // Thursday
 | ||||
|   const auto ci = absl::UTCTimeZone().At(absl::UnixEpoch()); | ||||
|   EXPECT_EQ(absl::CivilSecond(1970, 1, 1, 0, 0, 0), ci.cs); | ||||
|   EXPECT_EQ(absl::ZeroDuration(), ci.subsecond); | ||||
|   EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(absl::CivilDay(ci.cs))); | ||||
| } | ||||
| 
 | ||||
| TEST(Time, Breakdown) { | ||||
|  | @ -95,26 +116,26 @@ TEST(Time, Breakdown) { | |||
|   absl::Time t = absl::UnixEpoch(); | ||||
| 
 | ||||
|   // The Unix epoch as seen in NYC.
 | ||||
|   absl::Time::Breakdown bd = t.In(tz); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 1969, 12, 31, 19, 0, 0, -18000, false); | ||||
|   EXPECT_EQ(absl::ZeroDuration(), bd.subsecond); | ||||
|   EXPECT_EQ(3, bd.weekday);  // Wednesday
 | ||||
|   auto ci = tz.At(t); | ||||
|   EXPECT_CIVIL_INFO(ci, 1969, 12, 31, 19, 0, 0, -18000, false); | ||||
|   EXPECT_EQ(absl::ZeroDuration(), ci.subsecond); | ||||
|   EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(absl::CivilDay(ci.cs))); | ||||
| 
 | ||||
|   // Just before the epoch.
 | ||||
|   t -= absl::Nanoseconds(1); | ||||
|   bd = t.In(tz); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 1969, 12, 31, 18, 59, 59, -18000, false); | ||||
|   EXPECT_EQ(absl::Nanoseconds(999999999), bd.subsecond); | ||||
|   EXPECT_EQ(3, bd.weekday);  // Wednesday
 | ||||
|   ci = tz.At(t); | ||||
|   EXPECT_CIVIL_INFO(ci, 1969, 12, 31, 18, 59, 59, -18000, false); | ||||
|   EXPECT_EQ(absl::Nanoseconds(999999999), ci.subsecond); | ||||
|   EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(absl::CivilDay(ci.cs))); | ||||
| 
 | ||||
|   // Some time later.
 | ||||
|   t += absl::Hours(24) * 2735; | ||||
|   t += absl::Hours(18) + absl::Minutes(30) + absl::Seconds(15) + | ||||
|        absl::Nanoseconds(9); | ||||
|   bd = t.In(tz); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 1977, 6, 28, 14, 30, 15, -14400, true); | ||||
|   EXPECT_EQ(8, bd.subsecond / absl::Nanoseconds(1)); | ||||
|   EXPECT_EQ(2, bd.weekday);  // Tuesday
 | ||||
|   ci = tz.At(t); | ||||
|   EXPECT_CIVIL_INFO(ci, 1977, 6, 28, 14, 30, 15, -14400, true); | ||||
|   EXPECT_EQ(8, ci.subsecond / absl::Nanoseconds(1)); | ||||
|   EXPECT_EQ(absl::Weekday::tuesday, absl::GetWeekday(absl::CivilDay(ci.cs))); | ||||
| } | ||||
| 
 | ||||
| TEST(Time, AdditiveOperators) { | ||||
|  | @ -550,67 +571,63 @@ TEST(Time, ToChronoTime) { | |||
|             absl::ToChronoTime(absl::UnixEpoch() - tick)); | ||||
| } | ||||
| 
 | ||||
| TEST(Time, ConvertDateTime) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
|   const absl::TimeZone goog = | ||||
|       absl::time_internal::LoadTimeZone("America/Los_Angeles"); | ||||
| TEST(Time, TimeZoneAt) { | ||||
|   const absl::TimeZone nyc = | ||||
|       absl::time_internal::LoadTimeZone("America/New_York"); | ||||
|   const std::string fmt = "%a, %e %b %Y %H:%M:%S %z (%Z)"; | ||||
| 
 | ||||
|   // A simple case of normalization.
 | ||||
|   absl::TimeConversion oct32 = ConvertDateTime(2013, 10, 32, 8, 30, 0, goog); | ||||
|   EXPECT_TRUE(oct32.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, oct32.kind); | ||||
|   absl::TimeConversion nov01 = ConvertDateTime(2013, 11, 1, 8, 30, 0, goog); | ||||
|   EXPECT_FALSE(nov01.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, nov01.kind); | ||||
|   EXPECT_EQ(oct32.pre, nov01.pre); | ||||
|   EXPECT_EQ("Fri,  1 Nov 2013 08:30:00 -0700 (PDT)", | ||||
|             absl::FormatTime(fmt, nov01.pre, goog)); | ||||
|   // A non-transition where the civil time is unique.
 | ||||
|   absl::CivilSecond nov01(2013, 11, 1, 8, 30, 0); | ||||
|   const auto nov01_ci = nyc.At(nov01); | ||||
|   EXPECT_EQ(absl::TimeZone::TimeInfo::UNIQUE, nov01_ci.kind); | ||||
|   EXPECT_EQ("Fri,  1 Nov 2013 08:30:00 -0400 (EDT)", | ||||
|             absl::FormatTime(fmt, nov01_ci.pre, nyc)); | ||||
|   EXPECT_EQ(nov01_ci.pre, nov01_ci.trans); | ||||
|   EXPECT_EQ(nov01_ci.pre, nov01_ci.post); | ||||
|   EXPECT_EQ(nov01_ci.pre, absl::FromCivil(nov01, nyc)); | ||||
| 
 | ||||
|   // A Spring DST transition, when there is a gap in civil time
 | ||||
|   // and we prefer the later of the possible interpretations of a
 | ||||
|   // non-existent time.
 | ||||
|   absl::TimeConversion mar13 = ConvertDateTime(2011, 3, 13, 2, 15, 0, nyc); | ||||
|   EXPECT_FALSE(mar13.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::SKIPPED, mar13.kind); | ||||
|   absl::CivilSecond mar13(2011, 3, 13, 2, 15, 0); | ||||
|   const auto mar_ci = nyc.At(mar13); | ||||
|   EXPECT_EQ(absl::TimeZone::TimeInfo::SKIPPED, mar_ci.kind); | ||||
|   EXPECT_EQ("Sun, 13 Mar 2011 03:15:00 -0400 (EDT)", | ||||
|             absl::FormatTime(fmt, mar13.pre, nyc)); | ||||
|             absl::FormatTime(fmt, mar_ci.pre, nyc)); | ||||
|   EXPECT_EQ("Sun, 13 Mar 2011 03:00:00 -0400 (EDT)", | ||||
|             absl::FormatTime(fmt, mar13.trans, nyc)); | ||||
|             absl::FormatTime(fmt, mar_ci.trans, nyc)); | ||||
|   EXPECT_EQ("Sun, 13 Mar 2011 01:15:00 -0500 (EST)", | ||||
|             absl::FormatTime(fmt, mar13.post, nyc)); | ||||
|   EXPECT_EQ(mar13.pre, absl::FromDateTime(2011, 3, 13, 2, 15, 0, nyc)); | ||||
|             absl::FormatTime(fmt, mar_ci.post, nyc)); | ||||
|   EXPECT_EQ(mar_ci.trans, absl::FromCivil(mar13, nyc)); | ||||
| 
 | ||||
|   // A Fall DST transition, when civil times are repeated and
 | ||||
|   // we prefer the earlier of the possible interpretations of an
 | ||||
|   // ambiguous time.
 | ||||
|   absl::TimeConversion nov06 = ConvertDateTime(2011, 11, 6, 1, 15, 0, nyc); | ||||
|   EXPECT_FALSE(nov06.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::REPEATED, nov06.kind); | ||||
|   absl::CivilSecond nov06(2011, 11, 6, 1, 15, 0); | ||||
|   const auto nov06_ci = nyc.At(nov06); | ||||
|   EXPECT_EQ(absl::TimeZone::TimeInfo::REPEATED, nov06_ci.kind); | ||||
|   EXPECT_EQ("Sun,  6 Nov 2011 01:15:00 -0400 (EDT)", | ||||
|             absl::FormatTime(fmt, nov06.pre, nyc)); | ||||
|             absl::FormatTime(fmt, nov06_ci.pre, nyc)); | ||||
|   EXPECT_EQ("Sun,  6 Nov 2011 01:00:00 -0500 (EST)", | ||||
|             absl::FormatTime(fmt, nov06.trans, nyc)); | ||||
|             absl::FormatTime(fmt, nov06_ci.trans, nyc)); | ||||
|   EXPECT_EQ("Sun,  6 Nov 2011 01:15:00 -0500 (EST)", | ||||
|             absl::FormatTime(fmt, nov06.post, nyc)); | ||||
|   EXPECT_EQ(nov06.pre, absl::FromDateTime(2011, 11, 6, 1, 15, 0, nyc)); | ||||
|             absl::FormatTime(fmt, nov06_ci.post, nyc)); | ||||
|   EXPECT_EQ(nov06_ci.pre, absl::FromCivil(nov06, nyc)); | ||||
| 
 | ||||
|   // Check that (time_t) -1 is handled correctly.
 | ||||
|   absl::TimeConversion minus1 = ConvertDateTime(1969, 12, 31, 18, 59, 59, nyc); | ||||
|   EXPECT_FALSE(minus1.normalized); | ||||
|   EXPECT_EQ(absl::TimeConversion::UNIQUE, minus1.kind); | ||||
|   EXPECT_EQ(-1, absl::ToTimeT(minus1.pre)); | ||||
|   absl::CivilSecond minus1(1969, 12, 31, 18, 59, 59); | ||||
|   const auto minus1_cl = nyc.At(minus1); | ||||
|   EXPECT_EQ(absl::TimeZone::TimeInfo::UNIQUE, minus1_cl.kind); | ||||
|   EXPECT_EQ(-1, absl::ToTimeT(minus1_cl.pre)); | ||||
|   EXPECT_EQ("Wed, 31 Dec 1969 18:59:59 -0500 (EST)", | ||||
|             absl::FormatTime(fmt, minus1.pre, nyc)); | ||||
|             absl::FormatTime(fmt, minus1_cl.pre, nyc)); | ||||
|   EXPECT_EQ("Wed, 31 Dec 1969 23:59:59 +0000 (UTC)", | ||||
|             absl::FormatTime(fmt, minus1.pre, utc)); | ||||
|             absl::FormatTime(fmt, minus1_cl.pre, absl::UTCTimeZone())); | ||||
| } | ||||
| 
 | ||||
| // FromDateTime(year, mon, day, hour, min, sec, UTCTimeZone()) has
 | ||||
| // a specialized fastpath implementation which we exercise here.
 | ||||
| TEST(Time, FromDateTimeUTC) { | ||||
| // FromCivil(CivilSecond(year, mon, day, hour, min, sec), UTCTimeZone())
 | ||||
| // has a specialized fastpath implementation, which we exercise here.
 | ||||
| TEST(Time, FromCivilUTC) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
|   const std::string fmt = "%a, %e %b %Y %H:%M:%S %z (%Z)"; | ||||
|   const int kMax = std::numeric_limits<int>::max(); | ||||
|  | @ -618,65 +635,36 @@ TEST(Time, FromDateTimeUTC) { | |||
|   absl::Time t; | ||||
| 
 | ||||
|   // 292091940881 is the last positive year to use the fastpath.
 | ||||
|   t = absl::FromDateTime(292091940881, kMax, kMax, kMax, kMax, kMax, utc); | ||||
|   t = absl::FromCivil( | ||||
|       absl::CivilSecond(292091940881, kMax, kMax, kMax, kMax, kMax), utc); | ||||
|   EXPECT_EQ("Fri, 25 Nov 292277026596 12:21:07 +0000 (UTC)", | ||||
|             absl::FormatTime(fmt, t, utc)); | ||||
|   t = absl::FromDateTime(292091940882, kMax, kMax, kMax, kMax, kMax, utc); | ||||
|   EXPECT_EQ("infinite-future", absl::FormatTime(fmt, t, utc));  // no overflow
 | ||||
|   t = absl::FromDateTime( | ||||
|       std::numeric_limits<int64_t>::max(), kMax, kMax, kMax, kMax, kMax, utc); | ||||
|   t = absl::FromCivil( | ||||
|       absl::CivilSecond(292091940882, kMax, kMax, kMax, kMax, kMax), utc); | ||||
|   EXPECT_EQ("infinite-future", absl::FormatTime(fmt, t, utc));  // no overflow
 | ||||
| 
 | ||||
|   // -292091936940 is the last negative year to use the fastpath.
 | ||||
|   t = absl::FromDateTime(-292091936940, kMin, kMin, kMin, kMin, kMin, utc); | ||||
|   t = absl::FromCivil( | ||||
|       absl::CivilSecond(-292091936940, kMin, kMin, kMin, kMin, kMin), utc); | ||||
|   EXPECT_EQ("Fri,  1 Nov -292277022657 10:37:52 +0000 (UTC)", | ||||
|             absl::FormatTime(fmt, t, utc)); | ||||
|   t = absl::FromDateTime(-292091936941, kMin, kMin, kMin, kMin, kMin, utc); | ||||
|   t = absl::FromCivil( | ||||
|       absl::CivilSecond(-292091936941, kMin, kMin, kMin, kMin, kMin), utc); | ||||
|   EXPECT_EQ("infinite-past", absl::FormatTime(fmt, t, utc));  // no underflow
 | ||||
|   t = absl::FromDateTime( | ||||
|       std::numeric_limits<int64_t>::min(), kMin, kMin, kMin, kMin, kMin, utc); | ||||
|   EXPECT_EQ("infinite-past", absl::FormatTime(fmt, t, utc));  // no overflow
 | ||||
| 
 | ||||
|   // Check that we're counting leap years correctly.
 | ||||
|   t = absl::FromDateTime(1900, 2, 28, 23, 59, 59, utc); | ||||
|   t = absl::FromCivil(absl::CivilSecond(1900, 2, 28, 23, 59, 59), utc); | ||||
|   EXPECT_EQ("Wed, 28 Feb 1900 23:59:59 +0000 (UTC)", | ||||
|             absl::FormatTime(fmt, t, utc)); | ||||
|   t = absl::FromDateTime(1900, 3, 1, 0, 0, 0, utc); | ||||
|   t = absl::FromCivil(absl::CivilSecond(1900, 3, 1, 0, 0, 0), utc); | ||||
|   EXPECT_EQ("Thu,  1 Mar 1900 00:00:00 +0000 (UTC)", | ||||
|             absl::FormatTime(fmt, t, utc)); | ||||
|   t = absl::FromDateTime(2000, 2, 29, 23, 59, 59, utc); | ||||
|   t = absl::FromCivil(absl::CivilSecond(2000, 2, 29, 23, 59, 59), utc); | ||||
|   EXPECT_EQ("Tue, 29 Feb 2000 23:59:59 +0000 (UTC)", | ||||
|             absl::FormatTime(fmt, t, utc)); | ||||
|   t = absl::FromDateTime(2000, 3, 1, 0, 0, 0, utc); | ||||
|   t = absl::FromCivil(absl::CivilSecond(2000, 3, 1, 0, 0, 0), utc); | ||||
|   EXPECT_EQ("Wed,  1 Mar 2000 00:00:00 +0000 (UTC)", | ||||
|             absl::FormatTime(fmt, t, utc)); | ||||
| 
 | ||||
|   // Check normalization.
 | ||||
|   const std::string ymdhms = "%Y-%m-%d %H:%M:%S"; | ||||
|   t = absl::FromDateTime(2015, 1, 1, 0, 0, 60, utc); | ||||
|   EXPECT_EQ("2015-01-01 00:01:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, 1, 0, 60, 0, utc); | ||||
|   EXPECT_EQ("2015-01-01 01:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, 1, 24, 0, 0, utc); | ||||
|   EXPECT_EQ("2015-01-02 00:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, 32, 0, 0, 0, utc); | ||||
|   EXPECT_EQ("2015-02-01 00:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 13, 1, 0, 0, 0, utc); | ||||
|   EXPECT_EQ("2016-01-01 00:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 13, 32, 60, 60, 60, utc); | ||||
|   EXPECT_EQ("2016-02-03 13:01:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, 1, 0, 0, -1, utc); | ||||
|   EXPECT_EQ("2014-12-31 23:59:59", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, 1, 0, -1, 0, utc); | ||||
|   EXPECT_EQ("2014-12-31 23:59:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, 1, -1, 0, 0, utc); | ||||
|   EXPECT_EQ("2014-12-31 23:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, -1, 0, 0, 0, utc); | ||||
|   EXPECT_EQ("2014-12-30 00:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, -1, 1, 0, 0, 0, utc); | ||||
|   EXPECT_EQ("2014-11-01 00:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, -1, -1, -1, -1, -1, utc); | ||||
|   EXPECT_EQ("2014-10-29 22:58:59", absl::FormatTime(ymdhms, t, utc)); | ||||
| } | ||||
| 
 | ||||
| TEST(Time, ToTM) { | ||||
|  | @ -684,8 +672,10 @@ TEST(Time, ToTM) { | |||
| 
 | ||||
|   // Compares the results of ToTM() to gmtime_r() for lots of times over the
 | ||||
|   // course of a few days.
 | ||||
|   const absl::Time start = absl::FromDateTime(2014, 1, 2, 3, 4, 5, utc); | ||||
|   const absl::Time end = absl::FromDateTime(2014, 1, 5, 3, 4, 5, utc); | ||||
|   const absl::Time start = | ||||
|       absl::FromCivil(absl::CivilSecond(2014, 1, 2, 3, 4, 5), utc); | ||||
|   const absl::Time end = | ||||
|       absl::FromCivil(absl::CivilSecond(2014, 1, 5, 3, 4, 5), utc); | ||||
|   for (absl::Time t = start; t < end; t += absl::Seconds(30)) { | ||||
|     const struct tm tm_bt = ToTM(t, utc); | ||||
|     const time_t tt = absl::ToTimeT(t); | ||||
|  | @ -711,12 +701,12 @@ TEST(Time, ToTM) { | |||
|   // Checks that the tm_isdst field is correct when in standard time.
 | ||||
|   const absl::TimeZone nyc = | ||||
|       absl::time_internal::LoadTimeZone("America/New_York"); | ||||
|   absl::Time t = absl::FromDateTime(2014, 3, 1, 0, 0, 0, nyc); | ||||
|   absl::Time t = absl::FromCivil(absl::CivilSecond(2014, 3, 1, 0, 0, 0), nyc); | ||||
|   struct tm tm = ToTM(t, nyc); | ||||
|   EXPECT_FALSE(tm.tm_isdst); | ||||
| 
 | ||||
|   // Checks that the tm_isdst field is correct when in daylight time.
 | ||||
|   t = absl::FromDateTime(2014, 4, 1, 0, 0, 0, nyc); | ||||
|   t = absl::FromCivil(absl::CivilSecond(2014, 4, 1, 0, 0, 0), nyc); | ||||
|   tm = ToTM(t, nyc); | ||||
|   EXPECT_TRUE(tm.tm_isdst); | ||||
| 
 | ||||
|  | @ -808,8 +798,8 @@ TEST(Time, TMRoundTrip) { | |||
|       absl::time_internal::LoadTimeZone("America/New_York"); | ||||
| 
 | ||||
|   // Test round-tripping across a skipped transition
 | ||||
|   absl::Time start = absl::FromDateTime(2014, 3, 9, 0, 0, 0, nyc); | ||||
|   absl::Time end = absl::FromDateTime(2014, 3, 9, 4, 0, 0, nyc); | ||||
|   absl::Time start = absl::FromCivil(absl::CivilHour(2014, 3, 9, 0), nyc); | ||||
|   absl::Time end = absl::FromCivil(absl::CivilHour(2014, 3, 9, 4), nyc); | ||||
|   for (absl::Time t = start; t < end; t += absl::Minutes(1)) { | ||||
|     struct tm tm = ToTM(t, nyc); | ||||
|     absl::Time rt = FromTM(tm, nyc); | ||||
|  | @ -817,8 +807,8 @@ TEST(Time, TMRoundTrip) { | |||
|   } | ||||
| 
 | ||||
|   // Test round-tripping across an ambiguous transition
 | ||||
|   start = absl::FromDateTime(2014, 11, 2, 0, 0, 0, nyc); | ||||
|   end = absl::FromDateTime(2014, 11, 2, 4, 0, 0, nyc); | ||||
|   start = absl::FromCivil(absl::CivilHour(2014, 11, 2, 0), nyc); | ||||
|   end = absl::FromCivil(absl::CivilHour(2014, 11, 2, 4), nyc); | ||||
|   for (absl::Time t = start; t < end; t += absl::Minutes(1)) { | ||||
|     struct tm tm = ToTM(t, nyc); | ||||
|     absl::Time rt = FromTM(tm, nyc); | ||||
|  | @ -826,8 +816,8 @@ TEST(Time, TMRoundTrip) { | |||
|   } | ||||
| 
 | ||||
|   // Test round-tripping of unique instants crossing a day boundary
 | ||||
|   start = absl::FromDateTime(2014, 6, 27, 22, 0, 0, nyc); | ||||
|   end = absl::FromDateTime(2014, 6, 28, 4, 0, 0, nyc); | ||||
|   start = absl::FromCivil(absl::CivilHour(2014, 6, 27, 22), nyc); | ||||
|   end = absl::FromCivil(absl::CivilHour(2014, 6, 28, 4), nyc); | ||||
|   for (absl::Time t = start; t < end; t += absl::Minutes(1)) { | ||||
|     struct tm tm = ToTM(t, nyc); | ||||
|     absl::Time rt = FromTM(tm, nyc); | ||||
|  | @ -980,27 +970,27 @@ TEST(Time, ConversionSaturation) { | |||
|   EXPECT_EQ(min_timespec_sec, ts.tv_sec); | ||||
|   EXPECT_EQ(0, ts.tv_nsec); | ||||
| 
 | ||||
|   // Checks how Time::In() saturates on infinities.
 | ||||
|   absl::Time::Breakdown bd = absl::InfiniteFuture().In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, std::numeric_limits<int64_t>::max(), 12, 31, 23, | ||||
|   // Checks how TimeZone::At() saturates on infinities.
 | ||||
|   auto ci = utc.At(absl::InfiniteFuture()); | ||||
|   EXPECT_CIVIL_INFO(ci, std::numeric_limits<int64_t>::max(), 12, 31, 23, | ||||
|                             59, 59, 0, false); | ||||
|   EXPECT_EQ(absl::InfiniteDuration(), bd.subsecond); | ||||
|   EXPECT_EQ(4, bd.weekday);  // Thursday
 | ||||
|   EXPECT_EQ(365, bd.yearday); | ||||
|   EXPECT_STREQ("-00", bd.zone_abbr);  // artifact of absl::Time::In()
 | ||||
|   bd = absl::InfinitePast().In(utc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, std::numeric_limits<int64_t>::min(), 1, 1, 0, 0, | ||||
|   EXPECT_EQ(absl::InfiniteDuration(), ci.subsecond); | ||||
|   EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(absl::CivilDay(ci.cs))); | ||||
|   EXPECT_EQ(365, absl::GetYearDay(absl::CivilDay(ci.cs))); | ||||
|   EXPECT_STREQ("-00", ci.zone_abbr);  // artifact of TimeZone::At()
 | ||||
|   ci = utc.At(absl::InfinitePast()); | ||||
|   EXPECT_CIVIL_INFO(ci, std::numeric_limits<int64_t>::min(), 1, 1, 0, 0, | ||||
|                             0, 0, false); | ||||
|   EXPECT_EQ(-absl::InfiniteDuration(), bd.subsecond); | ||||
|   EXPECT_EQ(7, bd.weekday);  // Sunday
 | ||||
|   EXPECT_EQ(1, bd.yearday); | ||||
|   EXPECT_STREQ("-00", bd.zone_abbr);  // artifact of absl::Time::In()
 | ||||
|   EXPECT_EQ(-absl::InfiniteDuration(), ci.subsecond); | ||||
|   EXPECT_EQ(absl::Weekday::sunday, absl::GetWeekday(absl::CivilDay(ci.cs))); | ||||
|   EXPECT_EQ(1, absl::GetYearDay(absl::CivilDay(ci.cs))); | ||||
|   EXPECT_STREQ("-00", ci.zone_abbr);  // artifact of TimeZone::At()
 | ||||
| 
 | ||||
|   // Approach the maximal Time value from below.
 | ||||
|   t = absl::FromDateTime(292277026596, 12, 4, 15, 30, 6, utc); | ||||
|   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 6), utc); | ||||
|   EXPECT_EQ("292277026596-12-04T15:30:06+00:00", | ||||
|             absl::FormatTime(absl::RFC3339_full, t, utc)); | ||||
|   t = absl::FromDateTime(292277026596, 12, 4, 15, 30, 7, utc); | ||||
|   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 7), utc); | ||||
|   EXPECT_EQ("292277026596-12-04T15:30:07+00:00", | ||||
|             absl::FormatTime(absl::RFC3339_full, t, utc)); | ||||
|   EXPECT_EQ( | ||||
|  | @ -1008,21 +998,21 @@ TEST(Time, ConversionSaturation) { | |||
| 
 | ||||
|   // Checks that we can also get the maximal Time value for a far-east zone.
 | ||||
|   const absl::TimeZone plus14 = absl::FixedTimeZone(14 * 60 * 60); | ||||
|   t = absl::FromDateTime(292277026596, 12, 5, 5, 30, 7, plus14); | ||||
|   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 5, 30, 7), plus14); | ||||
|   EXPECT_EQ("292277026596-12-05T05:30:07+14:00", | ||||
|             absl::FormatTime(absl::RFC3339_full, t, plus14)); | ||||
|   EXPECT_EQ( | ||||
|       absl::UnixEpoch() + absl::Seconds(std::numeric_limits<int64_t>::max()), t); | ||||
| 
 | ||||
|   // One second later should push us to infinity.
 | ||||
|   t = absl::FromDateTime(292277026596, 12, 4, 15, 30, 8, utc); | ||||
|   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 8), utc); | ||||
|   EXPECT_EQ("infinite-future", absl::FormatTime(absl::RFC3339_full, t, utc)); | ||||
| 
 | ||||
|   // Approach the minimal Time value from above.
 | ||||
|   t = absl::FromDateTime(-292277022657, 1, 27, 8, 29, 53, utc); | ||||
|   t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 53), utc); | ||||
|   EXPECT_EQ("-292277022657-01-27T08:29:53+00:00", | ||||
|             absl::FormatTime(absl::RFC3339_full, t, utc)); | ||||
|   t = absl::FromDateTime(-292277022657, 1, 27, 8, 29, 52, utc); | ||||
|   t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 52), utc); | ||||
|   EXPECT_EQ("-292277022657-01-27T08:29:52+00:00", | ||||
|             absl::FormatTime(absl::RFC3339_full, t, utc)); | ||||
|   EXPECT_EQ( | ||||
|  | @ -1030,14 +1020,15 @@ TEST(Time, ConversionSaturation) { | |||
| 
 | ||||
|   // Checks that we can also get the minimal Time value for a far-west zone.
 | ||||
|   const absl::TimeZone minus12 = absl::FixedTimeZone(-12 * 60 * 60); | ||||
|   t = absl::FromDateTime(-292277022657, 1, 26, 20, 29, 52, minus12); | ||||
|   t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 26, 20, 29, 52), | ||||
|                       minus12); | ||||
|   EXPECT_EQ("-292277022657-01-26T20:29:52-12:00", | ||||
|             absl::FormatTime(absl::RFC3339_full, t, minus12)); | ||||
|   EXPECT_EQ( | ||||
|       absl::UnixEpoch() + absl::Seconds(std::numeric_limits<int64_t>::min()), t); | ||||
| 
 | ||||
|   // One second before should push us to -infinity.
 | ||||
|   t = absl::FromDateTime(-292277022657, 1, 27, 8, 29, 51, utc); | ||||
|   t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 51), utc); | ||||
|   EXPECT_EQ("infinite-past", absl::FormatTime(absl::RFC3339_full, t, utc)); | ||||
| } | ||||
| 
 | ||||
|  | @ -1051,38 +1042,97 @@ TEST(Time, ExtendedConversionSaturation) { | |||
|       absl::time_internal::LoadTimeZone("America/New_York"); | ||||
|   const absl::Time max = | ||||
|       absl::FromUnixSeconds(std::numeric_limits<int64_t>::max()); | ||||
|   absl::Time::Breakdown bd; | ||||
|   absl::TimeZone::CivilInfo ci; | ||||
|   absl::Time t; | ||||
| 
 | ||||
|   // The maximal time converted in each zone.
 | ||||
|   bd = max.In(syd); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 292277026596, 12, 5, 2, 30, 7, 39600, true); | ||||
|   t = absl::FromDateTime(292277026596, 12, 5, 2, 30, 7, syd); | ||||
|   ci = syd.At(max); | ||||
|   EXPECT_CIVIL_INFO(ci, 292277026596, 12, 5, 2, 30, 7, 39600, true); | ||||
|   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 7), syd); | ||||
|   EXPECT_EQ(max, t); | ||||
|   bd = max.In(nyc); | ||||
|   ABSL_INTERNAL_EXPECT_TIME(bd, 292277026596, 12, 4, 10, 30, 7, -18000, false); | ||||
|   t = absl::FromDateTime(292277026596, 12, 4, 10, 30, 7, nyc); | ||||
|   ci = nyc.At(max); | ||||
|   EXPECT_CIVIL_INFO(ci, 292277026596, 12, 4, 10, 30, 7, -18000, false); | ||||
|   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 7), nyc); | ||||
|   EXPECT_EQ(max, t); | ||||
| 
 | ||||
|   // One second later should push us to infinity.
 | ||||
|   t = absl::FromDateTime(292277026596, 12, 5, 2, 30, 8, syd); | ||||
|   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 8), syd); | ||||
|   EXPECT_EQ(absl::InfiniteFuture(), t); | ||||
|   t = absl::FromDateTime(292277026596, 12, 4, 10, 30, 8, nyc); | ||||
|   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 8), nyc); | ||||
|   EXPECT_EQ(absl::InfiniteFuture(), t); | ||||
| 
 | ||||
|   // And we should stick there.
 | ||||
|   t = absl::FromDateTime(292277026596, 12, 5, 2, 30, 9, syd); | ||||
|   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 9), syd); | ||||
|   EXPECT_EQ(absl::InfiniteFuture(), t); | ||||
|   t = absl::FromDateTime(292277026596, 12, 4, 10, 30, 9, nyc); | ||||
|   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 9), nyc); | ||||
|   EXPECT_EQ(absl::InfiniteFuture(), t); | ||||
| 
 | ||||
|   // All the way up to a saturated date/time, without overflow.
 | ||||
|   t = absl::FromDateTime( | ||||
|       std::numeric_limits<int64_t>::max(), 12, 31, 23, 59, 59, syd); | ||||
|   t = absl::FromCivil(absl::CivilSecond::max(), syd); | ||||
|   EXPECT_EQ(absl::InfiniteFuture(), t); | ||||
|   t = absl::FromDateTime( | ||||
|       std::numeric_limits<int64_t>::max(), 12, 31, 23, 59, 59, nyc); | ||||
|   t = absl::FromCivil(absl::CivilSecond::max(), nyc); | ||||
|   EXPECT_EQ(absl::InfiniteFuture(), t); | ||||
| } | ||||
| 
 | ||||
| TEST(Time, FromCivilAlignment) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
|   const absl::CivilSecond cs(2015, 2, 3, 4, 5, 6); | ||||
|   absl::Time t = absl::FromCivil(cs, utc); | ||||
|   EXPECT_EQ("2015-02-03T04:05:06+00:00", absl::FormatTime(t, utc)); | ||||
|   t = absl::FromCivil(absl::CivilMinute(cs), utc); | ||||
|   EXPECT_EQ("2015-02-03T04:05:00+00:00", absl::FormatTime(t, utc)); | ||||
|   t = absl::FromCivil(absl::CivilHour(cs), utc); | ||||
|   EXPECT_EQ("2015-02-03T04:00:00+00:00", absl::FormatTime(t, utc)); | ||||
|   t = absl::FromCivil(absl::CivilDay(cs), utc); | ||||
|   EXPECT_EQ("2015-02-03T00:00:00+00:00", absl::FormatTime(t, utc)); | ||||
|   t = absl::FromCivil(absl::CivilMonth(cs), utc); | ||||
|   EXPECT_EQ("2015-02-01T00:00:00+00:00", absl::FormatTime(t, utc)); | ||||
|   t = absl::FromCivil(absl::CivilYear(cs), utc); | ||||
|   EXPECT_EQ("2015-01-01T00:00:00+00:00", absl::FormatTime(t, utc)); | ||||
| } | ||||
| 
 | ||||
| TEST(Time, LegacyDateTime) { | ||||
|   const absl::TimeZone utc = absl::UTCTimeZone(); | ||||
|   const std::string ymdhms = "%Y-%m-%d %H:%M:%S"; | ||||
|   const int kMax = std::numeric_limits<int>::max(); | ||||
|   const int kMin = std::numeric_limits<int>::min(); | ||||
|   absl::Time t; | ||||
| 
 | ||||
|   t = absl::FromDateTime(std::numeric_limits<absl::civil_year_t>::max(), | ||||
|                          kMax, kMax, kMax, kMax, kMax, utc); | ||||
|   EXPECT_EQ("infinite-future", | ||||
|             absl::FormatTime(ymdhms, t, utc));  // no overflow
 | ||||
|   t = absl::FromDateTime(std::numeric_limits<absl::civil_year_t>::min(), | ||||
|                          kMin, kMin, kMin, kMin, kMin, utc); | ||||
|   EXPECT_EQ("infinite-past", | ||||
|             absl::FormatTime(ymdhms, t, utc));  // no overflow
 | ||||
| 
 | ||||
|   // Check normalization.
 | ||||
|   EXPECT_TRUE(absl::ConvertDateTime(2013, 10, 32, 8, 30, 0, utc).normalized); | ||||
|   t = absl::FromDateTime(2015, 1, 1, 0, 0, 60, utc); | ||||
|   EXPECT_EQ("2015-01-01 00:01:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, 1, 0, 60, 0, utc); | ||||
|   EXPECT_EQ("2015-01-01 01:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, 1, 24, 0, 0, utc); | ||||
|   EXPECT_EQ("2015-01-02 00:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, 32, 0, 0, 0, utc); | ||||
|   EXPECT_EQ("2015-02-01 00:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 13, 1, 0, 0, 0, utc); | ||||
|   EXPECT_EQ("2016-01-01 00:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 13, 32, 60, 60, 60, utc); | ||||
|   EXPECT_EQ("2016-02-03 13:01:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, 1, 0, 0, -1, utc); | ||||
|   EXPECT_EQ("2014-12-31 23:59:59", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, 1, 0, -1, 0, utc); | ||||
|   EXPECT_EQ("2014-12-31 23:59:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, 1, -1, 0, 0, utc); | ||||
|   EXPECT_EQ("2014-12-31 23:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, 1, -1, 0, 0, 0, utc); | ||||
|   EXPECT_EQ("2014-12-30 00:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, -1, 1, 0, 0, 0, utc); | ||||
|   EXPECT_EQ("2014-11-01 00:00:00", absl::FormatTime(ymdhms, t, utc)); | ||||
|   t = absl::FromDateTime(2015, -1, -1, -1, -1, -1, utc); | ||||
|   EXPECT_EQ("2014-10-29 22:58:59", absl::FormatTime(ymdhms, t, utc)); | ||||
| } | ||||
| 
 | ||||
| }  // namespace
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue