Parse timestamp with pluggable tzinfo options.
(value, tzinfo)
| 792 | |
| 793 | |
| 794 | def _parse_timestamp_with_tzinfo(value, tzinfo): |
| 795 | """Parse timestamp with pluggable tzinfo options.""" |
| 796 | if isinstance(value, (int, float)): |
| 797 | # Possibly an epoch time. |
| 798 | return datetime.datetime.fromtimestamp(value, tzinfo()) |
| 799 | else: |
| 800 | try: |
| 801 | return datetime.datetime.fromtimestamp(float(value), tzinfo()) |
| 802 | except (TypeError, ValueError): |
| 803 | pass |
| 804 | try: |
| 805 | # In certain cases, a timestamp marked with GMT can be parsed into a |
| 806 | # different time zone, so here we provide a context which will |
| 807 | # enforce that GMT == UTC. |
| 808 | return dateutil.parser.parse(value, tzinfos={'GMT': tzutc()}) |
| 809 | except (TypeError, ValueError) as e: |
| 810 | raise ValueError(f'Invalid timestamp "{value}": {e}') |
| 811 | |
| 812 | |
| 813 | def parse_timestamp(value): |
no test coverage detected