3

I'm using the dateutil library to parse some date strings, and getting weird results. I assumed the following date strings would all be equal, and that the timezone abbreviation in parenthesis was actually optional, but dropping it gets me a completely different value:

import datetime   
import dateutil.parser

parsed_d1 = dateutil.parser.parse('Sun May 13 2012 00:00:00 GMT-0400 (EDT)')   
parsed_d2 = dateutil.parser.parse('Sun May 13 2012 00:00:00 GMT-0400')   
parsed_d3 = dateutil.parser.parse('Sun May 13 2012 00:00:00-0400')   

print str(parsed_d1)   
print str(parsed_d2)   
print str(parsed_d3) 

Output:

2012-05-13 00:00:00-04:00   
2012-05-13 00:00:00+04:00   
2012-05-13 00:00:00-04:00  

Can anyone explain what's going on here?

6
  • the first one and last one are the same, so the problem is just with the 2nd one being different? Commented May 13, 2012 at 23:08
  • correct- the second one is the trouble Commented May 13, 2012 at 23:10
  • which version are you using? Could be a bug in the lib, did you check the bug reports? I remember having this type of an issue with a php lib, it turned out the timezone parser was buggy Commented May 13, 2012 at 23:16
  • I notice you put EDT on the end of the first datetime string. Is that where you / what your computer is currently set to? I don't think you need the GMT. Commented May 13, 2012 at 23:20
  • 1
    My results are different, for the same code: 2012-05-13 00:00:00+04:00 2012-05-13 00:00:00+04:00 2012-05-13 00:00:00-04:00 Commented May 14, 2012 at 2:19

1 Answer 1

3

EDT is for the USA, which is to the west of the UK. the sun rises in the east. so the sun is overhead in the UK before the USA. so you need to add 4 hours to EDT to get GMT. this is why i need to call my parents (in the UK) by late afternoon, or they are in bed. in other words: "EDT +4 is GMT".

now the source for this is at http://bazaar.launchpad.net/~dateutil/dateutil/trunk/view/head:/dateutil/parser.py and a comment that seems to be associated with parsing GMT-0400 says

# Check for something like GMT+3, or BRST+3. Notice
# that it doesn't mean "I am 3 hours after GMT", but
# "my time +3 is GMT". If found, we reverse the
# logic so that timezone parsing code will get it
# right.

which means that GMT-0400 is equivalent to "my time -4 is GMT". which is not the same as above.

also, if you look at the code, a trailing (EDT) is processed after this, and so takes priority. and i think that the third case, with the final, simple -0400 is processed as you expect.

in other words (it seems to me, from scanning the code) the GMT-0400 form is working as the code documents, but not as you expect. that line is not equivalent to the other two.

i have no idea why the code works this way; i am just reporting what i read.

finally, note that the general approach in that code is to work through the entire date string chunk by chunk, applying different logic to different places. there is not that much checking to make sure that the logic in different places is consistent (so no error is thrown for the apparent contradiction in the first line). personally, i would prefer a library that uses python's own date parsing routines, but tries different format strings - i suspect that would be more reliable (but perhaps less flexible).

UPDATE i had forgotten about this post, but a while after after writing this reply i wrote simple-date to handle parsing of timezones. it takes an approach more like i said i preferred - instead of trying to be clever, it searches the pytz database for matches.

Sign up to request clarification or add additional context in comments.

4 Comments

How ugly can something get!
@cacba if you're using python 3, you might prefer simple-date which i wrote to try simplify this. although it is not as simple as i hoped - the problem turns out to be a hard one.
The conclusion I came to was there was no general solution to the problem. EST is two timezones. Datetime parsing is broken.
The python datetime makes me miss the java docs.oracle.com/javase/7/docs/api/java/text/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.