Python和rfc3339时间戳

2024-05-23 19:44:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我在哪里可以找到建立RFC3339时间的程序?在


Tags: 程序时间rfc3339
3条回答

这是基于RFC第10页的例子。唯一的区别是,我显示的微秒值是6位数,与googledrive的时间戳一致。在

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

rfc3339非常灵活-http://www.ietf.org/rfc/rfc3339.txt-它有效地定义了一大堆格式。您可以使用标准的python时间格式-http://docs.python.org/3.3/library/datetime.html#strftime-strptime-behavior生成几乎所有的时间

但是,有一个奇怪之处,那就是它们允许(可选)在一个数字时区偏移量(%z)的小时和分钟之间使用:)。python不会显示它,所以如果您想包含它,您需要python-rfc339或类似的代码。在

对于解析rfc339,simple date将处理所有格式。但由于它使用python打印例程,因此无法处理上面的:情况。在

python-rfc3339对我来说很好。在

相关问题 更多 >