python - tz.gettz() 在 Windows 10 中返回 None
问题描述
我正在尝试使用以下代码将 UTC 时间转换为本地时区。它在 ubuntu 18.04 上运行良好。但在 Windows 10 中出现错误。因为tz.gettz()
return None
.
在命令提示符下出现以下错误:
astimezone() 参数 1 必须是 datetime.tzinfo,而不是 None。
这是代码:
from dateutil import tz
def convert_to_localtz(self,date):
tz1 = self._context.get('tz')// Value is 'Asia/Calcutta'
to_zone = tz.gettz(tz1)
print'to_zone',to_zone // Prints None
from_zone = tz.tzutc()
utc = date.replace(tzinfo=from_zone)
date = utc.astimezone(to_zone)
return date
安装的python版本是3.7
我该如何解决这个问题?
解决方案
来自文档
在 Windows 上,该标准被扩展为包括操作系统提供的特定于 Windows 的区域名称:
>>> gettz('Egypt Standard Time') tzwin('Egypt Standard Time')
和
name – 时区名称(IANA,或者,在 Windows 上,Windows 键),...
所以我认为你tz1
应该是India Standard Time
(至少基于https://support.microsoft.com/en-us/help/973627/microsoft-time-zone-index-values)。
推荐阅读
- java - 从 Firebase 检索位置数据未进入 MapActivity
- html - href 不适用于导航链接折叠类
- javascript - 更改字体真棒图标 onclick reactjs
- vba - 如何使用 VBA 偏移 MS PowerPoint 中的背景图像?
- javascript - 无论路径如何, new Audio("./mysound.mp3") 如何工作?
- javascript - 如何使用 javascript jquery 获取 tbody 标题?
- node.js - 当应用程序输出通过管道传输时,不会为 ctrl-c 按键调用 SIGINT、beforeExit 和 exit 事件处理程序
- javascript - 如何格式化和显示没有时区的日期?
- python - 谷歌云 NL API 数据到 Pandas Dataframe
- neo4j - 图数据库或关系数据库公用表扩展:比较非循环图查询性能