Coverage for o2/models/days.py: 86%

29 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-05-16 11:18 +0000

1from datetime import datetime 

2from enum import Enum 

3 

4 

5class DAY(str, Enum): 

6 """The days of the week.""" 

7 

8 MONDAY = "MONDAY" 

9 TUESDAY = "TUESDAY" 

10 WEDNESDAY = "WEDNESDAY" 

11 THURSDAY = "THURSDAY" 

12 FRIDAY = "FRIDAY" 

13 SATURDAY = "SATURDAY" 

14 SUNDAY = "SUNDAY" 

15 

16 def next_day(self) -> "DAY": 

17 """Get the next day.""" 

18 index = DAYS.index(self) 

19 return DAYS[(index + 1) % len(DAYS)] 

20 

21 def previous_day(self) -> "DAY": 

22 """Get the previous day.""" 

23 index = DAYS.index(self) 

24 return DAYS[(index - 1) % len(DAYS)] 

25 

26 @staticmethod 

27 def from_weekday(weekday: int) -> "DAY": 

28 """Get the day from the weekday (e.g from datetime.weekday()).""" 

29 return DAYS[weekday] 

30 

31 @staticmethod 

32 def from_date(date: datetime) -> "DAY": 

33 """Get the day from the date.""" 

34 return DAY.from_weekday(date.weekday()) 

35 

36 

37DAYS = [ 

38 DAY.MONDAY, 

39 DAY.TUESDAY, 

40 DAY.WEDNESDAY, 

41 DAY.THURSDAY, 

42 DAY.FRIDAY, 

43 DAY.SATURDAY, 

44 DAY.SUNDAY, 

45] 

46 

47 

48def day_range(start: DAY, end: DAY) -> list[DAY]: 

49 """Get the range of days between two days.""" 

50 start_idx = DAYS.index(start) 

51 end_idx = DAYS.index(end) 

52 return DAYS[start_idx : end_idx + 1] 

53 

54 

55def is_day_in_range(day: DAY, start: DAY, end: DAY) -> bool: 

56 """Check if a day is in a range of days.""" 

57 return DAYS.index(start) <= DAYS.index(day) <= DAYS.index(end)