Skip to content
Permalink
Browse files
fix(datastore): loading civil types in non UTC location is incorrect (#…
…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
IlyaFaer and crwilcox committed Dec 4, 2020
1 parent 652fc75 commit 9ac287d2abfb6bdcdceabb67fa0d93fb7b0dd863
Showing with 39 additions and 3 deletions.
  1. +3 −3 datastore/load.go
  2. +36 −0 datastore/load_test.go
@@ -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)
@@ -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

0 comments on commit 9ac287d

Please sign in to comment.