获取所有可用时区 [英] Get all available timezones
问题描述
我目前正在开发支持多个时区的应用程序.
I'm currently working on an application that is required to support multiple timezones.
为此,我正在使用dateutil库.现在,我需要一种向用户显示dateutil库支持的所有可用时区的列表.
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.
还建议将tz作为字符串存储在欧洲/柏林"之类的数据库中吗?
Also, is it advisable to store the tz as strings in the database like "Europe/Berlin"?
推荐答案
dateutil
将使用OS时区信息,但也带有其自己的压缩时区信息文件.
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)
您需要对列表进行一些排序和过滤;在该列表中既有缩写(3个字母)的时区代码,又有地区/城市"条目.
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.
请注意,这会将所有数据加载到内存中;如果不希望这样做,则需要自己加载tar文件:
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.zoneinfo
zi_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]
这篇关于获取所有可用时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!