0

I am trying to convert time into epoch time. I am getting the error of: ValueError: time data '11/Jan/2014:08:33:48 -0800' does not match format '%d/%b/%Y:%H:%M:%S %Z'

import time

def epoch_time(epoch):
    d = "11/Jan/2014:08:33:48 -0800"
    p='%d/%b/%Y:%H:%M:%S %Z'

    epoch = int(time.mktime(time.strptime(d,p)))
    print epoch

What is wrong with my above code?

My second question is where is TZ defined in os.environ[TZ]='UTC'? I found this in another post.

Update: My data comes in this format: 11/Jan/2014:08:33:48 -0800 - it comes with time zone info of -8000. I can not modify the data but need to modify the code. Using Python 2.7.6

>>> from dateutil import parser
>>> dd = parser.parse('11/Jan/2014:08:33:48 -0800')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/dateutil/parser.py", line 1008, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/dateutil/parser.py", line 395, in parse
    raise ValueError("Unknown string format")
ValueError: Unknown string format
>>> 

1 Answers1

0

You misunderstood %Z(Time zone name (no characters if no time zone exists).)

Try this:

import time

def epoch_time(epoch):
    d = "14/Jan/2014:09:36:50 PST"
    p='%d/%b/%Y:%H:%M:%S %Z'

    epoch = int(time.mktime(time.strptime(d,p)))
    print epoch
a = None
epoch_time(a)

Time Zone Abbreviations – Worldwide List


According to this answer, there is a better solution to your question.

from dateutil import parser
yourDate = parser.parse(yourString)

If you are using python3, as @jpmc26 recommended, use the %z syntax would be fine.

d = "11/Jan/2014:08:33:48 -0800"
p='%d/%b/%Y:%H:%M:%S %z' #python3 required 
Community
  • 1
  • 1
luoluo
  • 5,353
  • 3
  • 30
  • 41
  • Actually my actual time is in this format: 11/Jan/2014:08:33:48 -0800 - it comes with time zone info of -8000. I can not modify the data but need to modify the code – user4687194 Sep 13 '15 at 03:26
  • @user4687194 Use lowercase `%z`: https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior. (Feel free to edit that into the answer if you like.) – jpmc26 Sep 13 '15 at 03:31
  • When I use lowercase z, I get the error: ValueError: 'z' is a bad directive in format '%d/%b/%Y:%H:%M:%S %z' – user4687194 Sep 13 '15 at 03:40
  • Yes, `%z` was supported in `python3` add this to the answer. Meanwhile find a better solution. @user4687194. – luoluo Sep 13 '15 at 03:55
  • Could you give a cleaner answer - I am getting errors. (I installed default util, no prob there) – user4687194 Sep 13 '15 at 04:25
  • @user4687194 I think the suggestion to use `dateutils` is as clean as you're going to get without upgrading or writing your own code. – jpmc26 Sep 13 '15 at 07:55
  • Pls see my updated post - error "Unknown string format" - I get this error even without -8000, the timezone – user4687194 Sep 13 '15 at 08:10
  • How about `python3` and `%z`? – luoluo Sep 13 '15 at 08:28
  • @jpmc26 what code upgrade is needed within this epoch conversion? – user4687194 Sep 13 '15 at 17:10