python - 显示 : character in the timezone offset using datetime. strftime

标签 python datetime timezone timezone-offset

<分区>

提供给 strftime 的格式字符串是什么,它会给出与我在 isoformat(' ') 中看到的相同的输出?

>>> from datetime import datetime
>>> import pytz 
>>> dt = datetime.now(tz=pytz.UTC).replace(microsecond=0)
>>> print dt
2014-05-29 13:11:00+00:00
>>> dt.isoformat(' ')
'2014-05-29 13:11:00+00:00'
>>> dt.strftime('%Y-%m-%d %H:%M:%S%z')
'2014-05-29 13:11:00+0000'

datetime 的 __str__ 行为从哪里获得偏移量中的额外冒号?我查看了 formatting options并且只能分别为 +HHMM 或名称找到 %z 和 %Z。

我查看了 datetime.__str__ 的实现,但没有得到任何提示,它只是说 pass(?!)。我认为它最终委托(delegate)给 isoformat(' ') 但我不明白它是如何/在哪里实现的。

最佳答案

Datetime 是用 C 实现的,你会发现 Python 的 str 默认使用的 tp_str 函数只是调用了 isoformat().

此外,datetime.strftime 方法调用 libc 的 strftime 函数,它给出了没有分隔符的时区差异,而 datetime.isoformat 调用一个直接为 Python 实现的方法,其中可以传递分隔符,在本例中为冒号。

关于python - 显示 : character in the timezone offset using datetime. strftime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23934319/

相关文章:

python - 使用 QVariantAnimation 显示图像列表

python - 将父前缀号码扩展到给定的子号码

python - Django : DiscoverRunner overriding raise error

java - 无法将 UTC 转换为 IST,它仍然只返回 UTC

powershell - 在 Powershell 中格式化不同时区的时间/日期

python - 为什么pytz好像没有任何时区信息?

python - 线程与异步网络(扭曲)Python

ruby-on-rails - 为什么介于两者之间? ruby 中 Date 和 DateTime 的工作方式不同?

javascript - 在几分钟内得到两个日期之间的差异

java - Joda Time : Convert local to UTC ambiguity