def test_different_days(self): vm = time(2, 0) self.assertTrue( different_days(datetime(2005, 2, 3, 1, 15), datetime(2005, 2, 3, 2, 15), vm)) self.assertFalse( different_days(datetime(2005, 2, 3, 11, 15), datetime(2005, 2, 3, 12, 15), vm))
def append(self, entry): """Append a new entry to the time log.""" now = datetime.datetime.now().replace(second=0, microsecond=0) last = self.window.last_time() if last and different_days(now, last, self.virtual_midnight): # next day: reset self.window self.reread() self._append(now, entry)
def count_days(self): """Count days that have entries.""" count = 0 last = None for start, stop, duration, entry in self.all_entries(): if last is None or different_days(last, start, self.virtual_midnight): last = start count += 1 return count
def all_entries(self): """Iterate over all entries. Yields (start, stop, duration, entry) tuples. The first entry has a duration of 0. """ stop = None for item in self.items: start = stop stop = item[0] entry = item[1] if start is None or different_days(start, stop, self.virtual_midnight): start = stop duration = stop - start yield start, stop, duration, entry
def last_entry(self): """Return the last entry (or None if there are no events). It is always true that self.last_entry() == list(self.all_entries())[-1] """ if not self.items: return None stop = self.items[-1][0] entry = self.items[-1][1] if len(self.items) == 1: start = stop else: start = self.items[-2][0] if different_days(start, stop, self.virtual_midnight): start = stop duration = stop - start return start, stop, duration, entry