OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/time_format.h" | 5 #include "chrome/common/time_format.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 TimeRemainingFormat() { | 33 TimeRemainingFormat() { |
34 BuildFormats(true, &short_formatter_); | 34 BuildFormats(true, &short_formatter_); |
35 BuildFormats(false, &long_formatter_); | 35 BuildFormats(false, &long_formatter_); |
36 } | 36 } |
37 ~TimeRemainingFormat() { | 37 ~TimeRemainingFormat() { |
38 STLDeleteContainerPointers(short_formatter_.begin(), | 38 STLDeleteContainerPointers(short_formatter_.begin(), |
39 short_formatter_.end()); | 39 short_formatter_.end()); |
40 STLDeleteContainerPointers(long_formatter_.begin(), | 40 STLDeleteContainerPointers(long_formatter_.begin(), |
41 long_formatter_.end()); | 41 long_formatter_.end()); |
42 } | 42 } |
43 friend Singleton<TimeRemainingFormat>; | 43 friend class Singleton<TimeRemainingFormat>; |
44 friend DefaultSingletonTraits<TimeRemainingFormat>; | 44 friend struct DefaultSingletonTraits<TimeRemainingFormat>; |
45 | 45 |
46 std::vector<PluralFormat*> long_formatter_; | 46 std::vector<PluralFormat*> long_formatter_; |
47 std::vector<PluralFormat*> short_formatter_; | 47 std::vector<PluralFormat*> short_formatter_; |
48 static void BuildFormats(bool short_version, | 48 static void BuildFormats(bool short_version, |
49 std::vector<PluralFormat*>* time_formats); | 49 std::vector<PluralFormat*>* time_formats); |
50 static PluralFormat* createFallbackFormat(const PluralRules& rules, | 50 static PluralFormat* createFallbackFormat(const PluralRules& rules, |
51 int index, | 51 int index, |
52 bool short_version); | 52 bool short_version); |
53 | 53 |
54 DISALLOW_EVIL_CONSTRUCTORS(TimeRemainingFormat); | 54 DISALLOW_EVIL_CONSTRUCTORS(TimeRemainingFormat); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 scoped_ptr<PluralRules> rules( | 109 scoped_ptr<PluralRules> rules( |
110 PluralRules::forLocale(Locale::getDefault(), err)); | 110 PluralRules::forLocale(Locale::getDefault(), err)); |
111 if (U_FAILURE(err)) { | 111 if (U_FAILURE(err)) { |
112 err = U_ZERO_ERROR; | 112 err = U_ZERO_ERROR; |
113 UnicodeString fallback_rules("one: n is 1", -1, US_INV); | 113 UnicodeString fallback_rules("one: n is 1", -1, US_INV); |
114 rules.reset(PluralRules::createRules(fallback_rules, err)); | 114 rules.reset(PluralRules::createRules(fallback_rules, err)); |
115 DCHECK(U_SUCCESS(err)); | 115 DCHECK(U_SUCCESS(err)); |
116 } | 116 } |
117 for (int i = 0; i < 4; ++i) { | 117 for (int i = 0; i < 4; ++i) { |
118 UnicodeString pattern; | 118 UnicodeString pattern; |
119 for (int j = 0; j < arraysize(kKeywords); ++j) { | 119 for (size_t j = 0; j < arraysize(kKeywords); ++j) { |
120 int msg_id = short_version ? kTimeMsgIds[i][j] : kTimeLeftMsgIds[i][j]; | 120 int msg_id = short_version ? kTimeMsgIds[i][j] : kTimeLeftMsgIds[i][j]; |
121 if (msg_id == kInvalidMsgId) continue; | 121 if (msg_id == kInvalidMsgId) continue; |
122 std::string sub_pattern = WideToUTF8(l10n_util::GetString(msg_id)); | 122 std::string sub_pattern = WideToUTF8(l10n_util::GetString(msg_id)); |
123 // NA means this keyword is not used in the current locale. | 123 // NA means this keyword is not used in the current locale. |
124 // Even if a translator translated for this keyword, we do not | 124 // Even if a translator translated for this keyword, we do not |
125 // use it unless it's 'other' (j=0) or it's defined in the rules | 125 // use it unless it's 'other' (j=0) or it's defined in the rules |
126 // for the current locale. Special-casing of 'other' will be removed | 126 // for the current locale. Special-casing of 'other' will be removed |
127 // once ICU's isKeyword is fixed to return true for isKeyword('other'). | 127 // once ICU's isKeyword is fixed to return true for isKeyword('other'). |
128 if (sub_pattern.compare("NA") != 0 && | 128 if (sub_pattern.compare("NA") != 0 && |
129 (j == 0 || rules->isKeyword(kKeywords[j]))) { | 129 (j == 0 || rules->isKeyword(kKeywords[j]))) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 // Anything bigger gets "X days left" | 206 // Anything bigger gets "X days left" |
207 } else { | 207 } else { |
208 number = static_cast<int>(delta.ToInternalValue() / | 208 number = static_cast<int>(delta.ToInternalValue() / |
209 Time::kMicrosecondsPerDay); | 209 Time::kMicrosecondsPerDay); |
210 time_string = formatters[3]->format(number, error); | 210 time_string = formatters[3]->format(number, error); |
211 } | 211 } |
212 | 212 |
213 // With the fallback added, this should never fail. | 213 // With the fallback added, this should never fail. |
214 DCHECK(U_SUCCESS(error)); | 214 DCHECK(U_SUCCESS(error)); |
215 int capacity = time_string.length() + 1; | 215 int capacity = time_string.length() + 1; |
216 std::wstring formatted; | 216 string16 result_utf16; |
217 time_string.extract(static_cast<UChar*>(WriteInto(&formatted, capacity)), | 217 time_string.extract(static_cast<UChar*>( |
| 218 WriteInto(&result_utf16, capacity)), |
218 capacity, error); | 219 capacity, error); |
219 DCHECK(U_SUCCESS(error)); | 220 DCHECK(U_SUCCESS(error)); |
220 return formatted; | 221 return UTF16ToWide(result_utf16); |
221 } | 222 } |
222 | 223 |
223 // static | 224 // static |
224 std::wstring TimeFormat::TimeRemaining(const TimeDelta& delta) { | 225 std::wstring TimeFormat::TimeRemaining(const TimeDelta& delta) { |
225 return TimeRemainingImpl(delta, false); | 226 return TimeRemainingImpl(delta, false); |
226 } | 227 } |
227 | 228 |
228 // static | 229 // static |
229 std::wstring TimeFormat::TimeRemainingShort(const TimeDelta& delta) { | 230 std::wstring TimeFormat::TimeRemainingShort(const TimeDelta& delta) { |
230 return TimeRemainingImpl(delta, true); | 231 return TimeRemainingImpl(delta, true); |
231 } | 232 } |
232 | 233 |
233 // static | 234 // static |
234 std::wstring TimeFormat::RelativeDate( | 235 std::wstring TimeFormat::RelativeDate( |
235 const Time& time, | 236 const Time& time, |
236 const Time* optional_midnight_today) { | 237 const Time* optional_midnight_today) { |
237 Time midnight_today = optional_midnight_today ? *optional_midnight_today : | 238 Time midnight_today = optional_midnight_today ? *optional_midnight_today : |
238 Time::Now().LocalMidnight(); | 239 Time::Now().LocalMidnight(); |
239 | 240 |
240 // Filter out "today" and "yesterday" | 241 // Filter out "today" and "yesterday" |
241 if (time >= midnight_today) | 242 if (time >= midnight_today) |
242 return l10n_util::GetString(IDS_PAST_TIME_TODAY); | 243 return l10n_util::GetString(IDS_PAST_TIME_TODAY); |
243 else if (time >= midnight_today - | 244 else if (time >= midnight_today - |
244 TimeDelta::FromMicroseconds(Time::kMicrosecondsPerDay)) | 245 TimeDelta::FromMicroseconds(Time::kMicrosecondsPerDay)) |
245 return l10n_util::GetString(IDS_PAST_TIME_YESTERDAY); | 246 return l10n_util::GetString(IDS_PAST_TIME_YESTERDAY); |
246 | 247 |
247 return std::wstring(); | 248 return std::wstring(); |
248 } | 249 } |
249 | 250 |
OLD | NEW |