GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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? #1200

New issue
Copy link
New issue
Copy link
Closed
Closed
Saving datetime objects to Datastore - no documentation?#1200
Copy link
Assignees
lukesneeringer
Labels
api: datastoreIssues related to the Datastore API.priority: p2Moderately-important priority. Fix may not be included in next release.

Description

@thesandlord
thesandlord
opened on Oct 21, 2015
Issue body actions

Can 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 )
#works
Reacted by soeirodefaria and Robert Navarro

Activity

  1. tseaver commented on Oct 21, 2015

    @tseaver
    tseaver
    on Oct 21, 2015
    Contributor
    More actions

    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.

  2. igama commented on Oct 21, 2015

    @igama
    igama
    on Oct 21, 2015
    More actions

    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.

  3. thesandlord commented on Oct 21, 2015

    @thesandlord
    thesandlord
    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.

  4. igama commented on Oct 21, 2015

    @igama
    igama
    on Oct 21, 2015
    More actions

    @thesandlord thanks, think I misread the documentation somewhere.

  5. dhermes
    added
    api: datastoreIssues related to the Datastore API.
    docs
    on Dec 31, 2015
  6. lukesneeringer
    added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Apr 19, 2017
  7. patitonav commented on May 17, 2017

    @patitonav
    patitonav
    on May 17, 2017
    More actions

    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?

  8. dhermes commented on May 17, 2017

    @dhermes
    dhermes
    on May 17, 2017
    Contributor
    More actions

    @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).

  9. patitonav commented on May 17, 2017

    @patitonav
    patitonav
    on May 17, 2017
    More actions

    Yes. You were right thanks

  10. lukesneeringer commented on Aug 11, 2017

    @lukesneeringer
    lukesneeringer
    on Aug 11, 2017
    Contributor
    More actions

    This seems resolved.

  11. lukesneeringer
    closed this as completedon Aug 11, 2017
  12. JustinBeckwith
    assigned
    lukesneeringer
    on Feb 1, 2021
  13. parthea
    added 2 commits that reference this issue on Nov 24, 2025

    fix: updates _get_transitive_schema_fields and tests (#1200)

    740bb63

    build(deps): bump certifi from 2022.9.24 to 2022.12.7 in /synthtool/g…

    5e67599
  14. parthea
    added a commit that references this issue on Mar 6, 2026

    chore(deps): update all dependencies (#1200)

    4c929f5
  15. parthea
    added a commit that references this issue on Sep 8, 2026

    docs: migrate code from googleapis/python-bigquery (#18303)

    23f9a74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @lukesneeringer
    lukesneeringer

Labels

api: datastoreIssues related to the Datastore API.priority: p2Moderately-important priority. Fix may not be included in next release.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.