1

在哪里可以找到构建 RFC 3339 时间的例程?

4

3 回答 3

1

这是基于 RFC 第 10 页上的示例。唯一的区别是我显示了一个六位数的微秒值,符合 Google Drive 的时间戳。

from math import floor

def build_rfc3339_phrase(datetime_obj):
    datetime_phrase = datetime_obj.strftime('%Y-%m-%dT%H:%M:%S')
    us = datetime_obj.strftime('%f')

    seconds = datetime_obj.utcoffset().total_seconds()

    if seconds is None:
        datetime_phrase += 'Z'
    else:
        # Append: decimal, 6-digit uS, -/+, hours, minutes
        datetime_phrase += ('.%.6s%s%02d:%02d' % (
                            us,
                            ('-' if seconds < 0 else '+'),
                            abs(int(floor(seconds / 3600))),
                            abs(seconds % 3600)
                            ))

    return datetime_phrase
于 2013-02-23T21:52:25.697 回答
0

rfc3339 is very flexible - http://www.ietf.org/rfc/rfc3339.txt - it effectively defines a whole pile of formats. you can generate almost all of them just using the standard python time formatting - http://docs.python.org/3.3/library/datetime.html#strftime-strptime-behavior

however, there is one oddity, and that is they allow (it's optional) a : between hours and minutes in a numerical timezone offset (%z). python won't display that, so if you want to include that you need python-rfc3339 or similar.

for parsing rfc3339, simple-date will handle all formats. but since it uses python print routines it cannot handle the : case above.

于 2013-07-17T12:55:43.560 回答
0

python-rfc3339对我来说非常有用。

于 2013-07-17T12:49:03.330 回答