本文整理匯總了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)