mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-09-23 03:50:09 +00:00
46d80d56a5
Django uses `str(date)` for backends other than PostgreSQL, so do not break "YYYY-MM-DD" formatting, just in case.
39 lines
887 B
Python
39 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")
|