1// Copyright 2020 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14package bttest 15 16import ( 17 "math" 18 "testing" 19 "time" 20 21 "cloud.google.com/go/bigtable" 22) 23 24func TestTimestampConversion(t *testing.T) { 25 // 1. Test a Timestamp converting to Time. 26 var ts1 bigtable.Timestamp = 1583863200000000 27 t1 := ts1.Time().UTC() 28 want1 := time.Date(2020, time.March, 10, 18, 0, 0, 0, time.UTC) 29 30 if !want1.Equal(t1) { 31 t.Errorf("Mismatched time got %v wanted %v", t1, want1) 32 } 33 // 2. Test a reversed Timestamp converting to Time. 34 reverse := math.MaxInt64 - ts1 35 36 got2 := reverse.Time().UTC() 37 want2 := time.Date(294196, time.October, 31, 10, 0, 54, 775807000, time.UTC) 38 39 if !want2.Equal(got2) { 40 t.Errorf("Mismatched time got %v wanted %v", got2, want2) 41 } 42 43 // 3. Test a Time converted to Timestamp then converted back to Time. 44 t2 := time.Date(2016, time.October, 3, 14, 7, 7, 0, time.UTC) 45 ts2 := bigtable.Timestamp(t2.UnixNano() / 1000) 46 47 got3 := ts2.Time().UTC() 48 want3 := time.Date(2016, time.October, 3, 14, 7, 7, 0, time.UTC) 49 if !want3.Equal(got3) { 50 t.Errorf("Mismatched time got %v wanted %v", got3, want3) 51 } 52} 53