
    ]h +                     D    d Z ddlmZ ddlmZmZ d Zd Z G d d      Zy)	z
This module defines a Duration class.

The class Duration allows to define durations in years and months and can be
used as limited replacement for timedelta objects.
    )	timedelta)ROUND_FLOORDecimalc                 z    | |z
  ||z
  }}||z  j                  t              }|||z  z
  }||z  }t        |      |fS )z-
    A divmod function with boundaries.

    )to_integralr   int)vallowhighabdivmods          V/var/www/core.comfia.cic-ware.com/crm/lib/python3.12/site-packages/isodate/duration.pyfquotmodr      sO     9dSjqAq5

k
*C
cAg+C 3JCs8S=    c                 J    |dv ry|dv ry| dz  dk(  s| dz  dk7  r	| dz  dk(  ry	y
)zO
    Determines the number of days of a specific month in a specific year.
    )               
         )      	         i  r   d   r          )yearmonths     r   max_days_in_monthr'      sB     ''	s
q0qQr   c                       e Zd ZdZ	 	 	 	 	 	 	 	 	 ddZd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZeZd ZeZd Zd Zd Zd ZddZy)DurationaY  
    A class which represents a duration.

    The difference to datetime.timedelta is, that this class handles also
    differences given in years and months.
    A Duration treats differences given in year, months separately from all
    other components.

    A Duration can be used almost like any timedelta object, however there
    are some restrictions:
      * It is not really possible to compare Durations, because it is unclear,
        whether a duration of 1 year is bigger than 365 days or not.
      * Equality is only tested between the two (year, month vs. timedelta)
        basic components.

    A Duration can also be converted into a datetime object, but this requires
    a start date or an end date.

    The algorithm to add a duration to a date is defined at
    http://www.w3.org/TR/xmlschema-2/#adding-durations-to-dateTimes
    c
           	          t        |t              st        t        |            }t        |	t              st        t        |	            }	|| _        |	| _        t        |||||||      | _        y)zN
        Initialise this Duration instance with the given parameters.
        N)
isinstancer   strmonthsyearsr   tdelta)
selfdayssecondsmicrosecondsmillisecondsminuteshoursweeksr-   r.   s
             r   __init__zDuration.__init__A   s]     &'*S[)F%)CJ'E
