Te$BdZddlZddlZddlmZddlmZddlm Z ddlm Z gdZ eGd d eZ d Zd Zdd dZ ddefdZ d defdZd!defdZdefdZdZdZdZdZd"defdZdS)#zYTime humanizing functions. These are largely borrowed from Django's `contrib.humanize`. N)Enum)total_ordering)gettext)ngettext) naturaldelta naturaltime naturalday naturaldate precisedeltac4eZdZdZdZdZdZdZdZdZ dZ d Z d S) UnitrrcL|j|jur|j|jkStSN) __class__valueNotImplemented)selfothers m/builddir/build/BUILD/imunify360-venv-2.3.3/opt/imunify360/venv/lib/python3.11/site-packages/humanize/time.py__lt__z Unit.__lt__$s& >U_ , ,: + +N) __name__ __module__ __qualname__ MICROSECONDS MILLISECONDSSECONDSMINUTESHOURSDAYSMONTHSYEARSrrrrrsKLLGG E D F Errc>tjSr)dtdatetimenowr*rr_nowr/*s ;??  rcH|jdkrt}|||zz S|S)zReturn an "absolute" value for a timedelta, always representing a time distance. Args: delta (datetime.timedelta): Input timedelta. Returns: datetime.timedelta: Absolute timedelta. r)daysr/)deltar.s r abs_timedeltar3.s, zA~~ffcEk"" Lrr.cX|st}t|tjr|}||z }nht|tjr||z }|}nF t |}tj|}||z }n#t tf$rd|fcYSwxYw|t|fS)zTurn a value into a date and a timedelta which represents how long ago it was. If that's not possible, return `(None, value)`. )secondsN) r/ isinstancer,r- timedeltaint ValueError TypeErrorr3)rr.dater2s rdate_and_deltar==s ff%%% e  E2< ( ( U{ JJEL///E;DDI&   ;     u%% %%s)BBBTr6returnct|}|tjtjtjfvrt d|d|}t ||\}}||S|}t|j}t|j } | dz} | dz} t| dz}| s_| dkrX|dkr|tjkr)|j d krtd d |j |j zS|tjks%|tjkr3d |j cxkrd kr!nn|j d z } td d| | zStdS|dkrtdS|dkrtdd||zSd|cxkrdkrnntdSd|cxkrdkrnn|dz} tdd| | zSd|cxkrdkrnntdSd|kr|dz} tdd| | zSdS| dkrj| dkrtdS|stdd| | zS|stdd| | zS|dkrtd Std!d"||zS| dkrh|s| std#S|std$d%| | zS|r)|dkrtd&Std'd(||zStd$d%| | zStd)d*| | zS)+aReturn a natural representation of a timedelta or number of seconds. This is similar to `naturaltime`, but does not add tense to the result. Args: value (datetime.timedelta): A timedelta or a number of seconds. months (bool): If `True`, then a number of months (based on 30.5 days) will be used for fuzziness between years. minimum_unit (str): The lowest unit that can be used. when (datetime.datetime): Point in time relative to which _value_ is interpreted. Defaults to the current time in the local timezone. Returns: str: A natural representation of the amount of time elapsed. Examples Compare two timestamps in a custom local timezone:: import datetime as dt from dateutil.tz import gettz berlin = gettz("Europe/Berlin") now = dt.datetime.now(tz=berlin) later = now + dt.timedelta(minutes=30) assert naturaldelta(later, when=now) == "30 minutes" zMinimum unit 'z' not supportedr4Nm>@rr%d microsecond%d microsecondsi@B%d millisecond%d millisecondsa momentza second< %d second %d secondsxza minute %d minute %d minutesi zan hour%d hour%d hoursza day%d day%d daysza month%d month %d monthsza yearz1 year, %d dayz1 year, %d daysz1 year, 1 monthz1 year, %d monthz1 year, %d months%d year%d years)rupperr$r#r"r:r=absr6r1r9 microsecondsr_)rmonths minimum_unitwhentmpr<r2 use_monthsr6r1years millisecondsminuteshourss rrrTsB |!!## $C 4<!2D4EFFFG,GGGHHHL D111KD% | J%-  G uz??D CKE #:D   F :>TAXX a<<t000U5G$5N5N-/@%BTUU()!222 111E.:::::::::$1D8 -/@,OO"#Z== \\Z== r\\Kw??'I I 7 S Z== G " " " "d " " " " "mGKw??'I I W ' ' ' 'x ' ' ' ' 'Y<<  G^^tOEIz599EA A^ ! 199W::  JHi66= = J)T::TAA1||# K@@6II ! Nd NX;;  N,.?FFM M  N{{*+++/1DfMMPVV,.?FFM M :u55==rFc^|p t}t||\}}||St|tjtjfr||k}|rt dnt d}t||||}|t dkrt dS||zS)aReturn a natural representation of a time in a resolution that makes sense. This is more or less compatible with Django's `naturaltime` filter. Args: value (datetime.datetime, int): A `datetime` or a number of seconds. future (bool): Ignored for `datetime`s, where the tense is always figured out based on the current time. For integers, the return value will be past tense by default, unless future is `True`. months (bool): If `True`, then a number of months (based on 30.5 days) will be used for fuzziness between years. minimum_unit (str): The lowest unit that can be used. when (datetime.datetime): Point in time relative to which _value_ is interpreted. Defaults to the current time in the local timezone. Returns: str: A natural representation of the input in a resolution that makes sense. r4Nz %s from nowz%s ago)r]rGr.)r/r=r7r,r-r8rZr) rfuturer[r\r]r.r<r2agos rr r s2 .$&&C C000KD% | %"+r|455$ 5!M   !H++C  4 @ @ @E * xx ;r%b %dc tj|j|j|j}n&#t $r|cYSt tf$r|cYSwxYw|tjz }|j dkrtdS|j dkrtdS|j dkrtdS| |S)zReturn a natural day. For date values that are tomorrow, today or yesterday compared to present day return representing string. Otherwise, return a string formatted according to `format`. rtodayrtomorrow yesterday) r,r<yearmonthdayAttributeError OverflowErrorr:rir1rZstrftime)rformatr2s rr r s EK;;  : &  BGMMOO #E zQzz q}} r  ~~ >>& ! !!%( A A  A cL tj|j|j|j}n&#t $r|cYSt tf$r|cYSwxYwt|tj z }|j dkrt|dSt|S)zKLike `naturalday`, but append a year for dates more than ~five months away.gc@z%b %d %Y) r,r<rmrnrorprqr:r3rir1r )rr2s rr r s EK;;  : &  %"'--//1 2 2E z\!!%,,, e  rtcL||kr||z dfS||vrd|fSt||S)aDivide `value` by `divisor` returning the quotient and remainder. If `unit` is `minimum_unit`, makes the quotient a float number and the remainder will be zero. The rational is that if `unit` is the unit of the quotient, we cannot represent the remainder because it would require a unit smaller than the `minimum_unit`. >>> from humanize.time import _quotient_and_remainder, Unit >>> _quotient_and_remainder(36, 24, Unit.DAYS, Unit.DAYS, []) (1.5, 0) If unit is in `suppress`, the quotient will be zero and the remainder will be the initial value. The idea is that if we cannot use `unit`, we are forced to use a lower unit so we cannot do the division. >>> _quotient_and_remainder(36, 24, Unit.DAYS, Unit.HOURS, [Unit.DAYS]) (0, 36) In other case return quotient and remainder as `divmod` would do it. >>> _quotient_and_remainder(36, 24, Unit.DAYS, Unit.HOURS, []) (1, 12) r)divmod)rdivisorunitr\suppresss r_quotient_and_remainderr{sC2 |##   5zeW%%%rcF||kr |||z zdfS||vr d|||zzfS||fS)aReturn a tuple with two values. If the unit is in `suppress`, multiply `value1` by `ratio` and add it to `value2` (carry to right). The idea is that if we cannot represent `value1` we need to represent it in a lower unit. >>> from humanize.time import _carry, Unit >>> _carry(2, 6, 24, Unit.DAYS, Unit.SECONDS, [Unit.DAYS]) (0, 54) If the unit is the minimum unit, `value2` is divided by `ratio` and added to `value1` (carry to left). We assume that `value2` has a lower unit so we need to carry it to `value1`. >>> _carry(2, 6, 24, Unit.DAYS, Unit.DAYS, []) (2.25, 0) Otherwise, just return the same input: >>> _carry(2, 6, 24, Unit.DAYS, Unit.SECONDS, []) (2, 6) rr*)value1value2ratiorymin_unitrzs r_carryr8sM. x%'++   6FUN*++rc\||vr'tD]}||kr||vr|cStd|S)aReturn a minimum unit suitable that is not suppressed. If not suppressed, return the same unit: >>> from humanize.time import _suitable_minimum_unit, Unit >>> _suitable_minimum_unit(Unit.HOURS, []).name 'HOURS' But if suppressed, find a unit greather than the original one that is not suppressed: >>> _suitable_minimum_unit(Unit.HOURS, [Unit.HOURS]).name 'DAYS' >>> _suitable_minimum_unit(Unit.HOURS, [Unit.HOURS, Unit.DAYS]).name 'MONTHS' z@Minimum unit is suppressed and no suitable replacement was found)rr:)rrzrys r_suitable_minimum_unitrWsW$8  Dh4x#7#7  N    Orcrt|}tD]}||krn|| |S)aExtend suppressed units (if any) with all units lower than the minimum unit. >>> from humanize.time import _suppress_lower_units, Unit >>> [x.name for x in sorted(_suppress_lower_units(Unit.SECONDS, [Unit.DAYS]))] ['MICROSECONDS', 'MILLISECONDS', 'DAYS'] )setradd)rrzus r_suppress_lower_unitsrusB8}}H  == E Q Orr*%0.2fc t|\}}||Sd|D}t|}t||}~t ||}|j}|j}|j} tt\} } } } }}}}t|d|||\}}t|d|||\}}t||d|||\}}t|d|||\}}t|d| ||\}}t|| d| ||\}} t| d | ||\}} t| d d | ||\} }d d |fdd|fdd|fdd|fdd|fdd|fdd|fdd| fg}g}ttt|D]\}}|\}}}|d ks|si||krct|||}||kr4tj|d d kr|d|}|||z||krnt%|d kr|d Sd|dd}|d} t)d|| fzS) a%Return a precise representation of a timedelta. ```pycon >>> import datetime as dt >>> from humanize.time import precisedelta >>> delta = dt.timedelta(seconds=3633, days=2, microseconds=123000) >>> precisedelta(delta) '2 days, 1 hour and 33.12 seconds' ``` A custom `format` can be specified to control how the fractional part is represented: ```pycon >>> precisedelta(delta, format="%0.4f") '2 days, 1 hour and 33.1230 seconds' ``` Instead, the `minimum_unit` can be changed to have a better resolution; the function will still readjust the unit to use the greatest of the units that does not lose precision. For example setting microseconds but still representing the date with milliseconds: ```pycon >>> precisedelta(delta, minimum_unit="microseconds") '2 days, 1 hour, 33 seconds and 123 milliseconds' ``` If desired, some units can be suppressed: you will not see them represented and the time of the other units will be adjusted to keep representing the same timedelta: ```pycon >>> precisedelta(delta, suppress=['days']) '49 hours and 33.12 seconds' ``` Note that microseconds precision is lost if the seconds and all the units below are suppressed: ```pycon >>> delta = dt.timedelta(seconds=90, microseconds=100) >>> precisedelta(delta, suppress=['seconds', 'milliseconds', 'microseconds']) '1.50 minutes' ``` If the delta is too small to be represented with the minimum unit, a value of zero will be returned: ```pycon >>> delta = dt.timedelta(seconds=1) >>> precisedelta(delta, minimum_unit="minutes") '0.02 minutes' >>> delta = dt.timedelta(seconds=0.1) >>> precisedelta(delta, minimum_unit="minutes") '0 minutes' ``` NcLg|]!}t|"Sr*)rrW).0ss r z precisedelta..s$222AQWWYY222rr@rAiQrLrHg.ArBrrrUrVrSrTrQrRrOrPrMrNrIrJrErFrCrDz%dz, rkz %s and %s)r=rrWrrr1r6rYlistr{rzipreversedrmathmodfreplaceappendlenjoinrZ)!rr\rzrsr<r2rr1secsusecsr"r#r$r%r&r'r(r)r`r[rcrbmsecs_unusedfmtstextsryfmt singular_txt plural_txtfmt_txtheadtails! rr r sF!''KD% | 22222HL&&(()H%h99H%Xx88H :D =D  EOS PPLL,%vu *$UHhOOKE4*4vxRRLFDdItXxHHJD$)$eXxPPKE4+D"gxRRMGTuc7HhGGKD%* t\8XLE5 E1axJJNE7 J& [&) 9d# J& lG, lD) ,e4 ,e4 D E$..   c*-' j% 199U9tx'7'7|Z??GxDIe$4$4Q$7!$;$;!//$77 LL5 ) ) ) 8   E  5zzQQx 99U3B3Z D 9D [>>T4L ((r)Tr6N)FTr6N)rg)r6r*r)__doc__r-r,renumr functoolsri18nrrZr__all__rr/r3r=strrr r r r{rrrr r*rrrs  $$$$$$        4       "&&&&&&2  l>l>  l>l>l>l>b   ''  ''''T""""""4 #     &&&B   ><    \)\)PS\)\)\)\)\)\)r