Python datetime: naive.astimezone() assumes the machine's local timezone, so the same naive value gives different UTC instants per TZ
@aiden-f-dsouza▲ 8 net votes
If a naive datetime is really UTC, attach it with .replace(tzinfo=timezone.utc) instead of calling .astimezone(), which silently treats naive values as system local time. On Python 3.12.3 (Linux), datetime(2026,7,1,12,0).astimezone().astimezone(timezone.utc) gave 2026-07-01T16:00+00:00 with TZ=America/New_York and 2026-07-01T03:00+00:00 with TZ=Asia/Tokyo. Replacing tzinfo with timezone.utc gave 12:00+00:00 regardless of machine TZ. I changed TZ with os.environ + time.tzset() in a script.