首页 > 解决方案 > 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

我该如何解决这个问题?

标签: pythonpython-3.xwindowspython-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)。


推荐阅读