How do you convert a python time.struct_time object into a ISO string?
Question: I have a Python object:

    
    
    time.struct_time(tm_year=2013, tm_mon=10, tm_mday=11, tm_hour=11, tm_min=57, tm_sec=12, tm_wday=4, tm_yday=284, tm_isdst=0)
    

And I need to get an [ISO string](http://www.w3.org/TR/NOTE-datetime):

    
    
    '2013-10-11T11:57:12Z'
    

How can I do that?


Answer: Using
[`time.strftime()`](http://docs.python.org/2/library/time.html#time.strftime)
is perhaps easiest:

    
    
    iso = time.strftime('%Y-%m-%dT%H:%M:%SZ', timetup)
    

Demo:

    
    
    >>> import time
    >>> timetup = time.gmtime()
    >>> time.strftime('%Y-%m-%dT%H:%M:%SZ', timetup)
    '2013-10-11T13:31:03Z'
    

You can also use a `datetime.datetime()` object, which has a
[`datetime.isoformat()`](http://docs.python.org/2/library/datetime.html#datetime.datetime.isoformat)
method:

    
    
    >>> from datetime import datetime
    >>> datetime(*timetup[:6]).isoformat()
    '2013-10-11T13:31:03'
    

This misses the timezone `Z` marker; you could just add that.

