time.py 文件源码

python
阅读 33 收藏 0 点赞 0 评论 0

项目:GLaDOS2 作者: TheComet 项目源码 文件源码
def validate_timezone(zone):
    """Return an IETF timezone from the given IETF zone or common abbreviation.
    If the length of the zone is 4 or less, it will be upper-cased before being
    looked up; otherwise it will be title-cased. This is the expected
    case-insensitivity behavior in the majority of cases. For example, ``'edt'``
    and ``'america/new_york'`` will both return ``'America/New_York'``.
    If the zone is not valid, ``ValueError`` will be raised. If ``pytz`` is not
    available, and the given zone is anything other than ``'UTC'``,
    ``ValueError`` will be raised.
    """
    if zone is None:
        return None
    if not pytz:
        if zone.upper() != 'UTC':
            raise ValueError('Only UTC available, since pytz is not installed.')
        else:
            return zone

    zone = '/'.join(reversed(zone.split(', '))).replace(' ', '_')
    if len(zone) <= 4:
        zone = zone.upper()
    else:
        zone = zone.title()
    if zone in pytz.all_timezones:
        return zone
    else:
        raise ValueError("Invalid time zone.")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号