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
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 03:04 +0000
1from datetime import UTC, datetime
3from sqlalchemy.types import DateTime, TypeDecorator
6def get_utc_now():
7 """
8 Returns the current time in UTC.
9 """
10 return datetime.now(UTC)
13def get_utc_today():
14 """
15 Returns the current date in UTC.
16 """
17 return datetime.now(UTC).date()
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.
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
30 cache_ok = True
32 def process_bind_param(self, value: datetime | None, dialect):
33 if value is None:
34 return value
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
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
50 return value