2

I get the date and time as string like 2014-05-18T12:19:24+04:00

I found another question explaining how to handle dates in UTC timezone (2012-05-29T19:30:03.283Z)

What should I do with +04:00 in my case (if I want to store time in UTC timezone in Python)?

Upd. I've tried to parse it like below:

dt = '2014-05-19T14:48:50+04:00'
plus_position = dt.find('+') # remove column in the timezone part
colon_pos = dt.find(':', plus_position)
dt = dt[:colon_pos] + dt[colon_pos+1:]
dt = datetime.datetime.strptime(dt, '%Y-%m-%dT%H:%M:%S%z') # '2014-05-19T14:48:50+0400'

But it fails - 'z' is a bad directive in format '%Y-%m-%dT%H:%M:%S%z'

Community
  • 1
  • 1
LA_
  • 19,823
  • 58
  • 172
  • 308
  • `datetime.isoformat` produces a timezone with a colon. `%z` expects a timezone without a colon. Hahaha. Ha. =( – Ry- May 18 '14 at 14:34

1 Answers1

3

Using dateutil:

>>> import dateutil.parser
>>> dateutil.parser.parse('2014-05-18T12:19:24+04:00')
datetime.datetime(2014, 5, 18, 12, 19, 24, tzinfo=tzoffset(None, 14400))
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Thanks, but can I do the same without `dateutil`? It requires `six` to be installed... I don't like to use many 3rd party modules with GAE environment. – LA_ May 19 '14 at 11:26