Map ISO 3166 country code to a list of timezone names commonly used in that country. iso3166_code is the two letter code used to identify the country. >>> def print_list(list_of_strings): ... 'We use a helper so doctests work under Python 2.3 -> 3.x' ... for s in list_o
| 311 | |
| 312 | |
| 313 | class _CountryTimezoneDict(LazyDict): |
| 314 | """Map ISO 3166 country code to a list of timezone names commonly used |
| 315 | in that country. |
| 316 | |
| 317 | iso3166_code is the two letter code used to identify the country. |
| 318 | |
| 319 | >>> def print_list(list_of_strings): |
| 320 | ... 'We use a helper so doctests work under Python 2.3 -> 3.x' |
| 321 | ... for s in list_of_strings: |
| 322 | ... print(s) |
| 323 | |
| 324 | >>> print_list(country_timezones['nz']) |
| 325 | Pacific/Auckland |
| 326 | Pacific/Chatham |
| 327 | >>> print_list(country_timezones['ch']) |
| 328 | Europe/Zurich |
| 329 | >>> print_list(country_timezones['CH']) |
| 330 | Europe/Zurich |
| 331 | >>> print_list(country_timezones[unicode('ch')]) |
| 332 | Europe/Zurich |
| 333 | >>> print_list(country_timezones['XXX']) |
| 334 | Traceback (most recent call last): |
| 335 | ... |
| 336 | KeyError: 'XXX' |
| 337 | |
| 338 | Previously, this information was exposed as a function rather than a |
| 339 | dictionary. This is still supported:: |
| 340 | |
| 341 | >>> print_list(country_timezones('nz')) |
| 342 | Pacific/Auckland |
| 343 | Pacific/Chatham |
| 344 | """ |
| 345 | def __call__(self, iso3166_code): |
| 346 | """Backwards compatibility.""" |
| 347 | return self[iso3166_code] |
| 348 | |
| 349 | def _fill(self): |
| 350 | data = {} |
| 351 | zone_tab = open_resource('zone.tab') |
| 352 | try: |
| 353 | for line in zone_tab: |
| 354 | line = line.decode('UTF-8') |
| 355 | if line.startswith('#'): |
| 356 | continue |
| 357 | code, coordinates, zone = line.split(None, 4)[:3] |
| 358 | if zone not in all_timezones_set: # noqa |
| 359 | continue |
| 360 | try: |
| 361 | data[code].append(zone) |
| 362 | except KeyError: |
| 363 | data[code] = [zone] |
| 364 | self.data = data |
| 365 | finally: |
| 366 | zone_tab.close() |
| 367 | |
| 368 | |
| 369 | country_timezones = _CountryTimezoneDict() |