ó Üœ^c@sMdZdZdZddlZddlZddlZddd„ƒYZdS(s Cyril Jaquiers Copyright (c) 2004 Cyril JaquiertGPLiÿÿÿÿNtMyTimec Bs´eZdZdZdZdZed„ƒZed„ƒZ ed„ƒZ ed„ƒZ ed„ƒZ edd„ƒZ edd„ƒZejd ƒZejd ƒZd Zejed ƒd ed ƒfejedƒd ed#ƒfejedƒd ed(ddƒfejedƒd ed-dƒfejedƒd edƒfejedƒd edƒfejedƒd ed.ƒffZed„ƒZRS(/s`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. cCs4|t_|dk r'tjj|ƒndt_dS(srSet current time. Use None in order to always get the real current time. @param t the time to set or None N(RtalternateNowTimetNonetdatetimet fromtimestampt alternateNow(tt((s:/usr/lib/python2.7/site-packages/fail2ban/server/mytime.pytsetAlternateNow/s cCs |t_dS(srSet current time. Use None in order to always get the real current time. @param t the time to set or None N(RtmyTime(R((s:/usr/lib/python2.7/site-packages/fail2ban/server/mytime.pytsetTime<s cCs$tjdkrtjƒStjSdS(sqDecorate time.time() for the purpose of testing mocking @return time.time() if setTime was called with None N(RR Rttime(((s:/usr/lib/python2.7/site-packages/fail2ban/server/mytime.pyR Gs cCs-tjdkrtjƒStjtjƒSdS(suDecorate time.gmtime() for the purpose of testing mocking @return time.gmtime() if setTime was called with None N(RR RR tgmtime(((s:/usr/lib/python2.7/site-packages/fail2ban/server/mytime.pyR Ss cCsHtjdkrtjjƒStjtjkr5tjStjjtjƒS(swDecorate datetime.now() for the purpose of testing mocking @return datetime.now() if setTime was called with None N(RR RRtnowRRR(((s:/usr/lib/python2.7/site-packages/fail2ban/server/mytime.pyR ^s  cCs<tjdks|dk r(tj|ƒStjtjƒSdS(s{Decorate time.localtime() for the purpose of testing mocking @return time.localtime() if setTime was called with None N(RR RR t localtime(tx((s:/usr/lib/python2.7/site-packages/fail2ban/server/mytime.pyRjs s%Y-%m-%d %H:%M:%ScCs%tjj|ƒjddƒj|ƒS(sÔConvert 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 t microsecondi(RRtreplacetstrftime(tunixTimetformat((s:/usr/lib/python2.7/site-packages/fail2ban/server/mytime.pyttime2strus s(?i)(?<=[a-z])(\d)s (\d)\s+(\d)s(?i)(?<=[\d\s])(%s)\bs days?|da|dd?t*ii<sweeks?|wee?|ww?is months?|mon?imiinii syears?|yea?|yy?sseconds?|sec?|ss?isminutes?|min?|mm?shours?|hou?|hh?cCst|ttttfƒr|Stjjd|ƒ}x)tjD]\}}|j||ƒ}q>Wtj jd|ƒ}t |ƒS(s)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") s \1s\1+\2( t isinstancetinttlongtfloattcomplexRt _str2sec_preptsubt_str2sec_partst _str2sec_finiteval(tvaltrexptrpl((s:/usr/lib/python2.7/site-packages/fail2ban/server/mytime.pyt str2seconds’sNi i€Qi¨i`'i€: iGiµiøˆi  i€†iGiµiøˆi  i€†i(t__name__t __module__t__doc__RR RRt staticmethodRR R R R RRtretcompileRRt_str2sec_subparttstrRR$(((s:/usr/lib/python2.7/site-packages/fail2ban/server/mytime.pyR!s0         ($  &((t __author__t __copyright__t __license__RR)R R(((s:/usr/lib/python2.7/site-packages/fail2ban/server/mytime.pyts