如何将常量转换为时区?

How do I convert my constant into a timezone?

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

如何将常量转换为时区?

使用 pytz 库。

tz = pytz.timezone(TIME_ZONE)

这将为您提供一个可以传递给 .now()

的 tzinfo 对象