0

For testing purposes, I have deployed the following Firebase Cloud Function. When the function gets called, it adds a document to my Firestore collection with two fields containing two datetimes.

@https_fn.on_call(region='europe-west1', vpc_connector='connector1', vpc_connector_egress_settings=options.VpcEgressSetting('ALL_TRAFFIC'))
def testing(req: https_fn.CallableRequest):
    firestore_client: google.cloud.firestore.Client = firestore.client()
    visit_collection = firestore_client.collection('visits')
    visit_collection.add(
        {
            'test1': datetime.now().astimezone(),
            'test2': datetime.now().replace(hour=0, minute=0, second=0, microsecond=0).astimezone(),
        },
        'test'
    )

When looking at the contents of the document in Cloud Firestore in the Firebase Console, what I would expect for the values of the fields in my document is (if it would be 19:50):

test1: 23 March 2024 at 19:50:00 UTC+1
test2: 23 March 2024 at 00:00:00 UTC+1

Instead, what I see in the document is:

test1: 23 March 2024 at 19:50:00 UTC+1
test2: 23 March 2024 at 01:00:00 UTC+1
4
  • 1
    Please edit the question to be clear about how you are observing the results. Are you looking in the Firestore console? Or some other code to read the document? We should be able to duplicate and observe in exactly the same way as you. Commented Mar 23, 2024 at 19:20
  • I don't think you should use astimezone at all. Firestore stores all timestamp objects in UTC, and trying to mess with that will likely just cause problems. The console always renders dates in the timezone configured on your system, despite the fact that they are stored in UTC. I don't know what your expectation is for the use of astimezone, but consider just going without it and doing any conversions when you know that a conversion needs to be displayed. Commented Mar 23, 2024 at 19:50
  • Or perhaps only send it a UTC-aware timezone, e.g. from datetime import UTC, and use .astimezone(UTC) Commented Mar 23, 2024 at 19:59
  • @DougStevenson thanks for taking the time to look at the problem. Removing the .astimezone() has exactly the same output. It's very strange what happens inside the Cloud Function. When I run the code inside the Cloud Function locally on my laptop, the result is as expected (00:00 UTC+1 instead of 01:00 UTC+1). Commented Mar 23, 2024 at 19:59

1 Answer 1

0

I avoided the problem using the pytz package:

import pytz
from datetime import datetime, time
from firebase_functions import https_fn, options
from firebase_admin import initialize_app, auth, firestore
import google.cloud.firestore

@https_fn.on_call(region='europe-west1', vpc_connector='connector1', vpc_connector_egress_settings=options.VpcEgressSetting('ALL_TRAFFIC'))
def testing(req: https_fn.CallableRequest):
    firestore_client: google.cloud.firestore.Client = firestore.client()
    visit_collection = firestore_client.collection('visits')
    brussels_tz = pytz.timezone('Europe/Brussels')
    current_datetime = datetime.now(brussels_tz)
    current_date = current_datetime.date()
    midnight_brussels = datetime.combine(current_date, time())
    midnight_brussels = brussels_tz.localize(midnight_brussels)
    visit_collection.add(
        {
            'test1': current_datetime,
            'test2': midnight_brussels,
        },
        'test'
    )
Sign up to request clarification or add additional context in comments.

1 Comment

pytz has been deprecated since Python 3.9. Consider using zoneinfo - example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.