Skip to content

Commit

Permalink
Merge pull request #580 from ederag/master
Browse files Browse the repository at this point in the history
Fix handling of datetime arguments to from_start_end

Problem:

$ hamster export tsv 2020-01-21 2020-01-22
activity        start time      end time        duration minutes        category        description     tags
Reizen  2020-01-21 08:55        2020-01-21 10:00        65.0    MKIT            Work
Reizen  2020-01-22 07:55        2020-01-22 08:26        31.0    MKIT            Work
Reizen  2020-01-23 18:09        2020-01-23 19:09        60.0    Day-to-Day

Output for the 23rd was included even though user had expected only output up to the 22nd.

This problem is fixed with this patch set.
  • Loading branch information
mwilck authored Mar 17, 2020
2 parents 44c8e1d + 6754f66 commit e6b90fc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
51 changes: 33 additions & 18 deletions src/hamster/lib/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,26 +591,42 @@ def from_start_end(cls, start, end=None):
and to handle either hdays or datetimes.
"""
if isinstance(start, Range):
assert end is None, "range and end are mutually exclusive"
range = start
assert end is None, "end cannot be passed together with a Range"
return cls(start.start, start.end)
elif (start is None) or isinstance(start, datetime):
# This one must come first,
# because inheritance order is datetime < pdt.datetime < pdt.date.
pass
elif isinstance(start, hday):
# Idem, beware of the inheritance order;
# hday < date < pdt.date.
day = start
start = day.start
if end is None:
end = day.end
elif isinstance(start, pdt.date):
# transition from legacy
start = hday.from_pdt(start).start
else:
if isinstance(start, hday):
day = start
start = day.start
if end is None:
end = day.end
elif isinstance(start, pdt.date):
# transition from legacy
start = hday.from_pdt(start).start

if isinstance(end, hday):
end = end.end
elif isinstance(end, pdt.date):
end = hday.from_pdt(end).end
raise TypeError(
"\n First argument should be either Range, None, datetime or hday;"
"\n received {}".format(type(start))
)

range = Range(start, end)
if (end is None) or isinstance(end, datetime):
# same as above
pass
elif isinstance(end, hday):
end = end.end
elif isinstance(end, pdt.date):
# transition from legacy
end = hday.from_pdt(end).end
else:
raise TypeError(
"\n Second argument should be either None, datetime or hday;"
"\n received {}".format(type(start)))

return range
return cls(start, end)

@classmethod
def today(cls):
Expand Down Expand Up @@ -700,4 +716,3 @@ def format(self, fmt="human"):
def total_minutes(self):
"""Return the duration in minutes (float)."""
return self.total_seconds() / 60

10 changes: 10 additions & 0 deletions tests/stuff_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,16 @@ def test_range(self):
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_rounding(self):
dt1 = dt.datetime(2019, 12, 31, hour=13, minute=14, second=10, microsecond=11)
Expand Down

0 comments on commit e6b90fc

Please sign in to comment.