示例#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
示例#2
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
示例#3
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 "Z".

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

    if secs == 0:
        return "Z"

    return s_time_offset_from_secs(secs)
示例#4
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 "Z".

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

    if secs == 0:
        return "Z"

    return s_time_offset_from_secs(secs)
示例#5
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)
示例#6
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)
示例#7
0
def set_default_time_offset(s):
    global offset_default
    global s_offset_default
    offset_default = parse_time_offset(s)
    s_offset_default = s
示例#8
0
def set_default_time_offset(s):
    global offset_default
    global s_offset_default
    offset_default = parse_time_offset(s)
    s_offset_default = s