jshin | 77e507b | 2015-04-24 01:13:15 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chromeos/settings/timezone_settings_helper.h" |
| 6 | |
Hans Wennborg | 0fb6e08 | 2020-04-28 06:02:33 | [diff] [blame] | 7 | #include "base/check.h" |
Steven Bennetts | 036b9b6 | 2019-01-11 17:47:57 | [diff] [blame] | 8 | #include "base/component_export.h" |
jshin | 77e507b | 2015-04-24 01:13:15 | [diff] [blame] | 9 | |
| 10 | namespace chromeos { |
| 11 | namespace system { |
| 12 | |
Steven Bennetts | 036b9b6 | 2019-01-11 17:47:57 | [diff] [blame] | 13 | COMPONENT_EXPORT(CHROMEOS_SETTINGS) |
| 14 | const icu::TimeZone* GetKnownTimezoneOrNull( |
jshin | 77e507b | 2015-04-24 01:13:15 | [diff] [blame] | 15 | const icu::TimeZone& timezone, |
avi | dfcc90f1 | 2016-10-04 22:49:01 | [diff] [blame] | 16 | const std::vector<std::unique_ptr<icu::TimeZone>>& timezone_list) { |
| 17 | const icu::TimeZone* known_timezone = nullptr; |
jshin | 77e507b | 2015-04-24 01:13:15 | [diff] [blame] | 18 | icu::UnicodeString id, canonical_id; |
| 19 | timezone.getID(id); |
| 20 | UErrorCode status = U_ZERO_ERROR; |
| 21 | icu::TimeZone::getCanonicalID(id, canonical_id, status); |
| 22 | DCHECK(U_SUCCESS(status)); |
avi | dfcc90f1 | 2016-10-04 22:49:01 | [diff] [blame] | 23 | for (const auto& entry : timezone_list) { |
| 24 | if (*entry.get() == timezone) |
| 25 | return entry.get(); |
jshin | 77e507b | 2015-04-24 01:13:15 | [diff] [blame] | 26 | // Compare the canonical IDs as well. |
| 27 | // For instance, Asia/Ulan_Bator -> Asia/Ulaanbaatar or |
| 28 | // Canada/Pacific -> America/Vancouver |
| 29 | icu::UnicodeString entry_id, entry_canonical_id; |
| 30 | entry->getID(entry_id); |
| 31 | icu::TimeZone::getCanonicalID(entry_id, entry_canonical_id, status); |
| 32 | DCHECK(U_SUCCESS(status)); |
| 33 | if (entry_canonical_id == canonical_id) |
avi | dfcc90f1 | 2016-10-04 22:49:01 | [diff] [blame] | 34 | return entry.get(); |
jshin | 77e507b | 2015-04-24 01:13:15 | [diff] [blame] | 35 | // Last resort: If no match is found, the last timezone in the list |
| 36 | // with matching rules will be returned. |
| 37 | if (entry->hasSameRules(timezone)) |
avi | dfcc90f1 | 2016-10-04 22:49:01 | [diff] [blame] | 38 | known_timezone = entry.get(); |
jshin | 77e507b | 2015-04-24 01:13:15 | [diff] [blame] | 39 | } |
| 40 | |
avi | dfcc90f1 | 2016-10-04 22:49:01 | [diff] [blame] | 41 | // May return null if we did not find a matching timezone in our list. |
jshin | 77e507b | 2015-04-24 01:13:15 | [diff] [blame] | 42 | return known_timezone; |
| 43 | } |
| 44 | |
| 45 | } // namespace system |
| 46 | } // namespace chromeos |