Search code examples
google-app-enginepython-2.7google-cloud-sql

Datetime in Google Cloud SQL [GAE + Python]


I am having some problems storing a datetime variable in my Google Cloud SQL database. Everytime I store it, it will save it in UTC format and I want it in CEST+0200 (Spain's time zone).

With this function in Python, I can get the time I want:

def spain_timezone():
    fecha_actual_utc = datetime.now(timezone('UTC'))

    # Convert to Spain local time
    now_spain = fecha_actual_utc.astimezone(timezone('Europe/Madrid'))
    logging.info('time is %s', now_spain.strftime(fmt))

    return now_spain

Is there a way to save it as Spain's time zone?

Thank you.


Solution

  • you can use the pytz library to do this, i found in this answer a example, but is very simple to use

    import pytz
    import datetime
    
    fecha_actual_Madrid = pytz.timezone('Europe/Madrid')    
    logging.info('time is %s', datetime.datetime.now(fecha_actual_Madrid ))