Skip to content

Commit

Permalink
Remove python datetime utc deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
josecsotomorales committed Mar 14, 2024
1 parent 4dcc9ab commit 2f24557
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
8 changes: 4 additions & 4 deletions sqlalchemy_utils/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timezone

import sqlalchemy as sa

Expand All @@ -22,15 +22,15 @@ class SomeModel(Base, Timestamp):
id = sa.Column(sa.Integer, primary_key=True)
"""

created = sa.Column(sa.DateTime, default=datetime.utcnow, nullable=False)
updated = sa.Column(sa.DateTime, default=datetime.utcnow, nullable=False)
created = sa.Column(sa.DateTime, default=datetime.now(timezone.utc), nullable=False)
updated = sa.Column(sa.DateTime, default=datetime.now(timezone.utc), nullable=False)


@sa.event.listens_for(Timestamp, 'before_update', propagate=True)
def timestamp_before_update(mapper, connection, target):
# When a model with a timestamp is updated; force update the updated
# timestamp.
target.updated = datetime.utcnow()
target.updated = datetime.now(timezone.utc)


NOT_LOADED_REPR = '<not loaded>'
Expand Down
11 changes: 5 additions & 6 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from datetime import datetime

import pytest
import sqlalchemy as sa
import sqlalchemy.orm

from datetime import datetime, timezone
from sqlalchemy_utils import generic_repr, Timestamp


Expand All @@ -17,26 +16,26 @@ class Article(Base, Timestamp):
return Article

def test_created(self, session, Article):
then = datetime.utcnow()
then = datetime.now(timezone.utc)
article = Article()

session.add(article)
session.commit()

assert article.created >= then and article.created <= datetime.utcnow()
assert then <= article.created <= datetime.now(timezone.utc)

def test_updated(self, session, Article):
article = Article()

session.add(article)
session.commit()

then = datetime.utcnow()
then = datetime.now(timezone.utc)
article.name = "Something"

session.commit()

assert article.updated >= then and article.updated <= datetime.utcnow()
assert then <= article.updated <= datetime.now(timezone.utc)


class TestGenericRepr:
Expand Down

0 comments on commit 2f24557

Please sign in to comment.