fail2ban.server.mytime module

class fail2ban.server.mytime.MyTime

Bases: object

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.

Attributes:
alternateNow
alternateNowTime
myTime

Methods

gmtime()

Decorate time.gmtime() for the purpose of testing mocking

localtime([x])

Decorate time.localtime() for the purpose of testing mocking

now()

Decorate datetime.now() for the purpose of testing mocking

seconds2str(sec)

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.

setAlternateNow(t)

Set current time.

setTime(t)

Set current time.

str2seconds(val)

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 .

time()

Decorate time.time() for the purpose of testing mocking

time2str(unixTime[, format])

Convert time to a string representing as date and time using given format.

alternateNow = None
alternateNowTime = None
static gmtime()

Decorate time.gmtime() for the purpose of testing mocking

@return time.gmtime() if setTime was called with None

static localtime(x=None)

Decorate time.localtime() for the purpose of testing mocking

@return time.localtime() if setTime was called with None

myTime = None
static now()

Decorate datetime.now() for the purpose of testing mocking

@return datetime.now() if setTime was called with None

class seconds2str(sec)

Bases: object

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

static setAlternateNow(t)

Set current time.

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

@param t the time to set or None

static setTime(t)

Set current time.

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

@param t the time to set or None

static str2seconds(val)

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”)

static time()

Decorate time.time() for the purpose of testing mocking

@return time.time() if setTime was called with None

static time2str(unixTime, format='%Y-%m-%d %H:%M:%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