Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14360

Get local time zone name on Windows (Python 3.9 zoneinfo)

$
0
0

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 datetimefrom zoneinfo import ZoneInfonaive = 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 timeimport dateutiltzloc = 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.zoneinfopip install tzdata # needed on Windows

Viewing all articles
Browse latest Browse all 14360

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>