Skip to content

Commit

Permalink
Refactor to replace deprecated datetime.utcnow() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
joshhaug committed Mar 26, 2024
1 parent 4c1487f commit 7089bdc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/aerie_cli/persistent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import json
import shutil
import pickle
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

from appdirs import AppDirs

Expand Down Expand Up @@ -142,7 +142,7 @@ def _load_active_session(cls) -> None:
raise RuntimeError(f"Cannot parse session timestamp: {session_file.name}. Try deactivating your session.")

# If session hasn't been used since timeout, mark as inactive
if (datetime.utcnow() - t) > SESSION_TIMEOUT:
if (datetime.now(timezone.utc).replace(tzinfo=None) - t) > SESSION_TIMEOUT:
session_file.unlink()
raise NoActiveSessionError

Expand Down Expand Up @@ -177,7 +177,7 @@ def set_active_session(cls, session: AerieHost) -> bool:

cls._active_session = session

session_file = datetime.utcnow().strftime(SESSION_TIMESTAMP_FSTRING) + '.aerie_cli.session'
session_file = datetime.now(timezone.utc).strftime(SESSION_TIMESTAMP_FSTRING) + '.aerie_cli.session'
session_file = SESSION_FILE_DIRECTORY.joinpath(session_file)

with open(session_file, 'wb') as fid:
Expand Down
10 changes: 5 additions & 5 deletions tests/unit_tests/test_persistent_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_get_session_expired(persistent_path: Path):

# Create a mock old session with an "expired" session
old_session = MockAerieHost()
old_time = datetime.datetime.utcnow() - persistent.SESSION_TIMEOUT - \
old_time = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) - persistent.SESSION_TIMEOUT - \
datetime.timedelta(seconds=1)
old_fn = old_time.strftime(
persistent.SESSION_TIMESTAMP_FSTRING) + '.aerie_cli.session'
Expand All @@ -70,7 +70,7 @@ def test_get_session_broken(persistent_path: Path):

# Create a mock old session which fails the ping test
old_session = MockAerieHost(False)
old_time = datetime.datetime.utcnow()
old_time = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
old_fn = old_time.strftime(
persistent.SESSION_TIMESTAMP_FSTRING) + '.aerie_cli.session'
with open(persistent_path.joinpath(old_fn), 'wb') as fid:
Expand All @@ -89,7 +89,7 @@ def test_get_session(persistent_path: Path):

# Create a mock good session
old_session = MockAerieHost()
old_time = datetime.datetime.utcnow()
old_time = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
old_fn = old_time.strftime(
persistent.SESSION_TIMESTAMP_FSTRING) + '.aerie_cli.session'
with open(persistent_path.joinpath(old_fn), 'wb') as fid:
Expand All @@ -109,7 +109,7 @@ def test_get_session_multiple(persistent_path: Path):
# Create a mock good session
for i in range(2):
old_session = MockAerieHost()
old_time = datetime.datetime.utcnow() - datetime.timedelta(minutes=i)
old_time = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) - datetime.timedelta(minutes=i)
old_fn = old_time.strftime(
persistent.SESSION_TIMESTAMP_FSTRING) + '.aerie_cli.session'
with open(persistent_path.joinpath(old_fn), 'wb') as fid:
Expand Down Expand Up @@ -182,7 +182,7 @@ def test_unset_session_startup_persistent(persistent_path: Path):

# Create a mock good session
old_session = MockAerieHost(name="Old Session")
old_time = datetime.datetime.utcnow()
old_time = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
old_fn = old_time.strftime(
persistent.SESSION_TIMESTAMP_FSTRING) + '.aerie_cli.session'
with open(persistent_path.joinpath(old_fn), 'wb') as fid:
Expand Down

0 comments on commit 7089bdc

Please sign in to comment.