Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What might be causing timestamps to move back a day when they get put into Firestore using Python?

I have a very strange bug going on. I have some events data with a date column. When I push the data into a Firestore database using Python code, it moves the dates back one day.

So my data looks like this in my Python notebook

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I double checked by exporting to CSV

enter image description here

But in my Firestore console, that date shows up as this:

enter image description here

These are the Python functions I’m using to upload to Firestore:

def batch_data(iterable, n=1):
    l = len(iterable)
    for ndx in range(0, l, n):
        yield iterable[ndx:min(ndx + n, l)]

def write_to_firestore(data):

    store = firestore.client()

    collection_name = "events"

    for batched_data in batch_data(data, 499):
        batch = store.batch()
        for data_item in batched_data.iterrows():
            doc_ref = store.collection(collection_name).document()
            batch.set(doc_ref, data_item[1].to_dict())
        batch.commit()
    return None

write_to_firestore(events_df)

I cannot, for the life of me, figure out why it might be bumping the date back up one day.

>Solution :

We’d need to see the exact data you’re passing to Firestore, but most likely this is due to a change in the timezone offset.

You’re likely writing the Timestamp fields (which includes a date and a time) in the local time for your computer, and Firestore then translates that to UTC-8/Pacific Time (as you can see in the screenshot). If you read the value, it’ll actually translate it back to your local timezone.

If you want the date to remain as you have it in your code, be sure to set it to UTC already. Just be prepared to treat it as UTC when reading it too, because the Firestore SDK will likely return it in the local timezone then too.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading