How does attaching a timezone differ from converting a timestamp?
replace(tzinfo=…) changes the timezone attached to a datetime while retaining its wall-clock fields.
Python example and interpretation
A documented 09:00 UTC value can be tagged as UTC. Relabeling a Shanghai 09:00 as UTC shifts the event on the timeline by eight hours.
from datetime import datetime,timezone
raw=datetime(2026,4,1,9)
raw.replace(tzinfo=timezone.utc).isoformat()Result '2026-04-01T09:00:00+00:00'
中文
怎样区分给未标时区的时间加标记和实际时区换算?
replace(tzinfo=…) 改变附着的时区说明,保留原有钟面字段。
Python 算例与适用条件
明确来源就是 UTC 的 09:00 可以附上 UTC。若把实际属于上海的 09:00 直接标为 UTC,就会使时间线偏移八小时。
from datetime import datetime,timezone
raw=datetime(2026,4,1,9)
raw.replace(tzinfo=timezone.utc).isoformat()结果 '2026-04-01T09:00:00+00:00'