MCPcopy
hub / github.com/ArchiveBox/ArchiveBox / parse_date

Function parse_date

archivebox/util.py:142–161  ·  view source on GitHub ↗

Parse unix timestamps, iso format, and human-readable strings

(date: Any)

Source from the content-addressed store, hash-verified

140
141@enforce_types
142def parse_date(date: Any) -> Optional[datetime]:
143 """Parse unix timestamps, iso format, and human-readable strings"""
144
145 if date is None:
146 return None
147
148 if isinstance(date, datetime):
149 if date.tzinfo is None:
150 return date.replace(tzinfo=timezone.utc)
151
152 assert date.tzinfo.utcoffset(datetime.now()).seconds == 0, 'Refusing to load a non-UTC date!'
153 return date
154
155 if isinstance(date, (float, int)):
156 date = str(date)
157
158 if isinstance(date, str):
159 return dateparser(date, settings={'TIMEZONE': 'UTC'}).replace(tzinfo=timezone.utc)
160
161 raise ValueError('Tried to parse invalid date! {}'.format(date))
162
163
164@enforce_types

Callers 7

util.pyFile · 0.85
bookmarkedMethod · 0.85
guess_tsMethod · 0.85
from_jsonMethod · 0.85
from_jsonMethod · 0.85
archive_datesMethod · 0.85
write_link_to_sql_indexFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected