bigtable: timestamp to time conversion bug fix
Fixes #1784
Change-Id: I80bc22e231e2e08a63d5fb78d61d13f74ab08769
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/53210
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Cody Oss <codyoss@google.com>
Reviewed-by: Chris Cotter <cjcotter@google.com>
diff --git a/bigtable/bigtable.go b/bigtable/bigtable.go
index a66fc1d..7c67670 100644
--- a/bigtable/bigtable.go
+++ b/bigtable/bigtable.go
@@ -813,7 +813,7 @@
func Now() Timestamp { return Time(time.Now()) }
// Time converts a Timestamp into a time.Time.
-func (ts Timestamp) Time() time.Time { return time.Unix(0, int64(ts)*1e3) }
+func (ts Timestamp) Time() time.Time { return time.Unix(int64(ts)/1e6, int64(ts)%1e6*1e3) }
// TruncateToMilliseconds truncates a Timestamp to millisecond granularity,
// which is currently the only granularity supported.
diff --git a/bigtable/bttest/timestamp_test.go b/bigtable/bttest/timestamp_test.go
new file mode 100644
index 0000000..7f0d93e
--- /dev/null
+++ b/bigtable/bttest/timestamp_test.go
@@ -0,0 +1,52 @@
+// Copyright 2020 Google LLC
+//
+// 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.
+package bttest
+
+import (
+ "math"
+ "testing"
+ "time"
+
+ "cloud.google.com/go/bigtable"
+)
+
+func TestTimestampConversion(t *testing.T) {
+ // 1. Test a Timestamp converting to Time.
+ var ts1 bigtable.Timestamp = 1583863200000000
+ t1 := ts1.Time().UTC()
+ want1 := time.Date(2020, time.March, 10, 18, 0, 0, 0, time.UTC)
+
+ if !want1.Equal(t1) {
+ t.Errorf("Mismatched time got %v wanted %v", t1, want1)
+ }
+ // 2. Test a reversed Timestamp converting to Time.
+ reverse := math.MaxInt64 - ts1
+
+ got2 := reverse.Time().UTC()
+ want2 := time.Date(294196, time.October, 31, 10, 0, 54, 775807000, time.UTC)
+
+ if !want2.Equal(got2) {
+ t.Errorf("Mismatched time got %v wanted %v", got2, want2)
+ }
+
+ // 3. Test a Time converted to Timestamp then converted back to Time.
+ t2 := time.Date(2016, time.October, 3, 14, 7, 7, 0, time.UTC)
+ ts2 := bigtable.Timestamp(t2.UnixNano() / 1000)
+
+ got3 := ts2.Time().UTC()
+ want3 := time.Date(2016, time.October, 3, 14, 7, 7, 0, time.UTC)
+ if !want3.Equal(got3) {
+ t.Errorf("Mismatched time got %v wanted %v", got3, want3)
+ }
+}