Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Added hms_to_seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
merav-aharoni committed Jul 20, 2023
1 parent c9ca298 commit f8890bf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion qiskit_ibm_provider/ibm_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ def _runtime_run(
job_tags=job_tags,
session_id=session_id,
start_session=start_session,
max_execution_time=max_execution_time
max_execution_time=max_execution_time,
)
except RequestsApiError as ex:
raise IBMBackendApiError("Error submitting job: {}".format(str(ex))) from ex
Expand Down
14 changes: 7 additions & 7 deletions qiskit_ibm_provider/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

from qiskit.circuit import QuantumCircuit

from qiskit_ibm_provider.utils.converters import hms_to_seconds


def _active_session(func): # type: ignore
"""Decorator used to ensure the session is active."""
Expand Down Expand Up @@ -94,13 +96,11 @@ def __init__(
self._active = True
self._circuits_map: Dict[str, QuantumCircuit] = {}

# for now ignore hms_to_seconds in order not to copy it from qiskit_ibm_runtime
# self._max_time = (
# max_time
# if max_time is None or isinstance(max_time, int)
# else hms_to_seconds(max_time, "Invalid max_time value: ")
# )
self._max_time = max_time
self._max_time = (
max_time
if max_time is None or isinstance(max_time, int)
else hms_to_seconds(max_time, "Invalid max_time value: ")
)

# def close(self) -> None:
# """Close the session."""
Expand Down
36 changes: 31 additions & 5 deletions qiskit_ibm_provider/utils/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
from math import ceil
from typing import Union, Tuple, Any, Optional

import dateutil.parser
from dateutil import tz
from dateutil import tz, parser

from qiskit_ibm_provider.exceptions import IBMInputValueError


def utc_to_local(utc_dt: Union[datetime, str]) -> datetime:
Expand All @@ -33,7 +34,7 @@ def utc_to_local(utc_dt: Union[datetime, str]) -> datetime:
TypeError: If the input parameter value is not valid.
"""
if isinstance(utc_dt, str):
utc_dt = dateutil.parser.parse(utc_dt)
utc_dt = parser.parse(utc_dt)
if not isinstance(utc_dt, datetime):
raise TypeError("Input `utc_dt` is not string or datetime.")
utc_dt = utc_dt.replace(tzinfo=timezone.utc) # type: ignore[arg-type]
Expand All @@ -54,7 +55,7 @@ def local_to_utc(local_dt: Union[datetime, str]) -> datetime:
TypeError: If the input parameter value is not valid.
"""
if isinstance(local_dt, str):
local_dt = dateutil.parser.parse(local_dt)
local_dt = parser.parse(local_dt)
if not isinstance(local_dt, datetime):
raise TypeError("Input `local_dt` is not string or datetime.")

Expand Down Expand Up @@ -131,7 +132,7 @@ def str_to_utc(utc_dt: Optional[str]) -> Optional[datetime]:
"""
if not utc_dt:
return None
parsed_dt = dateutil.parser.isoparse(utc_dt)
parsed_dt = parser.isoparse(utc_dt)
return parsed_dt.replace(tzinfo=timezone.utc)


Expand Down Expand Up @@ -184,3 +185,28 @@ def duration_difference(date_time: datetime) -> str:
elif time_tuple[3]:
time_str += "{} sec".format(time_tuple[3])
return time_str


def hms_to_seconds(hms: str, msg_prefix: str = "") -> int:
"""Convert duration specified as hours minutes seconds to seconds.
Args:
hms: The string input duration (in hours minutes seconds). Ex: 2h 10m 20s
msg_prefix: Additional message to prefix the error.
Returns:
Total seconds (int) in the duration.
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))

0 comments on commit f8890bf

Please sign in to comment.