Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with firestore.SERVER_TIMESTAMP in Python

I have stored "dates" in firestore using firestore.SERVER_TIMESTAMP

When I retrieve this value and print it, I get the following:

<object object at 0x7f5f4b30eaa0>

What is the right way to format this?

like image 677
Rimo Avatar asked Nov 04 '25 13:11

Rimo


1 Answers

I tried the following code, and I get the result back as a Python datetime object:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

cred = credentials.Certificate('./public/scripts/cert.json')
firebase_admin.initialize_app(cred)
client = firestore.client()
client.collection('test').document('foo').set({
    'mytime':firestore.SERVER_TIMESTAMP
})

data = client.collection('test').document('foo').get().to_dict()
print 'Full object:', data
print 'Timestamp:', data['mytime']

Output:

Full object: {u'mytime': datetime.datetime(2018, 7, 6, 19, 17, 55, 31000, tzinfo=<UTC>)}
Timestamp: 2018-07-06 19:17:55.031000+00:00

Tested on Python 2.7 with latest Admin SDK.

like image 60
Hiranya Jayathilaka Avatar answered Nov 07 '25 08:11

Hiranya Jayathilaka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!