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.10.6, created at 2025-11-25 15:48 +0000

1from datetime import UTC, datetime 1a

2 

3from sqlalchemy.types import DateTime, TypeDecorator 1a

4 

5 

6def get_utc_now(): 1a

7 """ 

8 Returns the current time in UTC. 

9 """ 

10 return datetime.now(UTC) 1acdefghijklmnopqrstuvb

11 

12 

13def get_utc_today(): 1a

14 """ 

15 Returns the current date in UTC. 

16 """ 

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

18 

19 

20class NaiveDateTime(TypeDecorator): 1a

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 1a

30 cache_ok = True 1a

31 

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

33 if value is None: 1acdefghijklmnopqrstuvb

34 return value 1acb

35 

36 try: 1acdefghijklmnopqrstuvb

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

38 value = value.astimezone(UTC) 1acdefghijklmnopqrstuvb

39 return value.replace(tzinfo=None) 1acdefghijklmnopqrstuvb

40 except Exception: 

41 return value 

42 

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

44 try: 1acwxyzdefgABCDhijklmnopqrstuvb

45 if value is not None: 1acwxyzdefgABCDhijklmnopqrstuvb

46 value = value.replace(tzinfo=UTC) 1acwxyzdefgABCDhijklmnopqrstuvb

47 except Exception: 

48 pass 

49 

50 return value 1acwxyzdefgABCDhijklmnopqrstuvb