
    i              	       `   S r SSKrSSKJrJrJr  SSKJr  SSKJrJ	r	J
r
Jr  SSKJrJr  SSKJr  S\\\4   S	\4S
 jrS\\\4   S	\4S jrSS\\\4   S\S	\4S jjrS\\   S\S	\\   4S jrS\
S	\
4S jrS\\   S	\\   4S jrS\S	\	\\\\\4   4S jrS\S	\4S jrSS\S\S	\4S jjrg)z Utilities related to conversion.    N)datetime	timedeltatimezone)ceil)UnionTupleAnyOptional)tzparser)IBMInputValueErrorutc_dtreturnc                    [        U [        5      (       a  [        R                  " U 5      n [        U [        5      (       d  [        S5      eU R                  [        R                  S9n U R                  [        R                  " 5       5      nU$ )zConvert a UTC ``datetime`` object or string to a local timezone ``datetime``.

Args:
    utc_dt: Input UTC `datetime` or string.

Returns:
    A ``datetime`` with the local timezone.

Raises:
    TypeError: If the input parameter value is not valid.
z)Input `utc_dt` is not string or datetime.tzinfo)
isinstancestrr   parser   	TypeErrorreplacer   utc
astimezoner   tzlocal)r   local_dts     ]/home/james-whalen/.local/lib/python3.13/site-packages/qiskit_ibm_runtime/utils/converters.pyutc_to_localr      sg     &#f%fh''CDD^^8<<^0F  .HO    r   c                 z   [        U [        5      (       a  [        R                  " U 5      n [        U [        5      (       d  [        S5      eU R                  5       b  U R                  5       [        S5      :w  aA  U R                  [        R                  " 5       S9n U R                  [        R                  5      $ U $ )zConvert a local ``datetime`` object or string to a UTC ``datetime``.

Args:
    local_dt: Input local ``datetime`` or string.

Returns:
    A ``datetime`` in UTC.

Raises:
    TypeError: If the input parameter value is not valid.
z+Input `local_dt` is not string or datetime.r   r   )r   r   r   r   r   r   	utcoffsetr   r   r   r   r   UTC)r   s    r   local_to_utcr"   -   s     (C  <<)h))EFF #x'9'9';y|'K##2::<#8""266**Or   suffixc                 h    [        U 5      R                  5       nUS:X  a  UR                  SS5      nU$ )a  Convert a local ``datetime`` object or string to a UTC string.

Args:
    local_dt: Input local ``datetime`` or string.
    suffix: ``Z`` or ``+``, indicating whether the suffix should be ``Z`` or
        ``+00:00``.

Returns:
    UTC datetime in ISO format.
Zz+00:00)r"   	isoformatr   )r   r#   
utc_dt_strs      r   local_to_utc_strr(   E   s6     h'113J}''#6
r   input_dtto_utcc                 D    U c  gU(       a  [        U 5      $ [        U 5      $ )zConvert input timestamp timezone.

Args:
    input_dt: Timestamp to be converted.
    to_utc: True if to convert to UTC, otherwise to local timezone.

Returns:
    Converted timestamp, or ``None`` if input is ``None``.
N)r"   r   )r)   r*   s     r   
convert_tzr,   V   s&     H%%!!r   datac                 F   [        U [        5      (       a  [        U 5      $ [        U [        5      (       a  U  Vs/ s H  n[	        U5      PM     sn$ [        U [
        5      (       a/  U R                  5        VVs0 s H  u  p!U[	        U5      _M     snn$ U $ s  snf s  snnf )zRecursively convert all ``datetime`` in the input data from local time to UTC.

Note:
    Only lists and dictionaries are traversed.

Args:
    data: Data to be converted.

Returns:
    Converted data.
)r   r   r   listutc_to_local_alldictitems)r-   elemkeys      r   r0   r0   g   s     $!!D!!	D$		37844 &488	D$		=AZZ\J\	%d++\JJK 9Js   B:Bc                 x    U (       d  g[         R                  " U 5      nUR                  [        R                  S9$ )zConvert a UTC string to a ``datetime`` object with UTC timezone.

Args:
    utc_dt: Input UTC string in ISO format.

Returns:
    A ``datetime`` with the UTC timezone, or ``None`` if the input is ``None``.
Nr   )r   isoparser   r   r   )r   	parsed_dts     r   
str_to_utcr8   |   s0     'IHLL11r   secondsc                     [        U S-  5      n[        U S-  S-  5      n[        U S-  S-  5      nU S-  n SnU S:  a  [        [        U S-  5      5      nSn O[        U 5      n XX0U4$ )zConverts seconds in a datetime delta to a duration.

Args:
    seconds: Number of seconds in time delta.

Returns:
    A tuple containing the duration in terms of days,
    hours, minutes, seconds, and milliseconds.
Q      <   r      i  )intr   )r9   dayshoursminutesmillisecs        r   seconds_to_durationrE      s|     w9%&DDB&'E7b=B&'GlGH{tGdN+,g,(22r   	date_timec                 $   U R                  SS9[        R                  " 5       -
  n[        UR	                  5       5      nSnUS   (       a0  USR                  US   5      -  nUSR                  US   5      -  nU$ US   (       a0  USR                  US   5      -  nUS	R                  US
   5      -  nU$ US
   (       a0  USR                  US
   5      -  nUSR                  US   5      -  nU$ US   (       a  USR                  US   5      -  nU$ )zCompute the estimated duration until the given datetime.

Args:
    date_time: The input local datetime.

Returns:
    String giving the estimated duration.
Nr    r   z{} daysz {} hrsr?   z{} hrsz {} min   z{} minz {} sec   z{} sec)r   r   nowrE   total_secondsformat)rF   
time_delta
time_tupletime_strs       r   duration_differencerQ      s!    ""$"/(,,.@J$Z%=%=%?@J H!}I$$Z]33I$$Z]33 O 
AHOOJqM22I$$Z]33 O 
AHOOJqM22I$$Z]33 O 
AHOOJqM22Or   hms
msg_prefixc                 ,   [         R                  " SU 5      nSnU(       ae  U H]  nUS   n[        USS 5      nUS:X  a	  X6S-  -  nM%  US:X  a	  X6S-  -  nM4  US	:X  a	  X6S
-  -  nMC  US:X  a  X6-  nMO  [        U SU 35      e   U$ [        U SU 35      e)aS  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
z(\d+[dhms])r   Ndr;   hr<   mr>   sz Invalid input: )refindallr@   r   )rR   rS   parsed_timerL   	time_unitunitvalues          r   hms_to_secondsr`      s     **^S1KM$IR=D	#2'Es{.-+&(J<7Gv)NOO %   !J</?}!MNNr   )r%   )rH   )__doc__rZ   r   r   r   mathr   typingr   r   r	   r
   dateutilr   r   qiskit_ibm_runtime.exceptionsr   r   r   r"   r(   boolr,   r0   r8   floatr@   rE   rQ   r`    r   r   <module>ri      s    ' 	 2 2  . .  <x}- ( *53/ H 0uXs]3 S 3 ""(+ "T "hx>P ""3 3 *2x} 2(); 23 35c3S1H+I 3.8  8" " "c "r   