Coverage for opt/mealie/lib/python3.12/site-packages/mealie/db/models/_model_utils/datetime.py: 84%

25 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 03:04 +0000

1from datetime import UTC, datetime 

2 

3from sqlalchemy.types import DateTime, TypeDecorator 

4 

5 

6def get_utc_now(): 

7 """ 

8 Returns the current time in UTC. 

9 """ 

10 return datetime.now(UTC) 

11 

12 

13def get_utc_today(): 

14 """ 

15 Returns the current date in UTC. 

16 """ 

17 return datetime.now(UTC).date() 

18 

19 

20class NaiveDateTime(TypeDecorator): 

21 """ 

22 Mealie uses naive date times since the app handles timezones explicitly. 

23 All timezones are generated, stored, and retrieved as UTC. 

24 

25 This class strips the timezone from a datetime object when storing it so the database (i.e. postgres) 

26 doesn't do any timezone conversion when storing the datetime, then re-inserts UTC when retrieving it. 

27 """ 

28 

29 impl = DateTime 

30 cache_ok = True 

31 

32 def process_bind_param(self, value: datetime | None, dialect): 

33 if value is None: 

34 return value 

35 

36 try: 

37 if value.tzinfo is not None: 37 ↛ 39line 37 didn't jump to line 39 because the condition on line 37 was always true

38 value = value.astimezone(UTC) 

39 return value.replace(tzinfo=None) 

40 except Exception: 

41 return value 

42 

43 def process_result_value(self, value: datetime | None, dialect): 

44 try: 

45 if value is not None: 

46 value = value.replace(tzinfo=UTC) 

47 except Exception: 

48 pass 

49 

50 return value