Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/icalendar/tools.py: 70%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

20 statements  

1"""Utility functions for icalendar.""" 

2 

3from __future__ import annotations 

4 

5from datetime import date, datetime, tzinfo 

6 

7 

8def is_date(dt: date) -> bool: 

9 """Whether this is a date and not a datetime.""" 

10 return isinstance(dt, date) and not isinstance(dt, datetime) 

11 

12 

13def is_datetime(dt: date) -> bool: 

14 """Whether this is a date and not a datetime.""" 

15 return isinstance(dt, datetime) 

16 

17 

18def to_datetime(dt: date) -> datetime: 

19 """Make sure we have a datetime, not a date.""" 

20 if is_date(dt): 

21 return datetime(dt.year, dt.month, dt.day) # noqa: DTZ001 

22 return dt 

23 

24 

25def is_pytz(tz: tzinfo): 

26 """Whether the timezone requires localize() and normalize().""" 

27 return hasattr(tz, "localize") 

28 

29 

30def is_pytz_dt(dt: date): 

31 """Whether the time requires localize() and normalize().""" 

32 return is_datetime(dt) and is_pytz(dt.tzinfo) 

33 

34 

35def normalize_pytz(dt: date): 

36 """We have to normalize the time after a calculation if we use pytz. 

37 

38 pytz requires this function to be used in order to correctly calculate the 

39 timezone's offset after calculations. 

40 """ 

41 if is_pytz_dt(dt): 

42 return dt.tzinfo.normalize(dt) 

43 return dt 

44 

45 

46__all__ = [ 

47 "is_date", 

48 "is_datetime", 

49 "is_pytz", 

50 "is_pytz_dt", 

51 "normalize_pytz", 

52 "to_datetime", 

53]