Skip to content

Commit

Permalink
use Python's native timezone support so we can remove the dependenc…
Browse files Browse the repository at this point in the history
…y on `Babel`
  • Loading branch information
FelixSchwarz committed Dec 20, 2024
1 parent 0defb81 commit 3cd86de
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
8 changes: 4 additions & 4 deletions schwarz/column_alchemy/tests/utc_datetime_column_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
# the GPLv3 or (at your option) any later version.
# SPDX-License-Identifier: MIT or GPL-3.0-or-later

from datetime import datetime as DateTime
from datetime import datetime as DateTime, timedelta as TimeDelta, timezone

import pytest
from babel.util import FixedOffsetTimezone, UTC
from sqlalchemy import Column
from sqlalchemy.exc import StatementError

Expand All @@ -23,13 +22,14 @@ def ctx(db_ctx):


def test_utc_datetime_can_store_datetime_with_timezone(ctx):
dt = DateTime(2013, 5, 25, 9, 53, 24, tzinfo=FixedOffsetTimezone(-90))
tz = timezone(TimeDelta(hours=-1, minutes=-30))
dt = DateTime(2013, 5, 25, 9, 53, 24, tzinfo=tz)
with ctx.connection.begin():
inserted_id = ctx.insert_data(ctx.table, [{'timestamp': dt}])

dt_from_db = ctx.fetch_value(ctx.table, id=inserted_id)
assert dt_from_db == dt
assert dt_from_db.tzinfo == UTC
assert dt_from_db.tzinfo == timezone.utc


def test_utc_datetime_raises_exception_for_naive_datetime(ctx):
Expand Down
7 changes: 3 additions & 4 deletions schwarz/column_alchemy/utc_datetime_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
# the GPLv3 or (at your option) any later version.
# SPDX-License-Identifier: MIT or GPL-3.0-or-later

from datetime import datetime
from datetime import datetime, timezone

from babel.dates import UTC
from sqlalchemy.types import DateTime, TypeDecorator


Expand All @@ -33,7 +32,7 @@ def process_bind_param(self, value, dialect):
# since Python 3.6 ".astimetzone()" also works on naive datetime
# instances so we have to check this separately.
raise ValueError('naive datetime instance passed: %r' % value)
utc_dt = value.astimezone(UTC)
utc_dt = value.astimezone(timezone.utc)
if self._strip_tz:
return utc_dt.replace(tzinfo=None)
return utc_dt
Expand All @@ -43,6 +42,6 @@ def process_result_value(self, value, dialect):
return None
return datetime(value.year, value.month, value.day,
value.hour, value.minute, value.second,
value.microsecond, tzinfo=UTC)
value.microsecond, tzinfo=timezone.utc)
# -----------------------------------------------------------------------------

1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ zip_safe = false
include_package_data = true

install_requires =
Babel
# >= 1.2.3: Enum(…, values_callable=…)
SQLAlchemy >= 1.2.3

Expand Down

0 comments on commit 3cd86de

Please sign in to comment.