From 8841b5632b17ebbfb2eaf107819601ee76d5a035 Mon Sep 17 00:00:00 2001 From: Bradley White <14679271+devbww@users.noreply.github.com> Date: Wed, 8 Feb 2023 03:33:19 -0500 Subject: [PATCH 1/2] cleanup: avoid std::time_t as it is platform specific Part of #10775. --- examples/grpc_credential_types.cc | 6 +-- google/cloud/spanner/value_test.cc | 81 ++++++++++++++---------------- 2 files changed, 42 insertions(+), 45 deletions(-) diff --git a/examples/grpc_credential_types.cc b/examples/grpc_credential_types.cc index f336635494c..cfbb3e5a781 100644 --- a/examples/grpc_credential_types.cc +++ b/examples/grpc_credential_types.cc @@ -102,8 +102,8 @@ google::iam::credentials::v1::GenerateAccessTokenResponse UseAccessToken( /*scope=*/{"https://www.googleapis.com/auth/cloud-platform"}, duration); if (!token) throw std::move(token).status(); - auto const expiration = - std::chrono::system_clock::from_time_t(token->expire_time().seconds()); + auto const expiration = absl::ToChronoTime( + absl::FromUnixSeconds(token->expire_time().seconds())); std::cout << "Fetched token starting with " << token->access_token().substr(0, 8) << ", which will expire around " << absl::FromChrono(expiration) @@ -132,7 +132,7 @@ void UseAccessTokenUntilExpired(google::cloud::iam::IAMCredentialsClient client, auto token = UseAccessToken(std::move(client), argv); auto const& project_id = argv.at(1); auto const expiration = - std::chrono::system_clock::from_time_t(token.expire_time().seconds()); + absl::ToChronoTime(absl::FromUnixSeconds(token.expire_time().seconds())); auto const deadline = expiration + 4 * kTokenValidationPeriod; std::cout << "Running until " << absl::FromChrono(deadline) << ". This is past the access token expiration time (" diff --git a/google/cloud/spanner/value_test.cc b/google/cloud/spanner/value_test.cc index fbf368d73f1..950624ad5cb 100644 --- a/google/cloud/spanner/value_test.cc +++ b/google/cloud/spanner/value_test.cc @@ -39,8 +39,29 @@ using ::google::cloud::testing_util::StatusIs; using ::testing::HasSubstr; using ::testing::Not; -absl::Time MakeTime(std::time_t sec, int nanos) { - return absl::FromTimeT(sec) + absl::Nanoseconds(nanos); +absl::Time MakeTime(std::int64_t sec, int nanos) { + return absl::FromUnixSeconds(sec) + absl::Nanoseconds(nanos); +} + +std::vector TestTimes() { + std::vector times; + for (auto s : { + std::int64_t{-9223372035}, // near the limit of 64-bit/ns clock + std::int64_t{-2147483649}, // below min 32-bit value + std::int64_t{-2147483648}, // min 32-bit value + std::int64_t{-1}, // just before Unix epoch + std::int64_t{0}, // Unix epoch + std::int64_t{1}, // just after Unix epoch + std::int64_t{1561147549}, // contemporary + std::int64_t{2147483647}, // max 32-bit value + std::int64_t{2147483648}, // above max 32-bit value + std::int64_t{9223372036}, // near the limit of 64-bit/ns clock + }) { + for (auto nanos : {-1, 0, 1}) { + times.push_back(MakeTimestamp(MakeTime(s, nanos)).value()); + } + } + return times; } template @@ -177,27 +198,15 @@ TEST(Value, BasicSemantics) { TestBasicSemantics(v); } - for (time_t t : { - -9223372035LL, // near the limit of 64-bit/ns system_clock - -2147483649LL, // below min 32-bit int - -2147483648LL, // min 32-bit int - -1LL, 0LL, 1LL, // around the unix epoch - 1561147549LL, // contemporary - 2147483647LL, // max 32-bit int - 2147483648LL, // above max 32-bit int - 9223372036LL // near the limit of 64-bit/ns system_clock - }) { - for (auto nanos : {-1, 0, 1}) { - auto ts = MakeTimestamp(MakeTime(t, nanos)).value(); - SCOPED_TRACE("Testing: google::cloud::spanner::Timestamp " + - spanner_internal::TimestampToRFC3339(ts)); - TestBasicSemantics(ts); - std::vector v(5, ts); - TestBasicSemantics(v); - std::vector> ov(5, ts); - ov.resize(10); - TestBasicSemantics(ov); - } + for (auto ts : TestTimes()) { + SCOPED_TRACE("Testing: google::cloud::spanner::Timestamp " + + spanner_internal::TimestampToRFC3339(ts)); + TestBasicSemantics(ts); + std::vector v(5, ts); + TestBasicSemantics(v); + std::vector> ov(5, ts); + ov.resize(10); + TestBasicSemantics(ov); } for (auto x : { @@ -751,25 +760,13 @@ TEST(Value, ProtoConversionNumeric) { } TEST(Value, ProtoConversionTimestamp) { - for (time_t t : { - -9223372035LL, // near the limit of 64-bit/ns system_clock - -2147483649LL, // below min 32-bit int - -2147483648LL, // min 32-bit int - -1LL, 0LL, 1LL, // around the unix epoch - 1561147549LL, // contemporary - 2147483647LL, // max 32-bit int - 2147483648LL, // above max 32-bit int - 9223372036LL // near the limit of 64-bit/ns system_clock - }) { - for (auto nanos : {-1, 0, 1}) { - auto ts = MakeTimestamp(MakeTime(t, nanos)).value(); - Value const v(ts); - auto const p = spanner_internal::ToProto(v); - EXPECT_EQ(v, spanner_internal::FromProto(p.first, p.second)); - EXPECT_EQ(google::spanner::v1::TypeCode::TIMESTAMP, p.first.code()); - EXPECT_EQ(spanner_internal::TimestampToRFC3339(ts), - p.second.string_value()); - } + for (auto ts : TestTimes()) { + Value const v(ts); + auto const p = spanner_internal::ToProto(v); + EXPECT_EQ(v, spanner_internal::FromProto(p.first, p.second)); + EXPECT_EQ(google::spanner::v1::TypeCode::TIMESTAMP, p.first.code()); + EXPECT_EQ(spanner_internal::TimestampToRFC3339(ts), + p.second.string_value()); } } From 2e0a44867e80a14b48598dacff43d9cb9e379d84 Mon Sep 17 00:00:00 2001 From: Bradley White <14679271+devbww@users.noreply.github.com> Date: Wed, 8 Feb 2023 04:17:13 -0500 Subject: [PATCH 2/2] Attempt to fix Windows complaints about large integer literals --- google/cloud/spanner/value_test.cc | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/google/cloud/spanner/value_test.cc b/google/cloud/spanner/value_test.cc index 950624ad5cb..1af51e41c5f 100644 --- a/google/cloud/spanner/value_test.cc +++ b/google/cloud/spanner/value_test.cc @@ -46,16 +46,16 @@ absl::Time MakeTime(std::int64_t sec, int nanos) { std::vector TestTimes() { std::vector times; for (auto s : { - std::int64_t{-9223372035}, // near the limit of 64-bit/ns clock - std::int64_t{-2147483649}, // below min 32-bit value - std::int64_t{-2147483648}, // min 32-bit value - std::int64_t{-1}, // just before Unix epoch - std::int64_t{0}, // Unix epoch - std::int64_t{1}, // just after Unix epoch - std::int64_t{1561147549}, // contemporary - std::int64_t{2147483647}, // max 32-bit value - std::int64_t{2147483648}, // above max 32-bit value - std::int64_t{9223372036}, // near the limit of 64-bit/ns clock + std::int64_t{-9223372035LL}, // near the limit of 64-bit/ns clock + std::int64_t{-2147483649LL}, // below min 32-bit value + std::int64_t{-2147483648LL}, // min 32-bit value + std::int64_t{-1}, // just before Unix epoch + std::int64_t{0}, // Unix epoch + std::int64_t{1}, // just after Unix epoch + std::int64_t{1561147549LL}, // contemporary + std::int64_t{2147483647LL}, // max 32-bit value + std::int64_t{2147483648LL}, // above max 32-bit value + std::int64_t{9223372036LL}, // near the limit of 64-bit/ns clock }) { for (auto nanos : {-1, 0, 1}) { times.push_back(MakeTimestamp(MakeTime(s, nanos)).value());