Skip to content

Commit b7be886

Browse files
committed
Use easy duration calculation when possible; improve test code coverage
1 parent d47ff68 commit b7be886

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/undate/undate.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,15 @@ def duration(self) -> Timedelta | UnDelta:
573573
if self.precision == DatePrecision.DAY:
574574
return ONE_DAY
575575

576-
possible_max_days = set()
576+
# if year is known and no values are partially known,
577+
# we can calculate a time delta based on earliest + latest
578+
if self.known_year and not any(
579+
[self.is_partially_known(part) for part in ["year", "month", "day"]]
580+
):
581+
# subtract earliest from latest and add a day to include start day in the count
582+
return self.latest - self.earliest + ONE_DAY
577583

584+
possible_max_days = set()
578585
# if precision is month and year is unknown,
579586
# calculate month duration within a single year (not min/max)
580587
if self.precision == DatePrecision.MONTH:
@@ -600,13 +607,9 @@ def duration(self) -> Timedelta | UnDelta:
600607

601608
# if there is more than one possible value for number of days
602609
# due to range including lear year / non-leap year, return an uncertain delta
603-
if possible_max_days:
604-
if len(possible_max_days) > 1:
605-
return UnDelta(*possible_max_days)
606-
return Timedelta(possible_max_days.pop())
607-
608-
# otherwise, subtract earliest from latest and add a day to include start day in the count
609-
return self.latest - self.earliest + ONE_DAY
610+
if len(possible_max_days) > 1:
611+
return UnDelta(*possible_max_days)
612+
return Timedelta(possible_max_days.pop())
610613

611614
def _missing_digit_minmax(
612615
self, value: str, min_val: int, max_val: int

tests/test_undate.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,12 @@ def test_year_property(self):
189189
# unset year
190190
assert Undate(month=12, day=31).year == "XXXX"
191191

192-
# NOTE: no longer supported to inistalize undate with no date information
192+
# NOTE: no longer supported to initialize undate with no date information
193193
# force method to hit conditional for date precision
194-
# some_century = Undate()
195-
# some_century.precision = DatePrecision.CENTURY
196-
# assert some_century.year is None
194+
some_century = Undate(year="X")
195+
some_century.initial_values["year"] = None
196+
some_century.precision = DatePrecision.CENTURY
197+
assert some_century.year is None
197198

198199
def test_month_property(self):
199200
# one, two digit month
@@ -324,6 +325,9 @@ def test_gt_lt_unknown_years(self):
324325
assert not some_january >= year100
325326

326327
def test_lt_notimplemented(self):
328+
# unsupported type should bail out and return NotImplemented
329+
assert Undate(2022).__lt__("foo") == NotImplemented
330+
327331
# how to compare mixed precision where dates overlap?
328332
# if the second date falls *within* earliest/latest,
329333
# then it is not clearly less; not implemented?
@@ -356,6 +360,9 @@ def test_lt_notimplemented(self):
356360
def test_contains(self, date1, date2):
357361
assert date1 in date2
358362

363+
# unsupported type should bail out and return NotImplemented
364+
assert Undate(2022).__contains__("foo") == NotImplemented
365+
359366
testdata_not_contains = [
360367
# dates not in range
361368
(Undate(1980), Undate(2020)),

0 commit comments

Comments
 (0)