normalizing JSON datestrings to UTC python
Question: I have an important test that says "Calculate users that logged in during the
month of April normalized to the UTC timezone."

Items look as such:

    
    
    [ {u'email': u' ybartoletti@littel.biz',
      u'login_date': u'2014-05-08T22:30:57-04:00'},
     {u'email': u'woodie.crooks@kozey.com',
      u'login_date': u'2014-04-25T13:27:48-08:00'},
    ]
    

It seems to me that an item like `2014-04-13T17:12:20-04:00` means "April
13th, 2014, at 5:12:20 pm, 4 hours behind UTC". Then I just use strptime to
convert to datetime ([Converting JSON date string to python
datetime](http://stackoverflow.com/questions/10805589/converting-json-date-
string-to-python-datetime)), and subtract a timedelta of however many hours I
get from a regex that grabs the end of string? I feel this way because some
have a + at the end instead of -, like `2014-05-07T00:30:06+07:00`

Thank you


Answer: It is probably best to use the
[`dateutil.parser.parse`](https://pypi.python.org/pypi/python-dateutil/2.5.0)
and [`pytz`](https://pypi.python.org/pypi/pytz) packages for this purpose.
This will allow you to parse a string and convert it to a datetime object with
UTC timezone:

    
    
    >>> s = '2014-05-08T22:30:57-04:00'
    >>> import dateutil.parser
    >>> import pytz
    >>> pytz.UTC.normalize(dateutil.parser.parse(s))
    datetime.datetime(2014, 5, 9, 2, 30, 57, tzinfo=<UTC>)
    

