gpt4 book ai didi

python - 在 Python 中,如何打印完整的 ISO 8601 时间戳,包括当前时区

转载 作者:行者123 更新时间:2023-11-28 17:40:49 27 4
gpt4 key购买 nike

我需要以 ISO 8601 格式打印完整的本地日期/时间,包括本地时区信息,例如:

2007-04-05T12:30:00.0000-02:00

如果我有正确的 tzinfo 对象,我可以使用 datetime.isoformat() 打印它 - 但我如何获得它?

请注意,我停留在 Python 2.5,这可能会减少一些选项的可用性。

最佳答案

python 标准库不提供 tzinfo 实现。您需要对其进行子类化。 datetime module 中提供了示例。

接受的答案提供了错误的结果。例如在我的时区 +02,结果是 +01:59。这是因为在计算差异之前,需要在 localnow 和 utcnow 上将微秒替换为 0。

这是我的 python 2.5 版本:

# coding=utf-8


def isoformat_offset(dt, offset, dt_sep='T', hm_sep=True, short=True):
"""Return a string representing the date and time in ISO 8601 format,
YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM. If microseconds is 0 .mmmmmm is omitted.
The optional argument dt_sep (default 'T') is a one-character separator,
placed between the date and time portions of the result.
The optional argument hm_Sep (default True) indicates if a : separator
should be placed between the hours and minutes portions of the time zone
designator.
The optional argument short (default True) defines if the minute portion of
the time zone designator should be omitted in the case of zero minutes.

>>> from datetime import datetime
>>> cur = datetime(2017, 4, 26, 17, 14, 23, 123456)
>>> off = 2 * 3600 # +02:00
>>> isoformat_offset(cur, off)
'2017-04-26T17:14:23.123456+02'
>>> isoformat_offset(cur, off, ' ')
'2017-04-26 17:14:23.123456+02'
>>> isoformat_offset(cur, off, hm_sep=False)
'2017-04-26T17:14:23.123456+02'
>>> isoformat_offset(cur, off, short=False)
'2017-04-26T17:14:23.123456+02:00'
>>> isoformat_offset(cur, off, hm_sep=False, short=False)
'2017-04-26T17:14:23.123456+0200'
>>> cur = cur.replace(microsecond=0)
>>> isoformat_offset(cur, off)
'2017-04-26T17:14:23+02'
>>> off = -2 * 3600 # -02:00
>>> isoformat_offset(cur, off)
'2017-04-26T17:14:23-02'
>>> off = 2 * 3600 + 30 * 60 # +02:30
>>> isoformat_offset(cur, off)
'2017-04-26T17:14:23+02:30'
>>> isoformat_offset(cur, off, hm_sep=False)
'2017-04-26T17:14:23+0230'
"""
offset_hours = offset // 3600
offset_mins = (offset - offset_hours * 3600) // 60
frmt = '%s%+03d'
args = [dt.isoformat(dt_sep), offset_hours]
if (short is True and offset_mins > 0) or (short is False and offset_mins == 0):
if hm_sep is True:
frmt += ':'
frmt += '%02d'
args.append(offset_mins)
return frmt % tuple(args)

if __name__ == '__main__':
import doctest
doctest.testmod()

要根据此功能的需要以秒为单位获取本地时区,请使用 time module 中的否定 altzone:

from datetime import datetime
import time

now = datetime.now()
offset = -time.altzone
print(isoformat_offset(now, offset))

关于python - 在 Python 中,如何打印完整的 ISO 8601 时间戳,包括当前时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24575121/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com