Search code examples
pythondatetimeflasktimezonegcloud

datetime.now() and google cloud


I have flask webform that runs on gcloud, and I have noticed that when it runs the command

datetime.datetime.now()

I have different time depending if I run it as local server on my pc or through gcloud Does anyone know why (I am guessing is to do with the current timezone of the actual server, and how to tell gcloud my local time as opposed to theirs?

Thanks


Solution

  • So yes it is to do with the server timezone. If you dont provide a timezone for datetime to work off of it will use your local timezone. You can provide a timezone to datetime for it return a time/date based off that timezone.

    from datetime import datetime
    import pytz
    
    # Datetime using local system timezone
    datetime.now()
    
    # Datetime using specific timezone
    datetime.now(pytz.timezone('US/Central'))
    

    You can find all the pytz timezones available here. https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568