Reputation: 2749
I used this
from pytz import country_timezones
But It doesn't include below timezones
Africa/Asmera, Africa/Timbuktu, America/Argentina/ComodRivadavia, America/Atka, America/Buenos_Aires, America/Catamarca, America/Coral_Harbour, America/Cordoba, America/Ensenada, America/Fort_Wayne, America/Indianapolis, America/Jujuy, America/Knox_IN, America/Louisville, America/Mendoza, America/Montreal, America/Porto_Acre, America/Rosario, America/Shiprock, America/Virgin, Antarctica/South_Pole, Asia/Ashkhabad, Asia/Calcutta, Asia/Chongqing, Asia/Chungking, Asia/Dacca, Asia/Harbin, Asia/Istanbul, Asia/Kashgar, Asia/Katmandu, Asia/Macao, Asia/Saigon, Asia/Tel_Aviv, Asia/Thimbu, Asia/Ujung_Pandang, Asia/Ulan_Bator, Atlantic/Faeroe, Atlantic/Jan_Mayen, Australia/ACT, Australia/Canberra, Australia/LHI, Australia/NSW, Australia/North, Australia/Queensland, Australia/South, Australia/Tasmania, Australia/Victoria, Australia/West, Australia/Yancowinna, Brazil/Acre, Brazil/DeNoronha, Brazil/East, Brazil/West, CET, CST6CDT, Canada/Atlantic, Canada/Central, Canada/East-Saskatchewan, Canada/Eastern, Canada/Mountain, Canada/Newfoundland, Canada/Pacific, Canada/Saskatchewan, Canada/Yukon, Chile/Continental, Chile/EasterIsland, Cuba, EET, EST, EST5EDT, Egypt, Eire, Europe/Belfast, Europe/Nicosia, Europe/Tiraspol, GB, GB-Eire, Greenwich, HST, Hongkong, Iceland, Iran, Israel, Jamaica, Japan, Kwajalein, Libya, MET, MST, MST7MDT, Mexico/BajaNorte, Mexico/BajaSur, Mexico/General, NZ, NZ-CHAT, Navajo, PRC, PST8PDT, Pacific/Ponape, Pacific/Samoa, Pacific/Truk, Pacific/Yap, Poland, Portugal, ROC, ROK, Singapore, Turkey, UCT, US/Alaska, US/Aleutian, US/Arizona, US/Central, US/East-Indiana, US/Eastern, US/Hawaii, US/Indiana-Starke, US/Michigan, US/Mountain, US/Pacific, US/Samoa, UTC, Universal, W-SU, WET, Zulu
How can I convert these timezones to country code?
Upvotes: 3
Views: 3105
Reputation: 365657
You can't do what you want. Or, you can, but you'll get the results you're getting, not the results you want. Briefly, if you ask for "the country that uses Zulu", and no country uses Zulu, you won't be able to find anything. In more detail…
As the docs on Country Information say:
A mechanism is provided to access the timezones commonly in use for a particular country, looked up using the ISO 3166 country code.
However, "deprecated" zones like America/Buenos_Aires
and "historical" zones like US/Pacific
aren't in use in any particular country. Many of them do happen to be aliases for timezones that are in use in some country, like America/Argentina/Buenos_Aires
and America/Los_Angeles
, respectively, but that doesn't do you any good, because pytz
doesn't expose that information. You could file an enhancement request against pytz
to add that in a future version, if you think it's important.
At any rate, this is how you can identify the countries that use a given timezone, like this:
{country for country, timezones in country_timezones.items()
if timezone in timezones}
If you need to do lots of these lookups, you can of course build your own dict to make it faster and simpler:
timezone_countries = {}
for country, timezones in country_timezones.items():
for timezone in timezones:
timezone_countries.setdefault(timezone, set()).add(country)
And now it's just:
timezone_countries[timezone]
But either way, you may get an empty set, or a set of 3 countries, instead of 1. If the database actually says that there are 0 or 3 countries that use that timezone, that's what you're going to get.
Upvotes: 2