weekday.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from functools import total_ordering
  2. from .. import i18n
  3. from ..utils import str_coercible
  4. @str_coercible
  5. @total_ordering
  6. class WeekDay:
  7. NUM_WEEK_DAYS = 7
  8. def __init__(self, index):
  9. if not (0 <= index < self.NUM_WEEK_DAYS):
  10. raise ValueError(
  11. "index must be between 0 and %d" % self.NUM_WEEK_DAYS
  12. )
  13. self.index = index
  14. def __eq__(self, other):
  15. if isinstance(other, WeekDay):
  16. return self.index == other.index
  17. else:
  18. return NotImplemented
  19. def __hash__(self):
  20. return hash(self.index)
  21. def __lt__(self, other):
  22. return self.position < other.position
  23. def __repr__(self):
  24. return f'{self.__class__.__name__}({self.index!r})'
  25. def __unicode__(self):
  26. return self.name
  27. def get_name(self, width='wide', context='format'):
  28. names = i18n.babel.dates.get_day_names(
  29. width,
  30. context,
  31. i18n.get_locale()
  32. )
  33. return names[self.index]
  34. @property
  35. def name(self):
  36. return self.get_name()
  37. @property
  38. def position(self):
  39. return (
  40. self.index -
  41. i18n.get_locale().first_week_day
  42. ) % self.NUM_WEEK_DAYS