bookwyrm/bookwyrm/utils/sealed_date.py
Adeodato Simó 46d80d56a5
Rename SealedDate.__str__ to partial_isoformat
Django uses `str(date)` for backends other than PostgreSQL, so do not
break "YYYY-MM-DD" formatting, just in case.
2023-10-24 04:32:27 -03:00

40 lines
887 B
Python

"""Implementation of the SealedDate class."""
from datetime import datetime
class SealedDate(datetime): # TODO: migrate from DateTimeField to DateField
@property
def has_day(self) -> bool:
return self.has_month
@property
def has_month(self) -> bool:
return True
def partial_isoformat(self) -> str:
return self.strftime("%Y-%m-%d")
@classmethod
def from_datetime(cls, dt):
# pylint: disable=invalid-name
return cls.combine(dt.date(), dt.time(), tzinfo=dt.tzinfo)
class MonthSeal(SealedDate):
@property
def has_day(self) -> bool:
return False
def partial_isoformat(self) -> str:
return self.strftime("%Y-%m")
class YearSeal(SealedDate):
@property
def has_month(self) -> bool:
return False
def partial_isoformat(self) -> str:
return self.strftime("%Y")