【问题标题】:How do I convert my constant into a timezone? [duplicate]如何将常量转换为时区? [复制]
【发布时间】:2019-08-06 15:09:13
【问题描述】:

我正在使用 Python 3.7。我在我的设置文件中定义了这个...

TIME_ZONE = 'America/New York'

我想让“now()”函数知道时区,所以我可以做一些日期减法...

int(round((datetime.now(settings.TIME_ZONE) - article.created_on).total_seconds() / 60)) > 10

但我不断收到此错误

TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'str'

如何将常量转换为时区?

【问题讨论】:

    标签: python python-3.x timezone date-math


    【解决方案1】:

    使用pytz 库。

    tz = pytz.timezone(TIME_ZONE)
    

    这将为您提供一个 tzinfo 对象,您可以将其传递给 .now()

    【讨论】:

      猜你喜欢
      • 2021-05-31
      • 2016-03-08
      • 2021-08-04
      • 2011-12-03
      • 2019-01-06
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 2018-12-30
      相关资源
      最近更新 更多