ogf<dZdZdZddlZddlZddlZGddZy)z Cyril Jaquierz Copyright (c) 2004 Cyril JaquierGPLNc eZdZdZdZdZdZedZedZ edZ edZ edZ eddZ ed d Zej d Zej d Zd Zej ed zdedzfej edzdedzfej edzdedzfej edzdedzfej edzdedzfej edzdedzfej edzdedzffZedZGddZy)!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. Nc|t_|)tjj|t_ydt_yzrSet current time. Use None in order to always get the real current time. @param t the time to set or None N)ralternateNowTimedatetime fromtimestamp alternateNowts 8/usr/lib/python3/dist-packages/fail2ban/server/mytime.pysetAlternateNowzMyTime.setAlternateNow/s7&)*8""1% <@ c|t_yr)rmyTimer s rsetTimezMyTime.setTime<s &-rcjtjtjStjS)zqDecorate time.time() for the purpose of testing mocking @return time.time() if setTime was called with None )rrtimerrrz MyTime.timeGs$ ]] ))+ --rctjtjStjtjS)zuDecorate time.gmtime() for the purpose of testing mocking @return time.gmtime() if setTime was called with None )rrrgmtimerrrrz MyTime.gmtimeSs-  ]] ++- ++fmm $$rctjtjjStjtjk(rtj Stjj tjS)zwDecorate datetime.now() for the purpose of testing mocking @return datetime.now() if setTime was called with None )rrr nowrr r rrrrz MyTime.now^s\  ]]      !! ]]f---       ( ( 77rctj|tj|StjtjS)z{Decorate time.localtime() for the purpose of testing mocking @return time.localtime() if setTime was called with None )rrr localtime)xs rrzMyTime.localtimejs4  ]]am ..  .. ''rc|dkr/tjj|jdntjdddddd}|j|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) microsecondi' ;)r r replacestrftime)unixTimeformatdts rtime2strzMyTime.time2strusa,&&  WW$,$5$5dBBB$O V rz(?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?c&t|tttfr|Stj j d|}tjD]\}}|j ||}tjj d|}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)valrexprpls r str2secondszMyTime.str2secondss}&c5'*+ :  -#((idC #s 3  3/# crc"eZdZdZdZdZdZy)MyTime.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 c||_yN)sec)selfrBs r__init__zMyTime.seconds2str.__init__s 48rc|j}d}|dk\rlttt|dz }t |dzdz}|dz}|dk\r|t |dzdzz }|dz}|r|t |dzz }|dd S|d k\rlttt|d z }t |d zdz}|d z}|d k\r|t |d zdzz }|d z}|r|t |dzz }|dd S|dk\rlttt|dz }t |dzdz}|dz}|dk\r|t |dzdzz }|dz}|r|t |dzz }|dd S|dk\rYttt|dz }t |dzdz}|dz}|dk\r|t |dzdzz }|dz}|dd Sd}|dk\r|t |dzdzz }|dz}|r|t |dzz }n|jsd}|dd S)Ni3r+imzy zw zd r,r/zh r.izm i  ihzs z0 )rBr2roundr3str)rCscrs r__str__zMyTime.seconds2str.__str__sK xx1Q8m E%(5. !"A AsF dAAHAAv#ad)d QAFA#a&4-Q Sb6M6k E%(4- !A AsF dAAHABw#ae*t QQ"WQ#a&4-Q Sb6M5j E%(2+ A AtG tAQ$YQBw#ae*t QQ"WQ#a&4-Q Sb6M4i E%(2+ A AsF dAAHAAv#ad)d QAFA Sb6M 12gQUd AAGAQ$A HH A CR&=rc"|jSrA)rS)rCs r__repr__zMyTime.seconds2str.__repr__s ,,.rN)__name__ __module__ __qualname____doc__rDrSrUrrr seconds2strr?s *VrrZrA)z%Y-%m-%d %H:%M:%S)rVrWrXrYrrr staticmethodrrrrrrr)recompiler5r8_str2sec_subpartrOr7r=rZrrrrr!s  A A  %% 8 8((  12N+,2::01CM8IJ 2::!334C O8KL 2::01C@Yrbs+( 2   JJr