Coverage for arbejdstimer/__init__.py: 97.56%

29 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 15:36:40 +00:00

1import calendar 

2import datetime as dti 

3 

4# [[[fill git_describe()]]] 

5__version__ = '2022.10.23+parent.712c72f1' 

6# [[[end]]] (checksum: fb8505be2ec35cc533109c55fca3871f) 

7__version_info__ = tuple( 

8 e if '-' not in e else e.split('-')[0] for part in __version__.split('+') for e in part.split('.') if e != 'parent' 

9) 

10__all__: list[str] = [] 

11 

12 

13def day_count(year: int) -> int: 

14 """Number of days in year.""" 

15 return 365 + calendar.isleap(year) 

16 

17 

18def day_count_from_date(date: dti.date) -> int: 

19 """Number of days in year of date-""" 

20 return day_count(date.year) 

21 

22 

23def date_from_fractional_year(date: float) -> dti.date: 

24 """Going back ...""" 

25 y_int = int(date) 

26 rest_y_float = date - y_int 

27 rest_d_float = round(rest_y_float * day_count(y_int), 1) 

28 if rest_d_float: 

29 day_counts = [c for _, c in (calendar.monthrange(y_int, m) for m in range(1, 12 + 1))] 

30 day_cum = [day_counts[0]] + [0] * 11 

31 for m, c in enumerate(day_counts[1:], start=2): 

32 day_cum[m - 1] = day_cum[m - 2] + c 

33 m_int = 1 # Well, not really, but, ... happy linter 

34 for m, c in enumerate(day_cum, start=1): 34 ↛ 40line 34 didn't jump to line 40, because the loop on line 34 didn't complete

35 if c < rest_d_float: 

36 continue 

37 m_int = m 

38 break 

39 

40 d_int = int(rest_d_float - day_cum[m_int - 2]) 

41 else: 

42 m_int, d_int = 1, 1 

43 return dti.date(y_int, m_int, d_int) 

44 

45 

46def fractional_year_from_date(date: dti.date) -> float: 

47 """... and forth.""" 

48 return date.year + ((date - dti.date(date.year, 1, 1)).days / day_count(date.year))