def test_summer_time(self): cmp_at = datetime(year=2021, month=3, day=28, hour=0, minute=59) at, _ = p.parse('2021-03-28 01:59', self.utcnow, 'Europe/Berlin') self.assertEqual(at, cmp_at) cmp_at = datetime(year=2021, month=3, day=28, hour=1) at, _ = p.parse('2021-03-28 03:00', self.utcnow, 'Europe/Berlin') self.assertEqual(at, cmp_at)
def test_week(self): at_cmp = self.utcnow.replace(day=8) at2_cmp = self.utcnow.replace(day=15) at3_cmp = self.utcnow.replace(day=22) at, _ = p.parse('1w', self.utcnow) at2, _ = p.parse('2w', self.utcnow) at3, _ = p.parse('3 w', self.utcnow) self.assertEqual(at, at_cmp) self.assertEqual(at2, at2_cmp) self.assertEqual(at3, at3_cmp)
def test_month_word(self): at_cmp = self.utcnow.replace(month=2) at2_cmp = self.utcnow.replace(month=3) at3_cmp = self.utcnow.replace(month=4) at, _ = p.parse('1 mon', self.utcnow) at2, _ = p.parse('2month', self.utcnow) at3, _ = p.parse('3 months', self.utcnow) self.assertEqual(at, at_cmp) self.assertEqual(at2, at2_cmp) self.assertEqual(at3, at3_cmp)
def test_year_word(self): at_cmp = self.utcnow.replace(year=2022) at2_cmp = self.utcnow.replace(year=2023) at3_cmp = self.utcnow.replace(year=2024) at, _ = p.parse('1ye', self.utcnow) at2, _ = p.parse('2 year', self.utcnow) at3, _ = p.parse('3 years', self.utcnow) self.assertEqual(at, at_cmp) self.assertEqual(at2, at2_cmp) self.assertEqual(at3, at3_cmp)
def test_minute_word(self): at_cmp = self.utcnow.replace(minute=1) at2_cmp = self.utcnow.replace(minute=2) at3_cmp = self.utcnow.replace(minute=3) at, _ = p.parse('1min', self.utcnow) at2, _ = p.parse('2minut', self.utcnow) at3, _ = p.parse('3 minutes', self.utcnow) self.assertEqual(at, at_cmp) self.assertEqual(at2, at2_cmp) self.assertEqual(at3, at3_cmp)
def test_hour_word(self): at_cmp = self.utcnow.replace(hour=1) at2_cmp = self.utcnow.replace(hour=2) at3_cmp = self.utcnow.replace(hour=3) at, _ = p.parse('1ho', self.utcnow) at2, _ = p.parse('2hour', self.utcnow) at3, _ = p.parse('3 hours', self.utcnow) self.assertEqual(at, at_cmp) self.assertEqual(at2, at2_cmp) self.assertEqual(at3, at3_cmp)
def test_day_word(self): at_cmp = self.utcnow.replace(day=2) at2_cmp = self.utcnow.replace(day=3) at3_cmp = self.utcnow.replace(day=4) at, _ = p.parse('1da', self.utcnow) at2, _ = p.parse('2 day', self.utcnow) at3, _ = p.parse('3 days', self.utcnow) self.assertEqual(at, at_cmp) self.assertEqual(at2, at2_cmp) self.assertEqual(at3, at3_cmp)
def test_no_space(self): at_cmp = datetime(year=2021, month=1, day=2, hour=1, minute=1) at, info = p.parse('1y1mo1we 1d', self.utcnow) # undefined behaviour for resulting timespan # no-throw is neough to pass this test pass
def test_timezone_west_summer(self): now = datetime(year=2021, month=6, day=2) at_cmp = datetime(year=2021, month=6, day=30, hour=17) # chicago is 5h diff to utc in summer at, _ = p.parse('eom', now, 'America/Chicago') self.assertEqual(at, at_cmp)
def test_empty_args(self): at_cmp = datetime(year=2021, month=1, day=2, hour=1, minute=1) at, info = p.parse('mo 1mi 1h y 1d ', self.utcnow) self.assertEqual(at, at_cmp) self.assertNotEqual(info, '')
def test_timezone_west(self): now = datetime(year=2021, month=1, day=2) at_cmp = datetime(year=2021, month=1, day=31, hour=18) # chicago is 6h diff to utc in winter at, _ = p.parse('eom', now, 'America/Chicago') self.assertEqual(at, at_cmp)
def test_daylight_saving(self): now = datetime(year=2021, month=6, day=1) at_cmp = datetime(year=2021, month=6, day=30, hour=10) # summertime is 2h ahead of utc (not 1) at, _ = p.parse('eom', now, 'Europe/Berlin') self.assertEqual(at, at_cmp)
def test_timezone_in_last_day(self): # timezones towards the west can be stuck in the last year # therefore the result of absolute units (eod) might be different than expected # chicago is 6h 'in the past', so the eod will be new years eve chicago time # however this date is still in the future for utc # expected date is therefore +5:45h, as the reminder is 15min before midnight at_cmp = datetime(year=2021, month=1, day=1, hour=5, minute=45) at, _ = p.parse('eod', self.utcnow, 'America/Chicago') self.assertEqual(at, at_cmp)
def test_no_american_format(self): cmp_at = datetime(year=2021, month=2, day=3) at, _ = p.parse('3.2.2021', self.utcnow) self.assertEqual(at, cmp_at)
def test_eoy(self): at_cmp = datetime(year=2021, month=12, day=30, hour=23) at, _ = p.parse('2 eoy', self.utcnow, 'Europe/Berlin') self.assertEqual(at, at_cmp)
def test_timezone_convert(self): # timezone in winter is 1h difference cmp_at = datetime(year=2021, month=1, day=1, hour=0) at, _ = p.parse('2021-1-1 01:00', self.utcnow, 'Europe/Berlin') self.assertEqual(at, cmp_at)
def test_past_abs_date_fail(self): cmp_at = self.utcnow at, info = p.parse('2020', self.utcnow) self.assertEqual(at, cmp_at) self.assertNotEqual(info, '')
def test_eod(self): at_cmp = datetime(year=2021, month=1, day=1, hour=22, minute=45) at, _ = p.parse('eod', self.utcnow, 'Europe/Berlin') self.assertEqual(at, at_cmp)
def test_hour_wrap(self): at_cmp = datetime(year=2021, month=1, day=2, hour=2) at, _ = p.parse('26h', self.utcnow) self.assertEqual(at, at_cmp)
def test_week_wrap(self): at_cmp = datetime(year=2021, month=2, day=5) at, _ = p.parse('5w', self.utcnow) self.assertEqual(at, at_cmp)
def test_mixed_arg_order(self): at_cmp = datetime(year=2022, month=2, day=2, hour=1, minute=1) at, _ = p.parse('1mo 1 mi 1 h 1y 1d ', self.utcnow) self.assertEqual(at, at_cmp)
def test_combine_all_relatives(self): at_cmp = datetime(year=2022, month=2, day=2, hour=1, minute=1) at, _ = p.parse('1 y 1 mo 1 d 1h 1mi', self.utcnow) self.assertEqual(at, at_cmp)
def test_week_month(self): at, error = p.parse('1m', self.utcnow) self.assertEqual(at, self.utcnow) self.assertNotEqual(error, '')
def test_negative_intvl(self): at_cmp = self.utcnow at, info = p.parse('1mo -1y', self.utcnow) self.assertEqual(at, at_cmp)
def test_month_wrap(self): at_cmp = datetime(year=2022, month=2, day=1) at, _ = p.parse('13mo', self.utcnow) self.assertEqual(at, at_cmp)
def test_substract(self): at_cmp = datetime(year=2021, month=1, day=31) at, info = p.parse('1mo -1d', self.utcnow) self.assertEqual(at, at_cmp)
def test_day_wrap(self): at_cmp = datetime(year=2021, month=2, day=1) at, _ = p.parse('31d', self.utcnow) self.assertEqual(at, at_cmp)
def test_ignore_mixed_input(self): at_cmp = datetime(year=2021, month=1, day=31, hour=12) at, info = p.parse('5h 1y eom 5mi', self.utcnow) self.assertEqual(at, at_cmp) # all relatives must be ignored self.assertNotEqual(info, '') # warning must be issued
def test_minute_wrap(self): at_cmp = datetime(year=2021, month=1, day=1, hour=1, minute=1) at, _ = p.parse('61mi', self.utcnow) self.assertEqual(at, at_cmp)
def test_eom(self): at_cmp = datetime(year=2021, month=1, day=31, hour=11) at, _ = p.parse('eom', self.utcnow, 'Europe/Berlin') self.assertEqual(at, at_cmp)