本文整理匯總了Python中datetime.astimezone方法的典型用法代碼示例。如果您正苦於以下問題:Python datetime.astimezone方法的具體用法?Python datetime.astimezone怎麽用?Python datetime.astimezone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類datetime
的用法示例。
在下文中一共展示了datetime.astimezone方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: convert_date
# 需要導入模塊: import datetime [as 別名]
# 或者: from datetime import astimezone [as 別名]
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
示例2: datetime_exists
# 需要導入模塊: import datetime [as 別名]
# 或者: from datetime import astimezone [as 別名]
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
示例3: datetime_exists
# 需要導入模塊: import datetime [as 別名]
# 或者: from datetime import astimezone [as 別名]
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
示例4: to_local_timezone
# 需要導入模塊: import datetime [as 別名]
# 或者: from datetime import astimezone [as 別名]
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))
示例5: to_utc
# 需要導入模塊: import datetime [as 別名]
# 或者: from datetime import astimezone [as 別名]
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)
示例6: normalize_time
# 需要導入模塊: import datetime [as 別名]
# 或者: from datetime import astimezone [as 別名]
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
示例7: extended_normalize_time
# 需要導入模塊: import datetime [as 別名]
# 或者: from datetime import astimezone [as 別名]
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
示例8: resolve_imaginary
# 需要導入模塊: import datetime [as 別名]
# 或者: from datetime import astimezone [as 別名]
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