get all timezones in python

To get all timezones in python, you can make use of the pytz module. Here is an example code snippet that retrieves a list of all timezones:

main.py
import pytz

all_timezones = pytz.all_timezones

for timezone in all_timezones:
    print(timezone)
100 chars
7 lines

The pytz.all_timezones function returns a list containing the names of all available timezones. In the example code above, we loop through the list and print out each timezone. You can use this list to set the timezone for a datetime object. Here is an example:

main.py
from datetime import datetime
import pytz

all_timezones = pytz.all_timezones

timezone = pytz.timezone('America/New_York')
current_time = datetime.now(timezone)

print(current_time)
183 chars
10 lines

In the above example, we get the timezone object for 'America/New_York' and then for the current time in that timezone using datetime.now().

gistlibby LogSnag