Skip to content
Permalink
Browse files
fix(datastore): Ensure the datastore time is returned as UTC (#3521)
  • Loading branch information
crwilcox committed Jan 13, 2021
1 parent 84d4d8a commit 0e659e28da503b9520c83eb136df6e54d6c6daf7
Showing with 34 additions and 2 deletions.
  1. +2 −2 datastore/load.go
  2. +32 −0 datastore/load_test.go
@@ -369,7 +369,7 @@ func setVal(v reflect.Value, p Property) (s string) {
if ok {
s := micros / 1e6
ns := micros % 1e6
v.Set(reflect.ValueOf(time.Unix(s, ns)))
v.Set(reflect.ValueOf(time.Unix(s, ns).In(time.UTC)))
break
}
x, ok := pValue.(time.Time)
@@ -536,7 +536,7 @@ func propToValue(v *pb.Value) (interface{}, error) {
case *pb.Value_DoubleValue:
return v.DoubleValue, nil
case *pb.Value_TimestampValue:
return time.Unix(v.TimestampValue.Seconds, int64(v.TimestampValue.Nanos)), nil
return time.Unix(v.TimestampValue.Seconds, int64(v.TimestampValue.Nanos)).In(time.UTC), nil
case *pb.Value_KeyValue:
return protoToKey(v.KeyValue)
case *pb.Value_StringValue:
@@ -569,6 +569,38 @@ func TestLoadToInterface(t *testing.T) {
}
}

// Expect Local times to be represented in UTC
func TestTimezone(t *testing.T) {
src := &pb.Entity{
Key: keyToProto(testKey0),
Properties: map[string]*pb.Value{
"Time": {ValueType: &pb.Value_TimestampValue{TimestampValue: &timestamppb.Timestamp{Seconds: 1605504600}}},
},
}

dst := &struct{ Time time.Time }{
Time: time.Time{},
}
want := &struct{ Time time.Time }{
Time: time.Unix(1605504600, 0).In(time.UTC),
}

err := loadEntityProto(dst, src)
if err != nil {
t.Fatalf("loadEntityProto: %v", err)
}

if diff := testutil.Diff(dst, want); diff != "" {
t.Fatalf("Mismatch: got - want +\n%s", diff)
}
// Also, the Zones need to be compared as comparing times will not detect this difference.
dstZone, _ := dst.Time.Zone()
wantZone, _ := want.Time.Zone()
if diff := testutil.Diff(dstZone, wantZone); diff != "" {
t.Fatalf("Mismatch: got - want +\n%s", diff)
}
}

func TestAlreadyPopulatedDst(t *testing.T) {
testCases := []struct {
desc string

0 comments on commit 0e659e2

Please sign in to comment.