B #a0@sDdZddlmZddlmZmZddZddZGdd d eZ d S) 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)Decimal ROUND_FLOORcCs@||||}}||t}|||}||7}t||fS)z- A divmod function with boundaries. ) to_integralrint)vallowhighabdivmodr7/tmp/pip-target-jgxl_w8r/lib/python/isodate/duration.pyfquotmod%s  rcCsD|dkr dS|dkrdS|ddks<|ddkr@|ddkr@d Sd S) zO Determines the number of days of a specific month in a specific year. ) ) irdrr)yearmonthrrrmax_days_in_month6s$r#c @seZdZdZd"ddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e Z ddZeZddZddZddZddZd#d d!ZdS)$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 rc CsRt|tstt|}t| ts,tt| } ||_| |_t||||||||_dS)zN Initialise this Duration instance with the given parameters. N) isinstancerstrmonthsyearsrtdelta) selfdaysseconds microsecondsZ millisecondsminuteshoursweeksr'r(rrr__init__Zs     zDuration.__init__cCs|jS)N)__dict__)r*rrr __getstate__hszDuration.__getstate__cCs|j|dS)N)r2update)r*staterrr __setstate__kszDuration.__setstate__cCs t|j|S)zU Provide direct access to attributes of included timedelta instance. )getattrr))r*namerrr __getattr__nszDuration.__getattr__cCs\g}|jr|d|j|jrBd}|jdkr2d}|||j|t|jd|S)zW Return a string representation of this duration similar to timedelta. z%d yearsz %d monthsrz%d monthz, )r(appendr'r&r)join)r*paramsfmtrrr__str__ts zDuration.__str__cCs.d|jj|jj|jj|jj|jj|j|jfS)z= Return a string suitable for repr(x) calls. z&%s.%s(%d, %d, %d, years=%d, months=%d)) __class__ __module____name__r)r+r,r-r(r')r*rrr__repr__s  zDuration.__repr__cCst|j|j|jfS)zp Return a hash of this instance so that it can be used in, for example, dicts and sets. )hashr)r'r()r*rrr__hash__szDuration.__hash__cCs"t|j |j d}|j |_|S)zg A simple unary minus. Returns a new Duration instance with all it's negated. )r(r')r$r(r'r))r*Z negdurationrrr__neg__s zDuration.__neg__c Cst|tr8t|j|j|j|jd}|j|j|_|Syt|jrVt|js^td|j|j}t |dd\}}|j |j|}t ||}|j |kr|}n|j }|j |||d}|j|Stk rYnXy t|j|jd}|j||_|Stk rYnXtS)ze Durations can be added with Duration, timedelta, date and datetime objects. )r(r'z>fractional years or months not supported for date calculationsr )r!r"day)r%r$r(r'r)float is_integer ValueErrorr"rr!r#rGreplaceAttributeErrorNotImplemented) r*other newdurationnewmonthcarrynewyearmaxdaysnewdaynewdtrrr__add__s6       zDuration.__add__cCs6t|tr2t|j||j|d}|j||_|StS)N)r(r')r%rr$r(r'r)rM)r*rNrOrrr__mul__s  zDuration.__mul__cCsrt|tr8t|j|j|j|jd}|j|j|_|Sy t|j|jd}|j||_|Stk rlYnXtS)zj It is possible to subtract Duration and timedelta objects from Duration objects. )r(r')r%r$r(r'r) TypeErrorrM)r*rNrOrrr__sub__s   zDuration.__sub__c Cst|trt}||_||Syt|jrfractional years or months not supported for date calculationsrrF)r!r"rG)r%rr$r)rHr(rIr'rJr"rr!r#rGrKrLrM) r*rNZtmpdurrPrQrRrSrTrUrrr__rsub__s(     zDuration.__rsub__cCs`t|tr>|jd|j|jd|jkr:|j|jkr:dSdS|jdkr\|jdkr\|j|kSdS)z If the years, month part and the timedelta part are both equal, then the two Durations are considered equal. rTFr)r%r$r(r'r))r*rNrrr__eq__s   zDuration.__eq__cCs`t|tr>|jd|j|jd|jks6|j|jkr:dSdS|jdkr\|jdkr\|j|kSdS)z If the years, month part or the timedelta part is not equal, then the two Durations are considered not equal. rTFr)r%r$r(r'r))r*rNrrr__ne__#s   zDuration.__ne__NcCsP|dkr|dkrtd|dk r0|dk r0td|dk rD|||S|||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. Nzstart or end requiredzonly start or end allowed)rJ)r*startendrrr totimedelta4s zDuration.totimedelta) rrrrrrrrr)NN)rAr@ __qualname____doc__r1r3r6r9r>rBrDrErV__radd__rW__rmul__rYrZr[r\r_rrrrr$Cs&   , (r$N) radatetimerdecimalrrrr#objectr$rrrr s