How does utcnow() work without time-zones?

2019-08-24 03:19发布

问题:

I am reading on SO that the Python standard library does not define any time-zones.

Also, from the Python 2.7 datetime docs:

Note that no concrete tzinfo classes are supplied by the datetime module.

However, calling utcnow() generates the current time in UTC. Furthermore, Python can also provide the current local time with now(). I am assuming that this is only possible if Python "knows" the local time-to-UTC offset (in order to return the correct values).

But doesn't that mean that Python is timezone aware? What am I missing?

回答1:

Your OS provides the current timezone, as well as UTC time.

The OS does not, however, provide other timezone info, such as when DST switches take place. In other words, you can get the current time in the UTC timezone, but not accurately calculate local time across the year, as the offset to UTC changes with the summertime and wintertime switches.