diff --git a/qiskit_ibm_provider/utils/converters.py b/qiskit_ibm_provider/utils/converters.py index af68f4507..b19fb203d 100644 --- a/qiskit_ibm_provider/utils/converters.py +++ b/qiskit_ibm_provider/utils/converters.py @@ -12,6 +12,7 @@ """Utilities related to conversion.""" +import re from datetime import datetime, timedelta, timezone from math import ceil from typing import Union, Tuple, Any, Optional @@ -200,13 +201,25 @@ def hms_to_seconds(hms: str, msg_prefix: str = "") -> int: Raises: IBMInputValueError: when the given hms string is in an invalid format """ - try: - date_time = parser.parse(hms) - hours = date_time.hour - minutes = date_time.minute - seconds = date_time.second - return int( - timedelta(hours=hours, minutes=minutes, seconds=seconds).total_seconds() - ) - except parser.ParserError as parser_error: - raise IBMInputValueError(msg_prefix + str(parser_error)) + + parsed_time = re.findall(r"(\d+[dhms])", hms) + total_seconds = 0 + + if parsed_time: + for time_unit in parsed_time: + unit = time_unit[-1] + value = int(time_unit[:-1]) + if unit == "d": + total_seconds += value * 86400 + elif unit == "h": + total_seconds += value * 3600 + elif unit == "m": + total_seconds += value * 60 + elif unit == "s": + total_seconds += value + else: + raise IBMInputValueError(f"{msg_prefix} Invalid input: {unit}") + else: + raise IBMInputValueError(f"{msg_prefix} Invalid input: {parsed_time}") + + return total_seconds diff --git a/releasenotes/notes/hms-seconds-util-bug-5a2ab12d05224baa.yaml b/releasenotes/notes/hms-seconds-util-bug-5a2ab12d05224baa.yaml new file mode 100644 index 000000000..55a11fd5a --- /dev/null +++ b/releasenotes/notes/hms-seconds-util-bug-5a2ab12d05224baa.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + The utility function ``hms_to_seconds`` now handles values greater than one day. This means that + passing in values greater than one day for the ``max_time`` parameter in ``Sessions`` is + possible. diff --git a/test/unit/test_utils_converters.py b/test/unit/test_utils_converters.py index ebb495555..af109d1cd 100644 --- a/test/unit/test_utils_converters.py +++ b/test/unit/test_utils_converters.py @@ -30,12 +30,11 @@ def test_hms_to_seconds(self): ("3h 30m 30s", 12630), ("3h 30s", 10830), ("3h30m30s", 12630), + ("2d", 172800), + ("25h", 90000), + ("1d 1h 1m 1s", 90061), ] invalid_strings = [ - "2d", - "24h", - "60m", - "60s", "2w", ] for valid_string in valid_strings: