Home > Back-end >  What might be causing timestamps to move back a day when they get put into Firestore using Python?
What might be causing timestamps to move back a day when they get put into Firestore using Python?

Time:01-03

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

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.

CodePudding user response:

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.

CodePudding user response:

This is a typical syndrome when one of the timezone settings somewhere is set wrong.

  • Related