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 17:29 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 17:29 +0000
1from datetime import UTC, datetime 1a
3from sqlalchemy.types import DateTime, TypeDecorator 1a
6def get_utc_now(): 1a
7 """
8 Returns the current time in UTC.
9 """
10 return datetime.now(UTC) 1adghijklmbnopqrstuvwxeyzABCDEfFGHIJKLMNOPQRSTUVWXYZ0c
13def get_utc_today(): 1a
14 """
15 Returns the current date in UTC.
16 """
17 return datetime.now(UTC).date() 1bc
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.
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 """
29 impl = DateTime 1a
30 cache_ok = True 1a
32 def process_bind_param(self, value: datetime | None, dialect): 1a
33 if value is None: 1adghijklmbnopqrstuvwxeyzABCDEfFGHIJKLMNOPQRSTUVWXYZ0c
34 return value 1adbefc
36 try: 1adghijklmbnopqrstuvwxeyzABCDEfFGHIJKLMNOPQRSTUVWXYZ0c
37 if value.tzinfo is not None: 37 ↛ 39line 37 didn't jump to line 39 because the condition on line 37 was always true1adghijklmbnopqrstuvwxeyzABCDEfFGHIJKLMNOPQRSTUVWXYZ0c
38 value = value.astimezone(UTC) 1adghijklmbnopqrstuvwxeyzABCDEfFGHIJKLMNOPQRSTUVWXYZ0c
39 return value.replace(tzinfo=None) 1adghijklmbnopqrstuvwxeyzABCDEfFGHIJKLMNOPQRSTUVWXYZ0c
40 except Exception:
41 return value
43 def process_result_value(self, value: datetime | None, dialect): 1a
44 try: 2a d 1 2 3 4 5 6 7 8 9 ! # g h i j k l m $ % b ' ( ) n * o + p q , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbr tbubvbwbxbybzbAbBbCbDbs t u v w x e y z A B C D E f F G H I J K L M N O P Q R S T U V W X Y Z 0 c
45 if value is not None: 2a d 1 2 3 4 5 6 7 8 9 ! # g h i j k l m $ % b ' ( ) n * o + p q , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbr tbubvbwbxbybzbAbBbCbDbs t u v w x e y z A B C D E f F G H I J K L M N O P Q R S T U V W X Y Z 0 c
46 value = value.replace(tzinfo=UTC) 2a d 1 2 3 4 5 6 7 8 9 ! # g h i j k l m $ % b ' ( ) n * o + p q , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbr tbubvbwbxbybzbAbBbCbDbs t u v w x e y z A B C D E f F G H I J K L M N O P Q R S T U V W X Y Z 0 c
47 except Exception:
48 pass
50 return value 2a d 1 2 3 4 5 6 7 8 9 ! # g h i j k l m $ % b ' ( ) n * o + p q , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbr tbubvbwbxbybzbAbBbCbDbs t u v w x e y z A B C D E f F G H I J K L M N O P Q R S T U V W X Y Z 0 c