Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit 2d00165

Browse files
authored
feat!: remove Timestamp::Min() and Timestamp::Max() (#1182)
There is a question as to whether `Timestamp::Min()` and `Timestamp::Max()` should reflect the actual range of `class Timestamp` or the documented range of a Spanner TIMESTAMP field. So, until there is a demonstrated need one way or the other, let's simply remove them.
1 parent fd06727 commit 2d00165

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed
-50 Bytes
Binary file not shown.

google/cloud/spanner/timestamp.cc

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,12 @@ Timestamp Timestamp::FromProto(protobuf::Timestamp const& proto) {
264264
auto ts = FromCounts(proto.seconds(), proto.nanos());
265265
if (!ts) {
266266
// If the proto cannot be normalized (proto.nanos() would need to be
267-
// outside its documented [0..999999999] range), then we saturate.
268-
return proto.seconds() >= 0 ? Max() : Min();
267+
// outside its documented [0..999999999] range and have the same sign
268+
// as proto.seconds()), then we saturate.
269+
return proto.seconds() >= 0
270+
? Timestamp{std::numeric_limits<std::int64_t>::max(),
271+
kNanosPerSecond - 1}
272+
: Timestamp{std::numeric_limits<std::int64_t>::min(), 0};
269273
}
270274
return *ts;
271275
}
@@ -301,14 +305,6 @@ StatusOr<Timestamp> Timestamp::FromCounts(std::intmax_t sec,
301305
return Timestamp(static_cast<std::int64_t>(sec + carry), nanos);
302306
}
303307

304-
Timestamp Timestamp::Min() {
305-
return {std::numeric_limits<std::int64_t>::min(), 0};
306-
}
307-
308-
Timestamp Timestamp::Max() {
309-
return {std::numeric_limits<std::int64_t>::max(), kNanosPerSecond - 1};
310-
}
311-
312308
// (count * numerator/denominator) seconds => [sec, nsec]
313309
//
314310
// Only designed to handle the ratios of the std::chrono::duration helper

google/cloud/spanner/timestamp.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ class Timestamp {
8383
Timestamp& operator=(Timestamp const&) = default;
8484
///@}
8585

86-
/// @name Factory functions for special `Timestamp` values.
87-
///@{
88-
static Timestamp Min();
89-
static Timestamp Max();
90-
///@}
91-
9286
/// @name Relational operators
9387
///@{
9488
friend bool operator==(Timestamp const& a, Timestamp const& b) {

google/cloud/spanner/timestamp_test.cc

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,15 @@ TEST(Timestamp, FromProto) {
234234
EXPECT_EQ(internal::TimestampFromCounts(1576030524, 611422667).value(),
235235
internal::TimestampFromProto(proto));
236236

237-
proto.set_seconds(std::numeric_limits<std::int64_t>::min());
237+
proto.set_seconds(-62135596800);
238238
proto.set_nanos(0);
239-
EXPECT_EQ(Timestamp::Min(), internal::TimestampFromProto(proto));
239+
EXPECT_EQ(internal::TimestampFromCounts(-62135596800, 0).value(),
240+
internal::TimestampFromProto(proto));
240241

241-
proto.set_seconds(std::numeric_limits<std::int64_t>::max());
242+
proto.set_seconds(253402300799);
242243
proto.set_nanos(999999999);
243-
EXPECT_EQ(Timestamp::Max(), internal::TimestampFromProto(proto));
244+
EXPECT_EQ(internal::TimestampFromCounts(253402300799, 999999999).value(),
245+
internal::TimestampFromProto(proto));
244246
}
245247

246248
TEST(Timestamp, ToProto) {
@@ -254,12 +256,14 @@ TEST(Timestamp, ToProto) {
254256
EXPECT_EQ(1576030524, proto.seconds());
255257
EXPECT_EQ(611422667, proto.nanos());
256258

257-
proto = internal::TimestampToProto(Timestamp::Min());
258-
EXPECT_EQ(std::numeric_limits<std::int64_t>::min(), proto.seconds());
259+
proto = internal::TimestampToProto(
260+
internal::TimestampFromCounts(-62135596800, 0).value());
261+
EXPECT_EQ(-62135596800, proto.seconds());
259262
EXPECT_EQ(0, proto.nanos());
260263

261-
proto = internal::TimestampToProto(Timestamp::Max());
262-
EXPECT_EQ(std::numeric_limits<std::int64_t>::max(), proto.seconds());
264+
proto = internal::TimestampToProto(
265+
internal::TimestampFromCounts(253402300799, 999999999).value());
266+
EXPECT_EQ(253402300799, proto.seconds());
263267
EXPECT_EQ(999999999, proto.nanos());
264268
}
265269

@@ -354,8 +358,8 @@ TEST(Timestamp, ToChrono) {
354358
auto const tp5 = epoch + std::chrono::hours(2123456789 / 60 / 60);
355359
EXPECT_EQ(tp5, ts_pos.get<sys_time<std::chrono::hours>>().value());
356360

357-
auto const ts_big_pos = Timestamp::Max();
358-
auto const tp_big_pos = ts_big_pos.get<sys_time<std::chrono::milliseconds>>();
361+
auto const ts_big_pos = internal::TimestampFromCounts(20000000000, 0).value();
362+
auto const tp_big_pos = ts_big_pos.get<sys_time<std::chrono::nanoseconds>>();
359363
EXPECT_FALSE(tp_big_pos.ok());
360364
EXPECT_THAT(tp_big_pos.status().message(), HasSubstr("positive overflow"));
361365

@@ -380,8 +384,9 @@ TEST(Timestamp, ToChrono) {
380384
auto const tp10 = epoch - std::chrono::hours(2123456789 / 60 / 60);
381385
EXPECT_EQ(tp10, ts_neg.get<sys_time<std::chrono::hours>>().value());
382386

383-
auto const ts_big_neg = Timestamp::Min();
384-
auto const tp_big_neg = ts_big_neg.get<sys_time<std::chrono::milliseconds>>();
387+
auto const ts_big_neg =
388+
internal::TimestampFromCounts(-20000000000, 0).value();
389+
auto const tp_big_neg = ts_big_neg.get<sys_time<std::chrono::nanoseconds>>();
385390
EXPECT_FALSE(tp_big_neg.ok());
386391
EXPECT_THAT(tp_big_neg.status().message(), HasSubstr("negative overflow"));
387392
}

0 commit comments

Comments
 (0)