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