GeezerVison is showing github.com — Saving datetime objects to Datastore - no documentation? · Issue #1200 · googleapis/google-cloud-python
Saving datetime objects to Datastore - no documentation? #1200Labels api: datastoreIssues related to the Datastore API.priority: p2Moderately-important priority. Fix may not be included in next release. DescriptionCan the documentation be updated to show the right way to save timestamps to datastore? It took some trial and error to find the right way to do it.
Or is this just something obvious that I missed?
Example:
import time
import datetime
client = datastore.Client()
entity = datastore.Entity( key = client.key("Foo") )
entity["time"] = time.gmtime()
client.put( entity )
#does not work
entity = datastore.Entity( key = client.key("Foo") )
entity["time"] = datetime.datetime.now().date()
client.put( entity )
#does not work
entity = datastore.Entity( key = client.key("Foo") )
entity["time"] = datetime.datetime.now()
client.put( entity )
#worksReacted by soeirodefaria and Robert Navarro Activitytseaver commented on Oct 21, 2015 Thanks for reporting this issue! What you've found is that we only accept actual datetime.datetime instances. The first example fails because time.gmtime() returns a time.struct_time instance; the second example returns a datetime.date.
The entity docs should describe the types of fields we support, and the constraints on them. igama commented on Oct 21, 2015 I'm also looking in to this. For example,
client = datastore.Client(dataset_id=projectID)
record_key = client.key('Record', record_name)
record_entity = datastore.Entity(key=record_key)
record_entity['last_updated'] = datetime.datetime.fromtimestamp(
unix_timestamp).strftime('%Y-%m-%d %H:%M:%S')
client.put(record_entity)
Via http://console.developers.google.com the property appears as string. thesandlord commented on Oct 21, 2015 ContributorAuthor More actions @tseaver yup it would be awesome if the docs / examples explained what types were accepted (string, bytes, int, float, datetime, etc).
@igama The issue is you are converting to a string with the strftime method call. If you remove that part it should save as a timestamp. igama commented on Oct 21, 2015 @thesandlord thanks, think I misread the documentation somewhere. patitonav commented on May 17, 2017 I found another related issue:
When i try:
entity = datastore.Entity( key = client.key("Foo") )
entity["time"] = datetime.datetime.now()
client.put( entity )
Works fine, but affter when i look in the datastore dashboard i see 3 hours difference.
Any idea? dhermes commented on May 17, 2017 @patitonav You should use datetime.datetime.utcnow()
The underlying value is stored as a Timestamp, which
Represents seconds of UTC time since Unix epoch
When our code receives a "naive" timestamp (i.e. one without a timezone) it assumes the timestamp is UTC (via). patitonav commented on May 17, 2017 Yes. You were right thanks lukesneeringer commented on Aug 11, 2017
MetadataMetadataapi: datastoreIssues related to the Datastore API.priority: p2Moderately-important priority. Fix may not be included in next release. DevelopmentNo branches or pull requestsIssue actions
|