10

Checking out the zoneinfo module in Python 3.9, I was wondering if it also offers a convenient option to retrieve the local time zone (OS setting) on Windows.

On GNU/Linux, you can do

from datetime import datetime
from zoneinfo import ZoneInfo

naive = datetime(2020, 6, 11, 12)
aware = naive.replace(tzinfo=ZoneInfo('localtime'))

but on Windows, that throws

ZoneInfoNotFoundError: 'No time zone found with key localtime'

so would I still have to use a third-party library? e.g.

import time
import dateutil

tzloc = dateutil.tz.gettz(time.tzname[time.daylight])
aware = naive.replace(tzinfo=tzloc)

Since time.tzname[time.daylight] returns a localized name (German in my case, e.g. 'Mitteleuropäische Sommerzeit'), this doesn't work either:

aware = naive.replace(tzinfo=ZoneInfo(tzloc))

Any thoughts?


p.s. to try this on Python < 3.9, use backports (see also this answer):

pip install backports.zoneinfo
pip install tzdata # needed on Windows
FObersteiner
  • 22,500
  • 8
  • 42
  • 72

2 Answers2

10

You don't need to use zoneinfo to use the system local time zone. You can simply pass None (or omit) the time zone when calling datetime.astimezone.

From the docs:

If called without arguments (or with tz=None) the system local timezone is assumed. The .tzinfo attribute of the converted datetime instance will be set to an instance of timezone with the zone name and offset obtained from the OS.

Thus:

from datetime import datetime

naive = datetime(2020, 6, 11, 12)
aware = naive.astimezone()
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Does that work if daylight savings is different between the current date and the datetime you're trying to imprint? – Mark Ransom Jun 11 '20 at 22:21
  • @MarkRansom - Yes. – Matt Johnson-Pint Jun 12 '20 at 00:33
  • I just double checked and indeed you're right. This is a great hidden feature! – Mark Ransom Jun 12 '20 at 14:33
  • 1
    This is not a 100% solution on Windows, for example on my system datetime.now().astimezone().strftime( '%Y-%m-%d %H:%M:%I%Z%z' ) produces "2023-05-31 12:49:12Central Daylight Time-0500" instead of "2023-05-31 12:49:12CDT-0500". However datetime.now().astimezone( zoneinfo.ZoneInfo( 'America/Chicago' )).strftime( '%Y-%m-%d %H:%M:%I%Z%z' ) produces the expected '2023-05-31 12:57:12CDT-0500'. – royce3 May 31 '23 at 17:58
6

While astimezone(None) is convenient, sometimes you might want to get the IANA name of your time zone, not what Windows thinks is best for you.

Version 4 of tzlocal will also use zoneinfo for that whilst maintaining compatibility with pytz through the deprecation shim:

>>> import tzlocal
>>> print(tzlocal.get_localzone())
Europe/Berlin
>>> print(repr(tzlocal.get_localzone()))
_PytzShimTimezone(zoneinfo.ZoneInfo(key='Europe/Berlin'), 'Europe/Berlin')

[update] With version 5, the pytz deprecation shim is removed, see also tzlocal's readme:

>>> print(tzlocal.get_localzone())
Europe/Berlin
>>> print(repr(tzlocal.get_localzone()))
zoneinfo.ZoneInfo(key='Europe/Berlin')
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • Be warned version 5 of tzlocal has changes that make it incompatible for use inside of a logging handler, see https://github.com/regebro/tzlocal/issues/147 – royce3 May 31 '23 at 17:59
  • @royce3 thanks for the hint, haven't been using the module for a while. It seems to be a circular import bug, see https://github.com/jobec/rfc5424-logging-handler/pull/47 – FObersteiner Jun 01 '23 at 08:51