首页 > 解决方案 > 如何将常量转换为时区?

问题描述

我正在使用 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'

如何将常量转换为时区?

标签: pythonpython-3.xtimezonedate-math

解决方案


使用pytz图书馆。

tz = pytz.timezone(TIME_ZONE)

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


推荐阅读