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 on_manual_range_apply_clicked(self, button): self.current_range = "manual" # GtkCalendar January is 0, hence the + 1 year, month, day = self.get_widget("start_calendar").get_date() self.start_date = dt.date(year, month + 1, day) year, month, day = self.get_widget("end_calendar").get_date() self.end_date = dt.date(year, month + 1, day) # make sure we always have a valid range if self.end_date < self.start_date: self.start_date, self.end_date = self.end_date, self.start_date self.emit_range("manual", self.start_date, self.end_date)
def locale_first_weekday(): """figure if week starts on monday or sunday""" first_weekday = 6 #by default settle on monday try: process = os.popen("locale first_weekday week-1stday") week_offset, week_start = process.read().split('\n')[:2] process.close() week_start = dt.date(*time.strptime(week_start, "%Y%m%d")[:3]) week_offset = dt.timedelta(int(week_offset) - 1) beginning = week_start + week_offset first_weekday = int(beginning.strftime("%w")) except: logger.warn("WARNING - Failed to get first weekday from locale") return first_weekday
def date(self): """Selected day, as datetime.date.""" year, month, day = self.widget.get_date() # months start at 0 in Gtk.Calendar and at 1 in python date month += 1 return dt.date(year=year, month=month, day=day) if day else None
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)