Skip to content

Commit

Permalink
40 add support for different timezones (#98)
Browse files Browse the repository at this point in the history
* add support for timezone in input time extents

* added timezone testcase

* updated history.rst
  • Loading branch information
veenstrajelmer authored Apr 26, 2024
1 parent ec667c2 commit a95a06d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ UNRELEASED
* avoid duplicated periods in dataframe returned by `ddlpy.measurements_amount()` in https://github.com/Deltares/ddlpy/pull/93
* allow for different retrieval frequencies (including None) in `ddlpy.measurements()` in https://github.com/Deltares/ddlpy/pull/95
* only catch "Geen waarnemingen aanwezig!" error message and raise all others (for instance for a too large request) in https://github.com/Deltares/ddlpy/pull/97
* support for timezones in start_date/end_date in https://github.com/Deltares/ddlpy/pull/98

0.4.0 (2024-04-08)
------------------
Expand Down
17 changes: 13 additions & 4 deletions ddlpy/ddlpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,21 @@ def _check_convert_dates(start_date, end_date, return_str=True):
start_date = pd.Timestamp(start_date)
end_date = pd.Timestamp(end_date)

# check if timezones are the same
assert start_date.tz == end_date.tz

# set UTC timezone if tz is None
if start_date.tz is None:
start_date = pytz.UTC.localize(start_date)
if end_date.tz is None:
end_date = pytz.UTC.localize(end_date)

if start_date > end_date:
raise ValueError("start_date is larger than end_date")
raise ValueError(f"start_date {start_date} is larger than end_date {end_date}")

if return_str:
start_date_str = pytz.UTC.localize(start_date).isoformat(timespec='milliseconds')
end_date_str = pytz.UTC.localize(end_date).isoformat(timespec='milliseconds')
start_date_str = start_date.isoformat(timespec='milliseconds')
end_date_str = end_date.isoformat(timespec='milliseconds')
return start_date_str, end_date_str
else:
return start_date, end_date
Expand Down Expand Up @@ -388,7 +397,7 @@ def measurements(location, start_date, end_date, freq=dateutil.rrule.MONTHLY, cl

if clean_df:
measurements = _clean_dataframe(measurements)

return measurements


Expand Down
26 changes: 26 additions & 0 deletions tests/test_ddlpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest
import ddlpy
import dateutil
import numpy as np


@pytest.fixture
Expand Down Expand Up @@ -170,6 +171,31 @@ def test_measurements_duplicated(measurements):
assert isinstance(meas_clean.index, pd.DatetimeIndex)


def test_measurements_timezone_behaviour(location):
start_date = "2015-01-01 00:00:00 +01:00"
end_date = "2015-01-03 00:00:00 +01:00"
measurements = ddlpy.measurements(location, start_date=start_date, end_date=end_date)
assert str(measurements.index[0].tz) == 'UTC+01:00'
assert measurements.index[0] == pd.Timestamp(start_date)
assert measurements.index[-1] == pd.Timestamp(end_date)

data_amount_dag = ddlpy.measurements_amount(location, start_date=start_date, end_date=end_date, period="Dag")
# when retrieving with tzone +01:00 we expect 1 value on 2015-01-03
assert np.allclose(data_amount_dag["AantalMetingen"].values, [144,144,1])


start_date = "2015-01-01"
end_date = "2015-01-03"
measurements = ddlpy.measurements(location, start_date=start_date, end_date=end_date)
assert str(measurements.index[0].tz) == 'UTC+01:00'
assert measurements.index[0] == pd.Timestamp(start_date).tz_localize("UTC").tz_convert('UTC+01:00')
assert measurements.index[-1] == pd.Timestamp(end_date).tz_localize("UTC").tz_convert('UTC+01:00')

data_amount_dag = ddlpy.measurements_amount(location, start_date=start_date, end_date=end_date, period="Dag")
# when retrieving with tzone +00:00 we expect 7 values on 2015-01-03
assert np.allclose(data_amount_dag["AantalMetingen"].values, [138,144,7])


def test_nodataerror(location):
"""
Test whether a request that returns no data is indeed properly catched
Expand Down

0 comments on commit a95a06d

Please sign in to comment.