c

codeye

V1

2022/09/22阅读:7主题:默认主题

zoneinfo

zoneinfo包含在python标准模块中,在处理时区时非常有帮助。zoneinfo提供了对IANA时区的完整支持。zoneinfo所使用的时区基本上是IANA时区数据库。

什么是IANA时区? IANA这个词是指互联网号码分配机构。IANA是一个正在研究DNS根、IP地址和其他诸如时区的国际标准的组织。

IANA正在寻找的主要东西是根区管理、协议注册和其他许多东西。

除了这些,IANA还有一个时区数据库的清单,这被称为IANA时区数据库。

如何用Python来使用IANA时区? Python为我们提供了zoneinfo模块,它是建立在我们需要安装的python模块中的,它可以帮助我们访问IANA时区数据库。IANA时区数据库包含代表当地时间历史的代码和数据。时区数据库会及时更新。

我们可以使用python zoneinfo模块来帮助我们访问这些时区数据库。

zoneinfo模块是如何工作的? python zoneinfo模块使用系统中可用的时区,如果系统中没有可用的时区数据,那么它将使用tzdata来获取其值。

class ZoneInfo(tzinfo):
    _strong_cache_size = 8
    _strong_cache = collections.OrderedDict()
    _weak_cache = weakref.WeakValueDictionary()
    __module__ = "zoneinfo "

如果你正在创建一个跨平台的项目,那么你需要声明对tzdata的依赖性。

用Python zoneinfo处理夏令时

使用zoneinfo的好处是它与datetimbe算术兼容,我们可以很容易地对它进行计算。例如,请看下面的代码,我们如何在日期上增加1天,并且它也改变了月份。由于zoneinfo对数据时间的算术的兼容性,月份发生了变化。

from zoneinfo import ZoneInfo
from datetime import datetime, timedelta

用zoneinfo获取日期时间对象

dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles")
print(dt)

在时区上增加一天

dt_add = dt + timedelta(day=1) print(dt_add) 请确保你是在3.7以上的python版本上运行上述程序。如果你没有这样做,它会给你一个错误。

用zoneinfo查找所有可用的时区 你可以调用函数available_timezones()来获取时区路径上任何地方的IANA时区的有效密钥的所有时区。这很费时间,因为它没有被预先提取,而是在每次调用该函数时都要重新计算。

import zoneinfo

for key in zoneinfo.available_timezones():
    if key.split("/")[0] == 'Asia':
        print(key)

关键词过滤如:'Asia',发现结果中有'Asia/Chongqing'

cq =ZoneInfo("Asia/Chongqing")
localized =datetime(2022,9,19,tzinfo=cq)
print(f"Datetime:{localized},"
      f"Timezone:{localized.tzname()},"
      f"TZ Info: {localized.tzinfo}")

输出重庆的时区

Datetime:2022-09-19 00:00:00+08:00,Timezone:CST,TZ Info: Asia/Chongqing

亚洲的时区

