Esempio n. 1
0
def timestamp_from_tf(tf, time_offset=None):
    """
    Format a time and offset into a string.

    Arguments:
        tf
            a floating-point time value, seconds since the epoch.
        time_offset
            a string specifying an offset from UTC.  Examples:
            z or Z -- offset is 0 ("Zulu" time, UTC, aka GMT)
            -08:00 -- 8 hours earlier than UTC (Pacific time zone)
            "" -- empty string is technically not legal, but may work

    Notes:
        Returned string complies with RFC 3339.
        Example: 2003-12-13T18:30:02Z
        Example: 2003-12-13T18:30:02+02:00
    """

    if tf is None:
        return ""

    if time_offset is None:
        time_offset = s_offset_default

    # converting from tf to timestamp so *add* time offset
    tf += parse_time_offset(time_offset)

    try:
        s = time.strftime(_format_RFC3339, time.gmtime(tf))
    except ValueError:
        return "<!-- date out of range; tf is %.1f -->" % tf

    return s + time_offset
Esempio n. 2
0
def timestamp_from_tf(tf, time_offset=None):
    """
    Take a tf and return a timestamp string.

    Arguments:
        tf
            a floating-point UTC time value, seconds since the epoch.
        time_offset
            a string specifying an offset from UTC.  Examples:
            z or Z -- offset is 0 ("Zulu" time, UTC, aka GMT)
            PST -- 8 hours earlier than UTC (Pacific Standard Time)
            -0800 -- 8 hours earlier than UTC
            "" -- empty string is technically not legal, but may work

    Notes:
        Returned string is extended RFC 822 with 4-digit year.

        Example: "Tue, 10 Jun 2003 09:41:01 GMT"
    """

    if tf is None:
        return ""

    if time_offset is None:
        time_offset = s_offset_default

    # converting from tf to timestamp so *add* time offset
    tf += parse_time_offset(time_offset)

    try:
        s = time.strftime(_s_format_rfc822, time.gmtime(tf))
    except ValueError:
        return "<!-- date out of range; tf is %.1f -->" % tf

    return "%s %s" % (s, time_offset)
Esempio n. 3
0
def timestamp_from_tf(tf, time_offset=None):
    """
    Format a time and offset into a string.

    Arguments:
        tf
            a floating-point time value, seconds since the epoch.
        time_offset
            a string specifying an offset from UTC.  Examples:
            z or Z -- offset is 0 ("Zulu" time, UTC, aka GMT)
            -08:00 -- 8 hours earlier than UTC (Pacific time zone)
            "" -- empty string is technically not legal, but may work

    Notes:
        Returned string complies with RFC 3339.
        Example: 2003-12-13T18:30:02Z
        Example: 2003-12-13T18:30:02+02:00
    """

    if tf is None:
        return ""

    if time_offset is None:
        time_offset = s_offset_default


    # converting from tf to timestamp so *add* time offset
    tf += parse_time_offset(time_offset)

    try:
        s = time.strftime(_format_RFC3339, time.gmtime(tf))
    except ValueError:
        return "<!-- date out of range; tf is %.1f -->" % tf

    return s + time_offset
Esempio n. 4
0
def tf_from_timestamp(s_timestamp):
    """
    Take a RFC 882 timestamp string and return a time float value.

    timestamp example: "Tue, 10 Jun 2003 09:41:01 GMT"
    timestamp example: "10 Jun 2003 01:41:01 -0800"

    Note: according to RFC 822, weekday is optional.  This function
    ignores the weekday value if present.  The weekday can't change the
    date anyway.
    """

    # We want to be able to accept inputs that might be a little sloppy.
    #
    # strptime() has a rather fragile parser.  So, we will first clean
    # up and reformat the input string so that it is in exactly the
    # correct format to make strptime() happy.

    s_timestamp = s_timestamp.lstrip().rstrip()

    try:
        m = _pat_rfc822.search(s_timestamp)

        s_mday = m.group(1)
        s_mon = m.group(2)
        s_year = m.group(3)
        s_hour = m.group(4)
        s_min = m.group(5)
        s_sec = m.group(6)
        s_zone_offset = m.group(8)

        # convert two-digit year to four digits
        if len(s_year) == 2:
            y = int(s_year)
            if 32 <= y <= 99:
                s_year = "19" + s_year
            else:
                s_year = "20" + s_year

        # build string in perfect format
        s_date = "%s %s %s %s:%s:%s" % \
            (s_mday, s_mon, s_year, s_hour, s_min, s_sec)
        tup = time.strptime(s_date, _s_date_parse_format)

        # calendar.timegm() is like time.mktime() but doesn't adjust
        # from local to UTC; it just converts to a tf.
        tf = timegm(tup)

        # Use time offset from timestamp to adjust from UTC to correct.
        # If s_zone_offset is "GMT", "UTC", or "Z", offset is 0.

        # converting from timestamp to tf so *subtract* time offset
        tf -= parse_time_offset(s_zone_offset)
    except:
        return None

    return float(tf)
Esempio n. 5
0
def tf_from_timestamp(s_timestamp):
    """
    Take a RFC 882 timestamp string and return a time float value.

    timestamp example: "Tue, 10 Jun 2003 09:41:01 GMT"
    timestamp example: "10 Jun 2003 01:41:01 -0800"

    Note: according to RFC 822, weekday is optional.  This function
    ignores the weekday value if present.  The weekday can't change the
    date anyway.
    """

    # We want to be able to accept inputs that might be a little sloppy.
    #
    # strptime() has a rather fragile parser.  So, we will first clean
    # up and reformat the input string so that it is in exactly the
    # correct format to make strptime() happy.

    s_timestamp = s_timestamp.lstrip().rstrip()

    try:
        m = _pat_rfc822.search(s_timestamp)

        s_mday = m.group(1)
        s_mon = m.group(2)
        s_year = m.group(3)
        s_hour = m.group(4)
        s_min = m.group(5)
        s_sec = m.group(6)
        s_zone_offset = m.group(8)

        # convert two-digit year to four digits
        if len(s_year) == 2:
            y = int(s_year)
            if 32 <= y <= 99:
                s_year = "19" + s_year
            else:
                s_year = "20" + s_year

        # build string in perfect format
        s_date = "%s %s %s %s:%s:%s" % \
            (s_mday, s_mon, s_year, s_hour, s_min, s_sec)
        tup = time.strptime(s_date, _s_date_parse_format)

        # calendar.timegm() is like time.mktime() but doesn't adjust
        # from local to UTC; it just converts to a tf.
        tf = timegm(tup)

        # Use time offset from timestamp to adjust from UTC to correct.
        # If s_zone_offset is "GMT", "UTC", or "Z", offset is 0.

        # converting from timestamp to tf so *subtract* time offset
        tf -= parse_time_offset(s_zone_offset)
    except:
        return None

    return float(tf)
Esempio n. 6
0
def cleanup_time_offset(time_offset):
    """
    Given a time offset, return a time offset in a consistent format.

    If the offset is for UTC, always return a "GMT".

    Otherwise, return offset in this format: "(+|-)hh:mm"
    """
    secs = parse_time_offset(time_offset)

    if secs == 0:
        return "GMT"

    return s_time_offset_from_secs(secs)
Esempio n. 7
0
def cleanup_time_offset(time_offset):
    """
    Given a time offset, return a time offset in a consistent format.

    If the offset is for UTC, always return a "GMT".

    Otherwise, return offset in this format: "(+|-)hh:mm"
    """
    secs = parse_time_offset(time_offset)

    if secs == 0:
        return "GMT"

    return s_time_offset_from_secs(secs)
Esempio n. 8
0
def tf_from_timestamp(timestamp):
    """
    Take a RFC 3339 timestamp string and return a time float value.

    timestamp example: 2003-12-13T18:30:02Z
    timestamp example: 2003-12-13T18:30:02+02:00

    Leaving off the suffix is technically not legal, but allowed.
    """

    timestamp = timestamp.lstrip().rstrip()

    try:
        m = _pat_rfc3339.search(timestamp)
        year = int(m.group(1))
        mon = int(m.group(2))
        mday = int(m.group(3))
        hour = int(m.group(4))
        min = int(m.group(5))
        sec = int(m.group(6))
        s_zone_offset = m.group(8)

        tup = (year, mon, mday, hour, min, sec, -1, -1, 0)

        # calendar.timegm() is like time.mktime() but doesn't adjust
        # from local to UTC; it just converts to a tf.
        tf = timegm(tup)

        # Use time offset from timestamp to adjust from UTC to correct.
        # If s_zone_offset is "GMT", "UTC", or "Z", offset is 0.

        # converting from timestamp to tf so *subtract* time offset
        tf -= parse_time_offset(s_zone_offset)
    except:
        return None

    return float(tf)
Esempio n. 9
0
def tf_from_timestamp(timestamp):
    """
    Take a RFC 3339 timestamp string and return a time float value.

    timestamp example: 2003-12-13T18:30:02Z
    timestamp example: 2003-12-13T18:30:02+02:00

    Leaving off the suffix is technically not legal, but allowed.
    """

    timestamp = timestamp.lstrip().rstrip()

    try:
        m = _pat_rfc3339.search(timestamp)
        year = int(m.group(1))
        mon = int(m.group(2))
        mday = int(m.group(3))
        hour = int(m.group(4))
        min = int(m.group(5))
        sec = int(m.group(6))
        s_zone_offset = m.group(8)

        tup = (year, mon, mday, hour, min, sec, -1, -1, 0)

        # calendar.timegm() is like time.mktime() but doesn't adjust
        # from local to UTC; it just converts to a tf.
        tf = timegm(tup)

        # Use time offset from timestamp to adjust from UTC to correct.
        # If s_zone_offset is "GMT", "UTC", or "Z", offset is 0.

        # converting from timestamp to tf so *subtract* time offset
        tf -= parse_time_offset(s_zone_offset)
    except:
        return None

    return float(tf)
Esempio n. 10
0
def timestamp_from_tf(tf, time_offset=None):
    """
    Take a tf and return a timestamp string.

    Arguments:
        tf
            a floating-point UTC time value, seconds since the epoch.
        time_offset
            a string specifying an offset from UTC.  Examples:
            z or Z -- offset is 0 ("Zulu" time, UTC, aka GMT)
            PST -- 8 hours earlier than UTC (Pacific Standard Time)
            -0800 -- 8 hours earlier than UTC
            "" -- empty string is technically not legal, but may work

    Notes:
        Returned string is extended RFC 822 with 4-digit year.

        Example: "Tue, 10 Jun 2003 09:41:01 GMT"
    """

    if tf is None:
        return ""

    if time_offset is None:
        time_offset = s_offset_default


    # converting from tf to timestamp so *add* time offset
    tf += parse_time_offset(time_offset)

    try:
        s = time.strftime(_s_format_rfc822, time.gmtime(tf))
    except ValueError:
        return "<!-- date out of range; tf is %.1f -->" % tf

    return "%s %s" % (s, time_offset)
Esempio n. 11
0
def set_default_time_offset(s):
    global offset_default
    global s_offset_default
    offset_default = parse_time_offset(s)
    s_offset_default = s
Esempio n. 12
0
def set_default_time_offset(s):
    global offset_default
    global s_offset_default
    offset_default = parse_time_offset(s)
    s_offset_default = s