I have looked at many possible ways to parse python times. Using parse seems link the only method that should work. While trying to use datetime.strptime causes an error because %z
does not work with python 2.7. But using parse.parse incorrectly recognizes the time zone.
I parse both Fri Nov 9 09:04:02 2012 -0500
and Fri Nov 9 09:04:02 2012 -0800
and get the exact same timestamp in unix time. 1352480642
- My version of python 2.7.10
- My version of dateutil 1.5
Here is my code that runs the test.
#!/usr/bin/python
import time
from dateutil import parser
def get_timestamp(time_string):
timing = parser.parse(time_string)
return time.mktime(timing.timetuple())
test_time1 = "Fri Nov 9 09:04:02 2012 -0500"
test_time2 = "Fri Nov 9 09:04:02 2012 -0800"
print get_timestamp(test_time1)
print get_timestamp(test_time2)
Output
1352480642.0
1352480642.0
Expected output
1352469842.0
1352480642.0