dates.py 文件源码

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

项目:Habitica-todo 作者: eringiglio 项目源码 文件源码
def parse_date_utc(date, milliseconds=True):
    """Parses dates from ISO8601 or Epoch formats to a standard datetime object.

    This is particularly useful since Habitica returns dates in two
    formats::

        - iso8601 encoded strings
        - Long integer Epoch times

    Args:
        date (str): A date string in either iso8601 or Epoch format.
        milliseconds (bool): If True, then epoch times are treated as
            millisecond values, otherwise they are evaluated as seconds.

    Returns:
        datetime: The parsed date time in UTC.
    """

    parsed_date = None
    try:
        parsed_date = iso8601.parse_date(date)
    except iso8601.ParseError:
        value = int(date)
        # utcfromtimestamp expects values in seconds
        if milliseconds:
            value /= 1000
        parsed_date = datetime.utcfromtimestamp(value)

    return parsed_date.replace(tzinfo=pytz.utc)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号