diff --git a/fail2ban/server/strptime.py b/fail2ban/server/strptime.py index 56f1c753..2da27c58 100644 --- a/fail2ban/server/strptime.py +++ b/fail2ban/server/strptime.py @@ -107,17 +107,17 @@ def zone2offset(tz, dt): been validated already) dt: datetime instance for offset computation """ - if isinstance(tz, basestring): - if len(tz) <= 3: # short tz (hh only) - # [+-]hh --> [+-]hh*60 - return int(tz)*60 - if tz[3] != ':': - # [+-]hhmm --> [+-]1 * (hh*60 + mm) - return int(tz[0]+'1') * (int(tz[1:3])*60 + int(tz[3:5])) - else: - # [+-]hh:mm --> [+-]1 * (hh*60 + mm) - return int(tz[0]+'1') * (int(tz[1:3])*60 + int(tz[4:6])) - return tz + if isinstance(tz, int): + return tz + if len(tz) <= 3: # short tz (hh only) + # [+-]hh --> [+-]hh*60 + return int(tz)*60 + if tz[3] != ':': + # [+-]hhmm --> [+-]1 * (hh*60 + mm) + return (-1 if tz[0] == '-' else 1) * (int(tz[1:3])*60 + int(tz[3:5])) + else: + # [+-]hh:mm --> [+-]1 * (hh*60 + mm) + return (-1 if tz[0] == '-' else 1) * (int(tz[1:3])*60 + int(tz[4:6])) def reGroupDictStrptime(found_dict, msec=False, default_tz=None): """Return time from dictionary of strptime fields diff --git a/fail2ban/tests/datedetectortestcase.py b/fail2ban/tests/datedetectortestcase.py index cbf1ac21..56970ac5 100644 --- a/fail2ban/tests/datedetectortestcase.py +++ b/fail2ban/tests/datedetectortestcase.py @@ -99,16 +99,16 @@ class DateDetectorTest(LogCaptureTestCase): ('UTC', logdt, dtUTC), # UTC ('UTC-0430', logdt, dt(2017, 1, 23, 19, 30)), ('GMT+12', logdt, dt(2017, 1, 23, 3, 0)), - (None, logdt, dt(2017, 1, 23, 14, 0)), # default CET in our test-framework + (None, logdt, dt(2017, 1, 23, 14, 0)), # default CET in our test-framework ('UTC+0300', logdt+' GMT', dtUTC), # GMT wins ('UTC', logdt+' GMT', dtUTC), # GMT wins ('UTC-0430', logdt+' GMT', dtUTC), # GMT wins - (None, logdt+' GMT', dtUTC), # GMT wins + (None, logdt+' GMT', dtUTC), # GMT wins ('UTC', logdt+' -1045', dt(2017, 1, 24, 1, 45)), # -1045 wins - (None, logdt+' -10:45', dt(2017, 1, 24, 1, 45)), # -1045 wins + (None, logdt+' -10:45', dt(2017, 1, 24, 1, 45)), # -1045 wins ('UTC', logdt+' +0945', dt(2017, 1, 23, 5, 15)), # +0945 wins - (None, logdt+' +09:45', dt(2017, 1, 23, 5, 15)), # +0945 wins - (None, logdt+' Z', dtUTC), # Z wins (UTC) + (None, logdt+' +09:45', dt(2017, 1, 23, 5, 15)), # +0945 wins + (None, logdt+' Z', dtUTC), # Z wins (UTC) ): logSys.debug('== test %r with TZ %r', log, tz) dd.default_tz=tz; datelog, _ = dd.getTime(log)