本文整理汇总了Python中pytz.exceptions.UnknownTimeZoneError方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.UnknownTimeZoneError方法的具体用法?Python exceptions.UnknownTimeZoneError怎么用?Python exceptions.UnknownTimeZoneError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pytz.exceptions
的用法示例。
在下文中一共展示了exceptions.UnknownTimeZoneError方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cmd_set_timezone
# 需要导入模块: from pytz import exceptions [as 别名]
# 或者: from pytz.exceptions import UnknownTimeZoneError [as 别名]
def cmd_set_timezone(bot, update, args, chat):
if len(args) < 1:
bot.reply(update,
"No timezone specified. Find yours [here]({})!".format(TIMEZONE_LIST_URL),
parse_mode=telegram.ParseMode.MARKDOWN)
return
tz_name = args[0]
try:
tz = timezone(tz_name)
chat.timezone_name = tz_name
chat.save()
tz_str = datetime.now(tz).strftime('%Z %z')
bot.reply(update, "Timezone is set to {}".format(tz_str))
except UnknownTimeZoneError:
bot.reply(update,
"Unknown timezone. Find yours [here]({})!".format(TIMEZONE_LIST_URL),
parse_mode=telegram.ParseMode.MARKDOWN)
示例2: make_aware
# 需要导入模块: from pytz import exceptions [as 别名]
# 或者: from pytz.exceptions import UnknownTimeZoneError [as 别名]
def make_aware(naive_datetime, timezone_name):
# naive means: tzinfo is None
try:
tz = timezone(timezone_name)
aware_datetime = naive_datetime.replace(tzinfo=tz)
aware_datetime_in_utc = aware_datetime.astimezone(utc)
naive_datetime_as_utc_converted_to_tz = tz.localize(naive_datetime)
except UnknownTimeZoneError:
# ... handle the error ..
pass
# Getting a location's time zone offset from UTC in minutes:
# adapted solution from https://github.com/communikein and `phineas-pta <https://github.com/phineas-pta>`__
示例3: cron_preview
# 需要导入模块: from pytz import exceptions [as 别名]
# 或者: from pytz.exceptions import UnknownTimeZoneError [as 别名]
def cron_preview(request):
schedule = request.POST.get("schedule", "")
tz = request.POST.get("tz")
ctx = {"tz": tz, "dates": []}
try:
zone = pytz.timezone(tz)
now_local = timezone.localtime(timezone.now(), zone)
if len(schedule.split()) != 5:
raise ValueError()
it = croniter(schedule, now_local)
for i in range(0, 6):
ctx["dates"].append(it.get_next(datetime))
ctx["desc"] = str(ExpressionDescriptor(schedule, use_24hour_time_format=True))
except UnknownTimeZoneError:
ctx["bad_tz"] = True
except:
ctx["bad_schedule"] = True
return render(request, "front/cron_preview.html", ctx)
示例4: _validate_time_zone
# 需要导入模块: from pytz import exceptions [as 别名]
# 或者: from pytz.exceptions import UnknownTimeZoneError [as 别名]
def _validate_time_zone(param=None):
"""Validates time zone parameters
Args:
param (str, optional): The time zone parameter
Returns:
str: The passed in time zone
"""
if not param:
return None
if not isinstance(param, str):
logger.warning(
"Invalid %r param: %s is not of type %s.", "time_zone", param, str
)
return None
try:
timezone(param)
except UnknownTimeZoneError:
logger.warning(
"Invalid %r param: %s is not a valid time zone.", "time_zone", param
)
return None
return param
示例5: valid_timezone
# 需要导入模块: from pytz import exceptions [as 别名]
# 或者: from pytz.exceptions import UnknownTimeZoneError [as 别名]
def valid_timezone(timezone):
try:
pytz.timezone(timezone)
except UnknownTimeZoneError:
return False
return True
示例6: test_location_invalid_tz
# 需要导入模块: from pytz import exceptions [as 别名]
# 或者: from pytz.exceptions import UnknownTimeZoneError [as 别名]
def test_location_invalid_tz():
with pytest.raises(UnknownTimeZoneError):
Location(32.2, -111, 'invalid')
示例7: timezone
# 需要导入模块: from pytz import exceptions [as 别名]
# 或者: from pytz.exceptions import UnknownTimeZoneError [as 别名]
def timezone(zone):
r''' Return a datetime.tzinfo implementation for the given timezone
>>> from datetime import datetime, timedelta
>>> utc = timezone('UTC')
>>> eastern = timezone('US/Eastern')
>>> eastern.zone
'US/Eastern'
>>> timezone(unicode('US/Eastern')) is eastern
True
>>> utc_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc)
>>> loc_dt = utc_dt.astimezone(eastern)
>>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
>>> loc_dt.strftime(fmt)
'2002-10-27 01:00:00 EST (-0500)'
>>> (loc_dt - timedelta(minutes=10)).strftime(fmt)
'2002-10-27 00:50:00 EST (-0500)'
>>> eastern.normalize(loc_dt - timedelta(minutes=10)).strftime(fmt)
'2002-10-27 01:50:00 EDT (-0400)'
>>> (loc_dt + timedelta(minutes=10)).strftime(fmt)
'2002-10-27 01:10:00 EST (-0500)'
Raises UnknownTimeZoneError if passed an unknown zone.
>>> try:
... timezone('Asia/Shangri-La')
... except UnknownTimeZoneError:
... print('Unknown')
Unknown
>>> try:
... timezone(unicode('\N{TRADE MARK SIGN}'))
... except UnknownTimeZoneError:
... print('Unknown')
Unknown
'''
if zone.upper() == 'UTC':
return utc
try:
zone = ascii(zone)
except UnicodeEncodeError:
# All valid timezones are ASCII
raise UnknownTimeZoneError(zone)
zone = _unmunge_zone(zone)
if zone not in _tzinfo_cache:
if zone in all_timezones_set:
fp = open_resource(zone)
try:
_tzinfo_cache[zone] = build_tzinfo(zone, fp)
finally:
fp.close()
else:
raise UnknownTimeZoneError(zone)
return _tzinfo_cache[zone]
示例8: timezone
# 需要导入模块: from pytz import exceptions [as 别名]
# 或者: from pytz.exceptions import UnknownTimeZoneError [as 别名]
def timezone(zone):
r''' Return a datetime.tzinfo implementation for the given timezone
>>> from datetime import datetime, timedelta
>>> utc = timezone('UTC')
>>> eastern = timezone('US/Eastern')
>>> eastern.zone
'US/Eastern'
>>> timezone(unicode('US/Eastern')) is eastern
True
>>> utc_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc)
>>> loc_dt = utc_dt.astimezone(eastern)
>>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
>>> loc_dt.strftime(fmt)
'2002-10-27 01:00:00 EST (-0500)'
>>> (loc_dt - timedelta(minutes=10)).strftime(fmt)
'2002-10-27 00:50:00 EST (-0500)'
>>> eastern.normalize(loc_dt - timedelta(minutes=10)).strftime(fmt)
'2002-10-27 01:50:00 EDT (-0400)'
>>> (loc_dt + timedelta(minutes=10)).strftime(fmt)
'2002-10-27 01:10:00 EST (-0500)'
Raises UnknownTimeZoneError if passed an unknown zone.
>>> try:
... timezone('Asia/Shangri-La')
... except UnknownTimeZoneError:
... print('Unknown')
Unknown
>>> try:
... timezone(unicode('\N{TRADE MARK SIGN}'))
... except UnknownTimeZoneError:
... print('Unknown')
Unknown
'''
if zone.upper() == 'UTC':
return utc
try:
zone = ascii(zone)
except UnicodeEncodeError:
# All valid timezones are ASCII
raise UnknownTimeZoneError(zone)
zone = _unmunge_zone(zone)
if zone not in _tzinfo_cache:
if zone in all_timezones_set:
fp = open_resource(zone)
try:
_tzinfo_cache[zone] = build_tzinfo(zone, fp)
finally:
fp.close()
else:
raise UnknownTimeZoneError(zone)
return _tzinfo_cache[zone]
示例9: timezone
# 需要导入模块: from pytz import exceptions [as 别名]
# 或者: from pytz.exceptions import UnknownTimeZoneError [as 别名]
def timezone(zone):
r''' Return a datetime.tzinfo implementation for the given timezone
>>> from datetime import datetime, timedelta
>>> utc = timezone('UTC')
>>> eastern = timezone('US/Eastern')
>>> eastern.zone
'US/Eastern'
>>> timezone(unicode('US/Eastern')) is eastern
True
>>> utc_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc)
>>> loc_dt = utc_dt.astimezone(eastern)
>>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
>>> loc_dt.strftime(fmt)
'2002-10-27 01:00:00 EST (-0500)'
>>> (loc_dt - timedelta(minutes=10)).strftime(fmt)
'2002-10-27 00:50:00 EST (-0500)'
>>> eastern.normalize(loc_dt - timedelta(minutes=10)).strftime(fmt)
'2002-10-27 01:50:00 EDT (-0400)'
>>> (loc_dt + timedelta(minutes=10)).strftime(fmt)
'2002-10-27 01:10:00 EST (-0500)'
Raises UnknownTimeZoneError if passed an unknown zone.
>>> try:
... timezone('Asia/Shangri-La')
... except UnknownTimeZoneError:
... print('Unknown')
Unknown
>>> try:
... timezone(unicode('\N{TRADE MARK SIGN}'))
... except UnknownTimeZoneError:
... print('Unknown')
Unknown
'''
if zone is None:
raise UnknownTimeZoneError(None)
if zone.upper() == 'UTC':
return utc
try:
zone = ascii(zone)
except UnicodeEncodeError:
# All valid timezones are ASCII
raise UnknownTimeZoneError(zone)
zone = _case_insensitive_zone_lookup(_unmunge_zone(zone))
if zone not in _tzinfo_cache:
if zone in all_timezones_set: # noqa
fp = open_resource(zone)
try:
_tzinfo_cache[zone] = build_tzinfo(zone, fp)
finally:
fp.close()
else:
raise UnknownTimeZoneError(zone)
return _tzinfo_cache[zone]