weekday.py 1.3 KB

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