Asia/Colombo
Asia/Choibalsan
Asia/Ashgabat
Asia/Baghdad
Asia/Aden
Asia/Barnaul
Asia/Hovd
Asia/Tel_Aviv
Asia/Srednekolymsk
Asia/Dushanbe
Asia/Pontianak
Asia/Kuching
Asia/Aqtau
Asia/Ashkhabad
Asia/Tomsk
Asia/Chungking
Asia/Thimbu
Asia/Ust-Nera
Asia/Anadyr
Asia/Dacca
Asia/Qatar
Asia/Almaty
Asia/Samarkand
Asia/Saigon
Asia/Macao
Asia/Thimphu
Asia/Harbin
Asia/Ho_Chi_Minh
Asia/Kolkata
Asia/Damascus
Asia/Urumqi
Asia/Dhaka
Asia/Bahrain
Asia/Yekaterinburg
Asia/Beirut
Asia/Chongqing
Asia/Singapore
Asia/Yerevan
Asia/Rangoon
Asia/Irkutsk
Asia/Kuwait
Asia/Hong_Kong
Asia/Ulaanbaatar
Asia/Famagusta
Asia/Dili
Asia/Novokuznetsk
Asia/Aqtobe
Asia/Hebron
Asia/Dubai
Asia/Tokyo
Asia/Jerusalem
Asia/Vientiane
Asia/Sakhalin
Asia/Jayapura
Asia/Makassar
Asia/Shanghai
Asia/Riyadh
Asia/Tashkent
Asia/Kashgar
Asia/Baku
Asia/Qyzylorda
Asia/Jakarta
Asia/Qostanay
Asia/Manila
Asia/Katmandu
Asia/Macau
Asia/Kuala_Lumpur
Asia/Amman
Asia/Phnom_Penh
Asia/Chita
Asia/Krasnoyarsk
Asia/Pyongyang
Asia/Seoul
Asia/Omsk
Asia/Istanbul
Asia/Karachi
Asia/Vladivostok
Asia/Magadan
Asia/Bangkok
Asia/Nicosia
Asia/Kabul
Asia/Ulan_Bator
Asia/Tbilisi
Asia/Kamchatka
Asia/Kathmandu
Asia/Novosibirsk
Asia/Yangon
Asia/Gaza
Asia/Oral
Asia/Tehran
Asia/Bishkek
Asia/Atyrau
Asia/Brunei
Asia/Khandyga
Asia/Taipei
Asia/Calcutta
Asia/Ujung_Pandang
Asia/Muscat
Asia/Yakutsk

全部时区如下


