Get all available timezones

2024/10/5 17:23:07

I'm currently working on an application that is required to support multiple timezones.

For that, I'm using the dateutil library. Now, I need a way to present the user a list of all available timezones the dateutil library supports.

Also, is it advisable to store the tz as strings in the database like "Europe/Berlin"?

Answer

dateutil will use the OS timezone info, but does carry it's own compressed timezone info file too.

You could list the available names from the utility code that loads this data:

from dateutil.zoneinfo import get_zonefile_instance
zonenames = list(get_zonefile_instance().zones)

You'd need to sort and filter that list a little; there are both abbreviated (3 letter) timezone codes as well as "region/city" entries in this list.

Storing those names in a database is just fine.

Note that this loads all data into memory; if that's not desirable you'd need to load the tar file yourself:

import os
import tarfile
import dateutil.zoneinfozi_path = os.path.abspath(os.path.dirname(dateutil.zoneinfo.__file__))
zonesfile = tarfile.TarFile.open(os.path.join(zi_path, dateutil.zoneinfo.ZONEFILENAME))
zonenames = [zn.name for zn in zonesfile.getmembers()if not zn.isdir() and zn.name != dateutil.zoneinfo.METADATA_FN]
https://en.xdnf.cn/q/70462.html

Related Q&A

Load blob image data into QPixmap

I am writing a program using PyQt4 for front-end GUI and this program accesses a back-end database (which can be either MySQL or SQLite). I need to store some image data in the database and below is th…

Fetch a value of SQLalchemy instrumentedattribute

How can I fetch the value of a InstrumentedAttribute object in SQLalchemy:(Pdb) ResultLine.item_reference_1 <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x793dc90>The above statemen…

python super calling child methods

There are numerous questions on the usage of super() but none of them appears to answer my question. When calling super().__init__() from a subclass, all method calls in the super-constructor are actua…

How to create space between subplots? [duplicate]

This question already has answers here:Manipulation on vertical space in matplotlib subplots(3 answers)Closed 2 years ago.The title pretty much says it all. I have a notebook containing two subplots an…

How to (re)name an empty column header in a pandas dataframe without exporting to csv

I have a pandas dataframe df1 with an index column and an unnamed series of values. I want to assign a name to the unnamed series. The only way to do this that I know so far is to export to df1.csv usi…

Capturing the video stream from a website into a file

For my image classification project I need to collect classified images, and for me a good source would be different webcams around the world streaming video in the internet. Like this one:https://www.…

Recreating time series data using FFT results without using ifft

I analyzed the sunspots.dat data (below) using fft which is a classic example in this area. I obtained results from fft in real and imaginery parts. Then I tried to use these coefficients (first 20) to…

python BeautifulSoup get all href in Children of div

I am new to python and Ive been trying to get links and inner text from this html code : <div class="someclass"><ul class="listing"><li><a href="http://lin…

Python TypeError: sort() takes no positional arguments

I try to write a small class and want to sort the items based on the weight. The code is provided, class Bird:def __init__(self, weight):# __weight for the private variableself.__weight = weightdef wei…

Trouble with basemap subplots

I need to make a plot with n number of basemap subplots. But when I am doing this the all the values are plotted on the first subplot.My data is a set of n matrixes, stored in data_all.f, map = plt.sub…