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