Skip to content

Commit

Permalink
Merge pull request #33 from fema-ffrd/update_datetime_parse_funcs
Browse files Browse the repository at this point in the history
Edit datetime parse functions to handle time of 2400
  • Loading branch information
thwllms authored May 23, 2024
2 parents 883ef6a + deb0fd1 commit d042709
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ classifiers = [
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
version = "0.2.0"
version = "0.2.1"
dependencies = ["h5py", "geopandas", "pyarrow"]

[project.optional-dependencies]
Expand Down
29 changes: 24 additions & 5 deletions src/rashdf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,34 @@


def parse_ras_datetime(datetime_str: str) -> datetime:
"""Parse a datetime string from a RAS file into a datetime object.
"""
Parse a datetime string from a RAS file into a datetime object. If the datetime has
a time of 2400, then it is converted to midnight of the next day.
Parameters
----------
datetime_str (str): The datetime string to be parsed. The string should be in the format "ddMMMyyyy HHmm".
datetime_str (str): The datetime string to be parsed. The string should be in the format "ddMMMyyyy HH:mm:ss".
Returns
-------
datetime: A datetime object representing the parsed datetime.
"""
format = "%d%b%Y %H:%M:%S"
return datetime.strptime(datetime_str, format)

if datetime_str.endswith("24:00:00"):
datetime_str = datetime_str.replace("24:00:00", "00:00:00")
parsed_dt = datetime.strptime(datetime_str, format)
parsed_dt += timedelta(days=1)
else:
parsed_dt = datetime.strptime(datetime_str, format)

return parsed_dt


def parse_ras_simulation_window_datetime(datetime_str) -> datetime:
"""
Parse a datetime string from a RAS simulation window into a datetime object.
Parse a datetime string from a RAS simulation window into a datetime object.If the datetime has a
time of 2400, then it is converted to midnight of the next day.
Parameters
----------
Expand All @@ -35,7 +46,15 @@ def parse_ras_simulation_window_datetime(datetime_str) -> datetime:
datetime: A datetime object representing the parsed datetime.
"""
format = "%d%b%Y %H%M"
return datetime.strptime(datetime_str, format)

if datetime_str.endswith("2400"):
datetime_str = datetime_str.replace("2400", "0000")
parsed_dt = datetime.strptime(datetime_str, format)
parsed_dt += timedelta(days=1)
else:
parsed_dt = datetime.strptime(datetime_str, format)

return parsed_dt


def parse_run_time_window(window: str) -> Tuple[datetime, datetime]:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,35 @@ def test_convert_ras_hdf_value():
assert utils.convert_ras_hdf_value(b"15Mar2024 16:39:01") == datetime(
2024, 3, 15, 16, 39, 1
)
assert utils.convert_ras_hdf_value(b"15Mar2024 24:00:00") == datetime(
2024, 3, 16, 0, 0, 0
)
assert utils.convert_ras_hdf_value(b"15Mar2024 16:39:01 to 16Mar2024 16:39:01") == [
datetime(2024, 3, 15, 16, 39, 1),
datetime(2024, 3, 16, 16, 39, 1),
]
assert utils.convert_ras_hdf_value(b"18Mar2024 24:00:00 to 19Mar2024 24:00:00") == [
datetime(2024, 3, 19, 0, 0, 0),
datetime(2024, 3, 20, 0, 0, 0),
]
assert utils.convert_ras_hdf_value(b"01:23:45") == timedelta(
hours=1, minutes=23, seconds=45
)
assert utils.convert_ras_hdf_value(b"15Mar2024 2400") == datetime(
2024, 3, 16, 0, 0, 0
)
assert utils.convert_ras_hdf_value(b"15Mar2024 2315") == datetime(
2024, 3, 15, 23, 15, 0
)

assert utils.convert_ras_hdf_value(b"15Mar2024 1639 to 16Mar2024 1639") == [
datetime(2024, 3, 15, 16, 39, 0),
datetime(2024, 3, 16, 16, 39, 0),
]
assert utils.convert_ras_hdf_value(b"18Mar2024 2400 to 19Mar2024 2400") == [
datetime(2024, 3, 19, 0, 0, 0),
datetime(2024, 3, 20, 0, 0, 0),
]


def test_df_datetimes_to_str():
Expand Down

0 comments on commit d042709

Please sign in to comment.