I am trying to convert a time stamp of the format 20151021|133102[-0400/1] into an epoch time.
I want the code to run regardless of the native timezone of the machine it is running on, and be DST aware so the code will run over the switch to/from DST. I need the code to run under Python 2.7.x (ideally using only standard library modules so the code is portable.)
After wasting the afternoon googling, the best I have been able to do is:
time.mktime(time.strptime('20151021|133102','%Y%m%d|%H%M%S'))`
which does not allow me to incorporate either the offset, or the daylight savings flag.
I also tried the following:
time.mktime(time.strptime('20151021|133102-4000','%Y%m%d|%H%M%S%z'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/_strptime.py", line 467, in _strptime_time
return _strptime(data_string, format)[0]
File "/usr/lib/python2.7/_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y%m%d|%H%M%S%z'
but it seems that strptime does not allow the offset.
strptime does support:
time.mktime(time.strptime('20151021|133102EDT','%Y%m%d|%H%M%S%Z'))
but this means that I need to somehow convert the offset into a timezone abbreviation which seems sort of stupid--and might create an error depending on where the code is run.
I can't believe this isn't more straight forward given routine this problem should be. Any assistance would be much appreciated.