Parse a timestamp into a datetime object. Supported formats: * iso8601 * rfc822 * epoch (value is an integer) This will return a ``datetime.datetime`` object.
(value)
| 811 | |
| 812 | |
| 813 | def parse_timestamp(value): |
| 814 | """Parse a timestamp into a datetime object. |
| 815 | |
| 816 | Supported formats: |
| 817 | |
| 818 | * iso8601 |
| 819 | * rfc822 |
| 820 | * epoch (value is an integer) |
| 821 | |
| 822 | This will return a ``datetime.datetime`` object. |
| 823 | |
| 824 | """ |
| 825 | tzinfo_options = get_tzinfo_options() |
| 826 | for tzinfo in tzinfo_options: |
| 827 | try: |
| 828 | return _parse_timestamp_with_tzinfo(value, tzinfo) |
| 829 | except (OSError, OverflowError) as e: |
| 830 | logger.debug( |
| 831 | 'Unable to parse timestamp with "%s" timezone info.', |
| 832 | tzinfo.__name__, |
| 833 | exc_info=e, |
| 834 | ) |
| 835 | # For numeric values attempt fallback to using fromtimestamp-free method. |
| 836 | # From Python's ``datetime.datetime.fromtimestamp`` documentation: "This |
| 837 | # may raise ``OverflowError``, if the timestamp is out of the range of |
| 838 | # values supported by the platform C localtime() function, and ``OSError`` |
| 839 | # on localtime() failure. It's common for this to be restricted to years |
| 840 | # from 1970 through 2038." |
| 841 | try: |
| 842 | numeric_value = float(value) |
| 843 | except (TypeError, ValueError): |
| 844 | pass |
| 845 | else: |
| 846 | try: |
| 847 | for tzinfo in tzinfo_options: |
| 848 | return _epoch_seconds_to_datetime(numeric_value, tzinfo=tzinfo) |
| 849 | except (OSError, OverflowError) as e: |
| 850 | logger.debug( |
| 851 | 'Unable to parse timestamp using fallback method with "%s" ' |
| 852 | 'timezone info.', |
| 853 | tzinfo.__name__, |
| 854 | exc_info=e, |
| 855 | ) |
| 856 | raise RuntimeError( |
| 857 | f'Unable to calculate correct timezone offset for "{value}"' |
| 858 | ) |
| 859 | |
| 860 | |
| 861 | def parse_to_aware_datetime(value): |