Added the _get_year_text() utility to Period.Parser in the Mia core application.

This commit is contained in:
依瑪貓 2020-08-03 21:25:14 +08:00
parent cd6cd51444
commit 7b92ff7567

View File

@ -479,13 +479,7 @@ class Period:
self.invalid_period() self.invalid_period()
return return
self.end = datetime.date(year, 12, 31) self.end = datetime.date(year, 12, 31)
today = timezone.localdate() self.description = self._get_year_text(year)
if year == today.year:
self.description = gettext("This Year")
elif year == today.year - 1:
self.description = gettext("Last Year")
else:
self.description = str(year)
return return
# Until a specific year # Until a specific year
m = re.match("^-([0-9]{4})$", spec) m = re.match("^-([0-9]{4})$", spec)
@ -497,14 +491,8 @@ class Period:
self.invalid_period() self.invalid_period()
return return
self.start = datetime.date(2000, 1, 1) self.start = datetime.date(2000, 1, 1)
today = timezone.localdate() self.description = gettext(
if year == today.year: "Until %s") % self._get_year_text(year)
year_text = gettext("This Year")
elif year == today.year - 1:
year_text = gettext("Last Year")
else:
year_text = str(year)
self.description = gettext("Until %s") % year_text
return return
# All time # All time
if spec == "-": if spec == "-":
@ -651,6 +639,23 @@ class Period:
return gettext("Last Month") return gettext("Last Month")
return "%d/%d" % (year, month) return "%d/%d" % (year, month)
@staticmethod
def _get_year_text(year):
"""Returns the text description of a year.
Args:
year (int): The year.
Returns:
str: The description of the year.
"""
this_year = timezone.localdate().year
if year == this_year:
return gettext("This Year")
if year == this_year - 1:
return gettext("Last Year")
return str(year)
@staticmethod @staticmethod
def _get_date_text(day): def _get_date_text(day):
"""Returns the text description of a day. """Returns the text description of a day.