本文整理匯總了Python中datetime.datetime.timezone方法的典型用法代碼示例。如果您正苦於以下問題:Python datetime.timezone方法的具體用法?Python datetime.timezone怎麽用?Python datetime.timezone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類datetime.datetime
的用法示例。
在下文中一共展示了datetime.timezone方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __repr__
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def __repr__(self):
"""Convert to formal string, for repr().
>>> tz = timezone.utc
>>> repr(tz)
'datetime.timezone.utc'
>>> tz = timezone(timedelta(hours=-5), 'EST')
>>> repr(tz)
"datetime.timezone(datetime.timedelta(-1, 68400), 'EST')"
"""
if self is self.utc:
return "datetime.timezone.utc"
if self._name is None:
return "%s.%s(%r)" % (
self.__class__.__module__,
self.__class__.__name__,
self._offset,
)
return "%s.%s(%r, %r)" % (
self.__class__.__module__,
self.__class__.__name__,
self._offset,
self._name,
)
示例2: test_normalize_tz_local
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def test_normalize_tz_local(self, timezone):
# GH#13459
with tm.set_timezone(timezone):
rng = date_range('1/1/2000 9:30', periods=10, freq='D',
tz=tzlocal())
result = rng.normalize()
expected = date_range('1/1/2000', periods=10, freq='D',
tz=tzlocal())
tm.assert_index_equal(result, expected)
assert result.is_normalized
assert not rng.is_normalized
# ------------------------------------------------------------
# DatetimeIndex.__new__
示例3: test_dti_tz_constructors
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def test_dti_tz_constructors(self, tzstr):
""" Test different DatetimeIndex constructions with timezone
Follow-up of GH#4229
"""
arr = ['11/10/2005 08:00:00', '11/10/2005 09:00:00']
idx1 = to_datetime(arr).tz_localize(tzstr)
idx2 = pd.date_range(start="2005-11-10 08:00:00", freq='H', periods=2,
tz=tzstr)
idx3 = DatetimeIndex(arr, tz=tzstr)
idx4 = DatetimeIndex(np.array(arr), tz=tzstr)
for other in [idx2, idx3, idx4]:
tm.assert_index_equal(idx1, other)
# -------------------------------------------------------------
# Unsorted
示例4: test_dti_tz_constructors
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def test_dti_tz_constructors(self, tzstr):
""" Test different DatetimeIndex constructions with timezone
Follow-up of GH#4229
"""
arr = ['11/10/2005 08:00:00', '11/10/2005 09:00:00']
idx1 = to_datetime(arr).tz_localize(tzstr)
idx2 = DatetimeIndex(start="2005-11-10 08:00:00", freq='H', periods=2,
tz=tzstr)
idx3 = DatetimeIndex(arr, tz=tzstr)
idx4 = DatetimeIndex(np.array(arr), tz=tzstr)
for other in [idx2, idx3, idx4]:
tm.assert_index_equal(idx1, other)
# -------------------------------------------------------------
# Unsorted
示例5: __init__
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def __init__(self, offset, name=None):
"""
:param offset:
A timedelta with this timezone's offset from UTC
:param name:
Name of the timezone; if None, generate one.
"""
if not timedelta(hours=-24) < offset < timedelta(hours=24):
raise ValueError('Offset must be in [-23:59, 23:59]')
if offset.seconds % 60 or offset.microseconds:
raise ValueError('Offset must be full minutes')
self._offset = offset
if name is not None:
self._name = name
elif not offset:
self._name = 'UTC'
else:
self._name = 'UTC' + _format_offset(offset)
示例6: create_timezone
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def create_timezone(offset):
"""
Returns a new datetime.timezone object with the given offset.
Uses cached objects if possible.
:param offset:
A datetime.timedelta object; It needs to be in full minutes and between -23:59 and +23:59.
:return:
A datetime.timezone object
"""
try:
tz = _timezone_cache[offset]
except KeyError:
tz = _timezone_cache[offset] = timezone(offset)
return tz
示例7: test_attributes
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def test_attributes():
import datetime
import osxphotos
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
photos = photosdb.photos(uuid=["15uNd7%8RguTEgNPKHfTWw"])
assert len(photos) == 1
p = photos[0]
assert p.keywords == ["Kids"]
assert p.original_filename == "Pumkins2.jpg"
assert p.filename == "Pumkins2.jpg"
assert p.date == datetime.datetime(
2018, 9, 28, 16, 7, 7, 0, datetime.timezone(datetime.timedelta(seconds=-14400))
)
assert p.description == "Girl holding pumpkin"
assert p.title == "I found one!"
assert sorted(p.albums) == sorted(
["Pumpkin Farm", "AlbumInFolder", "Test Album (1)"]
)
assert p.persons == ["Katie"]
assert p.path.endswith(
"/tests/Test-10.14.6.photoslibrary/Masters/2019/07/27/20190727-131650/Pumkins2.jpg"
)
assert p.ismissing == False
示例8: test_attributes
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def test_attributes():
import datetime
import osxphotos
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
photos = photosdb.photos(uuid=["D79B8D77-BFFC-460B-9312-034F2877D35B"])
assert len(photos) == 1
p = photos[0]
assert p.keywords == ["Kids"]
assert p.original_filename == "Pumkins2.jpg"
assert p.filename == "D79B8D77-BFFC-460B-9312-034F2877D35B.jpeg"
assert p.date == datetime.datetime(
2018, 9, 28, 16, 7, 7, 0, datetime.timezone(datetime.timedelta(seconds=-14400))
)
assert p.description == "Girl holding pumpkin"
assert p.title == "I found one!"
assert sorted(p.albums) == ["Pumpkin Farm", "Test Album"]
assert p.persons == ["Katie"]
assert p.path.endswith(
"tests/Test-10.15.5.photoslibrary/originals/D/D79B8D77-BFFC-460B-9312-034F2877D35B.jpeg"
)
assert p.ismissing == False
示例9: __eq__
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def __eq__(self, other):
if type(other) != timezone:
return False
return self._offset == other._offset
示例10: __eq__
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def __eq__(self, other):
"""
Compare two timezones
:param other:
The other timezone to compare to
:return:
A boolean
"""
if type(other) != timezone:
return False
return self._offset == other._offset
示例11: tzname
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def tzname(self, dt):
"""
:param dt:
A datetime object; ignored.
:return:
Name of this timezone
"""
return self._name
示例12: tzinfo
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def tzinfo(self):
"""
:return:
If object is timezone aware, a datetime.tzinfo object, else None.
"""
return self._y2k.tzinfo
示例13: astimezone
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def astimezone(self, tz):
"""
Convert this extended_datetime to another timezone.
:param tz:
A datetime.tzinfo object.
:return:
A new extended_datetime or datetime.datetime object
"""
return extended_datetime.from_y2k(self._y2k.astimezone(tz))
示例14: test_attributes_2
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def test_attributes_2():
""" Test attributes including height, width, etc """
import datetime
import osxphotos
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
photos = photosdb.photos(uuid=[UUID_DICT["has_adjustments"]])
assert len(photos) == 1
p = photos[0]
assert p.keywords == ["wedding"]
assert p.original_filename == "wedding.jpg"
assert p.filename == "wedding.jpg"
assert p.date == datetime.datetime(
2019,
4,
15,
14,
40,
24,
86000,
datetime.timezone(datetime.timedelta(seconds=-14400)),
)
assert p.description == "Bride Wedding day"
assert p.title is None
assert sorted(p.albums) == []
assert p.persons == ["Maria"]
assert p.path.endswith("Masters/2019/07/27/20190727-131650/wedding.jpg")
assert not p.ismissing
assert p.hasadjustments
assert p.height == 1325
assert p.width == 1526
assert p.original_height == 1367
assert p.original_width == 2048
assert p.orientation == 1
assert p.original_orientation == 1
assert p.original_filesize == 460483
示例15: test_date_invalid
# 需要導入模塊: from datetime import datetime [as 別名]
# 或者: from datetime.datetime import timezone [as 別名]
def test_date_invalid():
""" Test date is invalid """
from datetime import datetime, timedelta, timezone
import osxphotos
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
photos = photosdb.photos(uuid=[UUID_DICT["date_invalid"]])
assert len(photos) == 1
p = photos[0]
delta = timedelta(seconds=p.tzoffset)
tz = timezone(delta)
assert p.date == datetime(1970, 1, 1).astimezone(tz=tz)