Skip to content

Commit

Permalink
Address DeprecationWarning for datetime.datetime.utcnow()
Browse files Browse the repository at this point in the history
We now use timezone-aware objects to represent datetimes in UTC, e.g. `datetime.datetime.now(datetime.UTC)`.
  • Loading branch information
pkhalaj committed Nov 20, 2024
1 parent c214917 commit 045331c
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __getattr__(cls, name):

# General information about the project.
project = u"Satpy"
copyright = u"2009-{}, The PyTroll Team".format(dt.datetime.utcnow().strftime("%Y")) # noqa: A001
copyright = u"2009-{}, The PyTroll Team".format(dt.datetime.now(dt.UTC).strftime("%Y")) # noqa: A001

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion satpy/readers/hrpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def __init__(self, filename, filename_info, filetype_info):
self.channels = {i: None for i in AVHRR_CHANNEL_NAMES}
self.units = {i: "counts" for i in AVHRR_CHANNEL_NAMES}

self.year = filename_info.get("start_time", dt.datetime.utcnow()).year
self.year = filename_info.get("start_time", dt.datetime.now(dt.UTC)).year

@cached_property
def times(self):
Expand Down
4 changes: 2 additions & 2 deletions satpy/tests/reader_tests/test_satpy_cf_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,15 @@ def cf_scene(datasets, common_attrs):
@pytest.fixture
def nc_filename(tmp_path):
"""Create an nc filename for viirs m band."""
now = dt.datetime.utcnow()
now = dt.datetime.now(dt.UTC)
filename = f"testingcfwriter{now:%Y%j%H%M%S}-viirs-mband-20201007075915-20201007080744.nc"
return str(tmp_path / filename)


@pytest.fixture
def nc_filename_i(tmp_path):
"""Create an nc filename for viirs i band."""
now = dt.datetime.utcnow()
now = dt.datetime.now(dt.UTC)
filename = f"testingcfwriter{now:%Y%j%H%M%S}-viirs-iband-20201007075915-20201007080744.nc"
return str(tmp_path / filename)

