Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix DeprecationWarning with datetime.utcnow() #86

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:

- uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.11'

- run: pip install -r requirements/linting.txt

Expand Down
8 changes: 7 additions & 1 deletion dirty_equals/_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ def _get_now(self) -> datetime:
if self.tz is None:
return datetime.now()
else:
return datetime.utcnow().replace(tzinfo=timezone.utc).astimezone(self.tz)
try:
from datetime import UTC

utc_now = datetime.now(UTC).replace(tzinfo=timezone.utc)
except ImportError:
utc_now = datetime.utcnow().replace(tzinfo=timezone.utc)
return utc_now.astimezone(self.tz)

def prepare(self, other: Any) -> datetime:
# update approx for every comparing, to check if other value is dirty equal
Expand Down
9 changes: 7 additions & 2 deletions tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ def test_repr():


def test_is_now_tz():
now_ny = datetime.utcnow().replace(tzinfo=timezone.utc).astimezone(pytz.timezone('America/New_York'))
try:
from datetime import UTC

utc_now = datetime.now(UTC).replace(tzinfo=timezone.utc)
except ImportError:
utc_now = datetime.utcnow().replace(tzinfo=timezone.utc)
now_ny = utc_now.astimezone(pytz.timezone('America/New_York'))
assert now_ny == IsNow(tz='America/New_York')
# depends on the time of year and DST
assert now_ny == IsNow(tz=timezone(timedelta(hours=-5))) | IsNow(tz=timezone(timedelta(hours=-4)))
Expand All @@ -111,7 +117,6 @@ def test_is_now_tz():
assert now.isoformat() == IsNow(iso_string=True)
assert now.isoformat() != IsNow

utc_now = datetime.utcnow().replace(tzinfo=timezone.utc)
assert utc_now == IsNow(tz=timezone.utc)


Expand Down