diff --git a/pyproject.toml b/pyproject.toml index fcb9a7c..d4aada4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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] diff --git a/src/rashdf/utils.py b/src/rashdf/utils.py index ac36d31..0eae79a 100644 --- a/src/rashdf/utils.py +++ b/src/rashdf/utils.py @@ -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 ---------- @@ -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]: diff --git a/tests/test_utils.py b/tests/test_utils.py index a80558d..4b7c58c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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():