Question

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
Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top