diff --git a/CHANGES.rst b/CHANGES.rst index 196295b6f8..7fe3e08d14 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -49,6 +49,8 @@ outlier_detection - Remove use of ``scipy.signal.medfilt`` which is undefined for ``nan`` inputs. [#8033] +- Replace uses of ``datetime.utcnow`` (deprecated in python 3.12) [#8051] + imprint ------- diff --git a/jwst/associations/association.py b/jwst/associations/association.py index 8edeb1696b..92de32e0ac 100644 --- a/jwst/associations/association.py +++ b/jwst/associations/association.py @@ -1,6 +1,6 @@ from collections.abc import MutableMapping from copy import deepcopy -from datetime import datetime +from datetime import datetime, timezone import json import jsonschema import logging @@ -564,7 +564,7 @@ def finalize(asns): def make_timestamp(): - timestamp = datetime.utcnow().strftime( + timestamp = datetime.now(timezone.utc).strftime( _TIMESTAMP_TEMPLATE ) return timestamp diff --git a/jwst/scripts/migrate_data.py b/jwst/scripts/migrate_data.py index cc9d195489..1409c744f8 100755 --- a/jwst/scripts/migrate_data.py +++ b/jwst/scripts/migrate_data.py @@ -4,7 +4,7 @@ Migrate .fits files whose format has changed between jwst package versions. """ import argparse -from datetime import datetime +from datetime import datetime, timezone import os import re import traceback @@ -166,7 +166,8 @@ def migrate_file(filename, args): return migrate_method(hdul) - hdul[0].header['HISTORY'] = f'Migrated with jwst {jwst.__version__} migrate_data script {datetime.utcnow().isoformat()}' + timestamp = datetime.now(timezone.utc).isoformat() + hdul[0].header['HISTORY'] = f'Migrated with jwst {jwst.__version__} migrate_data script {timestamp}' try: getattr(datamodels, model_type)(hdul, strict_validation=True) diff --git a/jwst/stpipe/tests/test_config.py b/jwst/stpipe/tests/test_config.py index e2f1f4bdcb..fc91957194 100644 --- a/jwst/stpipe/tests/test_config.py +++ b/jwst/stpipe/tests/test_config.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone import pytest import asdf @@ -56,7 +56,8 @@ def test_step_config_to_asdf(config): assert asdf_file["parameters"] == config.parameters assert asdf_file["steps"] == [s.to_asdf().tree for s in config.steps] assert asdf_file["meta"]["author"] == "" - assert (datetime.utcnow() - datetime.fromisoformat(asdf_file["meta"]["date"])) < timedelta(seconds=10) + current_time = datetime.now(timezone.utc).replace(tzinfo=None) + assert (current_time - datetime.fromisoformat(asdf_file["meta"]["date"])) < timedelta(seconds=10) assert asdf_file["meta"]["description"] == "Parameters for calibration step some.PipelineClass" assert asdf_file["meta"]["instrument"]["name"] == "" assert asdf_file["meta"]["origin"] == ""