【问题标题】:Datetime in Google Cloud SQL [GAE + Python]Google Cloud SQL [GAE + Python] 中的日期时间
【发布时间】:2014-08-12 21:49:15
【问题描述】:

我在我的 Google Cloud SQL 数据库中存储日期时间变量时遇到了一些问题。每次我存储它时,它都会以 UTC 格式保存它,我希望它在 CEST+0200(西班牙的时区)中。

通过Python中的这个函数,我可以得到我想要的时间:

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

有没有办法将其保存为西班牙的时区?

谢谢。

【问题讨论】:

  • 为什么不在检索时区时将其转换为西班牙语?
  • 对不起,你是什么意思?我可以恢复我想要的时间,但是保存在数据库中的日期是以UTC时区存储的。

标签: google-app-engine python-2.7 google-cloud-sql


【解决方案1】:

您可以使用pytz 库来执行此操作,我在此answer 中找到了一个示例,但使用起来非常简单

import pytz
import datetime

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

【讨论】:

  • 是的,我已经看过那个例子并且我已经使用了那个 python 库。最后我可以通过设置 Django settings.py USE_TZ = False 并使用函数 astimezone() 和 timezone('Europe/Madrid') 来解决它。非常感谢您的回答。
猜你喜欢
  • 1970-01-01
  • 2016-12-29
  • 2017-08-17
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多