
    Bd                     D    d Z dZdZddlZddlZddlZ G d d          ZdS )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dS )"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                 x    | t           _        | t          j                            |           ndt           _        dS 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%%%D 	    c                     | t           _        dS r   )r   myTimer   s    r   setTimezMyTime.setTime<   s     &---r   c                  X    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*     ]
+--
+fm
$
$$r   c                      t           j        t          j                                        S t           j        t           j        k    rt           j        S t          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^   sS     ]




!
!!]f---

			(	(	7	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   s2     ]am
.


.
'
''r   %Y-%m-%d %H:%M:%Sc                     | dk     r3t           j                             |                               d          nt          j         dddddd          }|                    |          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   ss     , && WW   $,$5dBBB$O$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                 8   t          | t          t          t          f          r| S t          j                            d|           } t          j        D ]\  }}|                    ||           } t          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		33  3//#	cr   c                   $    e Zd ZdZd Zd Zd ZdS )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                     || _         d S N)sec)selfrC   s     r   __init__zMyTime.seconds2str.__init__   s    4888r   c                    | j         }d}|dk    rt          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    rt          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    rt          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    rst          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-   r0         zh r/   i  zm i  
   ih      zs z0 )rC   r3   roundr4   str)rD   scrs       r   __str__zMyTime.seconds2str.__str__   s    x1Q8mmE%((5.!!""AAsFdAAHAAvv#ad))d
QAFA #a&&4-QSbS6M6kkE%((4-  !!AAsFdAAHABww#ae**t
QQ"WQ #a&&4-QSbS6M5jjE%((2+AAtGtAQ$YQBww#ae**t
QQ"WQ #a&&4-QSbS6M4iiE%((2+AAsFdAAHAAvv#ad))d
QAFASbS6M	12ggQUd	AAGA Q$AAH A
CRC&=r   c                 *    |                                  S rB   )rT   )rD   s    r   __repr__zMyTime.seconds2str.__repr__   s    
,,..r   N)__name__
__module____qualname____doc__rE   rT   rV   r   r   r   seconds2strr@      sL        	 	  * * *V    r   r[   rB   )r   )rW   rX   rY   rZ   r   r   r   staticmethodr   r   r   r   r   r   r*   recompiler6   r9   _str2sec_subpartrP   r8   r>   r[   r   r   r   r   r   !   s         
A 
A ,
A   , 	 	 ,	 % % ,% 	8 	8 ,	8 ( ( ( ,( 
 
 
 ,
 122N++, 2:011CCMM8IJ2:!3344CC
OO8KL2:011CC@Y<Z<Z8Z[2:!3344CC@V<W<W8WX2:!5566CCFF
C2:!5566CCGGD2:!3344CCJJG   ,89 9 9 9 9 9 9 9 9 9r   r   )
__author____copyright____license__r	   r]   r   r   r   r   r   <module>rc      sm   ( 
2  				 J J J J J J J J J Jr   