diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 28b8b8e..503e932 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/dirty_equals/_datetime.py b/dirty_equals/_datetime.py index 6337453..b4d6f8f 100644 --- a/dirty_equals/_datetime.py +++ b/dirty_equals/_datetime.py @@ -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 diff --git a/tests/test_datetime.py b/tests/test_datetime.py index 2d5d524..ad1b21a 100644 --- a/tests/test_datetime.py +++ b/tests/test_datetime.py @@ -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))) @@ -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)