Question

I'm trying to parse a mbox format email spool.

I have code that does this:

if string.find(line, 'Date: ') == 0:                     
    try:
        when = time.mktime(time.strptime(line[6:30], "%a, %d %b %Y %H:%M:%S"))

Usually it seems to work OK, except that when line = 'Date: Sat, 17 Apr 2004 22:29:37 -0400\n' it seems to give the wrong result (22:29:03 instead of 22:29:37).

Here's my pdb trace:

(Pdb) p line
'Date: Sat, 17 Apr 2004 22:29:37 -0400\n'
(Pdb) p time.strptime(line[6:30], "%a, %d %b %Y %H:%M:%S")
time.struct_time(tm_year=2004, tm_mon=4, tm_mday=17, tm_hour=22, tm_min=29, tm_sec=3, tm_wday=5, tm_yday=108, tm_isdst=-1)
(Pdb)

The result seems to be off by 34 seconds. What am I doing wrong?

Was it helpful?

Solution

You are slicing your line too short; the second value is exclusive, not inclusive:

>>> line[6:30]
'Sat, 17 Apr 2004 22:29:3'
>>> line[6:31]
'Sat, 17 Apr 2004 22:29:37'
>>> time.strptime(line[6:31], "%a, %d %b %Y %H:%M:%S")
time.struct_time(tm_year=2004, tm_mon=4, tm_mday=17, tm_hour=22, tm_min=29, tm_sec=37, tm_wday=5, tm_yday=108, tm_isdst=-1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top