'<wu
r   c                     | j                   S N)__dict__r0   s    r   __getstate__zDuration.__getstate__Z   s    }}r   c                 :    | j                   j                  |       y r:   )r;   update)r0   states     r   __setstate__zDuration.__setstate__]   s    U#r   c                 .    t        | j                  |      S )zU
        Provide direct access to attributes of included timedelta instance.
        )getattrr/   )r0   names     r   __getattr__zDuration.__getattr__`   s     t{{D))r   c                 >   g }| j                   r|j                  d| j                   z         | j                  r1d}| j                  dk  rd}|j                  || j                  z         |j                  t        | j                               dj                  |      S )zW
        Return a string representation of this duration similar to timedelta.
        z%d yearsz	%d monthsr   z%d monthz, )r.   appendr-   r,   r/   join)r0   paramsfmts      r   __str__zDuration.__str__f   s{     ::MM*tzz12;;C{{a MM#+,c$++&'yy  r   c                 
   d| j                   j                  | j                   j                  | j                  j                  | j                  j
                  | j                  j                  | j                  | j                  fz  S )z=
        Return a string suitable for repr(x) calls.
        z&%s.%s(%d, %d, %d, years=%d, months=%d))		__class__
__module____name__r/   r1   r2   r3   r.   r-   r<   s    r   __repr__zDuration.__repr__u   se     8NN%%NN##KKKKKK$$JJKK;
 
 	
r   c                 Z    t        | j                  | j                  | j                  f      S )zp
        Return a hash of this instance so that it can be used in, for
        example, dicts and sets.
        )hashr/   r-   r.   r<   s    r   __hash__zDuration.__hash__   s"    
 T[[$++tzz:;;r   c                 p    t        | j                   | j                         }| j                   |_        |S )zg
        A simple unary minus.

        Returns a new Duration instance with all it's negated.
        r.   r-   )r)   r.   r-   r/   )r0   negdurations     r   __neg__zDuration.__neg__   s0     djj[$++F"kk\r   c                    t        |t              r[t        | j                  |j                  z   | j                  |j                  z         }| j                  |j                  z   |_        |S 	 t        | j                        j                         r#t        | j                        j                         st        d      |j                  | j                  z   }t        |dd      \  }}|j                  | j                  z   |z   }t        ||      }|j                  |kD  r|}n|j                  }|j                  t        |      t        |      t        |            }| j                  |z   S # t        $ r Y nw xY w	 t        | j                  | j                        }| j                  |z   |_        |S # t        $ r Y t         S w xY w)ze
        Durations can be added with Duration, timedelta, date and datetime
        objects.
        rU   >fractional years or months not supported for date calculationsr      r%   r&   day)r+   r)   r.   r-   r/   float
is_integer
ValueErrorr&   r   r%   r'   r\   replacer   AttributeErrorNotImplemented)	r0   othernewdurationnewmonthcarrynewyearmaxdaysnewdaynewdts	            r   __add__zDuration.__add__   s   
 eX&"jj5;;.t{{U\\7QK "&u||!;K	 $**%002uT[[7I7T7T7V W  {{T[[0H&xB7OE8jj4::-5G':Gyy7" MM\XCK " E ;;&& 			 #DKKHK!%u!4K 			s%   -C=E+ +	E76E7;6F2 2	GGc                     t        |t              r=t        | j                  |z  | j                  |z        }| j
                  |z  |_        |S t        S )NrU   )r+   r   r)   r.   r-   r/   rb   r0   rc   rd   s      r   __mul__zDuration.__mul__   sH    eS!"e);DKKRWDWXK!%u!4Kr   c                 p   t        |t              r[t        | j                  |j                  z
  | j                  |j                  z
        }| j                  |j                  z
  |_        |S 	 t        | j                  | j                        }| j                  |z
  |_        |S # t
        $ r Y t        S w xY w)zj
        It is possible to subtract Duration and timedelta objects from Duration
        objects.
        rU   )r+   r)   r.   r-   r/   	TypeErrorrb   rm   s      r   __sub__zDuration.__sub__   s    
 eX&"jj5;;.t{{U\\7QK "&u||!;K	"DKKHK!%u!4K 		s   -6B$ $	B54B5c                 t   t        |t              rt               }||_        || z
  S 	 t	        | j
                        j                         r#t	        | j                        j                         st        d      |j                  | j                  z
  }t        |dd      \  }}|j                  | j
                  z
  |z   }t        ||      }|j                  |kD  r|}n|j                  }|j                  t        |      t        |      t        |            }|| j                  z
  S # t         $ r Y t"        S w xY w)a@  
        It is possible to subtract Duration objects from date, datetime and
        timedelta objects.

        TODO: there is some weird behaviour in date - timedelta ...
              if timedelta has seconds or microseconds set, then
              date - timedelta != date + (-timedelta)
              for now we follow this behaviour to avoid surprises when mixing
              timedeltas with Durations, but in case this ever changes in
              the stdlib we can just do:
                return -self + other
              instead of all the current code
        rY   r   rZ   r[   )r+   r   r)   r/   r]   r.   r^   r-   r_   r&   r   r%   r'   r\   r`   r   ra   rb   )	r0   rc   tmpdurre   rf   rg   rh   ri   rj   s	            r   __rsub__zDuration.__rsub__   s    eY'ZF!FMD= 	 $**%002uT[[7I7T7T7V W  {{T[[0H&xB7OE8jj4::-5G':Gyy7" MM\XCK " E 4;;&& 		s   C=D& &	D76D7c                 &   t        |t              rT| j                  dz  | j                  z   |j                  dz  |j                  z   k(  r| j                  |j                  k(  ryy| j                  dk(  r| j                  dk(  r| j                  |k(  S y)z
        If the years, month part and the timedelta part are both equal, then
        the two Durations are considered equal.
        r   TFr   r+   r)   r.   r-   r/   r0   rc   s     r   __eq__zDuration.__eq__  s|    
 eX&

R$++-b 5<</++- ::?t{{a/;;%''r   c                 &   t        |t              rT| j                  dz  | j                  z   |j                  dz  |j                  z   k7  s| j                  |j                  k7  ryy| j                  dk(  r| j                  dk(  r| j                  |k7  S y)z
        If the years, month part or the timedelta part is not equal, then
        the two Durations are considered not equal.
        r   TFr   rv   rw   s     r   __ne__zDuration.__ne__  s|    
 eX&

R$++-b 5<</, ::?t{{a/;;%''r   Nc                 b    ||t        d      ||t        d      ||| z   |z
  S ||| z
  z
  S )z
        Convert this duration into a timedelta object.

        This method requires a start datetime or end datetimem, but raises
        an exception if both are given.
        zstart or end requiredzonly start or end allowed)r_   )r0   startends      r   totimedeltazDuration.totimedelta/  sS     =S[455899DLE))cDj!!r   )	r   r   r   r   r   r   r   r   r   )NN)rO   rN   __qualname____doc__r8   r=   rA   rE   rK   rP   rS   rW   rk   __radd__rn   __rmul__rq   rt   rx   rz   r~   r$   r   r   r)   r)   *   s    0 
2$*!
<-^ H H*(T"""r   r)   N)	r   datetimer   decimalr   r   r   r'   r)   r$   r   r   <module>r      s(     ("
R" R"r   