Thời gian lấy dữ liệu được kiểm tra cần phải ngây thơ (không có múi giờ) hoặc tất cả nhận thức (múi giờ). Một ngoại lệ sẽ xảy ra nếu bạn cố gắng so sánh nhận thức và ngây thơ. Nếu tất cả các datetimes nhận thức được các múi giờ không thực sự có để phù hợp mà dường như được đưa vào xem xét khi so sánh.
ví dụ:
class RND(datetime.tzinfo):
""" Random timezone UTC -3 """
def utcoffset(self, dt):
return datetime.timedelta(hours=-3)
def tzname(self, dt):
return "RND"
def dst(self, dt):
return datetime.timedelta(hours=0)
april_fools = datetime.datetime(year=2017, month=4, day=1, hour=12, tzinfo=pytz.UTC)
random_dt = datetime.datetime(year=2017, month=4, day=1, hour=9, tzinfo=RND())
random_dt == april_fools
# True as the same time when converted back to utc.
# Between test of 3 naive datetimes
start_spring = datetime.datetime(year=2018, month=3, day=20)
end_spring = datetime.datetime(year=2018, month=6, day=21)
april_fools = datetime.datetime(year=2018, month=4, day=1)
if start_spring < april_fools < end_spring:
print "April fools is in spring"
Nguồn
2017-11-23 04:53:56