Expand Down
2 changes: 1 addition & 1 deletion satpy/tests/reader_tests/test_viirs_vgac_l1c_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
@pytest.fixture
def nc_filename(tmp_path):
"""Create an nc test data file and return its filename."""
now = dt.datetime.utcnow()
now = dt.datetime.now(dt.UTC)
filename = f"VGAC_VJ10XMOD_A{now:%Y%j_%H%M}_n004946_K005.nc"
filename_str = str(tmp_path / filename)
# Create test data
Expand Down
2 changes: 1 addition & 1 deletion satpy/tests/scene_tests/test_saving.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_save_datasets_bad_writer(self, tmp_path):
da.zeros((100, 200), chunks=50),
dims=("y", "x"),
attrs={"name": "test",
"start_time": dt.datetime.utcnow()}
"start_time": dt.datetime.now(dt.UTC)}
)
scn = Scene()
scn["test"] = ds1
Expand Down
4 changes: 2 additions & 2 deletions satpy/tests/writer_tests/test_geotiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _get_test_datasets_2d():
da.zeros((100, 200), chunks=50),
dims=("y", "x"),
attrs={"name": "test",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"units": "K",
"area": adef}
)
Expand Down Expand Up @@ -72,7 +72,7 @@ def _get_test_datasets_3d():
dims=("bands", "y", "x"),
coords={"bands": ["R", "G", "B"]},
attrs={"name": "test",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"area": adef}
)
return [ds1]
Expand Down
24 changes: 12 additions & 12 deletions satpy/tests/writer_tests/test_mitiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _get_test_datasets(self):
da.zeros((100, 200), chunks=50),
dims=("y", "x"),
attrs={"name": "1",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"platform_name": "TEST_PLATFORM_NAME",
"sensor": "TEST_SENSOR_NAME",
"area": area_def,
Expand All @@ -91,7 +91,7 @@ def _get_test_datasets(self):
da.zeros((100, 200), chunks=50),
dims=("y", "x"),
attrs={"name": "4",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"platform_name": "TEST_PLATFORM_NAME",
"sensor": "TEST_SENSOR_NAME",
"area": area_def,
Expand Down Expand Up @@ -132,7 +132,7 @@ def _get_test_datasets_sensor_set(self):
da.zeros((100, 200), chunks=50),
dims=("y", "x"),
attrs={"name": "1",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"platform_name": "TEST_PLATFORM_NAME",
"sensor": {"TEST_SENSOR_NAME"},
"area": area_def,
Expand All @@ -155,7 +155,7 @@ def _get_test_datasets_sensor_set(self):
da.zeros((100, 200), chunks=50),
dims=("y", "x"),
attrs={"name": "4",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"platform_name": "TEST_PLATFORM_NAME",
"sensor": {"TEST_SENSOR_NAME"},
"area": area_def,
Expand Down Expand Up @@ -197,7 +197,7 @@ def _get_test_dataset(self, bands=3):
da.zeros((bands, 100, 200), chunks=50),
dims=("bands", "y", "x"),
attrs={"name": "test",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"platform_name": "TEST_PLATFORM_NAME",
"sensor": "TEST_SENSOR_NAME",
"area": area_def,
Expand Down Expand Up @@ -226,7 +226,7 @@ def _get_test_one_dataset(self):
da.zeros((100, 200), chunks=50),
dims=("y", "x"),
attrs={"name": "test",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"platform_name": "TEST_PLATFORM_NAME",
"sensor": "avhrr",
"area": area_def,
Expand Down Expand Up @@ -255,7 +255,7 @@ def _get_test_one_dataset_sensor_set(self):
da.zeros((100, 200), chunks=50),
dims=("y", "x"),
attrs={"name": "test",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"platform_name": "TEST_PLATFORM_NAME",
"sensor": {"avhrr"},
"area": area_def,
Expand Down Expand Up @@ -288,7 +288,7 @@ def _get_test_dataset_with_bad_values(self, bands=3):
ds1 = xr.DataArray(rgb_data,
dims=("bands", "y", "x"),
attrs={"name": "test",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"platform_name": "TEST_PLATFORM_NAME",
"sensor": "TEST_SENSOR_NAME",
"area": area_def,
Expand Down Expand Up @@ -350,7 +350,7 @@ def _get_test_dataset_calibration(self, bands=6):
bands.append(p.attrs["name"])
data["bands"] = list(bands)
new_attrs = {"name": "datasets",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"platform_name": "TEST_PLATFORM_NAME",
"sensor": "test-sensor",
"area": area_def,
Expand Down Expand Up @@ -427,7 +427,7 @@ def _get_test_dataset_calibration_one_dataset(self, bands=1):
for p in scene:
calibration.append(p.attrs["calibration"])
new_attrs = {"name": "datasets",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"platform_name": "TEST_PLATFORM_NAME",
"sensor": "test-sensor",
"area": area_def,
Expand Down Expand Up @@ -472,7 +472,7 @@ def _get_test_dataset_three_bands_two_prereq(self, bands=3):
coords=[["R", "G", "B"], list(range(100)), list(range(200))],
dims=("bands", "y", "x"),
attrs={"name": "test",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"platform_name": "TEST_PLATFORM_NAME",
"sensor": "TEST_SENSOR_NAME",
"area": area_def,
Expand Down Expand Up @@ -504,7 +504,7 @@ def _get_test_dataset_three_bands_prereq(self, bands=3):
coords=[["R", "G", "B"], list(range(100)), list(range(200))],
dims=("bands", "y", "x"),
attrs={"name": "test",
"start_time": dt.datetime.utcnow(),
"start_time": dt.datetime.now(dt.UTC),
"platform_name": "TEST_PLATFORM_NAME",
"sensor": "TEST_SENSOR_NAME",
"area": area_def,
Expand Down
2 changes: 1 addition & 1 deletion satpy/tests/writer_tests/test_simple_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _get_test_datasets():
da.zeros((100, 200), chunks=50),
dims=("y", "x"),
attrs={"name": "test",
"start_time": dt.datetime.utcnow()}
"start_time": dt.datetime.now(dt.UTC)}
)
return [ds1]

Expand Down
2 changes: 1 addition & 1 deletion satpy/writers/awips_tiled.py
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,7 @@ def create_debug_lettered_tiles(**writer_kwargs):
sector_info = writer.awips_sectors[sector_id]
area_def, arr = _create_debug_array(sector_info, save_kwargs["num_subtiles"])

now = dt.datetime.utcnow()
now = dt.datetime.now(dt.UTC)

Check warning on line 1778 in satpy/writers/awips_tiled.py

View check run for this annotation

Codecov / codecov/patch

satpy/writers/awips_tiled.py#L1778

Added line #L1778 was not covered by tests
product = xr.DataArray(da.from_array(arr, chunks="auto"), attrs=dict(
name="debug_{}".format(sector_id),
platform_name="DEBUG",
Expand Down

0 comments on commit 045331c

Please sign in to comment.