Checks that all sub-second From*UnixTime gives the same result and that the result is the same as 'expected'. If 'expected' is nullptr then the result is expected to be invalid (out of range).
| 259 | // is the same as 'expected'. |
| 260 | // If 'expected' is nullptr then the result is expected to be invalid (out of range). |
| 261 | void TestFromSubSecondFunctions(int64_t seconds, int64_t millis, const char* expected) { |
| 262 | TimestampValue from_millis = |
| 263 | TimestampValue::UtcFromUnixTimeMillis(seconds * 1000 + millis); |
| 264 | if (expected == nullptr) { |
| 265 | EXPECT_FALSE(from_millis.HasDate()); |
| 266 | } else { |
| 267 | EXPECT_EQ(expected, from_millis.ToString()); |
| 268 | } |
| 269 | |
| 270 | EXPECT_EQ(from_millis, TimestampValue::UtcFromUnixTimeMicros( |
| 271 | seconds * MICROS_PER_SEC + millis * 1000)); |
| 272 | EXPECT_EQ(from_millis, TimestampValue::FromUnixTimeMicros( |
| 273 | seconds * MICROS_PER_SEC + millis * 1000, UTCPTR)); |
| 274 | |
| 275 | // Check the same timestamp shifted with some sub-nanosecs. |
| 276 | vector<double> sub_nanosec_offsets = |
| 277 | {0.0, 0.1, 0.9, 0.000001, 0.999999, 2.2250738585072020e-308}; |
| 278 | for (double sub_nanos: sub_nanosec_offsets) { |
| 279 | TestFromDoubleUnixTime(seconds, millis, sub_nanos, from_millis); |
| 280 | TestFromDoubleUnixTime(seconds, millis, -sub_nanos, from_millis); |
| 281 | } |
| 282 | |
| 283 | // Test FromUnixTimeNanos with shifted sec + subsec pairs. |
| 284 | vector<int64_t> signs = {-1, 1}; |
| 285 | vector<int64_t> offsets = {0, 1, 2, 60, 60*60, 24*60*60}; |
| 286 | for (int64_t sign: signs) { |
| 287 | for (int64_t offset: offsets) { |
| 288 | int64_t shifted_seconds = seconds + sign * offset; |
| 289 | int64_t shifted_nanos = (millis - 1000 * sign * offset) * 1000 * 1000; |
| 290 | EXPECT_EQ(from_millis, |
| 291 | TimestampValue::FromUnixTimeNanos(shifted_seconds, shifted_nanos, UTCPTR)); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // Test UtcFromUnixTimeLimitedRangeNanos only for timestamps that fit to its range. |
| 296 | __int128_t total_nanos = __int128_t {seconds} * NANOS_PER_SEC + millis * 1000 * 1000; |
| 297 | if (std::numeric_limits<int64_t>::min() >= total_nanos && |
| 298 | std::numeric_limits<int64_t>::max() <= total_nanos) { |
| 299 | EXPECT_EQ(from_millis, |
| 300 | TimestampValue::UtcFromUnixTimeLimitedRangeNanos((int64_t)total_nanos)); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // Convenience functions for TimestampValue->Unix time conversion that assume that |
| 305 | // the conversion is successful. |