سؤال

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"
هل كانت مفيدة؟

المحلول

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')
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top