python - 如何将常量转换为时区?
问题描述
我正在使用 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)
这将为您提供一个 tzinfo 对象,您可以将其传递给.now()
推荐阅读
- niftynet - Niftynet 如何处理多 GPU 训练
- mysql - SQL 从表中获取行,除非值存在
- jsf - c:if 作为组合的一部分:insertChildren
- r - 有条件地使用二级阈值水平
- java - 裁剪后如何从图库中选择图像并保存在应用程序中?
- javascript - SignalR .net 核心。没有收到 signalr.js?
- winforms - 拖放,源来自应用程序或文件资源管理器?
- php - 我想访问控制器中的当前用户
- c# - 在Angular 6中传递带有空字符串的整数值时未调用API Get方法
- python - Python:如何获取列表集的所有有序组合