timezone.cc 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  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
  3. // found in the LICENSE file.
  4. #include "base/i18n/timezone.h"
  5. #include <memory>
  6. #include <string>
  7. #include "third_party/icu/source/common/unicode/unistr.h"
  8. #include "third_party/icu/source/i18n/unicode/timezone.h"
  9. namespace base {
  10. std::string CountryCodeForCurrentTimezone() {
  11. std::unique_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
  12. icu::UnicodeString id;
  13. // ICU returns '001' (world) for Etc/GMT. Preserve the old behavior
  14. // only for Etc/GMT while returning an empty string for Etc/UTC and
  15. // Etc/UCT because they're less likely to be chosen by mistake in UK in
  16. // place of Europe/London (Briitish Time).
  17. if (zone->getID(id) == UNICODE_STRING_SIMPLE("Etc/GMT"))
  18. return "GB";
  19. char region_code[4];
  20. UErrorCode status = U_ZERO_ERROR;
  21. int length = zone->getRegion(id, region_code, 4, status);
  22. // Return an empty string if region_code is a 3-digit numeric code such
  23. // as 001 (World) for Etc/UTC, Etc/UCT.
  24. return (U_SUCCESS(status) && length == 2)
  25. ? std::string(region_code, static_cast<size_t>(length))
  26. : std::string();
  27. }
  28. } // namespace base