import time
import calendar


def amazon2unixtime(amazon_timestr):
    """Given a string in Amazon-time format (i.e. ISOFORMAT),
    return the Unix time in seconds (i.e. as returned by time(2))
    """
    try:
        tm_utc = time.strptime(amazon_timestr, "%Y-%m-%dT%H:%M:%S.%fZ")
    except ValueError:
        tm_utc = None
    if tm_utc is None:
        tm_utc = time.strptime(amazon_timestr, "%Y-%m-%dT%H:%M:%SZ")
    time_sec = calendar.timegm(tm_utc)
    return time_sec