America/Marigot
Europe/Tirane
US/Mountain
Etc/GMT-1
CST6CDT
Pacific/Funafuti
America/Denver
Egypt
Canada/Yukon
Asia/Aden
America/Bahia
Asia/Jayapura
Europe/Paris
America/Merida
America/Montevideo
America/Ensenada
Africa/Nairobi
America/Yakutat
Africa/Lagos
Asia/Kathmandu
Asia/Urumqi
America/Asuncion
Pacific/Apia
Asia/Seoul
America/Manaus
America/Bogota
America/Nuuk
Asia/Baghdad
America/Sao_Paulo
Canada/Newfoundland
Arctic/Longyearbyen
Australia/Hobart
America/Louisville
Etc/GMT-5
Asia/Vladivostok
America/Cuiaba
Europe/Sarajevo
Asia/Shanghai
America/Caracas
Europe/Prague
Antarctica/Rothera
Asia/Dili
US/Samoa
America/Antigua
MET
America/Miquelon
Asia/Kuwait
US/Alaska
Europe/Tiraspol
America/Swift_Current
Europe/London
Europe/Vilnius
Pacific/Gambier
Asia/Nicosia
Etc/Zulu
America/Puerto_Rico
Etc/GMT+12
America/Indiana/Knox
Indian/Christmas
Pacific/Tongatapu
UTC
America/Argentina/La_Rioja
Africa/Bujumbura
Africa/Asmera
Europe/Andorra
Brazil/Acre
Europe/Zagreb
Africa/Harare
Hongkong
America/Indiana/Vincennes
US/Pacific
Brazil/East
America/Virgin
America/Goose_Bay
America/Belize
Canada/Pacific
Africa/Maputo
Africa/Ndjamena
Pacific/Port_Moresby
Asia/Tomsk
Europe/San_Marino
Europe/Volgograd
Asia/Bishkek
Asia/Ust-Nera
Europe/Helsinki
Africa/Gaborone
America/Argentina/Rio_Gallegos
Portugal
Pacific/Palau
Europe/Malta
Atlantic/Faroe
Asia/Gaza
Atlantic/Jan_Mayen
Asia/Qostanay
Eire
America/St_Vincent
Antarctica/South_Pole
Asia/Dushanbe
America/Aruba
America/Phoenix
Etc/GMT-3
NZ-CHAT
America/Ojinaga
Australia/Lindeman
Asia/Karachi
Africa/Bamako
Pacific/Fakaofo
Antarctica/Mawson
America/Dawson_Creek
Antarctica/DumontDUrville
America/Boa_Vista
America/Indianapolis
Asia/Anadyr
Asia/Brunei
Europe/Bratislava
Indian/Chagos
America/Cancun
Pacific/Tarawa
Africa/Porto-Novo
America/Rainy_River
America/Inuvik
Africa/Abidjan
Pacific/Auckland
Europe/Tallinn
Africa/Johannesburg
America/Monterrey
America/Port_of_Spain
America/Guatemala
America/Edmonton
Australia/Lord_Howe
America/Indiana/Indianapolis
Africa/Ouagadougou
Zulu
Atlantic/St_Helena
America/Shiprock
America/Eirunepe
America/Resolute
Australia/LHI
Antarctica/Davis
Canada/Central
Pacific/Niue
Asia/Yangon
Etc/GMT-7
America/Grand_Turk
Asia/Macau
Europe/Samara
ROC
America/Argentina/Catamarca
America/St_Johns
Asia/Muscat
Europe/Madrid
Asia/Choibalsan
America/Cambridge_Bay
Asia/Ujung_Pandang
Europe/Rome
Asia/Dacca
Atlantic/Cape_Verde
Australia/Victoria
Pacific/Kiritimati
HST
Etc/GMT0
Asia/Ashgabat
America/Guyana
PST8PDT
Africa/Accra
Pacific/Honolulu
Europe/Stockholm
Pacific/Midway
America/Argentina/Salta
Etc/GMT-8
America/Chicago
America/Kentucky/Monticello
Australia/Darwin
Pacific/Nauru
Asia/Phnom_Penh
Asia/Srednekolymsk
Asia/Jerusalem
America/Buenos_Aires
Australia/ACT
US/Central
Atlantic/Canary
Asia/Qatar
America/Fort_Nelson
Australia/Melbourne
Pacific/Pitcairn
America/Porto_Acre
Europe/Oslo
Etc/GMT+10
Asia/Istanbul
GMT-0
Africa/Conakry
America/Tegucigalpa
America/Punta_Arenas
Pacific/Fiji
Cuba
America/Lower_Princes
America/Argentina/Tucuman
Asia/Makassar
America/St_Kitts
America/Chihuahua
Europe/Saratov
Greenwich
Australia/NSW
Asia/Pyongyang
Europe/Vienna
Africa/Ceuta
Etc/GMT+3
Europe/Warsaw
Pacific/Majuro
Universal
Etc/GMT-12
America/Argentina/Cordoba
Europe/Isle_of_Man
Asia/Chongqing
Europe/Amsterdam
America/Nipigon
Pacific/Kanton
Indian/Kerguelen
EET
Asia/Pontianak
America/Yellowknife
Navajo
America/Atikokan
Asia/Kamchatka
Africa/Mbabane
Atlantic/South_Georgia
Etc/GMT+7
Europe/Brussels
Pacific/Pago_Pago
Mexico/BajaNorte
America/Indiana/Petersburg
Asia/Barnaul
Africa/Asmara
Asia/Beirut
Australia/Currie
America/Dawson
Asia/Ashkhabad
America/North_Dakota/Center
America/Rosario
MST7MDT
Pacific/Enderbury
Asia/Katmandu
Pacific/Easter
Israel
Africa/Blantyre
build/etc/localtime
Asia/Atyrau
Australia/Yancowinna
America/Anchorage
Asia/Chita
Asia/Harbin
US/Michigan
America/Martinique
Europe/Gibraltar
Asia/Novokuznetsk
Europe/Luxembourg
Europe/Athens
America/Thunder_Bay
America/Danmarkshavn
Asia/Saigon
America/Belem
Europe/Belfast
Pacific/Wake
Africa/Dakar
Africa/Douala
Mexico/BajaSur
Asia/Tbilisi
Kwajalein
Asia/Tokyo
America/Santiago
Asia/Aqtau
Etc/GMT+5
Jamaica
Pacific/Rarotonga
Etc/GMT-11
America/Juneau
America/Halifax
Japan
America/Cayman
Europe/Budapest
Europe/Podgorica
Indian/Maldives
Asia/Tel_Aviv
Pacific/Johnston
Indian/Antananarivo
Asia/Hong_Kong
America/Jamaica
Australia/Broken_Hill
Indian/Mahe
Asia/Amman
Etc/GMT+0
Atlantic/Stanley
Pacific/Chuuk
Poland
Chile/EasterIsland
Europe/Skopje
America/St_Thomas
Africa/Casablanca
Europe/Kirov
Africa/Niamey
Asia/Irkutsk
Etc/GMT-6
Europe/Busingen
America/Indiana/Winamac
America/Argentina/Jujuy
Pacific/Noumea
Africa/Juba
America/Managua
Africa/Tripoli
America/Indiana/Tell_City
Asia/Calcutta
Australia/Sydney
WET
America/Rankin_Inlet
Europe/Nicosia
Indian/Mauritius
America/Tijuana
Europe/Istanbul
Europe/Zurich
Asia/Hovd
Australia/Eucla
US/Eastern
Etc/GMT-14
Asia/Colombo
Africa/Djibouti
America/Argentina/Mendoza
Asia/Yekaterinburg
Africa/Banjul
Europe/Vatican
Indian/Cocos
Africa/Freetown
Europe/Kaliningrad
Antarctica/Casey
Etc/GMT
Atlantic/Madeira
Australia/Canberra
Turkey
Pacific/Norfolk
Asia/Aqtobe
UCT
America/Argentina/San_Juan
Antarctica/Macquarie
Asia/Macao
Canada/Eastern
Etc/GMT+2
Africa/Algiers
Etc/GMT+9
Etc/GMT-2
Asia/Ulan_Bator
America/Kentucky/Louisville
America/Thule
GMT+0
America/Pangnirtung
America/Guayaquil
Europe/Jersey
America/Los_Angeles
Asia/Damascus
America/Jujuy
Asia/Thimphu
Europe/Copenhagen
Europe/Riga
Asia/Singapore
Pacific/Pohnpei
Asia/Qyzylorda
Asia/Magadan
Indian/Reunion
Pacific/Guadalcanal
Atlantic/Faeroe
America/Argentina/ComodRivadavia
Pacific/Tahiti
Antarctica/Vostok
Africa/Mogadishu
America/Panama
America/Tortola
Africa/Windhoek
Asia/Rangoon
Europe/Kiev
Australia/Brisbane
Africa/Maseru
Europe/Mariehamn
America/Maceio
America/Santarem
Asia/Yerevan
Etc/GMT-10
Mexico/General
America/Metlakatla
America/Regina
Europe/Astrakhan
America/Cayenne
Australia/South
America/La_Paz
Africa/Kampala
Asia/Kabul
Asia/Krasnoyarsk
Europe/Ulyanovsk
Asia/Sakhalin
Asia/Ulaanbaatar
America/Indiana/Marengo
America/Fortaleza
America/Glace_Bay
Asia/Tashkent
Chile/Continental
Africa/Brazzaville
Asia/Bahrain
America/Barbados
Asia/Riyadh
Pacific/Saipan
America/Sitka
America/Port-au-Prince
Etc/GMT+6
America/Grenada
Asia/Baku
Atlantic/Bermuda
America/Rio_Branco
US/Indiana-Starke
America/Menominee
Pacific/Ponape
W-SU
US/East-Indiana
Etc/GMT-0
Indian/Comoro
Asia/Oral
Australia/Tasmania
America/Santa_Isabel
America/Lima
Europe/Minsk
GB
Etc/GMT+1
CET
Asia/Dubai
Asia/Yakutsk
America/Curacao
Antarctica/Troll
PRC
Europe/Dublin
Asia/Khandyga
Pacific/Kwajalein
Africa/Bangui
America/Nome
GB-Eire
America/New_York
Australia/Queensland
Africa/Timbuktu
Antarctica/Palmer
Europe/Sofia
Pacific/Chatham
America/Atka
Africa/Tunis
Europe/Uzhgorod
America/Porto_Velho
Etc/Universal
Africa/Kinshasa
Australia/Adelaide
America/Boise
America/Havana
Africa/Luanda
Brazil/West
Etc/GMT-13
Canada/Mountain
Europe/Bucharest
America/Creston
Pacific/Wallis
Asia/Tehran
America/Iqaluit
Canada/Atlantic
Europe/Chisinau
Australia/West
Africa/Bissau
America/Indiana/Vevay
America/Winnipeg
US/Hawaii
Antarctica/Syowa
America/Hermosillo
America/Bahia_Banderas
America/St_Lucia
America/Mazatlan
America/Costa_Rica
Africa/Addis_Ababa
Etc/GMT-9
Indian/Mayotte
Pacific/Guam
America/Cordoba
MST
Asia/Manila
Asia/Hebron
Australia/North
Etc/GMT+8
US/Arizona
Pacific/Efate
America/Vancouver
Australia/Perth
America/North_Dakota/New_Salem
Pacific/Yap
America/Godthab
Europe/Belgrade
America/Araguaina
Asia/Almaty
America/Knox_IN
Africa/Kigali
Etc/Greenwich
Libya
Africa/Lubumbashi
Atlantic/Reykjavik
Africa/Lusaka
America/Blanc-Sablon
Etc/GMT+11
GMT
America/Mexico_City
Africa/El_Aaiun
Asia/Kolkata
Pacific/Bougainville
America/Anguilla
Asia/Famagusta
Iceland
Pacific/Truk
Europe/Ljubljana
EST
America/Detroit
Africa/Sao_Tome
Singapore
GMT0
Asia/Novosibirsk
Africa/Libreville
America/Mendoza
Asia/Thimbu
Pacific/Galapagos
Europe/Berlin
America/El_Salvador
America/Montserrat
Asia/Samarkand
America/Noronha
America/Whitehorse
America/Montreal
Asia/Omsk
Europe/Moscow
Africa/Cairo
Factory
Africa/Nouakchott
America/Nassau
Etc/UTC
Asia/Jakarta
Europe/Simferopol
America/Coral_Harbour
Antarctica/McMurdo
Europe/Lisbon
America/Fort_Wayne
Europe/Zaporozhye
America/Guadeloupe
Asia/Chungking
America/North_Dakota/Beulah
Asia/Kashgar
America/Adak
Brazil/DeNoronha
Pacific/Samoa
America/Catamarca
Africa/Khartoum
Pacific/Kosrae
America/Kralendijk
Europe/Guernsey
Asia/Kuala_Lumpur
Etc/GMT+4
US/Aleutian
Africa/Dar_es_Salaam
Pacific/Marquesas
NZ
Africa/Malabo
America/Argentina/Buenos_Aires
Europe/Monaco
Asia/Vientiane
Iran
America/Argentina/Ushuaia
Etc/GMT-4
America/Moncton
Africa/Lome
America/Matamoros
Asia/Bangkok
Europe/Vaduz
America/Argentina/San_Luis
Asia/Ho_Chi_Minh
America/Scoresbysund
Canada/Saskatchewan
EST5EDT
America/Dominica
Asia/Taipei
ROK
America/Paramaribo
Asia/Kuching
America/Recife
Asia/Dhaka
America/Toronto
Atlantic/Azores
America/Campo_Grande
America/St_Barthelemy
Africa/Monrovia
Etc/UCT
America/Santo_Domingo

分类:

后端

标签:

后端

作者介绍

c
codeye
V1