Skip to content

Commit

Permalink
fix from_start_end: handle datetime first
Browse files Browse the repository at this point in the history
  • Loading branch information
ederag committed Mar 13, 2020
1 parent 6fa4c20 commit 93ab43f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
48 changes: 28 additions & 20 deletions src/hamster/lib/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,27 +590,35 @@ def from_start_end(cls, start, end=None):
Convenient to ease backward compatibility,
and to handle either hdays or datetimes.
"""

if isinstance(start, Range):
assert end is None, "range and end are mutually exclusive"
range = 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

range = Range(start, end)

return range
assert end is None, "end cannot be passed together with a Range"
return cls(start.start, start.end)
elif isinstance(start, datetime):
# This one must come first,
# because inheritance order is datetime < date < pdt.date.

This comment has been minimized.

Copy link
@mwilck

mwilck Mar 13, 2020

Contributor

Nit wrt the comment: AFAICS, datetime inherits only pdt.datetime, not 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

This comment has been minimized.

Copy link
@mwilck

mwilck Mar 13, 2020

Contributor

can start be an instance of pdt.datetime() ? If yes, where is that case handled?

if 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

return cls(start, end)

@classmethod
def today(cls):
Expand Down
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 93ab43f

Please sign in to comment.