在Python中生成RFC 3339时间戳 [英] Generate RFC 3339 timestamp in Python

查看:2580
本文介绍了在Python中生成RFC 3339时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Python中生成 RFC 3339 UTC时间戳。到目前为止,我已经能够执行以下操作:

I'm trying to generate an RFC 3339 UTC timestamp in Python. So far I've been able to do the following:

>>> d = datetime.datetime.now()
>>> print d.isoformat('T')
2011-12-18T20:46:00.392227

我的问题是设置UTC偏移量。

My problem is with setting the UTC offset.

根据 docs ,classmethod datetime.now([tz]),需要一个可选的 tz 参数其中 tz必须是类tzinfo子类的实例,而$ code> datetime.tzinfo 是用于时区信息对象的抽象基类。

According to the docs, the classmethod datetime.now([tz]), takes an optional tz argument where tz must be an instance of a class tzinfo subclass, and datetime.tzinfo is an abstract base class for time zone information objects.

这是我迷路的地方 - tzinfo是一个抽象类,我该怎么实现它?

This is where I get lost- How come tzinfo is an abstract class, and how am I supposed to implement it?

注意:在PHP中,简单如 timestamp =日期(DATE_RFC3339); ,这就是为什么我不明白为什么Python的方法如此复杂...)

(NOTE: In PHP it's as simple as timestamp = date(DATE_RFC3339);, which is why I can't understand why Python's approach is so convoluted...)

推荐答案

进一步向下与您链接的同一文档给出了一些例子,包括一个 UTC的完整代码(代表UTC),一个 FixedOffset class(表示与UTC固定偏移的时区,而与DST和whatnot的时区相反)以及其他几个。

Further down in the same doc that you linked to, it explains how to implement it, giving some examples, including full code for a UTC class (representing UTC), a FixedOffset class (representing a timezone with a fixed offset from UTC, as opposed to a timezone with DST and whatnot), and a few others.

这篇关于在Python中生成RFC 3339时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