Parse unix timestamps, iso format, and human-readable strings
(date: Any)
| 140 | |
| 141 | @enforce_types |
| 142 | def 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 |
no outgoing calls
no test coverage detected