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)
评论列表
文章目录