a =*f@s6dZdZdZddlZddlZddlZGdddZdS)z Cyril Jaquierz Copyright (c) 2004 Cyril JaquierZGPLNc @sReZdZdZdZdZdZeddZeddZ eddZ ed d Z ed d Z ed(d dZ ed)ddZedZedZdZeeddedfeeddedfeeddedfeeddedfeeddedfeed ded!feed"ded#ffZed$d%ZGd&d'd'ZdS)*MyTimea`A wrapper around time module primarily for testing purposes This class is a wrapper around time.time() and time.gmtime(). When performing unit test, it is very useful to get a fixed value from these functions. Thus, time.time() and time.gmtime() should never be called directly. This wrapper should be called instead. The API are equivalent. NcCs$|t_|durtj|ndt_dSzrSet current time. Use None in order to always get the real current time. @param t the time to set or None N)ralternateNowTimedatetime fromtimestamp alternateNowtr :/usr/lib/python3.9/site-packages/fail2ban/server/mytime.pysetAlternateNow/s zMyTime.setAlternateNowcCs |t_dSr)rmyTimerr r r setTime<s zMyTime.setTimecCstjdurtStjSdS)zqDecorate time.time() for the purpose of testing mocking @return time.time() if setTime was called with None N)rr timer r r r rGs z MyTime.timecCs"tjdurtSttjSdS)zuDecorate time.gmtime() for the purpose of testing mocking @return time.gmtime() if setTime was called with None N)rr rgmtimer r r r rSs z MyTime.gmtimecCs4tjdurtjStjtjkr&tjStjtjS)zwDecorate datetime.now() for the purpose of testing mocking @return datetime.now() if setTime was called with None N)rr rnowrrrr r r r r^s    z MyTime.nowcCs,tjdus|durt|SttjSdS)z{Decorate time.localtime() for the purpose of testing mocking @return time.localtime() if setTime was called with None N)rr r localtime)xr r r rjs zMyTime.localtime%Y-%m-%d %H:%M:%ScCs:|dkrtj|jddntdddddd}||S) zConvert time to a string representing as date and time using given format. Default format is ISO 8601, YYYY-MM-DD HH:MM:SS without microseconds. @return ISO-capable string representation of given unixTime lpr)Z microsecondi' ;)rrreplacestrftime)ZunixTimeformatZdtr r r time2strus zMyTime.time2strz(?i)(?<=[a-z])(\d)z (\d)\s+(\d)z(?i)(?<=[\d\s])(%s)\bz days?|da|dd?*Qzweeks?|wee?|ww?: z months?|mon?gTDAzyears?|yea?|yy?g~~Azseconds?|sec?|ss?zminutes?|min?|mm?<zhours?|hou?|hh?cCsTt|tttfr|Stjd|}tjD]\}}|||}q(tjd|}t |S)a)Wraps string expression like "1h 2m 3s" into number contains seconds (3723). The string expression will be evaluated as mathematical expression, spaces between each groups will be wrapped to "+" operand (only if any operand does not specified between). Because of case insensitivity and overwriting with minutes ("m" or "mm"), the short replacement for month are "mo" or "mon". Ex: 1hour+30min = 5400 0d 1h 30m = 5400 1year-6mo = 15778800 6 months = 15778800 warn: month is not 30 days, it is a year in seconds / 12, the leap years will be respected also: >>>> float(str2seconds("1month")) / 60 / 60 / 24 30.4375 >>>> float(str2seconds("1year")) / 60 / 60 / 24 365.25 @returns number (calculated seconds from expression "val") z \1z\1+\2) isinstanceintfloatcomplexr _str2sec_prepsub_str2sec_parts _str2sec_finieval)valZrexpZrplr r r str2secondsszMyTime.str2secondsc@s(eZdZdZddZddZddZdS) zMyTime.seconds2straConverts seconds to string on demand (if string representation needed). Ex: seconds2str(86400*390) = 1y 3w 4d seconds2str(86400*368) = 1y 3d seconds2str(86400*365.5) = 1y seconds2str(86400*2+3600*7+60*15) = 2d 7h 15m seconds2str(86400*2+3599) = 2d 1h seconds2str(3600-5) = 1h seconds2str(3600-10) = 59m 50s seconds2str(59) = 59s cCs ||_dSN)sec)selfr/r r r __init__szMyTime.seconds2str.__init__cCs:|j}d}|dkrttt|d}t|dd}|d;}|dkrb|t|dd7}|d;}|rv|t|d7}|dd S|d krttt|d }t|d d}|d ;}|d kr|t|d d7}|d ;}|r|t|d7}|dd S|dkrxttt|d}t|dd}|d;}|dkrV|t|dd7}|d;}|rl|t|d7}|dd S|dkrttt|d}t|dd}|d;}|dkr|t|dd7}|d;}|dd Sd}|dkr |t|dd7}|d;}|r"|t|d7}n |js.d}|dd S)Ni3rimzy zw zd rr"zh r!izm i  ihzs z0 )r/r$roundr%str)r0scrr r r __str__sN          zMyTime.seconds2str.__str__cCs|Sr.)r?)r0r r r __repr__szMyTime.seconds2str.__repr__N)__name__ __module__ __qualname____doc__r1r?r@r r r r seconds2strs +rE)N)r)rArBrCrDr rr staticmethodr rrrrrrrecompiler'r*Z_str2sec_subpartr;r)r-rEr r r r r!s@            r) __author__Z __copyright__Z __license__rrGrrr r r r s