Question

How to convert a time string with time zone information to a iso format string?

eg:

Time string with time zone info

time = "2012-01-01T10:30:00-05:00"

ISO format time string

time = "2012-01-01T15:30:00Z"
Was it helpful?

Solution

def timestamp(time):
    form dateutil import parser
    time = parser.parse(time) #Converts string in to datetime object 
    from pytz import UTC
    if time.tzinfo: #Checks if the time is aware or naive
        time = time.astimezone(UTC) #Converts aware time into UTC
    return time.strftime('%Y%m%dT%H%M%SZ')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top