Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get tz_info object corresponding to current timezone?

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

like image 255
random guy Avatar asked Nov 05 '09 15:11

random guy


People also ask

How do I get the current timezone in Python?

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.

What timezone is datetime NOW ()?

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.

Which Python library is used for 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 ).


1 Answers

>>> 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' >>>  
like image 144
Alex Martelli Avatar answered Sep 21 '22 06:09

Alex Martelli