utils.py 文件源码

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

项目:gitsome 作者: donnemartin 项目源码 文件源码
def timestamp_parameter(timestamp, allow_none=True):
    """Function to check the conformance of timestamps passed by users.

    This will check that a string is a valid format and allow users to pass a
    datetime object which we will then convert to a proper ISO8601 date-time
    string.

    :param timestamp: string to be validated or datetime object to be
        converted.
    :param bool allow_none: whether or not to allow timestamp to be None.
        Default: ``True``
    :returns: valid ISO8601 string
    :rtype: str
    :raises: ValueError
    """
    if timestamp is None:
        if allow_none:
            return None
        raise ValueError("Timestamp value cannot be None")

    if isinstance(timestamp, datetime.datetime):
        return timestamp.isoformat() + 'Z'

    if isinstance(timestamp, compat.basestring):
        if not ISO_8601.match(timestamp):
            raise ValueError(("Invalid timestamp: %s is not a valid ISO-8601"
                              " formatted date") % timestamp)
        return timestamp

    raise ValueError("Cannot accept type %s for timestamp" % type(timestamp))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号