Skip to content
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
3 changes: 3 additions & 0 deletions continuous-integration/requirements-3.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ flake8==7.3.0
# via emsarray (pyproject.toml)
fonttools==4.59.1
# via matplotlib
freezegun==1.5.5
# via emsarray (pyproject.toml)
fsspec==2025.7.0
# via dask
geojson==3.2.0
Expand Down Expand Up @@ -188,6 +190,7 @@ pytest-mpl==0.17.0
# via emsarray (pyproject.toml)
python-dateutil==2.9.0.post0
# via
# freezegun
# matplotlib
# pandas
pytz==2025.2
Expand Down
3 changes: 3 additions & 0 deletions continuous-integration/requirements-3.12.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ flake8==7.3.0
# via emsarray (pyproject.toml)
fonttools==4.59.1
# via matplotlib
freezegun==1.5.5
# via emsarray (pyproject.toml)
fsspec==2025.7.0
# via dask
geojson==3.2.0
Expand Down Expand Up @@ -186,6 +188,7 @@ pytest-mpl==0.17.0
# via emsarray (pyproject.toml)
python-dateutil==2.9.0.post0
# via
# freezegun
# matplotlib
# pandas
pytz==2025.2
Expand Down
3 changes: 3 additions & 0 deletions continuous-integration/requirements-3.13.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ flake8==7.3.0
# via emsarray (pyproject.toml)
fonttools==4.59.1
# via matplotlib
freezegun==1.5.5
# via emsarray (pyproject.toml)
fsspec==2025.7.0
# via dask
geojson==3.2.0
Expand Down Expand Up @@ -186,6 +188,7 @@ pytest-mpl==0.17.0
# via emsarray (pyproject.toml)
python-dateutil==2.9.0.post0
# via
# freezegun
# matplotlib
# pandas
pytz==2025.2
Expand Down
2 changes: 1 addition & 1 deletion continuous-integration/requirements-minimum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,6 @@ zipp~=3.20.2
# emsarray
# pytz
# tzdata
pytz
tzdata
certifi
pytz
3 changes: 3 additions & 0 deletions docs/releases/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ Next release (in development)
:ref:`how to set the clim parameter in plots <example-plot-with-clim>`
(:pr:`179`).
* Bumped pinned dependencies (:pr:`183`).
* Fixed :func:`emsarray.utils.datetime_from_np_time`
when the system timezone is not UTC and a specific timezone is requested
(:issue:`176`, :pr:`183`).
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ testing = [
"types-pytz",
"flake8",
"isort",
"freezegun",
]

[project.scripts]
Expand Down
8 changes: 5 additions & 3 deletions src/emsarray/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,15 +695,17 @@ def datetime_from_np_time(
The timezone that the numpy datetime is in.
Defaults to UTC, as xarray will convert all time variables to UTC when
opening files.
The returned Python datetime will be in this timezone.

Returns
=======
datetime.datetime
A timezone aware Python datetime.datetime instance.
"""
epoc = numpy.datetime64('1970-01-01')
timestamp = (np_time - epoc).astype('timedelta64[ns]')
return datetime.datetime.fromtimestamp(timestamp.astype(float) / 1e9, tz=tz)
np_epoc = numpy.datetime64('1970-01-01')
ns_since_epoc = (np_time - np_epoc).astype('timedelta64[ns]')
py_epoc = datetime.datetime(1970, 1, 1, tzinfo=tz)
return py_epoc + datetime.timedelta(seconds=ns_since_epoc.astype(float) / 1e9)


class RequiresExtraException(Exception):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pathlib
from importlib.metadata import version

import freezegun
import netCDF4
import numpy
import numpy.testing
Expand Down Expand Up @@ -569,3 +570,25 @@ def test_wind_dimension_renamed():
)
wound = utils.wind_dimension(data_array, ['y', 'x'], [5, 4], linear_dimension='ix')
xarray.testing.assert_equal(wound, expected)


@pytest.mark.parametrize('tz_offset', [0, 10, -4])
def test_datetime_from_np_time(tz_offset: int):
# Change the system timezone to `tz_offset`
with freezegun.freeze_time(tz_offset=tz_offset):
np_time = numpy.datetime64('2025-08-18T12:05:00.123456')

# Test that converting works using the UTC default timezone,
# regardless of system timezone
py_time_utc = utils.datetime_from_np_time(np_time)
assert py_time_utc == datetime.datetime(2025, 8, 18, 12, 5, 0, 123456, tzinfo=datetime.UTC)

# Test that converting works when interpreted in the system timezone.
py_tz_system = datetime.timezone(datetime.timedelta(hours=tz_offset))
py_time_local = utils.datetime_from_np_time(np_time, tz=py_tz_system)
assert py_time_local == datetime.datetime(2025, 8, 18, 12, 5, 0, 123456, tzinfo=py_tz_system)

# Test that converting works when using some other arbitrary timezone.
py_tz_eucla = datetime.timezone(datetime.timedelta(hours=8, minutes=45))
py_time_eucla = utils.datetime_from_np_time(np_time, tz=py_tz_eucla)
assert py_time_eucla == datetime.datetime(2025, 8, 18, 12, 5, 0, 123456, tzinfo=py_tz_eucla)
Loading