Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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