python compare time

Posted by Jesse Siu on Stack Overflow See other posts from Stack Overflow or by Jesse Siu
Published on 2012-09-11T03:22:07Z Indexed on 2012/09/11 3:38 UTC
Read the original article Hit count: 153

Filed under:

i want to using python create filter for a log file. get recent 7 days record. but when i didn't know how to compare time. like current time is 11/9/2012, i want to get records from 04/9/2012 to now

the log file like

 Sat Sep  2 03:32:13 2012 [pid 12461] CONNECT: Client "66.249.68.236"
 Sat Sep  2 03:32:13 2012 [pid 12460] [ftp] OK LOGIN: Client "66.249.68.236", anon     password "[email protected]"
 Sat Sep  2 03:32:14 2012 [pid 12462] [ftp] OK DOWNLOAD: Client "66.249.68.236",   "/pub/10.5524/100001_101000/100022/readme.txt", 451

i using this one

  def OnlyRecent(line):
       print time.strptime(line.split("[")[0].strip(),"%a %b %d %H:%M:%S %Y")
       print time.time()
    if  time.strptime(line.split("[")[0].strip(),"%a %b %d %H:%M:%S %Y") < time.time():
        return True
    return False

But it shows

   (2012, 9, 2, 3, 32, 13, 5, 246, -1) 
    1347332968.08
   (2012, 9, 2, 3, 32, 13, 5, 246, -1)
    1347332968.08
   (2012, 9, 2, 3, 32, 14, 5, 246, -1)
    1347332968.08

the time format is different, and it can't compare time. So how to set this comparison in 7 days. Thanks

© Stack Overflow or respective owner

Related posts about python