1

I have the following string in Python - 'Oct 01 2013 07 30 PM EST'. I'm trying to convert to a datetime object but am getting the following error:

ValueError: time data 'Oct 01 2013 07 30 PM EST' does not match format
'%b %d %Y %I %M %p %Z'

Not sure where I am going wrong! Please help. My code is below:

from datetime import datetime
date_object = datetime.strptime(str(date), '%b %d %Y %I %M %p %Z')
sheepez
  • 986
  • 1
  • 10
  • 26
sewardth
  • 347
  • 2
  • 13
  • 1
    the reason is timezone. please check this question http://stackoverflow.com/questions/1703546/parsing-date-time-string-with-timezone-abbreviated-name-in-python – oleg Sep 22 '13 at 19:50
  • Ah, thank you! I just removed the timezone and it works now. – sewardth Sep 22 '13 at 23:56

2 Answers2

3

Python strptime is platform-dependent:

http://docs.python.org/2/library/time.html#time.strptime

Support for the %Z directive is based on the values contained in tzname and whether daylight is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).

On my kit (OSX 10.8.5, python 2.7.2), for example, datetime.now().strftime("%Z") is an empty string.

There are external libraries like http://pytz.sourceforge.net/ to help with timezones

SheetJS
  • 22,470
  • 12
  • 65
  • 75
1

There are two EST, which cause the confusion: http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations

Eastern Standard Time (North America)   UTC-05
Eastern Standard Time (Australia)       UTC+10
Mingyu
  • 31,751
  • 14
  • 55
  • 60
  • While technically true, that is not the origin of the issue at hand. Try a 3-letter code that is unique (like `EDT`, eastern daylight time) – SheetJS Sep 23 '13 at 04:20