Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions qiskit_ibm_provider/utils/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions releasenotes/notes/hms-seconds-util-bug-5a2ab12d05224baa.yaml
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 3 additions & 4 deletions test/unit/test_utils_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down