blob: b4203a6f4713868abc69469010c73937dd68c1ff [file] [log] [blame]
jshin77e507b2015-04-24 01:13:151// 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 Wennborg0fb6e082020-04-28 06:02:337#include "base/check.h"
Steven Bennetts036b9b62019-01-11 17:47:578#include "base/component_export.h"
jshin77e507b2015-04-24 01:13:159
10namespace chromeos {
11namespace system {
12
Steven Bennetts036b9b62019-01-11 17:47:5713COMPONENT_EXPORT(CHROMEOS_SETTINGS)
14const icu::TimeZone* GetKnownTimezoneOrNull(
jshin77e507b2015-04-24 01:13:1515 const icu::TimeZone& timezone,
avidfcc90f12016-10-04 22:49:0116 const std::vector<std::unique_ptr<icu::TimeZone>>& timezone_list) {
17 const icu::TimeZone* known_timezone = nullptr;
jshin77e507b2015-04-24 01:13:1518 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));
avidfcc90f12016-10-04 22:49:0123 for (const auto& entry : timezone_list) {
24 if (*entry.get() == timezone)
25 return entry.get();
jshin77e507b2015-04-24 01:13:1526 // 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)
avidfcc90f12016-10-04 22:49:0134 return entry.get();
jshin77e507b2015-04-24 01:13:1535 // 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))
avidfcc90f12016-10-04 22:49:0138 known_timezone = entry.get();
jshin77e507b2015-04-24 01:13:1539 }
40
avidfcc90f12016-10-04 22:49:0141 // May return null if we did not find a matching timezone in our list.
jshin77e507b2015-04-24 01:13:1542 return known_timezone;
43}
44
45} // namespace system
46} // namespace chromeos