2020from typing import Dict , Optional , Union
2121
2222from undate .converters .base import BaseDateConverter
23- from undate .date import ONE_DAY , ONE_MONTH_MAX , Date , DatePrecision , Timedelta , UnDelta
23+ from undate .date import ONE_DAY , Date , DatePrecision , Timedelta , UnDelta
2424
2525
2626class Calendar (StrEnum ):
@@ -466,15 +466,19 @@ def duration(self) -> Timedelta | UnDelta:
466466 self .calendar_converter .LEAP_YEAR ,
467467 self .calendar_converter .NON_LEAP_YEAR ,
468468 ]
469- # TODO: what about partially known years like 191X ?
469+ # TODO: handle partially known years like 191X,
470+ # switch to representative years (depends on calendar)
471+ # (to be implemented as part of ambiguous year duration)
470472 else :
471473 # otherwise, get possible durations for all possible months
472474 # for a known year
473475 possible_years = [self .earliest .year ]
474476
475477 # for every possible month and year, get max days for that month,
476478 possible_max_days = set ()
477- # appease mypy, which says month values could be None here
479+ # appease mypy, which says month values could be None here;
480+ # Date object allows optional month, but earliest/latest initialization
481+ # should always be day-precision dates
478482 if self .earliest .month is not None and self .latest .month is not None :
479483 for possible_month in range (self .earliest .month , self .latest .month + 1 ):
480484 for year in possible_years :
@@ -488,22 +492,16 @@ def duration(self) -> Timedelta | UnDelta:
488492 if len (possible_max_days ) > 1 :
489493 return UnDelta (* possible_max_days )
490494
491- # otherwise, calculate timedelta normally
492- max_day = list (possible_max_days )[0 ]
493- latest = Date (self .earliest .year , self .earliest .month , max_day )
495+ # otherwise, calculate timedelta normally based on maximum day
496+ max_day = list (possible_max_days )[0 ]
497+ latest = Date (self .earliest .year , self .earliest .month , max_day )
494498
495- delta = latest - self .earliest + ONE_DAY
496- # month duration can't ever be more than 31 days
497- # (could we ever know if it's smaller?)
499+ return latest - self .earliest + ONE_DAY
498500
499- # if granularity == month but not known month, duration = 31
500- if delta .astype (int ) > 31 :
501- # FIXME: this depends on calendar!
502- return ONE_MONTH_MAX
503- return delta
501+ # TODO: handle year precision + unknown/partially known year
502+ # (will be handled in separate branch)
504503
505504 # otherwise, calculate based on earliest/latest range
506-
507505 # subtract earliest from latest and add a day to count start day
508506 return self .latest - self .earliest + ONE_DAY
509507
0 commit comments