@@ -550,17 +550,14 @@ def __init__(
550550 ua = None ,
551551 ** kwargs ,
552552 ):
553- for param in ("date" , "lower" , "upper" ):
554- if param in kwargs :
555- self .__init__ (** kwargs [param ])
556- return
557- self .year = year # Year is required, but sometimes passed in as a 'date' dict.
558- self .month = month
559- self .day = day
560- self .significant_digits = (
561- int (significant_digits ) if significant_digits else None
553+ super ().__init__ (
554+ year = year ,
555+ month = month ,
556+ day = day ,
557+ significant_digits = significant_digits ,
558+ ** kwargs ,
562559 )
563- self .ua = ua if ua else None
560+ self .ua = ua
564561 self .negative = self .year .startswith ("-" )
565562
566563 def __str__ (self ):
@@ -576,16 +573,8 @@ def _get_fuzzy_padding(self, lean):
576573 padding = relativedelta ()
577574
578575 if self .year :
579- year_no_symbol = self .year .lstrip ("-" )
580- years_padding = self ._calculate_years_padding (multiplier , year_no_symbol )
581- # Reverse the padding for negative years and earliest calculations
582- # if self.negative:
583- # years_padding = -years_padding if lean == EARLIEST else years_padding
584- # else:
585- # years_padding = years_padding if lean == EARLIEST else -years_padding
586-
576+ years_padding = self ._years_padding (multiplier )
587577 padding += years_padding
588-
589578 if self .month :
590579 padding += relativedelta (
591580 months = int (multiplier * appsettings .PADDING_MONTH_PRECISION .months )
@@ -594,151 +583,53 @@ def _get_fuzzy_padding(self, lean):
594583 padding += relativedelta (
595584 days = int (multiplier * appsettings .PADDING_DAY_PRECISION .days )
596585 )
597-
598586 return padding
599587
600- def _calculate_years_padding (self , multiplier , year_no_symbol ):
601- if self .precision == PRECISION_MILLENIUM :
602- return relativedelta (
603- years = int (multiplier * appsettings .PADDING_MILLENNIUM_PRECISION .years )
604- )
605- elif self .precision == PRECISION_CENTURY :
606- return relativedelta (
607- years = int (multiplier * appsettings .PADDING_CENTURY_PRECISION .years )
608- )
609- elif self .precision == PRECISION_DECADE :
610- return relativedelta (
611- years = int (multiplier * appsettings .PADDING_DECADE_PRECISION .years )
612- )
613- else :
614- return relativedelta (
615- years = int (multiplier * appsettings .PADDING_YEAR_PRECISION .years )
616- )
588+ def _years_padding (self , multiplier ):
589+ """Calculate year padding based on the precision."""
590+ precision_settings = {
591+ PRECISION_MILLENIUM : appsettings .PADDING_MILLENNIUM_PRECISION .years ,
592+ PRECISION_CENTURY : appsettings .PADDING_CENTURY_PRECISION .years ,
593+ PRECISION_DECADE : appsettings .PADDING_DECADE_PRECISION .years ,
594+ PRECISION_YEAR : appsettings .PADDING_YEAR_PRECISION .years ,
595+ }
596+ years = precision_settings .get (self .precision , 0 )
597+ return relativedelta (years = int (multiplier * years ))
617598
618599 def lower_fuzzy (self ):
619- time_empty_time_tuple = tuple (TIME_EMPTY_TIME )
620- time_empty_extras_tuple = tuple (TIME_EMPTY_EXTRAS )
621600 strict_val = (
622601 self .lower_strict ()
623602 ) # negative handled in the lower_strict() override
624-
625- if self .negative :
626- adjusted = apply_delta (sub , strict_val , self ._get_fuzzy_padding (LATEST ))
627- if (
628- self .precision == PRECISION_YEAR
629- or self .precision == PRECISION_DECADE
630- or self .precision == PRECISION_CENTURY
631- or self .precision == PRECISION_MILLENIUM
632- ):
633- adjusted = struct_time (
634- (adjusted .tm_year , 1 , 1 )
635- + time_empty_time_tuple
636- + time_empty_extras_tuple
637- )
638- elif self .precision == PRECISION_MONTH :
639- adjusted = struct_time (
640- (adjusted .tm_year , adjusted .tm_mon , 1 )
641- + time_empty_time_tuple
642- + time_empty_extras_tuple
643- )
644- else :
645- adjusted = apply_delta (sub , strict_val , self ._get_fuzzy_padding (EARLIEST ))
646- if (
647- self .precision == PRECISION_YEAR
648- or self .precision == PRECISION_DECADE
649- or self .precision == PRECISION_CENTURY
650- or self .precision == PRECISION_MILLENIUM
651- ):
652- adjusted = struct_time (
653- (adjusted .tm_year , 1 , 1 )
654- + time_empty_time_tuple
655- + time_empty_extras_tuple
656- )
657- elif self .precision == PRECISION_MONTH :
658- days_in_month = calendar .monthrange (adjusted .tm_year , adjusted .tm_mon )[
659- 1
660- ]
661- adjusted = struct_time (
662- (adjusted .tm_year , adjusted .tm_mon , days_in_month )
663- + time_empty_time_tuple
664- + time_empty_extras_tuple
665- )
666-
603+ adjusted = apply_delta (sub , strict_val , self ._get_fuzzy_padding (EARLIEST ))
667604 return adjusted
668605
669606 def upper_fuzzy (self ):
670- time_empty_time_tuple = tuple (TIME_EMPTY_TIME )
671- time_empty_extras_tuple = tuple (TIME_EMPTY_EXTRAS )
672607 strict_val = (
673608 self .upper_strict ()
674609 ) # negative handled in the upper_strict() override
675610
676- if self .negative :
677- adjusted = apply_delta (add , strict_val , self ._get_fuzzy_padding (LATEST ))
678- if (
679- self .precision == PRECISION_YEAR
680- or self .precision == PRECISION_DECADE
681- or self .precision == PRECISION_CENTURY
682- or self .precision == PRECISION_MILLENIUM
683- ):
684- adjusted = struct_time (
685- (adjusted .tm_year , 12 , 31 )
686- + time_empty_time_tuple
687- + time_empty_extras_tuple
688- )
689- elif self .precision == PRECISION_MONTH :
690- days_in_month = calendar .monthrange (adjusted .tm_year , adjusted .tm_mon )[
691- 1
692- ]
693- adjusted = struct_time (
694- (adjusted .tm_year , adjusted .tm_mon , days_in_month )
695- + time_empty_time_tuple
696- + time_empty_extras_tuple
697- )
698- else :
699- adjusted = apply_delta (add , strict_val , self ._get_fuzzy_padding (LATEST ))
700- if (
701- self .precision == PRECISION_YEAR
702- or self .precision == PRECISION_DECADE
703- or self .precision == PRECISION_CENTURY
704- or self .precision == PRECISION_MILLENIUM
705- ):
706- adjusted = struct_time (
707- (adjusted .tm_year , 12 , 31 )
708- + time_empty_time_tuple
709- + time_empty_extras_tuple
710- )
711- elif self .precision == PRECISION_MONTH :
712- adjusted = struct_time (
713- (adjusted .tm_year , adjusted .tm_mon , 1 )
714- + time_empty_time_tuple
715- + time_empty_extras_tuple
716- )
717-
611+ adjusted = apply_delta (add , strict_val , self ._get_fuzzy_padding (LATEST ))
718612 return adjusted
719613
720614 def lower_strict (self ):
721615 if self .negative :
722616 strict_val = self ._strict_date (
723617 lean = LATEST
724618 ) # gets the year right, but need to adjust day and month
725- if (
726- self . precision == PRECISION_YEAR
727- or self . precision == PRECISION_DECADE
728- or self . precision == PRECISION_CENTURY
729- or self . precision == PRECISION_MILLENIUM
619+ if self . precision in (
620+ PRECISION_YEAR ,
621+ PRECISION_DECADE ,
622+ PRECISION_CENTURY ,
623+ PRECISION_MILLENIUM ,
730624 ):
731625 return struct_time (
732626 (strict_val .tm_year , 1 , 1 )
733627 + tuple (TIME_EMPTY_TIME )
734628 + tuple (TIME_EMPTY_EXTRAS )
735629 )
736630 elif self .precision == PRECISION_MONTH :
737- days_in_month = calendar .monthrange (
738- strict_val .tm_year , strict_val .tm_mon
739- )[1 ]
740631 return struct_time (
741- (strict_val .tm_year , strict_val .tm_mon , days_in_month )
632+ (strict_val .tm_year , strict_val .tm_mon , 1 )
742633 + tuple (TIME_EMPTY_TIME )
743634 + tuple (TIME_EMPTY_EXTRAS )
744635 )
@@ -750,11 +641,11 @@ def lower_strict(self):
750641 def upper_strict (self ):
751642 if self .negative :
752643 strict_val = self ._strict_date (lean = EARLIEST )
753- if (
754- self . precision == PRECISION_YEAR
755- or self . precision == PRECISION_DECADE
756- or self . precision == PRECISION_CENTURY
757- or self . precision == PRECISION_MILLENIUM
644+ if self . precision in (
645+ PRECISION_YEAR ,
646+ PRECISION_DECADE ,
647+ PRECISION_CENTURY ,
648+ PRECISION_MILLENIUM ,
758649 ):
759650 return struct_time (
760651 (strict_val .tm_year , 12 , 31 )
0 commit comments