Converted the passed in value to a datetime object with tzinfo. This function can be used to normalize all timestamp inputs. This function accepts a number of different types of inputs, but will always return a datetime.datetime object with time zone information. The input par
(value)
| 859 | |
| 860 | |
| 861 | def parse_to_aware_datetime(value): |
| 862 | """Converted the passed in value to a datetime object with tzinfo. |
| 863 | |
| 864 | This function can be used to normalize all timestamp inputs. This |
| 865 | function accepts a number of different types of inputs, but |
| 866 | will always return a datetime.datetime object with time zone |
| 867 | information. |
| 868 | |
| 869 | The input param ``value`` can be one of several types: |
| 870 | |
| 871 | * A datetime object (both naive and aware) |
| 872 | * An integer representing the epoch time (can also be a string |
| 873 | of the integer, i.e '0', instead of 0). The epoch time is |
| 874 | considered to be UTC. |
| 875 | * An iso8601 formatted timestamp. This does not need to be |
| 876 | a complete timestamp, it can contain just the date portion |
| 877 | without the time component. |
| 878 | |
| 879 | The returned value will be a datetime object that will have tzinfo. |
| 880 | If no timezone info was provided in the input value, then UTC is |
| 881 | assumed, not local time. |
| 882 | |
| 883 | """ |
| 884 | # This is a general purpose method that handles several cases of |
| 885 | # converting the provided value to a string timestamp suitable to be |
| 886 | # serialized to an http request. It can handle: |
| 887 | # 1) A datetime.datetime object. |
| 888 | if isinstance(value, _DatetimeClass): |
| 889 | datetime_obj = value |
| 890 | else: |
| 891 | # 2) A string object that's formatted as a timestamp. |
| 892 | # We document this as being an iso8601 timestamp, although |
| 893 | # parse_timestamp is a bit more flexible. |
| 894 | datetime_obj = parse_timestamp(value) |
| 895 | if datetime_obj.tzinfo is None: |
| 896 | # I think a case would be made that if no time zone is provided, |
| 897 | # we should use the local time. However, to restore backwards |
| 898 | # compat, the previous behavior was to assume UTC, which is |
| 899 | # what we're going to do here. |
| 900 | datetime_obj = datetime_obj.replace(tzinfo=tzutc()) |
| 901 | else: |
| 902 | datetime_obj = datetime_obj.astimezone(tzutc()) |
| 903 | return datetime_obj |
| 904 | |
| 905 | |
| 906 | def datetime2timestamp(dt, default_timezone=None): |