Added the __get_since_spec and __get_until_spec methods to simplify the __get_spec method in the PeriodSpecification utility.

This commit is contained in:
依瑪貓 2023-03-08 19:26:49 +08:00
parent 0ef6409f75
commit 2515c1ea1f

View File

@ -173,34 +173,43 @@ class PeriodSpecification:
:return: The period specification. :return: The period specification.
""" """
if self.__start is None and self.__end is None:
return "-"
if self.__end is None:
return self.__get_since_spec()
if self.__start is None: if self.__start is None:
if self.__end is None: return self.__get_until_spec()
return "-" try:
else: return self.__get_year_spec()
if self.__end.day != _month_end(self.__end).day: except ValueError:
return "-%04d-%02d-%02d" % ( pass
self.__end.year, self.__end.month, self.__end.day) try:
if self.__end.month != 12: return self.__get_month_spec()
return "-%04d-%02d" % (self.__end.year, self.__end.month) except ValueError:
return "-%04d" % self.__end.year pass
else: return self.__get_day_spec()
if self.__end is None:
if self.__start.day != 1: def __get_since_spec(self) -> str:
return "%04d-%02d-%02d-" % ( """Returns the period specification without the end day.
self.__start.year, self.__start.month, self.__start.day)
if self.__start.month != 1: :return: The period specification without the end day
return "%04d-%02d-" % (self.__start.year, self.__start.month) """
return "%04d-" % self.__start.year if self.__start.month == 1 and self.__start.day == 1:
else: return self.__start.strftime("%Y-")
try: if self.__start.day == 1:
return self.__get_year_spec() return self.__start.strftime("%Y-%m-")
except ValueError: return self.__start.strftime("%Y-%m-%d-")
pass
try: def __get_until_spec(self) -> str:
return self.__get_month_spec() """Returns the period specification without the start day.
except ValueError:
pass :return: The period specification without the start day
return self.__get_day_spec() """
if self.__end.month == 12 and self.__end.day == 31:
return self.__end.strftime("-%Y")
if (self.__end + datetime.timedelta(days=1)).day == 1:
return self.__end.strftime("-%Y-%m")
return self.__end.strftime("-%Y-%m-%d")
def __get_year_spec(self) -> str: def __get_year_spec(self) -> str:
"""Returns the period specification as a year range. """Returns the period specification as a year range.