OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/i18n/timezone.h" | 5 #include "base/i18n/timezone.h" |
6 | 6 |
| 7 #include <string.h> |
| 8 |
7 #include <map> | 9 #include <map> |
8 | 10 |
9 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
10 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
11 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
12 #include "third_party/icu/source/i18n/unicode/timezone.h" | 14 #include "third_party/icu/source/i18n/unicode/timezone.h" |
13 | 15 |
14 namespace base { | 16 namespace base { |
15 | 17 |
16 namespace { | 18 namespace { |
17 | 19 |
18 class TimezoneMap { | 20 class TimezoneMap { |
19 public: | 21 public: |
20 static TimezoneMap* GetInstance() { | 22 static TimezoneMap* GetInstance() { |
21 return Singleton<TimezoneMap>::get(); | 23 return Singleton<TimezoneMap>::get(); |
22 } | 24 } |
23 | 25 |
24 std::string CountryCodeForTimezone(const std::string& olson_code) { | 26 std::string CountryCodeForTimezone(const std::string& olson_code) { |
25 std::map<std::string, std::string>::iterator iter = map_.find(olson_code); | 27 std::map<const char*, const char*, CompareCStrings>::iterator iter = |
| 28 map_.find(olson_code.c_str()); |
26 if (iter != map_.end()) | 29 if (iter != map_.end()) |
27 return iter->second; | 30 return iter->second; |
28 | 31 |
29 return std::string(); | 32 return std::string(); |
30 } | 33 } |
31 | 34 |
32 private: | 35 private: |
33 TimezoneMap() { | 36 TimezoneMap() { |
34 // These mappings are adapted from zone.tab, which is available at | 37 // These mappings are adapted from zone.tab, which is available at |
35 // <http://www.ietf.org/timezones/data/zone.tab> and is a part of public | 38 // <http://www.ietf.org/timezones/data/zone.tab> and is a part of public |
36 // domain. | 39 // domain. |
37 struct OlsonCodeData { | 40 struct OlsonCodeData { |
38 std::string country_code; | 41 const char* country_code; |
39 std::string olson_code; | 42 const char* olson_code; |
40 } olson_code_data[] = { | 43 }; |
| 44 static const OlsonCodeData olson_code_data[] = { |
41 { "AD", "Europe/Andorra" }, | 45 { "AD", "Europe/Andorra" }, |
42 { "AE", "Asia/Dubai" }, | 46 { "AE", "Asia/Dubai" }, |
43 { "AF", "Asia/Kabul" }, | 47 { "AF", "Asia/Kabul" }, |
44 { "AG", "America/Antigua" }, | 48 { "AG", "America/Antigua" }, |
45 { "AI", "America/Anguilla" }, | 49 { "AI", "America/Anguilla" }, |
46 { "AL", "Europe/Tirane" }, | 50 { "AL", "Europe/Tirane" }, |
47 { "AM", "Asia/Yerevan" }, | 51 { "AM", "Asia/Yerevan" }, |
48 { "AO", "Africa/Luanda" }, | 52 { "AO", "Africa/Luanda" }, |
49 { "AQ", "Antarctica/McMurdo" }, | 53 { "AQ", "Antarctica/McMurdo" }, |
50 { "AQ", "Antarctica/Rothera" }, | 54 { "AQ", "Antarctica/Rothera" }, |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 { "YT", "Indian/Mayotte" }, | 456 { "YT", "Indian/Mayotte" }, |
453 { "ZA", "Africa/Johannesburg" }, | 457 { "ZA", "Africa/Johannesburg" }, |
454 { "ZM", "Africa/Lusaka" }, | 458 { "ZM", "Africa/Lusaka" }, |
455 { "ZW", "Africa/Harare" }, | 459 { "ZW", "Africa/Harare" }, |
456 // The mappings below are custom additions to zone.tab. | 460 // The mappings below are custom additions to zone.tab. |
457 { "GB", "Etc/GMT" }, | 461 { "GB", "Etc/GMT" }, |
458 { "GB", "Etc/UTC" }, | 462 { "GB", "Etc/UTC" }, |
459 { "GB", "Etc/UCT" }, | 463 { "GB", "Etc/UCT" }, |
460 }; | 464 }; |
461 | 465 |
462 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(olson_code_data); ++i) { | 466 for (size_t i = 0; i < arraysize(olson_code_data); ++i) |
463 map_[olson_code_data[i].olson_code] = olson_code_data[i].country_code; | 467 map_[olson_code_data[i].olson_code] = olson_code_data[i].country_code; |
464 } | |
465 | 468 |
466 // These are mapping from old codenames to new codenames. They are also | 469 // These are mapping from old codenames to new codenames. They are also |
467 // part of public domain, and available at | 470 // part of public domain, and available at |
468 // <http://www.ietf.org/timezones/data/backward>. | 471 // <http://www.ietf.org/timezones/data/backward>. |
469 struct LinkData { | 472 struct LinkData { |
470 std::string old_code; | 473 const char* old_code; |
471 std::string new_code; | 474 const char* new_code; |
472 } link_data[] = { | 475 }; |
| 476 static const LinkData link_data[] = { |
473 { "Africa/Asmera", "Africa/Asmara" }, | 477 { "Africa/Asmera", "Africa/Asmara" }, |
474 { "Africa/Timbuktu", "Africa/Bamako" }, | 478 { "Africa/Timbuktu", "Africa/Bamako" }, |
475 { "America/Argentina/ComodRivadavia", "America/Argentina/Catamarca" }, | 479 { "America/Argentina/ComodRivadavia", "America/Argentina/Catamarca" }, |
476 { "America/Atka", "America/Adak" }, | 480 { "America/Atka", "America/Adak" }, |
477 { "America/Buenos_Aires", "America/Argentina/Buenos_Aires" }, | 481 { "America/Buenos_Aires", "America/Argentina/Buenos_Aires" }, |
478 { "America/Catamarca", "America/Argentina/Catamarca" }, | 482 { "America/Catamarca", "America/Argentina/Catamarca" }, |
479 { "America/Coral_Harbour", "America/Atikokan" }, | 483 { "America/Coral_Harbour", "America/Atikokan" }, |
480 { "America/Cordoba", "America/Argentina/Cordoba" }, | 484 { "America/Cordoba", "America/Argentina/Cordoba" }, |
481 { "America/Ensenada", "America/Tijuana" }, | 485 { "America/Ensenada", "America/Tijuana" }, |
482 { "America/Fort_Wayne", "America/Indiana/Indianapolis" }, | 486 { "America/Fort_Wayne", "America/Indiana/Indianapolis" }, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 { "US/Michigan", "America/Detroit" }, | 579 { "US/Michigan", "America/Detroit" }, |
576 { "US/Mountain", "America/Denver" }, | 580 { "US/Mountain", "America/Denver" }, |
577 { "US/Pacific", "America/Los_Angeles" }, | 581 { "US/Pacific", "America/Los_Angeles" }, |
578 { "US/Samoa", "Pacific/Pago_Pago" }, | 582 { "US/Samoa", "Pacific/Pago_Pago" }, |
579 { "UTC", "Etc/UTC" }, | 583 { "UTC", "Etc/UTC" }, |
580 { "Universal", "Etc/UTC" }, | 584 { "Universal", "Etc/UTC" }, |
581 { "W-SU", "Europe/Moscow" }, | 585 { "W-SU", "Europe/Moscow" }, |
582 { "Zulu", "Etc/UTC" }, | 586 { "Zulu", "Etc/UTC" }, |
583 }; | 587 }; |
584 | 588 |
585 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(link_data); ++i) { | 589 for (size_t i = 0; i < arraysize(link_data); ++i) |
586 map_[link_data[i].old_code] = map_[link_data[i].new_code]; | 590 map_[link_data[i].old_code] = map_[link_data[i].new_code]; |
587 } | |
588 } | 591 } |
589 | 592 |
590 friend struct DefaultSingletonTraits<TimezoneMap>; | 593 friend struct DefaultSingletonTraits<TimezoneMap>; |
591 | 594 |
592 std::map<std::string, std::string> map_; | 595 struct CompareCStrings { |
| 596 bool operator()(const char* str1, const char* str2) const { |
| 597 return strcmp(str1, str2) < 0; |
| 598 } |
| 599 }; |
| 600 std::map<const char*, const char*, CompareCStrings> map_; |
593 | 601 |
594 DISALLOW_COPY_AND_ASSIGN(TimezoneMap); | 602 DISALLOW_COPY_AND_ASSIGN(TimezoneMap); |
595 }; | 603 }; |
596 | 604 |
597 } // namespace | 605 } // namespace |
598 | 606 |
599 std::string CountryCodeForCurrentTimezone() { | 607 std::string CountryCodeForCurrentTimezone() { |
600 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); | 608 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); |
601 icu::UnicodeString id; | 609 icu::UnicodeString id; |
602 zone->getID(id); | 610 zone->getID(id); |
603 string16 olson_code(id.getBuffer(), id.length()); | 611 string16 olson_code(id.getBuffer(), id.length()); |
604 return TimezoneMap::GetInstance()->CountryCodeForTimezone( | 612 return TimezoneMap::GetInstance()->CountryCodeForTimezone( |
605 UTF16ToUTF8(olson_code)); | 613 UTF16ToUTF8(olson_code)); |
606 } | 614 } |
607 | 615 |
608 } // namespace base | 616 } // namespace base |
OLD | NEW |