BF+ENH: added %m-%d-%Y pattern + do not add %Y for Feb 29 fix if already present in the pattern

although %m-%d-%Y is ambioius with %d-%m-%Y it comes after so it should not
be too dangerous (i.e. in upcoming days having smth like 02-01 should work as before matching
first one first) and proper fix to select between the two should follow some time
soon
_tent/robust_datematching
Yaroslav Halchenko 13 years ago
parent 10729f96b9
commit b257be4cd1

@ -117,6 +117,12 @@ class DateDetector:
template.setRegex("\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}") template.setRegex("\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}")
template.setPattern("%d-%m-%Y %H:%M:%S") template.setPattern("%d-%m-%Y %H:%M:%S")
self.__templates.append(template) self.__templates.append(template)
# 01-27-2012 16:22:44.252
template = DateStrptime()
template.setName("Month-Day-Year Hour:Minute:Second[.Millisecond]")
template.setRegex("\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}")
template.setPattern("%m-%d-%Y %H:%M:%S")
self.__templates.append(template)
# TAI64N # TAI64N
template = DateTai64n() template = DateTai64n()
template.setName("TAI64N") template.setName("TAI64N")

@ -140,12 +140,22 @@ class DateStrptime(DateTemplate):
conv = self.convertLocale(dateMatch.group()) conv = self.convertLocale(dateMatch.group())
try: try:
date = list(time.strptime(conv, self.getPattern())) date = list(time.strptime(conv, self.getPattern()))
except ValueError, e: except (ValueError, re.error), e:
# Try to add the current year to the pattern. Should fix # Try to add the current year to the pattern. Should fix
# the "Feb 29" issue. # the "Feb 29" issue.
opattern = self.getPattern()
# makes sense only if %Y is not in already:
if not '%Y' in opattern:
pattern = "%s %%Y" % opattern
conv += " %s" % MyTime.gmtime()[0] conv += " %s" % MyTime.gmtime()[0]
pattern = "%s %%Y" % self.getPattern()
date = list(time.strptime(conv, pattern)) date = list(time.strptime(conv, pattern))
else:
# we are helpless here
raise ValueError(
"Given pattern %r does not match. Original "
"exception was %r and Feb 29 workaround could not "
"be tested due to already present year mark in the "
"pattern" % (opattern, e))
if date[0] < 2000: if date[0] < 2000:
# There is probably no year field in the logs # There is probably no year field in the logs
date[0] = MyTime.gmtime()[0] date[0] = MyTime.gmtime()[0]

@ -67,6 +67,7 @@ class DateDetectorTest(unittest.TestCase):
"Jan 23 21:59:59", "Jan 23 21:59:59",
"2005.01.23 21:59:59", "2005.01.23 21:59:59",
"23/01/2005 21:59:59", "23/01/2005 21:59:59",
"01-23-2005 21:59:59.252", # reported on f2b, causes Feb29 fix to break
): ):
log = sdate + "[sshd] error: PAM: Authentication failure" log = sdate + "[sshd] error: PAM: Authentication failure"
# exclude # exclude

Loading…
Cancel
Save