【问题标题】:Python and RFC 3339 timestamps [duplicate]Python 和 RFC 3339 时间戳 [重复]
【发布时间】:2013-02-09 08:51:15
【问题描述】:

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

【问题讨论】:

    标签: python datetime rfc3339


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      python-rfc3339 非常适合我。

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 2012-01-23
          • 2016-04-14
          • 1970-01-01
          • 2010-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-06
          • 1970-01-01
          相关资源
          最近更新 更多