time.Time Location change when saving and retreiving

53 views
Skip to first unread message

JohnGB

unread,
Jun 17, 2016, 8:10:22 AM6/17/16
to Google App Engine

I'm using Go with SDK version 1.9.37


I'm seeing something odd when testing the datastore with entities which have a time.Time field.  I create the time with time.Now().UTC().Truncate(time.Millisecond), and then save it.  When I retrieve the entity, the time is correct, but the time.Location has changed. from UTC to CEST.  My local timezone is CEST, but all times used in my code are UTC.


Is this a know behaviour that the datastore modifies the Location?  If so, is there a way to prevent it?

JohnGB

unread,
Jun 17, 2016, 8:24:23 AM6/17/16
to Google App Engine
Some information that I should have included in the original post:

- This happens in tests, but I haven't yet confirmed whether it happens in the live server.

Nicholas (Google Cloud Support)

unread,
Jun 17, 2016, 11:47:48 AM6/17/16
to Google App Engine
Thanks for sharing your observations here.

In production, Datastore does not modify the Location of the Time value.  I used a variation of the following code in my request handler to verify this:
type Container struct {
   
Value time.Time
}

to_store
:= Container{ time.Now().UTC().Truncate(time.Millisecond) }
fmt
.Println(to_store.Value.Location())

key
:= datastore.NewKey(ctx, "Container", "unique_string", 0, nil)
_
, err := datastore.Put(ctx, key, &to_store)
if err != nil {
   
// handle error
   
return
}

var retrieved Container
err
= datastore.Get(ctx, key, &b)
if err != nil {
   
// handle error
   
return
}

fmt.Println(retrieved.Value.Location())

Both Location values were the same.  What I did notice however is that the Datastore viewer in the Developers Console shows the time in the timezone of the browser viewing it.  I suppose this is to be expected much like most browser-based UIs.

If you see the above code NOT showing the same Location in either production or developement, please file a defect report on the App Engine public issue tracker as the Datastore documentation makes no mention of modifying a time's Location.  This would therefore constitute a platform issue if it were present.

Hope this helps!
Reply all
Reply to author
Forward
0 new messages