timezone_settings_helper.cc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #include "ash/components/settings/timezone_settings_helper.h"
  5. #include "base/check.h"
  6. #include "base/component_export.h"
  7. namespace ash {
  8. namespace system {
  9. COMPONENT_EXPORT(ASH_SETTINGS)
  10. const icu::TimeZone* GetKnownTimezoneOrNull(
  11. const icu::TimeZone& timezone,
  12. const std::vector<std::unique_ptr<icu::TimeZone>>& timezone_list) {
  13. const icu::TimeZone* known_timezone = nullptr;
  14. icu::UnicodeString id, canonical_id;
  15. timezone.getID(id);
  16. UErrorCode status = U_ZERO_ERROR;
  17. icu::TimeZone::getCanonicalID(id, canonical_id, status);
  18. DCHECK(U_SUCCESS(status));
  19. for (const auto& entry : timezone_list) {
  20. if (*entry.get() == timezone)
  21. return entry.get();
  22. // Compare the canonical IDs as well.
  23. // For instance, Asia/Ulan_Bator -> Asia/Ulaanbaatar or
  24. // Canada/Pacific -> America/Vancouver
  25. icu::UnicodeString entry_id, entry_canonical_id;
  26. entry->getID(entry_id);
  27. icu::TimeZone::getCanonicalID(entry_id, entry_canonical_id, status);
  28. DCHECK(U_SUCCESS(status));
  29. if (entry_canonical_id == canonical_id)
  30. return entry.get();
  31. // Last resort: If no match is found, the last timezone in the list
  32. // with matching rules will be returned.
  33. if (entry->hasSameRules(timezone))
  34. known_timezone = entry.get();
  35. }
  36. // May return null if we did not find a matching timezone in our list.
  37. return known_timezone;
  38. }
  39. } // namespace system
  40. } // namespace ash