| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
| 12 #include "base/singleton.h" | 12 #include "base/singleton.h" |
| 13 #include "base/stl_util-inl.h" | 13 #include "base/stl_util-inl.h" |
| 14 #include "base/string16.h" |
| 14 #include "base/time.h" | 15 #include "base/time.h" |
| 15 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 16 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
| 17 #include "unicode/datefmt.h" | 18 #include "unicode/datefmt.h" |
| 18 #include "unicode/locid.h" | 19 #include "unicode/locid.h" |
| 19 #include "unicode/plurfmt.h" | 20 #include "unicode/plurfmt.h" |
| 20 #include "unicode/plurrule.h" | 21 #include "unicode/plurrule.h" |
| 21 #include "unicode/smpdtfmt.h" | 22 #include "unicode/smpdtfmt.h" |
| 22 | 23 |
| 23 using base::Time; | 24 using base::Time; |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 } | 248 } |
| 248 pattern += UNICODE_STRING_SIMPLE(" other{# ") + kUnits[index][1] + suffix; | 249 pattern += UNICODE_STRING_SIMPLE(" other{# ") + kUnits[index][1] + suffix; |
| 249 UErrorCode err = U_ZERO_ERROR; | 250 UErrorCode err = U_ZERO_ERROR; |
| 250 icu::PluralFormat* format = new icu::PluralFormat(rules, pattern, err); | 251 icu::PluralFormat* format = new icu::PluralFormat(rules, pattern, err); |
| 251 DCHECK(U_SUCCESS(err)); | 252 DCHECK(U_SUCCESS(err)); |
| 252 return format; | 253 return format; |
| 253 } | 254 } |
| 254 | 255 |
| 255 Singleton<TimeFormatter> time_formatter; | 256 Singleton<TimeFormatter> time_formatter; |
| 256 | 257 |
| 257 static std::wstring FormatTimeImpl(const TimeDelta& delta, | 258 static string16 FormatTimeImpl(const TimeDelta& delta, FormatType format_type) { |
| 258 FormatType format_type) { | |
| 259 if (delta.ToInternalValue() < 0) { | 259 if (delta.ToInternalValue() < 0) { |
| 260 NOTREACHED() << "Negative duration"; | 260 NOTREACHED() << "Negative duration"; |
| 261 return std::wstring(); | 261 return string16(); |
| 262 } | 262 } |
| 263 | 263 |
| 264 int number; | 264 int number; |
| 265 | 265 |
| 266 const std::vector<icu::PluralFormat*>& formatters = | 266 const std::vector<icu::PluralFormat*>& formatters = |
| 267 time_formatter->formatter(format_type); | 267 time_formatter->formatter(format_type); |
| 268 | 268 |
| 269 UErrorCode error = U_ZERO_ERROR; | 269 UErrorCode error = U_ZERO_ERROR; |
| 270 icu::UnicodeString time_string; | 270 icu::UnicodeString time_string; |
| 271 // Less than a minute gets "X seconds left" | 271 // Less than a minute gets "X seconds left" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 289 // Anything bigger gets "X days left" | 289 // Anything bigger gets "X days left" |
| 290 } else { | 290 } else { |
| 291 number = static_cast<int>(delta.ToInternalValue() / | 291 number = static_cast<int>(delta.ToInternalValue() / |
| 292 Time::kMicrosecondsPerDay); | 292 Time::kMicrosecondsPerDay); |
| 293 time_string = formatters[3]->format(number, error); | 293 time_string = formatters[3]->format(number, error); |
| 294 } | 294 } |
| 295 | 295 |
| 296 // With the fallback added, this should never fail. | 296 // With the fallback added, this should never fail. |
| 297 DCHECK(U_SUCCESS(error)); | 297 DCHECK(U_SUCCESS(error)); |
| 298 int capacity = time_string.length() + 1; | 298 int capacity = time_string.length() + 1; |
| 299 string16 result_utf16; | 299 string16 result; |
| 300 time_string.extract(static_cast<UChar*>( | 300 time_string.extract(static_cast<UChar*>( |
| 301 WriteInto(&result_utf16, capacity)), | 301 WriteInto(&result, capacity)), |
| 302 capacity, error); | 302 capacity, error); |
| 303 DCHECK(U_SUCCESS(error)); | 303 DCHECK(U_SUCCESS(error)); |
| 304 return UTF16ToWide(result_utf16); | 304 return result; |
| 305 } | 305 } |
| 306 | 306 |
| 307 // static | 307 // static |
| 308 std::wstring TimeFormat::TimeElapsed(const TimeDelta& delta) { | 308 string16 TimeFormat::TimeElapsed(const TimeDelta& delta) { |
| 309 return FormatTimeImpl(delta, FORMAT_ELAPSED); | 309 return FormatTimeImpl(delta, FORMAT_ELAPSED); |
| 310 } | 310 } |
| 311 | 311 |
| 312 // static | 312 // static |
| 313 std::wstring TimeFormat::TimeRemaining(const TimeDelta& delta) { | 313 string16 TimeFormat::TimeRemaining(const TimeDelta& delta) { |
| 314 return FormatTimeImpl(delta, FORMAT_REMAINING); | 314 return FormatTimeImpl(delta, FORMAT_REMAINING); |
| 315 } | 315 } |
| 316 | 316 |
| 317 // static | 317 // static |
| 318 std::wstring TimeFormat::TimeRemainingShort(const TimeDelta& delta) { | 318 string16 TimeFormat::TimeRemainingShort(const TimeDelta& delta) { |
| 319 return FormatTimeImpl(delta, FORMAT_SHORT); | 319 return FormatTimeImpl(delta, FORMAT_SHORT); |
| 320 } | 320 } |
| 321 | 321 |
| 322 // static | 322 // static |
| 323 std::wstring TimeFormat::RelativeDate( | 323 string16 TimeFormat::RelativeDate( |
| 324 const Time& time, | 324 const Time& time, |
| 325 const Time* optional_midnight_today) { | 325 const Time* optional_midnight_today) { |
| 326 Time midnight_today = optional_midnight_today ? *optional_midnight_today : | 326 Time midnight_today = optional_midnight_today ? *optional_midnight_today : |
| 327 Time::Now().LocalMidnight(); | 327 Time::Now().LocalMidnight(); |
| 328 | 328 |
| 329 // Filter out "today" and "yesterday" | 329 // Filter out "today" and "yesterday" |
| 330 if (time >= midnight_today) | 330 if (time >= midnight_today) |
| 331 return l10n_util::GetString(IDS_PAST_TIME_TODAY); | 331 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); |
| 332 else if (time >= midnight_today - | 332 else if (time >= midnight_today - |
| 333 TimeDelta::FromMicroseconds(Time::kMicrosecondsPerDay)) | 333 TimeDelta::FromMicroseconds(Time::kMicrosecondsPerDay)) |
| 334 return l10n_util::GetString(IDS_PAST_TIME_YESTERDAY); | 334 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); |
| 335 | 335 |
| 336 return std::wstring(); | 336 return string16(); |
| 337 } | 337 } |
| OLD | NEW |