| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/browser/ui/webui/options2/chromeos/timezone_options_util.h" |
| 6 |
| 7 #include "base/i18n/rtl.h" |
| 8 #include "base/lazy_instance.h" |
| 9 #include "base/string_util.h" |
| 10 #include "base/stringprintf.h" |
| 11 #include "base/synchronization/lock.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" |
| 14 #include "chrome/browser/chromeos/system/timezone_settings.h" |
| 15 #include "grit/generated_resources.h" |
| 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "unicode/calendar.h" |
| 18 #include "unicode/timezone.h" |
| 19 #include "unicode/ures.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 struct UResClose { |
| 24 inline void operator() (UResourceBundle* b) const { |
| 25 ures_close(b); |
| 26 } |
| 27 }; |
| 28 |
| 29 static base::LazyInstance<base::Lock>::Leaky |
| 30 g_timezone_bundle_lock = LAZY_INSTANCE_INITIALIZER; |
| 31 |
| 32 string16 GetExemplarCity(const icu::TimeZone& zone) { |
| 33 // TODO(jungshik): After upgrading to ICU 4.6, use U_ICUDATA_ZONE |
| 34 static const char* zone_bundle_name = NULL; |
| 35 |
| 36 // These will be leaked at the end. |
| 37 static UResourceBundle *zone_bundle = NULL; |
| 38 static UResourceBundle *zone_strings = NULL; |
| 39 |
| 40 UErrorCode status = U_ZERO_ERROR; |
| 41 { |
| 42 base::AutoLock lock(g_timezone_bundle_lock.Get()); |
| 43 if (zone_bundle == NULL) |
| 44 zone_bundle = ures_open(zone_bundle_name, uloc_getDefault(), &status); |
| 45 |
| 46 if (zone_strings == NULL) |
| 47 zone_strings = ures_getByKey(zone_bundle, "zone_strings", NULL, &status); |
| 48 } |
| 49 |
| 50 icu::UnicodeString zone_id; |
| 51 zone.getID(zone_id); |
| 52 std::string zone_id_str; |
| 53 zone_id.toUTF8String(zone_id_str); |
| 54 |
| 55 // Resource keys for timezones use ':' in place of '/'. |
| 56 ReplaceSubstringsAfterOffset(&zone_id_str, 0, "/", ":"); |
| 57 scoped_ptr_malloc<UResourceBundle, UResClose> zone_item( |
| 58 ures_getByKey(zone_strings, zone_id_str.c_str(), NULL, &status)); |
| 59 icu::UnicodeString city; |
| 60 if (!U_FAILURE(status)) { |
| 61 city = icu::ures_getUnicodeStringByKey(zone_item.get(), "ec", &status); |
| 62 if (U_SUCCESS(status)) |
| 63 return string16(city.getBuffer(), city.length()); |
| 64 } |
| 65 |
| 66 // Fallback case in case of failure. |
| 67 ReplaceSubstringsAfterOffset(&zone_id_str, 0, ":", "/"); |
| 68 // Take the last component of a timezone id (e.g. 'Baz' in 'Foo/Bar/Baz'). |
| 69 // Depending on timezones, keeping all but the 1st component |
| 70 // (e.g. Bar/Baz) may be better, but our current list does not have |
| 71 // any timezone for which that's the case. |
| 72 std::string::size_type slash_pos = zone_id_str.rfind('/'); |
| 73 if (slash_pos != std::string::npos && slash_pos < zone_id_str.size()) |
| 74 zone_id_str.erase(0, slash_pos + 1); |
| 75 // zone id has '_' in place of ' '. |
| 76 ReplaceSubstringsAfterOffset(&zone_id_str, 0, "_", " "); |
| 77 return ASCIIToUTF16(zone_id_str); |
| 78 } |
| 79 |
| 80 // Gets the given timezone's name for visualization. |
| 81 string16 GetTimezoneName(const icu::TimeZone& timezone) { |
| 82 // Instead of using the raw_offset, use the offset in effect now. |
| 83 // For instance, US Pacific Time, the offset shown will be -7 in summer |
| 84 // while it'll be -8 in winter. |
| 85 int raw_offset, dst_offset; |
| 86 UDate now = icu::Calendar::getNow(); |
| 87 UErrorCode status = U_ZERO_ERROR; |
| 88 timezone.getOffset(now, false, raw_offset, dst_offset, status); |
| 89 DCHECK(U_SUCCESS(status)); |
| 90 int offset = raw_offset + dst_offset; |
| 91 // |offset| is in msec. |
| 92 int minute_offset = std::abs(offset) / 60000; |
| 93 int hour_offset = minute_offset / 60; |
| 94 int min_remainder = minute_offset % 60; |
| 95 // Some timezones have a non-integral hour offset. So, we need to use hh:mm |
| 96 // form. |
| 97 std::string offset_str = base::StringPrintf(offset >= 0 ? |
| 98 "UTC+%d:%02d" : "UTC-%d:%02d", hour_offset, min_remainder); |
| 99 |
| 100 // TODO(jungshik): When coming up with a better list of timezones, we also |
| 101 // have to come up with better 'display' names. One possibility is to list |
| 102 // multiple cities (e.g. "Los Angeles, Vancouver .." in the order of |
| 103 // the population of a country the city belongs to.). |
| 104 // We can also think of using LONG_GENERIC or LOCATION once we upgrade |
| 105 // to ICU 4.6. |
| 106 // In the meantime, we use "LONG" name with "Exemplar City" to distinguish |
| 107 // multiple timezones with the same "LONG" name but with different |
| 108 // rules (e.g. US Mountain Time in Denver vs Phoenix). |
| 109 icu::UnicodeString name; |
| 110 timezone.getDisplayName(dst_offset != 0, icu::TimeZone::LONG, name); |
| 111 string16 result(l10n_util::GetStringFUTF16( |
| 112 IDS_OPTIONS_SETTINGS_TIMEZONE_DISPLAY_TEMPLATE, ASCIIToUTF16(offset_str), |
| 113 string16(name.getBuffer(), name.length()), GetExemplarCity(timezone))); |
| 114 base::i18n::AdjustStringForLocaleDirection(&result); |
| 115 return result; |
| 116 } |
| 117 |
| 118 } // namespace |
| 119 |
| 120 namespace options2 { |
| 121 |
| 122 // Creates a list of pairs of each timezone's ID and name. |
| 123 base::ListValue* GetTimezoneList() { |
| 124 const std::vector<icu::TimeZone*> &timezones = |
| 125 chromeos::system::TimezoneSettings::GetInstance()->GetTimezoneList(); |
| 126 base::ListValue* timezoneList = new base::ListValue(); |
| 127 for (std::vector<icu::TimeZone*>::const_iterator iter = timezones.begin(); |
| 128 iter != timezones.end(); ++iter) { |
| 129 const icu::TimeZone* timezone = *iter; |
| 130 base::ListValue* option = new base::ListValue(); |
| 131 option->Append(Value::CreateStringValue( |
| 132 chromeos::system::TimezoneSettings::GetTimezoneID(*timezone))); |
| 133 option->Append(Value::CreateStringValue(GetTimezoneName(*timezone))); |
| 134 timezoneList->Append(option); |
| 135 } |
| 136 return timezoneList; |
| 137 } |
| 138 |
| 139 } // namespace options2 |
| OLD | NEW |