Skip to content

Commit

Permalink
Add support for datetime with zoneinfo tzinfo
Browse files Browse the repository at this point in the history
By converting input_datetime (string or datetime object) to a string before applying pandas' to_datetime, compatibility issues between pandas and zoneinfo are circumvented (pandas-dev/pandas#37654).

+ fix typos and remove duplicate code
  • Loading branch information
dbkhout committed Jan 21, 2022
1 parent b66a796 commit 6a58e92
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/pywaterinfo/waterinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def _parse_date(input_datetime, timezone="UTC"):
we normalize everything to UTC by default. Hence, we interpret the user
input as UTC, provide the input to the API as CET and request the returned
output data as UTC. If the user provides a timezone, we interpret user input as
the given timezone, doe the request in CET and return th output data in the
the given timezone, do the request in CET and return the output data in the
requested timezone.
Parameters
Expand All @@ -359,16 +359,12 @@ def _parse_date(input_datetime, timezone="UTC"):
f"{timezone} is not a valid timezone string."
)

input_timestamp = pd.to_datetime(input_datetime)
input_timestamp = pd.to_datetime(str(input_datetime))

if input_timestamp.tz: # timestamp already contains tz info
return input_timestamp.tz_convert("CET").strftime("%Y-%m-%d %H:%M:%S")
else:
return (
input_timestamp.tz_localize(timezone)
.tz_convert("CET")
.strftime("%Y-%m-%d %H:%M:%S")
)
if not input_timestamp.tz: # timestamp does not contain tz info
input_timestamp = input_timestamp.tz_localize(timezone)

return input_timestamp.tz_convert("CET").strftime("%Y-%m-%d %H:%M:%S")

def _parse_period(self, start=None, end=None, period=None, timezone="UTC"):
"""Check the from/to/period arguments when requesting
Expand Down

0 comments on commit 6a58e92

Please sign in to comment.