def test_datetime_hday(self): date_time = dt.datetime(2018, 8, 13, 23, 10) # 2018-08-13 23:10 expected = dt.date(2018, 8, 13) self.assertEqual(date_time.hday(), expected) date_time = dt.datetime(2018, 8, 14, 0, 10) # 2018-08-14 0:10 expected = dt.date(2018, 8, 13) self.assertEqual(date_time.hday(), expected) today = dt.hday.today() self.assertEqual(type(today), dt.hday)
def test_datetime_from_day_time(self): day = dt.date(2018, 8, 13) time = dt.time(23, 10) expected = dt.datetime(2018, 8, 13, 23, 10) # 2018-08-13 23:10 self.assertEqual(dt.datetime.from_day_time(day, time), expected) day = dt.date(2018, 8, 13) time = dt.time(0, 10) expected = dt.datetime(2018, 8, 14, 0, 10) # 2018-08-14 00:10 self.assertEqual(dt.datetime.from_day_time(day, time), expected)
def test_range(self): t1 = dt.datetime(2020, 1, 15, 13, 30) t2 = dt.datetime(2020, 1, 15, 15, 30) range = dt.Range(t1, t2) fact = Fact(range=range) self.assertEqual(fact.range.start, t1) self.assertEqual(fact.range.end, t2) fact = Fact(start=t1, end=t2) self.assertEqual(fact.range.start, t1) self.assertEqual(fact.range.end, t2) # backward compatibility (before v3.0) fact = Fact(start_time=t1, end_time=t2) self.assertEqual(fact.range.start, t1) self.assertEqual(fact.range.end, t2)
def test_rounding(self): dt1 = dt.datetime(2019, 12, 31, hour=13, minute=14, second=10, microsecond=11) self.assertEqual(dt1.second, 0) self.assertEqual(dt1.microsecond, 0) self.assertEqual(str(dt1), "2019-12-31 13:14")
def test_range(self): day = dt.hday(2020, 2, 2) time = dt.time(21, 20) base = dt.datetime.from_day_time(day, time) range = dt.Range(base, base + dt.timedelta(minutes=30)) range_str = range.format(default_day=day) self.assertEqual(range_str, "21:20 - 21:50") range = dt.Range(None, base) range_str = range.format(default_day=day) self.assertEqual(range_str, "-- - 21:20") # issue #576 start = dt.datetime(2020, 3, 8, 17, 7) end = dt.datetime(2020, 3, 8, 18, 6) range = dt.Range.from_start_end(start, end) self.assertEqual(range.start, start) self.assertEqual(range.end, end) # check passthrough range2 = dt.Range.from_start_end(range) self.assertEqual(range2.start, range.start) self.assertEqual(range2.end, range.end)
def test_type_stability(self): dt1 = dt.datetime(2020, 1, 10, hour=13, minute=30) dt2 = dt.datetime(2020, 1, 10, hour=13, minute=40) delta = dt2 - dt1 self.assertEqual(type(delta), dt.timedelta) _sum = dt1 + delta self.assertEqual(_sum, dt.datetime(2020, 1, 10, hour=13, minute=40)) self.assertEqual(type(_sum), dt.datetime) _sub = dt1 - delta self.assertEqual(_sub, dt.datetime(2020, 1, 10, hour=13, minute=20)) self.assertEqual(type(_sub), dt.datetime) opposite = -delta self.assertEqual(opposite, dt.timedelta(minutes=-10)) self.assertEqual(type(opposite), dt.timedelta) _sum = delta + delta self.assertEqual(_sum, dt.timedelta(minutes=20)) self.assertEqual(type(_sum), dt.timedelta) _sub = delta - delta self.assertEqual(_sub, dt.timedelta()) self.assertEqual(type(_sub), dt.timedelta)
def test_parse_datetime_range(self): # only match clean s = "10.00@cat" (start, end), rest = dt.Range.parse(s, position="head") self.assertEqual(start, None) self.assertEqual(end, None) s = "12:02" (start, end), rest = dt.Range.parse(s) self.assertEqual(start.strftime("%H:%M"), "12:02") self.assertEqual(end, None) s = "12:03 13:04" (start, end), rest = dt.Range.parse(s) self.assertEqual(start.strftime("%H:%M"), "12:03") self.assertEqual(end.strftime("%H:%M"), "13:04") s = "12:35 activity" (start, end), rest = dt.Range.parse(s, position="head") self.assertEqual(start.strftime("%H:%M"), "12:35") self.assertEqual(end, None) s = "2019-12-01 12:33 activity" (start, end), rest = dt.Range.parse(s, position="head") self.assertEqual(start.strftime("%Y-%m-%d %H:%M"), "2019-12-01 12:33") self.assertEqual(end, None) ref = dt.datetime(2019, 11, 29, 13, 55) # 2019-11-29 13:55 s = "-25 activity" (start, end), rest = dt.Range.parse(s, position="head", ref=ref) self.assertEqual(start.strftime("%Y-%m-%d %H:%M"), "2019-11-29 13:30") self.assertEqual(end, None) s = "+25 activity" (start, end), rest = dt.Range.parse(s, position="head", ref=ref) self.assertEqual(start.strftime("%Y-%m-%d %H:%M"), "2019-11-29 14:20") self.assertEqual(end, None) s = "-55 -25 activity" (start, end), rest = dt.Range.parse(s, position="head", ref=ref) self.assertEqual(start.strftime("%Y-%m-%d %H:%M"), "2019-11-29 13:00") self.assertEqual(end.strftime("%Y-%m-%d %H:%M"), "2019-11-29 13:30") s = "+25 +55 activity" (start, end), rest = dt.Range.parse(s, position="head", ref=ref) self.assertEqual(start.strftime("%Y-%m-%d %H:%M"), "2019-11-29 14:20") self.assertEqual(end.strftime("%Y-%m-%d %H:%M"), "2019-11-29 14:50") s = "-55 -120 activity" (start, end), rest = dt.Range.parse(s, position="head", ref=ref) self.assertEqual(start.strftime("%Y-%m-%d %H:%M"), "2019-11-29 13:00") self.assertEqual(end.strftime("%Y-%m-%d %H:%M"), "2019-11-29 11:55") s = "-50 20 activity" (start, end), rest = dt.Range.parse(s, position="head", ref=ref) self.assertEqual(start.strftime("%Y-%m-%d %H:%M"), "2019-11-29 13:05") self.assertEqual(end.strftime("%Y-%m-%d %H:%M"), "2019-11-29 13:25") s = "2019-12-05" # single hamster day (start, end), rest = dt.Range.parse(s, ref=ref) just_before = start - dt.timedelta(seconds=1) just_after = end + dt.timedelta(seconds=1) self.assertEqual(just_before.hday(), pdt.date(2019, 12, 4)) self.assertEqual(just_after.hday(), pdt.date(2019, 12, 6)) s = "2019-12-05 2019-12-07" # hamster days range (start, end), rest = dt.Range.parse(s, ref=ref) just_before = start - dt.timedelta(seconds=1) just_after = end + dt.timedelta(seconds=1) self.assertEqual(just_before.hday(), dt.date(2019, 12, 4)) self.assertEqual(just_after.hday(), dt.date(2019, 12, 8)) s = "14:30 - --" (start, end), rest = dt.Range.parse(s, ref=ref) self.assertEqual(start.strftime("%H:%M"), "14:30") self.assertEqual(end, None)