python / python/cpython

datetime.datetime.now() returns wrong time on Windows when env var TZ is set

Open
#124,286 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

Europe/Berlin is UTC+2 right now, not UTC+1

Works fine on Linux OSes (builds).

C:\Users\work>python
Python 3.12.6 (tags/v3.12.6:a4a2d2b, Sep  6 2024, 20:11:23) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['TZ'] = 'Europe/Berlin'
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2024, 9, 20, 12, 15, 24, 771320)
>>> datetime.datetime.now(tz=datetime.UTC)
datetime.datetime(2024, 9, 20, 11, 15, 45, 162005, tzinfo=datetime.timezone.utc)
>>>
CPython versions tested on:

3.12

Operating systems tested on:

Windows

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported Python 3.12 behavior on Windows with TZ set to Europe/Berlin, comparing datetime.datetime.now() with datetime.datetime.now(tz=datetime.UTC). Trace the datetime and Windows timezone handling involved; done means local time reflects the correct Berlin offset and the behavior has regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.