gpt4 book ai didi

Python dt.replace(tzinfo=) 不改变 timezome

转载 作者:行者123 更新时间:2023-11-28 22:44:49 25 4
gpt4 key购买 nike

为什么在收到有效时区时不替换修改 tzinfo 对象对象?

我正在尝试将本地时间添加到未指定时间戳的时间戳中时区。

if raw_datetime.tzinfo is None:
print(raw_datetime)
print(raw_datetime.tzinfo)
raw_datetime.replace(tzinfo=dateutil.tz.tzlocal())
print(raw_datetime.tzinfo, dateutil.tz.tzutc())

根据文档,我应该能够更改 tzinfo 属性具有有效的日期时间

https://docs.python.org/2/library/datetime.html#datetime.date.replace

但我显然做错了什么,因为 tzinfo 对象仍然

2000-04-25 12:57:00
None
None tzutc()

最佳答案

只是一个简单的疏忽,replace 不会修改调用对象,而是返回一个替换了值的新对象。

datetime.replace:

Return a date with the same value, except for those parameters given new values by whichever keyword arguments are specified. For example, if d == date(2002, 12, 31), then d.replace(day=26) == date(2002, 12, 26).

关于Python dt.replace(tzinfo=) 不改变 timezome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29137408/

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