本文整理汇总了Python中pytz.exceptions方法的典型用法代码示例。如果您正苦于以下问题:Python pytz.exceptions方法的具体用法?Python pytz.exceptions怎么用?Python pytz.exceptions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pytz
的用法示例。
在下文中一共展示了pytz.exceptions方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _localize_or_normalize
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import exceptions [as 别名]
def _localize_or_normalize(self, func, dt, is_dst=False):
"""localize() and normalize() have common code paths
Args:
func:
dt:
is_dst: (Default value = False)
"""
# super() returns a dt.tzinfo of class pytz.tzinfo.FooBar. We need to return type EWSTimeZone
if is_dst is not False:
# Not all pytz timezones support 'is_dst' argument. Only pass it on if it's set explicitly.
try:
res = getattr(super(EWSTimeZone, self), func)(dt, is_dst=is_dst)
except pytz.exceptions.AmbiguousTimeError as exc:
raise AmbiguousTimeError(str(dt)) from exc
except pytz.exceptions.NonExistentTimeError as exc:
raise NonExistentTimeError(str(dt)) from exc
else:
res = getattr(super(EWSTimeZone, self), func)(dt)
if not isinstance(res.tzinfo, EWSTimeZone):
return res.replace(tzinfo=self.from_pytz(res.tzinfo))
return res
示例2: test_nonexistent_raise_coerce
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import exceptions [as 别名]
def test_nonexistent_raise_coerce(self):
# See issue 13057
from pytz.exceptions import NonExistentTimeError
times = ['2015-03-08 01:00', '2015-03-08 02:00', '2015-03-08 03:00']
index = DatetimeIndex(times)
tz = 'US/Eastern'
pytest.raises(NonExistentTimeError,
index.tz_localize, tz=tz)
pytest.raises(NonExistentTimeError,
index.tz_localize, tz=tz, errors='raise')
result = index.tz_localize(tz=tz, errors='coerce')
test_times = ['2015-03-08 01:00-05:00', 'NaT',
'2015-03-08 03:00-04:00']
expected = DatetimeIndex(test_times)\
.tz_localize('UTC').tz_convert('US/Eastern')
tm.assert_index_equal(result, expected)
# test utility methods
示例3: localzone
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import exceptions [as 别名]
def localzone(cls):
try:
tz = tzlocal.get_localzone()
except pytz.exceptions.UnknownTimeZoneError:
raise UnknownTimeZone("Failed to guess local timezone")
return cls.from_pytz(tz)
示例4: timezone
# 需要导入模块: import pytz [as 别名]
# 或者: from pytz import exceptions [as 别名]
def timezone(cls, location):
# Like pytz.timezone() but returning EWSTimeZone instances
try:
tz = pytz.timezone(location)
except pytz.exceptions.UnknownTimeZoneError:
raise UnknownTimeZone("Timezone '%s' is unknown by pytz" % location)
return cls.from_pytz(tz)