
    ogf                     <    d Z dZdZddlZddlZddlZ G d d      Zy)zCyril Jaquierz Copyright (c) 2004 Cyril JaquierGPL    Nc                      e Zd ZdZdZdZdZed        Zed        Z	ed        Z
ed        Zed        Zedd       Zed d	       Z ej                   d
      Z ej                   d      ZdZ ej                   edz        d ed      z   f ej                   edz        d ed      z   f ej                   edz        d ed      z   f ej                   edz        d ed      z   f ej                   edz        d ed      z   f ej                   edz        d ed      z   f ej                   edz        d ed      z   ffZed        Z G d d      Zy)!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         _        | )t        j                  j                  |       t         _        ydt         _        yzrSet current time.

		Use None in order to always get the real current time.

		@param t the time to set or None
		N)r   alternateNowTimedatetimefromtimestampalternateNowts    8/usr/lib/python3/dist-packages/fail2ban/server/mytime.pysetAlternateNowzMyTime.setAlternateNow/   s7     &)*8""1% 	<@ 	    c                     | t         _        yr   )r   myTimer   s    r   setTimezMyTime.setTime<   s     &-r   c                  j    t         j                  t        j                         S t         j                  S )zqDecorate time.time() for the purpose of testing mocking

		@return time.time() if setTime was called with None
		)r   r   time r   r   r   zMyTime.timeG   s$     ]]
))+
--r   c                      t         j                  t        j                         S t        j                  t         j                        S )zuDecorate time.gmtime() for the purpose of testing mocking

		@return time.gmtime() if setTime was called with None
		)r   r   r   gmtimer   r   r   r   zMyTime.gmtimeS   s-     ]]
++-
++fmm
$$r   c                     t         j                  t        j                  j                         S t         j                  t         j                  k(  rt         j
                  S t        j                  j                  t         j                        S )zwDecorate datetime.now() for the purpose of testing mocking

		@return datetime.now() if setTime was called with None
		)r   r   r	   nowr   r   r
   r   r   r   r   z
MyTime.now^   s\     ]]





!!]]f---


				(	(	77r   c                     t         j                  | t        j                  |       S t        j                  t         j                        S )z{Decorate time.localtime() for the purpose of testing mocking

		@return time.localtime() if setTime was called with None
		)r   r   r   	localtime)xs    r   r   zMyTime.localtimej   s4     ]]am
..

..
''r   c                     | dk  r/t         j                   j                  |       j                  d      nt        j                   dddddd      }|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
		l    p r   )microsecondi'           ;   )r	   r
   replacestrftime)unixTimeformatdts      r   time2strzMyTime.time2stru   sa     , &&WW $,$5$5dBBB$O  
V	r   z(?i)(?<=[a-z])(\d)z(\d)\s+(\d)z(?i)(?<=[\d\s])(%s)\bzdays?|da|dd?*Q zweeks?|wee?|ww?:	 zmonths?|mon?g    TDAzyears?|yea?|yy?g    ~~Azseconds?|sec?|ss?   zminutes?|min?|mm?<   zhours?|hou?|hh?  c                 &   t        | t        t        t        f      r| S t        j
                  j                  d|       } t        j                  D ]  \  }}|j                  ||       }  t        j                  j                  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)

isinstanceintfloatcomplexr   _str2sec_prepsub_str2sec_parts_str2sec_finieval)valrexprpls      r   str2secondszMyTime.str2seconds   s}    & c5'*+
:  -#(( idC	#s	3  3/#	cr   c                   "    e Zd ZdZd Zd Zd Zy)MyTime.seconds2stra  Converts 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                     || _         y N)sec)selfrB   s     r   __init__zMyTime.seconds2str.__init__   s	    48r   c                    | j                   }d}|dk\  rlt        t        t        |      dz              }t	        |dz        dz   }|dz  }|dk\  r|t	        |dz        dz   z  }|dz  }|r|t	        |      dz   z  }|d d	 S |d
k\  rlt        t        t        |      dz              }t	        |dz        dz   }|dz  }|dk\  r|t	        |dz        dz   z  }|dz  }|r|t	        |      dz   z  }|d d	 S |dk\  rlt        t        t        |      dz              }t	        |dz        dz   }|dz  }|dk\  r|t	        |dz        dz   z  }|dz  }|r|t	        |      dz   z  }|d d	 S |dk\  rYt        t        t        |      dz              }t	        |dz        dz   }|dz  }|dk\  r|t	        |dz        dz   z  }|dz  }|d d	 S d}|dk\  r|t	        |dz        dz   z  }|dz  }|r|t	        |      dz   z  }n| j                   sd}|d d	 S )N   i3r+   im  zy    zw zd r,   r/         zh r.   i  zm i  
   ih      zs z0 )rB   r2   roundr3   str)rC   scrs       r   __str__zMyTime.seconds2str.__str__   sK    xx1Q8mE%(5.!"AAsFdAAHAAv#ad)d
QAFA#a&4-QSb6M6kE%(4- !AAsFdAAHABw#ae*t
QQ"WQ#a&4-QSb6M5jE%(2+AAtGtAQ$YQBw#ae*t
QQ"WQ#a&4-QSb6M4iE%(2+AAsFdAAHAAv#ad)d
QAFASb6M	12gQUd	AAGAQ$AHHA
CR&=r   c                 "    | j                         S rA   )rS   )rC   s    r   __repr__zMyTime.seconds2str.__repr__   s    
,,.r   N)__name__
__module____qualname____doc__rD   rS   rU   r   r   r   seconds2strr?      s    	*Vr   rZ   rA   )z%Y-%m-%d %H:%M:%S)rV   rW   rX   rY   r   r   r   staticmethodr   r   r   r   r   r   r)   recompiler5   r8   _str2sec_subpartrO   r7   r=   rZ   r   r   r   r   r   !   s    
A 
A   	 	 % % 	8 	8 ( ( 
 
 12N+, 2::01CM8IJ2::!334C
O8KL2::01C@Y<Z8Z[2::!334C@V<W8WX2::!556CF
C2::!556CGD2::!334CJG  89 9r   r   )
__author____copyright____license__r	   r\   r   r   r   r   r   <module>rb      s+   ( 
2  	 J Jr   