Skip to content

Commit

Permalink
fix(datastore): Ensure the datastore time is returned as UTC (#3521)
Browse files Browse the repository at this point in the history
  • Loading branch information
crwilcox committed Jan 13, 2021
1 parent 84d4d8a commit 0e659e2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions datastore/load.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
32 changes: 32 additions & 0 deletions datastore/load_test.go
Expand Up @@ -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
Expand Down

0 comments on commit 0e659e2

Please sign in to comment.