Skip to content

Commit

Permalink
fix(datastore): loading civil types in non UTC location is incorrect (#…
Browse files Browse the repository at this point in the history
…3376)

* fix(datastore): loading civil types in non UTC location is incorrect

* process LoadLocation errors

Co-authored-by: Christopher Wilcox <crwilcox@google.com>
  • Loading branch information
Ilya Gurov and crwilcox committed Dec 4, 2020
1 parent 652fc75 commit 9ac287d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
6 changes: 3 additions & 3 deletions datastore/load.go
Expand Up @@ -384,13 +384,13 @@ func setVal(v reflect.Value, p Property) (s string) {
}
v.Set(reflect.ValueOf(x))
case typeOfCivilDate:
date := civil.DateOf(pValue.(time.Time))
date := civil.DateOf(pValue.(time.Time).In(time.UTC))
v.Set(reflect.ValueOf(date))
case typeOfCivilDateTime:
dateTime := civil.DateTimeOf(pValue.(time.Time))
dateTime := civil.DateTimeOf(pValue.(time.Time).In(time.UTC))
v.Set(reflect.ValueOf(dateTime))
case typeOfCivilTime:
timeVal := civil.TimeOf(pValue.(time.Time))
timeVal := civil.TimeOf(pValue.(time.Time).In(time.UTC))
v.Set(reflect.ValueOf(timeVal))
default:
ent, ok := pValue.(*Entity)
Expand Down
36 changes: 36 additions & 0 deletions datastore/load_test.go
Expand Up @@ -439,6 +439,42 @@ type withUntypedInterface struct {
Field interface{}
}

func TestLoadCivilTimeInNonUTCZone(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 civil.Time }{
Time: civil.Time{},
}
want := &struct{ Time civil.Time }{
Time: civil.Time{
Hour: 5,
Minute: 30,
},
}
loc, err := time.LoadLocation("Africa/Cairo")
if err != nil {
t.Fatalf("LoadLocation: %v", err)
}
time.Local = loc

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)
}
loc, err = time.LoadLocation("UTC")
if err != nil {
t.Fatalf("LoadLocation: %v", err)
}
time.Local = loc
}

func TestLoadToInterface(t *testing.T) {
testCases := []struct {
name string
Expand Down

0 comments on commit 9ac287d

Please sign in to comment.