o ;s*bX@s6dZdZdZddlZddlZddlZGdddZdS)z Cyril Jaquierz Copyright (c) 2004 Cyril JaquierGPLNc @sDeZdZdZdZdZdZeddZeddZ eddZ ed d Z ed d Z ed&d dZ ed'ddZedZedZdZeeddedfeeddedfeeddedfeeddedfeeddedfeed ded!feed"ded#ffZed$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|t_dSdt_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 8/usr/lib/python3/dist-packages/fail2ban/server/mytime.pysetAlternateNow/s  zMyTime.setAlternateNowcCs |t_dSr)rmyTimer r r r setTime<s zMyTime.setTimecCstjdur tStjS)zqDecorate time.time() for the purpose of testing mocking @return time.time() if setTime was called with None N)rrtimer r r r rGs z MyTime.timecCstjdur tSttjS)zuDecorate time.gmtime() for the purpose of testing mocking @return time.gmtime() if setTime was called with None N)rrrgmtimer r r r rSs  z MyTime.gmtimecCs4tjdur tjStjtjkrtjStjtjS)zwDecorate datetime.now() for the purpose of testing mocking @return datetime.now() if setTime was called with None N)rrrnowrrrr r r r r^s   z MyTime.nowcCs(tjdus |durt|SttjS)z{Decorate time.localtime() for the purpose of testing mocking @return time.localtime() if setTime was called with None N)rrr localtime)xr r r rjs  zMyTime.localtime%Y-%m-%d %H:%M:%ScCs:|dkrtj|jddn tdddddd}||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' ;)rrreplacestrftime)unixTimeformatdtr r r time2strus  zMyTime.time2strz(?i)(?<=[a-z])(\d)z (\d)\s+(\d)z(?i)(?<=[\d\s])(%s)\bz days?|da|dd?*iQzweeks?|wee?|ww?i: z months?|mon?gTDAzyears?|yea?|yy?g~~Azseconds?|sec?|ss?zminutes?|min?|mm?<zhours?|hou?|hh?icCsTt|tttfr |Stjd|}tjD] \}}|||}qtjd|}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)valrexprplr r r str2secondsszMyTime.str2seconds)N)r)__name__ __module__ __qualname____doc__rrr staticmethodr rrrrrr recompiler(r+_str2sec_subpartstrr*r0r r r r r!s@           r) __author__ __copyright__ __license__rr6rrr r r r s