def test_dst(self): tzinfoobject = UTCOffset(minutes=240) # This would raise ISOFormatError or a TypeError if dst info is invalid result = datetime.datetime.now(tzinfoobject) # Hacky way to make sure the tzinfo is what we'd expect self.assertEqual(result.tzinfo.utcoffset(None), datetime.timedelta(hours=4))
def test_pickle(self): # Make sure timezone objects are pickleable testutcoffset = UTCOffset(name='UTC', minutes=0) utcoffsetpickle = pickle.dumps(testutcoffset) resultutcoffset = pickle.loads(utcoffsetpickle) self.assertEqual(resultutcoffset._name, testutcoffset._name) self.assertEqual(resultutcoffset._utcdelta, testutcoffset._utcdelta)
def build_timezone(cls, negative=None, Z=None, hh=None, mm=None, name=""): if Z is True: # Z -> UTC return UTCOffset(name="UTC", minutes=0) if hh is not None: tzhour = cls.cast(hh, int, thrownmessage="Invalid hour string.") else: tzhour = 0 if mm is not None: tzminute = cls.cast(mm, int, thrownmessage="Invalid minute string.") else: tzminute = 0 if negative is True: return UTCOffset(name=name, minutes=-(tzhour * 60 + tzminute)) return UTCOffset(name=name, minutes=tzhour * 60 + tzminute)
def build_timezone(cls, negative=None, Z=None, hh=None, mm=None, name=''): negative, Z, hh, mm, name = cls.range_check_timezone( negative, Z, hh, mm, name) if Z is True: #Z -> UTC return UTCOffset(name='UTC', minutes=0) tzhour = int(hh) if mm is not None: tzminute = int(mm) else: tzminute = 0 if negative is True: return UTCOffset(name=name, minutes=-(tzhour * 60 + tzminute)) return UTCOffset(name=name, minutes=tzhour * 60 + tzminute)
def test_repr(self): self.assertEqual(str(UTCOffset(minutes=0)), '+0:00:00 UTC') self.assertEqual(str(UTCOffset(minutes=60)), '+1:00:00 UTC') self.assertEqual(str(UTCOffset(minutes=-60)), '-1:00:00 UTC') self.assertEqual(str(UTCOffset(minutes=12)), '+0:12:00 UTC') self.assertEqual(str(UTCOffset(minutes=-12)), '-0:12:00 UTC') self.assertEqual(str(UTCOffset(minutes=83)), '+1:23:00 UTC') self.assertEqual(str(UTCOffset(minutes=-83)), '-1:23:00 UTC') self.assertEqual(str(UTCOffset(minutes=1440)), '+1 day, 0:00:00 UTC') self.assertEqual(str(UTCOffset(minutes=-1440)), '-1 day, 0:00:00 UTC') self.assertEqual(str(UTCOffset(minutes=2967)), '+2 days, 1:27:00 UTC') self.assertEqual(str(UTCOffset(minutes=-2967)), '-2 days, 1:27:00 UTC')
def test_build_interval(self): #Intervals are contingent on durations, make sure they work #<duration>/<end> testtuples = ( ({ 'end': (('1981', '04', '05', None, None, None, 'date'), ('01', '01', '00', None, 'time'), 'datetime'), 'duration': (None, '1', None, None, None, None, None, 'duration') }, datetime.datetime(year=1981, month=4, day=5, hour=1, minute=1), datetime.datetime(year=1981, month=3, day=5, hour=1, minute=1)), ({ 'end': ('1981', '04', '05', None, None, None, 'date'), 'duration': (None, '1', None, None, None, None, None, 'duration') }, datetime.date(year=1981, month=4, day=5), datetime.date(year=1981, month=3, day=5)), ({ 'end': ('2014', '11', '12', None, None, None, 'date'), 'duration': (None, None, None, None, '1', None, None, 'duration') }, datetime.date(year=2014, month=11, day=12), datetime.datetime(year=2014, month=11, day=11, hour=23)), ({ 'end': ('2014', '11', '12', None, None, None, 'date'), 'duration': (None, None, None, None, '4', '54', '6.5', 'duration') }, datetime.date(year=2014, month=11, day=12), datetime.datetime(year=2014, month=11, day=11, hour=19, minute=5, second=53, microsecond=500000)), ({ 'end': (('2050', '03', '01', None, None, None, 'date'), ('13', '00', '00', (False, True, None, None, 'Z', 'timezone'), 'time'), 'datetime'), 'duration': (None, None, None, None, '10', None, None, 'duration') }, datetime.datetime(year=2050, month=3, day=1, hour=13, tzinfo=UTCOffset(name='UTC', minutes=0)), datetime.datetime(year=2050, month=3, day=1, hour=3, tzinfo=UTCOffset(name='UTC', minutes=0))), #Make sure we truncate, not round #https://bitbucket.org/nielsenb/aniso8601/issues/10/sub-microsecond-precision-in-durations-is #https://bitbucket.org/nielsenb/aniso8601/issues/21/sub-microsecond-precision-is-lost-when ({ 'end': ('1989', '03', '01', None, None, None, 'date'), 'duration': (None, None, '1.9999999999999999', None, None, None, None, 'duration') }, datetime.date(year=1989, month=3, day=1), datetime.datetime(year=1989, month=2, day=15, hour=0, minute=0, second=0, microsecond=1)), ({ 'end': ('1989', '03', '01', None, None, None, 'date'), 'duration': (None, None, None, '1.9999999999999999', None, None, None, 'duration') }, datetime.date(year=1989, month=3, day=1), datetime.datetime(year=1989, month=2, day=27, hour=0, minute=0, second=0, microsecond=1)), ({ 'end': ('2001', '01', '01', None, None, None, 'date'), 'duration': (None, None, None, None, '14.9999999999999999', None, None, 'duration') }, datetime.date(year=2001, month=1, day=1), datetime.datetime(year=2000, month=12, day=31, hour=9, minute=0, second=0, microsecond=1)), ({ 'end': ('2001', '01', '01', None, None, None, 'date'), 'duration': (None, None, None, None, None, '0.00000000999', None, 'duration') }, datetime.date(year=2001, month=1, day=1), datetime.datetime(year=2001, month=1, day=1)), ({ 'end': ('2001', '01', '01', None, None, None, 'date'), 'duration': (None, None, None, None, None, '0.0000000999', None, 'duration') }, datetime.date(year=2001, month=1, day=1), datetime.datetime(year=2000, month=12, day=31, hour=23, minute=59, second=59, microsecond=999995)), ({ 'end': ('2018', '03', '06', None, None, None, 'date'), 'duration': (None, None, None, None, None, None, '0.0000001', 'duration') }, datetime.date(year=2018, month=3, day=6), datetime.datetime(year=2018, month=3, day=6)), ({ 'end': ('2018', '03', '06', None, None, None, 'date'), 'duration': (None, None, None, None, None, None, '2.0000048', 'duration') }, datetime.date(year=2018, month=3, day=6), datetime.datetime(year=2018, month=3, day=5, hour=23, minute=59, second=57, microsecond=999996)), #<start>/<duration> ({ 'start': ('2018', '03', '06', None, None, None, 'date'), 'duration': (None, None, None, None, None, None, '0.0000001', 'duration') }, datetime.date(year=2018, month=3, day=6), datetime.datetime(year=2018, month=3, day=6)), ({ 'start': ('2018', '03', '06', None, None, None, 'date'), 'duration': (None, None, None, None, None, None, '2.0000048', 'duration') }, datetime.date(year=2018, month=3, day=6), datetime.datetime(year=2018, month=3, day=6, hour=0, minute=0, second=2, microsecond=4)), ({ 'start': (('1981', '04', '05', None, None, None, 'date'), ('01', '01', '00', None, 'time'), 'datetime'), 'duration': (None, '1', None, '1', None, '1', None, 'duration') }, datetime.datetime(year=1981, month=4, day=5, hour=1, minute=1), datetime.datetime(year=1981, month=5, day=6, hour=1, minute=2)), ({ 'start': ('1981', '04', '05', None, None, None, 'date'), 'duration': (None, '1', None, '1', None, None, None, 'duration') }, datetime.date(year=1981, month=4, day=5), datetime.date(year=1981, month=5, day=6)), ({ 'start': ('2014', '11', '12', None, None, None, 'date'), 'duration': (None, None, None, None, '1', None, None, 'duration') }, datetime.date(year=2014, month=11, day=12), datetime.datetime(year=2014, month=11, day=12, hour=1, minute=0)), ({ 'start': ('2014', '11', '12', None, None, None, 'date'), 'duration': (None, None, None, None, '4', '54', '6.5', 'duration') }, datetime.date(year=2014, month=11, day=12), datetime.datetime(year=2014, month=11, day=12, hour=4, minute=54, second=6, microsecond=500000)), ({ 'start': ('2014', '11', '12', None, None, None, 'date'), 'duration': (None, None, None, None, '4', '54', '6.5', 'duration') }, datetime.date(year=2014, month=11, day=12), datetime.datetime(year=2014, month=11, day=12, hour=4, minute=54, second=6, microsecond=500000)), ({ 'start': (('2050', '03', '01', None, None, None, 'date'), ('13', '00', '00', (False, True, None, None, 'Z', 'timezone'), 'time'), 'datetime'), 'duration': (None, None, None, None, '10', None, None, 'duration') }, datetime.datetime(year=2050, month=3, day=1, hour=13, tzinfo=UTCOffset(name='UTC', minutes=0)), datetime.datetime(year=2050, month=3, day=1, hour=23, tzinfo=UTCOffset(name='UTC', minutes=0))), #Make sure we truncate, not round #https://bitbucket.org/nielsenb/aniso8601/issues/10/sub-microsecond-precision-in-durations-is #https://bitbucket.org/nielsenb/aniso8601/issues/21/sub-microsecond-precision-is-lost-when ({ 'start': ('1989', '03', '01', None, None, None, 'date'), 'duration': (None, None, '1.9999999999999999', None, None, None, None, 'duration') }, datetime.date(year=1989, month=3, day=1), datetime.datetime(year=1989, month=3, day=14, hour=23, minute=59, second=59, microsecond=999999)), ({ 'start': ('1989', '03', '01', None, None, None, 'date'), 'duration': (None, None, None, '1.9999999999999999', None, None, None, 'duration') }, datetime.date(year=1989, month=3, day=1), datetime.datetime(year=1989, month=3, day=2, hour=23, minute=59, second=59, microsecond=999999)), ({ 'start': ('2001', '01', '01', None, None, None, 'date'), 'duration': (None, None, None, None, '14.9999999999999999', None, None, 'duration') }, datetime.date(year=2001, month=1, day=1), datetime.datetime(year=2001, month=1, day=1, hour=14, minute=59, second=59, microsecond=999999)), ({ 'start': ('2001', '01', '01', None, None, None, 'date'), 'duration': (None, None, None, None, None, '0.00000000999', None, 'duration') }, datetime.date(year=2001, month=1, day=1), datetime.datetime(year=2001, month=1, day=1)), ({ 'start': ('2001', '01', '01', None, None, None, 'date'), 'duration': (None, None, None, None, None, '0.0000000999', None, 'duration') }, datetime.date(year=2001, month=1, day=1), datetime.datetime(year=2001, month=1, day=1, hour=0, minute=0, second=0, microsecond=5)), ({ 'start': ('2018', '03', '06', None, None, None, 'date'), 'duration': (None, None, None, None, None, None, '0.0000001', 'duration') }, datetime.date(year=2018, month=3, day=6), datetime.datetime(year=2018, month=3, day=6)), ({ 'start': ('2018', '03', '06', None, None, None, 'date'), 'duration': (None, None, None, None, None, None, '2.0000048', 'duration') }, datetime.date(year=2018, month=3, day=6), datetime.datetime(year=2018, month=3, day=6, hour=0, minute=0, second=2, microsecond=4)), #<start>/<end> ({ 'start': (('1980', '03', '05', None, None, None, 'date'), ('01', '01', '00', None, 'time'), 'datetime'), 'end': (('1981', '04', '05', None, None, None, 'date'), ('01', '01', '00', None, 'time'), 'datetime') }, datetime.datetime(year=1980, month=3, day=5, hour=1, minute=1), datetime.datetime(year=1981, month=4, day=5, hour=1, minute=1)), ({ 'start': (('1980', '03', '05', None, None, None, 'date'), ('01', '01', '00', None, 'time'), 'datetime'), 'end': ('1981', '04', '05', None, None, None, 'date') }, datetime.datetime(year=1980, month=3, day=5, hour=1, minute=1), datetime.date(year=1981, month=4, day=5)), ({ 'start': ('1980', '03', '05', None, None, None, 'date'), 'end': (('1981', '04', '05', None, None, None, 'date'), ('01', '01', '00', None, 'time'), 'datetime') }, datetime.date(year=1980, month=3, day=5), datetime.datetime(year=1981, month=4, day=5, hour=1, minute=1)), ({ 'start': ('1980', '03', '05', None, None, None, 'date'), 'end': ('1981', '04', '05', None, None, None, 'date') }, datetime.date(year=1980, month=3, day=5), datetime.date(year=1981, month=4, day=5)), ({ 'start': ('1981', '04', '05', None, None, None, 'date'), 'end': ('1980', '03', '05', None, None, None, 'date') }, datetime.date(year=1981, month=4, day=5), datetime.date(year=1980, month=3, day=5)), ({ 'start': (('2050', '03', '01', None, None, None, 'date'), ('13', '00', '00', (False, True, None, None, 'Z', 'timezone'), 'time'), 'datetime'), 'end': (('2050', '05', '11', None, None, None, 'date'), ('15', '30', '00', (False, True, None, None, 'Z', 'timezone'), 'time'), 'datetime') }, datetime.datetime(year=2050, month=3, day=1, hour=13, tzinfo=UTCOffset(name='UTC', minutes=0)), datetime.datetime(year=2050, month=5, day=11, hour=15, minute=30, tzinfo=UTCOffset(name='UTC', minutes=0))), #Make sure we truncate, not round #https://bitbucket.org/nielsenb/aniso8601/issues/10/sub-microsecond-precision-in-durations-is ({ 'start': (('1980', '03', '05', None, None, None, 'date'), ('01', '01', '00.0000001', None, 'time'), 'datetime'), 'end': (('1981', '04', '05', None, None, None, 'date'), ('14', '43', '59.9999997', None, 'time'), 'datetime') }, datetime.datetime(year=1980, month=3, day=5, hour=1, minute=1), datetime.datetime(year=1981, month=4, day=5, hour=14, minute=43, second=59, microsecond=999999)), #Some relativedelta examples #http://dateutil.readthedocs.org/en/latest/examples.html#relativedelta-examples ({ 'start': ('2003', '1', '27', None, None, None, 'date'), 'duration': (None, '1', None, None, None, None, None, 'duration') }, datetime.date(year=2003, month=1, day=27), datetime.date(year=2003, month=2, day=27)), ({ 'start': ('2003', '1', '31', None, None, None, 'date'), 'duration': (None, '1', None, None, None, None, None, 'duration') }, datetime.date(year=2003, month=1, day=31), datetime.date(year=2003, month=2, day=28)), ({ 'start': ('2003', '1', '31', None, None, None, 'date'), 'duration': (None, '2', None, None, None, None, None, 'duration') }, datetime.date(year=2003, month=1, day=31), datetime.date(year=2003, month=3, day=31)), ({ 'start': ('2000', '2', '28', None, None, None, 'date'), 'duration': ('1', None, None, None, None, None, None, 'duration') }, datetime.date(year=2000, month=2, day=28), datetime.date(year=2001, month=2, day=28)), ({ 'start': ('1999', '2', '28', None, None, None, 'date'), 'duration': ('1', None, None, None, None, None, None, 'duration') }, datetime.date(year=1999, month=2, day=28), datetime.date(year=2000, month=2, day=28)), ({ 'start': ('1999', '3', '1', None, None, None, 'date'), 'duration': ('1', None, None, None, None, None, None, 'duration') }, datetime.date(year=1999, month=3, day=1), datetime.date(year=2000, month=3, day=1)), ({ 'end': ('2001', '2', '28', None, None, None, 'date'), 'duration': ('1', None, None, None, None, None, None, 'duration') }, datetime.date(year=2001, month=2, day=28), datetime.date(year=2000, month=2, day=28)), ({ 'end': ('2001', '3', '1', None, None, None, 'date'), 'duration': ('1', None, None, None, None, None, None, 'duration') }, datetime.date(year=2001, month=3, day=1), datetime.date(year=2000, month=3, day=1))) for testtuple in testtuples: result = RelativeTimeBuilder.build_interval(**testtuple[0]) self.assertEqual(result[0], testtuple[1]) self.assertEqual(result[1], testtuple[2])