當前位置: 首頁>>代碼示例>>Python>>正文


Python exceptions.UnknownTimeZoneError方法代碼示例

本文整理匯總了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) 
開發者ID:franciscod,項目名稱:telegram-twitter-forwarder-bot,代碼行數:21,代碼來源:commands.py

示例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>`__ 
開發者ID:MrMinimal64,項目名稱:timezonefinder,代碼行數:18,代碼來源:example.py

示例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) 
開發者ID:healthchecks,項目名稱:healthchecks,代碼行數:25,代碼來源:views.py

示例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 
開發者ID:cisco,項目名稱:mindmeld,代碼行數:26,代碼來源:request.py

示例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 
開發者ID:ActiDoo,項目名稱:gamification-engine,代碼行數:8,代碼來源:model.py

示例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') 
開發者ID:pvlib,項目名稱:pvlib-python,代碼行數:5,代碼來源:test_location.py

示例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] 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:60,代碼來源:__init__.py

示例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] 
開發者ID:Schibum,項目名稱:sndlatr,代碼行數:60,代碼來源:__init__.py

示例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] 
開發者ID:awslabs,項目名稱:predictive-maintenance-using-machine-learning,代碼行數:63,代碼來源:__init__.py


注:本文中的pytz.exceptions.UnknownTimeZoneError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。