Python datetime.astimezone() Examples
The following are 30
code examples of datetime.astimezone().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
datetime
, or try the search function
.
Example #1
Source File: load_internal_reporting_events.py From edx-analytics-pipeline with GNU Affero General Public License v3.0 | 6 votes |
def convert_date(self, date_string): """Converts date from string format to date object, for use by DateField.""" if date_string: try: # TODO: for now, return as a string. # When actually supporting DateField, then switch back to date. # ciso8601.parse_datetime(ts).astimezone(pytz.utc).date().isoformat() return self.date_field_for_converting.deserialize_from_string(date_string).isoformat() except ValueError: self.incr_counter(self.counter_category_name, 'Cannot convert to date', 1) # Don't bother to make sure we return a good value # within the interval, so we can find the output for # debugging. Should not be necessary, as this is only # used for the column value, not the partitioning. return u"BAD: {}".format(date_string) # return self.lower_bound_date_string else: self.incr_counter(self.counter_category_name, 'Missing date', 1) return date_string
Example #2
Source File: tz.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #3
Source File: load_internal_reporting_events.py From edx-analytics-pipeline with GNU Affero General Public License v3.0 | 5 votes |
def normalize_time(self, event_time): """ Convert time string to ISO-8601 format in UTC timezone. Returns None if string representation cannot be parsed. """ datetime = ciso8601.parse_datetime(event_time) if datetime: return datetime.astimezone(pytz.utc).isoformat() else: return None
Example #4
Source File: load_internal_reporting_events.py From edx-analytics-pipeline with GNU Affero General Public License v3.0 | 5 votes |
def extended_normalize_time(self, event_time): """ Convert time string to ISO-8601 format in UTC timezone. Returns None if string representation cannot be parsed. """ datetime = dateutil.parser.parse(event_time) if datetime: return datetime.astimezone(pytz.utc).isoformat() else: return None
Example #5
Source File: tz.py From aws-extender with MIT License | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #6
Source File: tz.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #7
Source File: tz.py From CogAlg with MIT License | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(UTC).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #8
Source File: tz.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #9
Source File: tz.py From plugin.video.emby with GNU General Public License v3.0 | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #10
Source File: tz.py From bash-lambda-layer with MIT License | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #11
Source File: i18n.py From googleapps-message-recall with Apache License 2.0 | 5 votes |
def to_utc(self, datetime): """Returns a datetime object converted to UTC and without tzinfo. :param datetime: A ``datetime`` object. :returns: A naive ``datetime`` object (no timezone), converted to UTC. """ if datetime.tzinfo is None: datetime = self.tzinfo.localize(datetime) return datetime.astimezone(pytz.UTC).replace(tzinfo=None)
Example #12
Source File: i18n.py From googleapps-message-recall with Apache License 2.0 | 5 votes |
def to_local_timezone(self, datetime): """Returns a datetime object converted to the local timezone. :param datetime: A ``datetime`` object. :returns: A ``datetime`` object normalized to a timezone. """ if datetime.tzinfo is None: datetime = datetime.replace(tzinfo=pytz.UTC) return self.tzinfo.normalize(datetime.astimezone(self.tzinfo))
Example #13
Source File: tz.py From aws-builders-fair-projects with Apache License 2.0 | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #14
Source File: tz.py From pipenv with MIT License | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(UTC).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #15
Source File: tz.py From deepWordBug with Apache License 2.0 | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #16
Source File: tz.py From aws-builders-fair-projects with Apache License 2.0 | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #17
Source File: tz.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #18
Source File: tz.py From vnpy_crypto with MIT License | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #19
Source File: tz.py From android_universal with MIT License | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #20
Source File: tz.py From recruit with Apache License 2.0 | 5 votes |
def datetime_exists(dt, tz=None): """ Given a datetime and a time zone, determine whether or not a given datetime would fall in a gap. :param dt: A :class:`datetime.datetime` (whose time zone will be ignored if ``tz`` is provided.) :param tz: A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If ``None`` or not provided, the datetime's own time zone will be used. :return: Returns a boolean value whether or not the "wall time" exists in ``tz``. .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: raise ValueError('Datetime is naive and no time zone provided.') tz = dt.tzinfo dt = dt.replace(tzinfo=None) # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt
Example #21
Source File: tz.py From bash-lambda-layer with MIT License | 4 votes |
def resolve_imaginary(dt): """ Given a datetime that may be imaginary, return an existing datetime. This function assumes that an imaginary datetime represents what the wall time would be in a zone had the offset transition not occurred, so it will always fall forward by the transition's change in offset. .. doctest:: >>> from dateutil import tz >>> from datetime import datetime >>> NYC = tz.gettz('America/New_York') >>> print(tz.resolve_imaginary(datetime(2017, 3, 12, 2, 30, tzinfo=NYC))) 2017-03-12 03:30:00-04:00 >>> KIR = tz.gettz('Pacific/Kiritimati') >>> print(tz.resolve_imaginary(datetime(1995, 1, 1, 12, 30, tzinfo=KIR))) 1995-01-02 12:30:00+14:00 As a note, :func:`datetime.astimezone` is guaranteed to produce a valid, existing datetime, so a round-trip to and from UTC is sufficient to get an extant datetime, however, this generally "falls back" to an earlier time rather than falling forward to the STD side (though no guarantees are made about this behavior). :param dt: A :class:`datetime.datetime` which may or may not exist. :return: Returns an existing :class:`datetime.datetime`. If ``dt`` was not imaginary, the datetime returned is guaranteed to be the same object passed to the function. .. versionadded:: 2.7.0 """ if dt.tzinfo is not None and not datetime_exists(dt): curr_offset = (dt + datetime.timedelta(hours=24)).utcoffset() old_offset = (dt - datetime.timedelta(hours=24)).utcoffset() dt += curr_offset - old_offset return dt
Example #22
Source File: tz.py From CogAlg with MIT License | 4 votes |
def resolve_imaginary(dt): """ Given a datetime that may be imaginary, return an existing datetime. This function assumes that an imaginary datetime represents what the wall time would be in a zone had the offset transition not occurred, so it will always fall forward by the transition's change in offset. .. doctest:: >>> from dateutil import tz >>> from datetime import datetime >>> NYC = tz.gettz('America/New_York') >>> print(tz.resolve_imaginary(datetime(2017, 3, 12, 2, 30, tzinfo=NYC))) 2017-03-12 03:30:00-04:00 >>> KIR = tz.gettz('Pacific/Kiritimati') >>> print(tz.resolve_imaginary(datetime(1995, 1, 1, 12, 30, tzinfo=KIR))) 1995-01-02 12:30:00+14:00 As a note, :func:`datetime.astimezone` is guaranteed to produce a valid, existing datetime, so a round-trip to and from UTC is sufficient to get an extant datetime, however, this generally "falls back" to an earlier time rather than falling forward to the STD side (though no guarantees are made about this behavior). :param dt: A :class:`datetime.datetime` which may or may not exist. :return: Returns an existing :class:`datetime.datetime`. If ``dt`` was not imaginary, the datetime returned is guaranteed to be the same object passed to the function. .. versionadded:: 2.7.0 """ if dt.tzinfo is not None and not datetime_exists(dt): curr_offset = (dt + datetime.timedelta(hours=24)).utcoffset() old_offset = (dt - datetime.timedelta(hours=24)).utcoffset() dt += curr_offset - old_offset return dt
Example #23
Source File: tz.py From aws-builders-fair-projects with Apache License 2.0 | 4 votes |
def resolve_imaginary(dt): """ Given a datetime that may be imaginary, return an existing datetime. This function assumes that an imaginary datetime represents what the wall time would be in a zone had the offset transition not occurred, so it will always fall forward by the transition's change in offset. .. doctest:: >>> from dateutil import tz >>> from datetime import datetime >>> NYC = tz.gettz('America/New_York') >>> print(tz.resolve_imaginary(datetime(2017, 3, 12, 2, 30, tzinfo=NYC))) 2017-03-12 03:30:00-04:00 >>> KIR = tz.gettz('Pacific/Kiritimati') >>> print(tz.resolve_imaginary(datetime(1995, 1, 1, 12, 30, tzinfo=KIR))) 1995-01-02 12:30:00+14:00 As a note, :func:`datetime.astimezone` is guaranteed to produce a valid, existing datetime, so a round-trip to and from UTC is sufficient to get an extant datetime, however, this generally "falls back" to an earlier time rather than falling forward to the STD side (though no guarantees are made about this behavior). :param dt: A :class:`datetime.datetime` which may or may not exist. :return: Returns an existing :class:`datetime.datetime`. If ``dt`` was not imaginary, the datetime returned is guaranteed to be the same object passed to the function. .. versionadded:: 2.7.0 """ if dt.tzinfo is not None and not datetime_exists(dt): curr_offset = (dt + datetime.timedelta(hours=24)).utcoffset() old_offset = (dt - datetime.timedelta(hours=24)).utcoffset() dt += curr_offset - old_offset return dt
Example #24
Source File: tz.py From aws-builders-fair-projects with Apache License 2.0 | 4 votes |
def resolve_imaginary(dt): """ Given a datetime that may be imaginary, return an existing datetime. This function assumes that an imaginary datetime represents what the wall time would be in a zone had the offset transition not occurred, so it will always fall forward by the transition's change in offset. .. doctest:: >>> from dateutil import tz >>> from datetime import datetime >>> NYC = tz.gettz('America/New_York') >>> print(tz.resolve_imaginary(datetime(2017, 3, 12, 2, 30, tzinfo=NYC))) 2017-03-12 03:30:00-04:00 >>> KIR = tz.gettz('Pacific/Kiritimati') >>> print(tz.resolve_imaginary(datetime(1995, 1, 1, 12, 30, tzinfo=KIR))) 1995-01-02 12:30:00+14:00 As a note, :func:`datetime.astimezone` is guaranteed to produce a valid, existing datetime, so a round-trip to and from UTC is sufficient to get an extant datetime, however, this generally "falls back" to an earlier time rather than falling forward to the STD side (though no guarantees are made about this behavior). :param dt: A :class:`datetime.datetime` which may or may not exist. :return: Returns an existing :class:`datetime.datetime`. If ``dt`` was not imaginary, the datetime returned is guaranteed to be the same object passed to the function. .. versionadded:: 2.7.0 """ if dt.tzinfo is not None and not datetime_exists(dt): curr_offset = (dt + datetime.timedelta(hours=24)).utcoffset() old_offset = (dt - datetime.timedelta(hours=24)).utcoffset() dt += curr_offset - old_offset return dt
Example #25
Source File: tz.py From android_universal with MIT License | 4 votes |
def resolve_imaginary(dt): """ Given a datetime that may be imaginary, return an existing datetime. This function assumes that an imaginary datetime represents what the wall time would be in a zone had the offset transition not occurred, so it will always fall forward by the transition's change in offset. .. doctest:: >>> from dateutil import tz >>> from datetime import datetime >>> NYC = tz.gettz('America/New_York') >>> print(tz.resolve_imaginary(datetime(2017, 3, 12, 2, 30, tzinfo=NYC))) 2017-03-12 03:30:00-04:00 >>> KIR = tz.gettz('Pacific/Kiritimati') >>> print(tz.resolve_imaginary(datetime(1995, 1, 1, 12, 30, tzinfo=KIR))) 1995-01-02 12:30:00+14:00 As a note, :func:`datetime.astimezone` is guaranteed to produce a valid, existing datetime, so a round-trip to and from UTC is sufficient to get an extant datetime, however, this generally "falls back" to an earlier time rather than falling forward to the STD side (though no guarantees are made about this behavior). :param dt: A :class:`datetime.datetime` which may or may not exist. :return: Returns an existing :class:`datetime.datetime`. If ``dt`` was not imaginary, the datetime returned is guaranteed to be the same object passed to the function. .. versionadded:: 2.7.0 """ if dt.tzinfo is not None and not datetime_exists(dt): curr_offset = (dt + datetime.timedelta(hours=24)).utcoffset() old_offset = (dt - datetime.timedelta(hours=24)).utcoffset() dt += curr_offset - old_offset return dt
Example #26
Source File: tz.py From aws-extender with MIT License | 4 votes |
def resolve_imaginary(dt): """ Given a datetime that may be imaginary, return an existing datetime. This function assumes that an imaginary datetime represents what the wall time would be in a zone had the offset transition not occurred, so it will always fall forward by the transition's change in offset. .. doctest:: >>> from dateutil import tz >>> from datetime import datetime >>> NYC = tz.gettz('America/New_York') >>> print(tz.resolve_imaginary(datetime(2017, 3, 12, 2, 30, tzinfo=NYC))) 2017-03-12 03:30:00-04:00 >>> KIR = tz.gettz('Pacific/Kiritimati') >>> print(tz.resolve_imaginary(datetime(1995, 1, 1, 12, 30, tzinfo=KIR))) 1995-01-02 12:30:00+14:00 As a note, :func:`datetime.astimezone` is guaranteed to produce a valid, existing datetime, so a round-trip to and from UTC is sufficient to get an extant datetime, however, this generally "falls back" to an earlier time rather than falling forward to the STD side (though no guarantees are made about this behavior). :param dt: A :class:`datetime.datetime` which may or may not exist. :return: Returns an existing :class:`datetime.datetime`. If ``dt`` was not imaginary, the datetime returned is guaranteed to be the same object passed to the function. .. versionadded:: 2.7.0 """ if dt.tzinfo is not None and not datetime_exists(dt): curr_offset = (dt + datetime.timedelta(hours=24)).utcoffset() old_offset = (dt - datetime.timedelta(hours=24)).utcoffset() dt += curr_offset - old_offset return dt
Example #27
Source File: tz.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 4 votes |
def resolve_imaginary(dt): """ Given a datetime that may be imaginary, return an existing datetime. This function assumes that an imaginary datetime represents what the wall time would be in a zone had the offset transition not occurred, so it will always fall forward by the transition's change in offset. .. doctest:: >>> from dateutil import tz >>> from datetime import datetime >>> NYC = tz.gettz('America/New_York') >>> print(tz.resolve_imaginary(datetime(2017, 3, 12, 2, 30, tzinfo=NYC))) 2017-03-12 03:30:00-04:00 >>> KIR = tz.gettz('Pacific/Kiritimati') >>> print(tz.resolve_imaginary(datetime(1995, 1, 1, 12, 30, tzinfo=KIR))) 1995-01-02 12:30:00+14:00 As a note, :func:`datetime.astimezone` is guaranteed to produce a valid, existing datetime, so a round-trip to and from UTC is sufficient to get an extant datetime, however, this generally "falls back" to an earlier time rather than falling forward to the STD side (though no guarantees are made about this behavior). :param dt: A :class:`datetime.datetime` which may or may not exist. :return: Returns an existing :class:`datetime.datetime`. If ``dt`` was not imaginary, the datetime returned is guaranteed to be the same object passed to the function. .. versionadded:: 2.7.0 """ if dt.tzinfo is not None and not datetime_exists(dt): curr_offset = (dt + datetime.timedelta(hours=24)).utcoffset() old_offset = (dt - datetime.timedelta(hours=24)).utcoffset() dt += curr_offset - old_offset return dt
Example #28
Source File: tz.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 4 votes |
def resolve_imaginary(dt): """ Given a datetime that may be imaginary, return an existing datetime. This function assumes that an imaginary datetime represents what the wall time would be in a zone had the offset transition not occurred, so it will always fall forward by the transition's change in offset. .. doctest:: >>> from dateutil import tz >>> from datetime import datetime >>> NYC = tz.gettz('America/New_York') >>> print(tz.resolve_imaginary(datetime(2017, 3, 12, 2, 30, tzinfo=NYC))) 2017-03-12 03:30:00-04:00 >>> KIR = tz.gettz('Pacific/Kiritimati') >>> print(tz.resolve_imaginary(datetime(1995, 1, 1, 12, 30, tzinfo=KIR))) 1995-01-02 12:30:00+14:00 As a note, :func:`datetime.astimezone` is guaranteed to produce a valid, existing datetime, so a round-trip to and from UTC is sufficient to get an extant datetime, however, this generally "falls back" to an earlier time rather than falling forward to the STD side (though no guarantees are made about this behavior). :param dt: A :class:`datetime.datetime` which may or may not exist. :return: Returns an existing :class:`datetime.datetime`. If ``dt`` was not imaginary, the datetime returned is guaranteed to be the same object passed to the function. .. versionadded:: 2.7.0 """ if dt.tzinfo is not None and not datetime_exists(dt): curr_offset = (dt + datetime.timedelta(hours=24)).utcoffset() old_offset = (dt - datetime.timedelta(hours=24)).utcoffset() dt += curr_offset - old_offset return dt
Example #29
Source File: tz.py From GraphicDesignPatternByPython with MIT License | 4 votes |
def resolve_imaginary(dt): """ Given a datetime that may be imaginary, return an existing datetime. This function assumes that an imaginary datetime represents what the wall time would be in a zone had the offset transition not occurred, so it will always fall forward by the transition's change in offset. .. doctest:: >>> from dateutil import tz >>> from datetime import datetime >>> NYC = tz.gettz('America/New_York') >>> print(tz.resolve_imaginary(datetime(2017, 3, 12, 2, 30, tzinfo=NYC))) 2017-03-12 03:30:00-04:00 >>> KIR = tz.gettz('Pacific/Kiritimati') >>> print(tz.resolve_imaginary(datetime(1995, 1, 1, 12, 30, tzinfo=KIR))) 1995-01-02 12:30:00+14:00 As a note, :func:`datetime.astimezone` is guaranteed to produce a valid, existing datetime, so a round-trip to and from UTC is sufficient to get an extant datetime, however, this generally "falls back" to an earlier time rather than falling forward to the STD side (though no guarantees are made about this behavior). :param dt: A :class:`datetime.datetime` which may or may not exist. :return: Returns an existing :class:`datetime.datetime`. If ``dt`` was not imaginary, the datetime returned is guaranteed to be the same object passed to the function. .. versionadded:: 2.7.0 """ if dt.tzinfo is not None and not datetime_exists(dt): curr_offset = (dt + datetime.timedelta(hours=24)).utcoffset() old_offset = (dt - datetime.timedelta(hours=24)).utcoffset() dt += curr_offset - old_offset return dt
Example #30
Source File: tz.py From pipenv with MIT License | 4 votes |
def resolve_imaginary(dt): """ Given a datetime that may be imaginary, return an existing datetime. This function assumes that an imaginary datetime represents what the wall time would be in a zone had the offset transition not occurred, so it will always fall forward by the transition's change in offset. .. doctest:: >>> from dateutil import tz >>> from datetime import datetime >>> NYC = tz.gettz('America/New_York') >>> print(tz.resolve_imaginary(datetime(2017, 3, 12, 2, 30, tzinfo=NYC))) 2017-03-12 03:30:00-04:00 >>> KIR = tz.gettz('Pacific/Kiritimati') >>> print(tz.resolve_imaginary(datetime(1995, 1, 1, 12, 30, tzinfo=KIR))) 1995-01-02 12:30:00+14:00 As a note, :func:`datetime.astimezone` is guaranteed to produce a valid, existing datetime, so a round-trip to and from UTC is sufficient to get an extant datetime, however, this generally "falls back" to an earlier time rather than falling forward to the STD side (though no guarantees are made about this behavior). :param dt: A :class:`datetime.datetime` which may or may not exist. :return: Returns an existing :class:`datetime.datetime`. If ``dt`` was not imaginary, the datetime returned is guaranteed to be the same object passed to the function. .. versionadded:: 2.7.0 """ if dt.tzinfo is not None and not datetime_exists(dt): curr_offset = (dt + datetime.timedelta(hours=24)).utcoffset() old_offset = (dt - datetime.timedelta(hours=24)).utcoffset() dt += curr_offset - old_offset return dt