How to convert timezone to country code in Python? -


i used this

from pytz import country_timezones 

but 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 convert these timezones country code?

you can't want. or, can, you'll results you're getting, not results want. briefly, if ask "the country uses zulu", , no country uses zulu, won't able find anything. in more detail…


as docs on country information say:

a mechanism provided access timezones commonly in use particular country, looked using iso 3166 country code.


however, "deprecated" zones america/buenos_aires , "historical" zones us/pacific aren't in use in particular country. many of them do happen aliases timezones are in use in country, america/argentina/buenos_aires , america/los_angeles, respectively, doesn't good, because pytz doesn't expose information. file enhancement request against pytz add in future version, if think it's important.


at rate, how can identify countries use given timezone, this:

{country country, timezones in country_timezones.items()  if timezone in timezones} 

if need lots of these lookups, can of course build own dict make faster , simpler:

timezone_countries = {} country, timezones in country_timezones.items():     timezone in timezones:         timezone_countries.setdefault(timezone, set()).add(country) 

and it's just:

timezone_countries[timezone] 

but either way, may empty set, or set of 3 countries, instead of 1. if database says there 0 or 3 countries use timezone, that's you're going get.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -