How do I get the Olson timezone name (such as Australia/Sydney) corresponding to the value given by C’s localtime call?
This is the value overridden via TZ, by symlinking /etc/localtime, or setting a TIMEZONE variable in time-related system configuration files.
I think best bet is to go thru all pytz timezones and check which one matches local timezone, each pytz timezone object contains info about utcoffset and tzname like CDT, EST, same info about local time can be obtained from
time.timezone/altzoneandtime.tzname, and I think that is enough to correctly match local timezone in pytz database e.g.output:
In production you can create such a mapping beforehand and save it instead of iterating always.
Testing script after changing timezone: