0

Really stuck here. I've had a look at various answers but zone of them seem to work (Python2.7)

I've got a string fileGenTime, representing a time/date. I need to parse it into a date/time and then convert it into UTC. I've tried using 'pytz' and then 'dateutil' but I'm not sure why it won't work.... Here's my attempt...

from datetime import datetime
from dateutil import tz

fileGenTime = 'Thu Jan  2 19:23:34 EST 2014'
fileGenTime = fileGenTime.replace('  ',' ') #double spaces...
wday, month, day, time, zone, year = fileGenTime.split(' ')
hour, minute, second = time.split(':')

localGenTime = datetime.strptime(day +' '+ month +' '+ year
 +' '+ hour +' '+ minute +' '+ second, '%d %b %Y %H %M %S')

I had initially tried including the zone in this last line and using %Z in the format arg... Now I'm trying to convert the local time into one with a time zone, and then use astimezone to convert it to UTC.

localZone = tz.gettz(zone)
localGenTime = localGenTime.replace(tzinfo = localZone)

normalisedTimezone = tz.gettz('UTC')
normalisedGenTime = localGenTime.astimezone(normalisedTimezone)

The error message I'm getting says.... "ValueError: month must be in 1..12"....eh?

I'm pulling my hair out! Any help would be appreciated!

Full traceback here....

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python27\lib\site-packages\python_dateutil-1.5-py2.7.egg\dateutil\tzwin.py", line 32, in utcoffset
    if self._isdst(dt):
  File "C:\Python27\lib\site-packages\python_dateutil-1.5-py2.7.egg\dateutil\tzwin.py", line 67, in _isdst
    self._dstweeknumber)
  File "C:\Python27\lib\site-packages\python_dateutil-1.5-py2.7.egg\dateutil\tzwin.py", line 166, in picknthweekday
    first = datetime.datetime(year, month, 1, hour, minute)
ValueError: month must be in 1..12
4
  • Could you add the full traceback? Commented Jan 30, 2014 at 10:52
  • Edited to include traceback Nigel, thanks Commented Jan 30, 2014 at 10:59
  • 1
    On my machine it runs ok. Your dateutil version is quite old. Can you try it with a newer version? Commented Jan 30, 2014 at 11:50
  • note: EST timezone abbriviation is ambigous Commented Mar 4, 2014 at 17:22

1 Answer 1

2

It looks like you're using Windows with an old version of dateutil and you've hit this dateutil bug.

Solutions (I've tried both, both worked):

  1. Upgrade dateutile from version 1.5 to version 2.2
  2. If for some reason you can't upgrade then you can delete files Lib/site-packages/dateutil/tzwin.py and Lib/site-packages/dateutil/tzwin.pyc. But that's pure evil. I would go with the option #1.
Sign up to request clarification or add additional context in comments.

Comments

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.