Is there a cross-platform function in python
(or pytz
) that returns a tzinfo
object corresponding to the timezone currently set on the computer?
environment variables cannot be counted on as they are not cross-platform
You can get the current time in a particular timezone by using the datetime module with another module called pytz . You can then check for all available timezones with the snippet below: from datetime import datetime import pytz zones = pytz. all_timezones print(zones) # Output: all timezones of the world.
The property UtcNow of the DateTime class returns the current date and time of the machine running the code, expressed in UTC format. UTC is a universal format to represent date and time as an alternative to local time. Also known as the GMT+00 timezone.
pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.4 or higher. It also solves the issue of ambiguous times at the end of daylight saving time, which you can read more about in the Python Library Reference ( datetime. tzinfo ).
>>> import datetime >>> today = datetime.datetime.now() >>> insummer = datetime.datetime(2009,8,15,10,0,0) >>> from pytz import reference >>> localtime = reference.LocalTimezone() >>> localtime.tzname(today) 'PST' >>> localtime.tzname(insummer) 'PDT' >>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With