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