| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/base/l10n/time_format.h" | 5 #include "ui/base/l10n/time_format.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 if (rules.isKeyword(UNICODE_STRING_SIMPLE("one"))) { | 274 if (rules.isKeyword(UNICODE_STRING_SIMPLE("one"))) { |
| 275 pattern += UNICODE_STRING_SIMPLE("one{# ") + kUnits[index][0] + suffix; | 275 pattern += UNICODE_STRING_SIMPLE("one{# ") + kUnits[index][0] + suffix; |
| 276 } | 276 } |
| 277 pattern += UNICODE_STRING_SIMPLE(" other{# ") + kUnits[index][1] + suffix; | 277 pattern += UNICODE_STRING_SIMPLE(" other{# ") + kUnits[index][1] + suffix; |
| 278 UErrorCode err = U_ZERO_ERROR; | 278 UErrorCode err = U_ZERO_ERROR; |
| 279 icu::PluralFormat* format = new icu::PluralFormat(rules, pattern, err); | 279 icu::PluralFormat* format = new icu::PluralFormat(rules, pattern, err); |
| 280 DCHECK(U_SUCCESS(err)); | 280 DCHECK(U_SUCCESS(err)); |
| 281 return format; | 281 return format; |
| 282 } | 282 } |
| 283 | 283 |
| 284 string16 FormatTimeImpl(const TimeDelta& delta, FormatType format_type) { | 284 base::string16 FormatTimeImpl(const TimeDelta& delta, FormatType format_type) { |
| 285 if (delta.ToInternalValue() < 0) { | 285 if (delta.ToInternalValue() < 0) { |
| 286 NOTREACHED() << "Negative duration"; | 286 NOTREACHED() << "Negative duration"; |
| 287 return string16(); | 287 return base::string16(); |
| 288 } | 288 } |
| 289 | 289 |
| 290 int number; | 290 int number; |
| 291 | 291 |
| 292 const std::vector<icu::PluralFormat*>& formatters = | 292 const std::vector<icu::PluralFormat*>& formatters = |
| 293 g_time_formatter.Get().formatter(format_type); | 293 g_time_formatter.Get().formatter(format_type); |
| 294 | 294 |
| 295 UErrorCode error = U_ZERO_ERROR; | 295 UErrorCode error = U_ZERO_ERROR; |
| 296 icu::UnicodeString time_string; | 296 icu::UnicodeString time_string; |
| 297 // Less than a minute gets "X seconds left" | 297 // Less than a minute gets "X seconds left" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 316 } else { | 316 } else { |
| 317 number = static_cast<int>(delta.ToInternalValue() / | 317 number = static_cast<int>(delta.ToInternalValue() / |
| 318 Time::kMicrosecondsPerDay); | 318 Time::kMicrosecondsPerDay); |
| 319 time_string = formatters[3]->format(number, error); | 319 time_string = formatters[3]->format(number, error); |
| 320 } | 320 } |
| 321 | 321 |
| 322 // With the fallback added, this should never fail. | 322 // With the fallback added, this should never fail. |
| 323 DCHECK(U_SUCCESS(error)); | 323 DCHECK(U_SUCCESS(error)); |
| 324 int capacity = time_string.length() + 1; | 324 int capacity = time_string.length() + 1; |
| 325 DCHECK_GT(capacity, 1); | 325 DCHECK_GT(capacity, 1); |
| 326 string16 result; | 326 base::string16 result; |
| 327 time_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)), | 327 time_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)), |
| 328 capacity, error); | 328 capacity, error); |
| 329 DCHECK(U_SUCCESS(error)); | 329 DCHECK(U_SUCCESS(error)); |
| 330 return result; | 330 return result; |
| 331 } | 331 } |
| 332 | 332 |
| 333 } // namespace | 333 } // namespace |
| 334 | 334 |
| 335 namespace ui { | 335 namespace ui { |
| 336 | 336 |
| 337 // static | 337 // static |
| 338 string16 TimeFormat::TimeElapsed(const TimeDelta& delta) { | 338 base::string16 TimeFormat::TimeElapsed(const TimeDelta& delta) { |
| 339 return FormatTimeImpl(delta, FORMAT_ELAPSED); | 339 return FormatTimeImpl(delta, FORMAT_ELAPSED); |
| 340 } | 340 } |
| 341 | 341 |
| 342 // static | 342 // static |
| 343 string16 TimeFormat::TimeRemaining(const TimeDelta& delta) { | 343 base::string16 TimeFormat::TimeRemaining(const TimeDelta& delta) { |
| 344 return FormatTimeImpl(delta, FORMAT_REMAINING); | 344 return FormatTimeImpl(delta, FORMAT_REMAINING); |
| 345 } | 345 } |
| 346 | 346 |
| 347 // static | 347 // static |
| 348 string16 TimeFormat::TimeRemainingLong(const TimeDelta& delta) { | 348 base::string16 TimeFormat::TimeRemainingLong(const TimeDelta& delta) { |
| 349 return FormatTimeImpl(delta, FORMAT_REMAINING_LONG); | 349 return FormatTimeImpl(delta, FORMAT_REMAINING_LONG); |
| 350 } | 350 } |
| 351 | 351 |
| 352 // static | 352 // static |
| 353 string16 TimeFormat::TimeRemainingShort(const TimeDelta& delta) { | 353 base::string16 TimeFormat::TimeRemainingShort(const TimeDelta& delta) { |
| 354 return FormatTimeImpl(delta, FORMAT_SHORT); | 354 return FormatTimeImpl(delta, FORMAT_SHORT); |
| 355 } | 355 } |
| 356 | 356 |
| 357 // static | 357 // static |
| 358 string16 TimeFormat::TimeDurationLong(const TimeDelta& delta) { | 358 base::string16 TimeFormat::TimeDurationLong(const TimeDelta& delta) { |
| 359 return FormatTimeImpl(delta, FORMAT_DURATION_LONG); | 359 return FormatTimeImpl(delta, FORMAT_DURATION_LONG); |
| 360 } | 360 } |
| 361 | 361 |
| 362 // static | 362 // static |
| 363 string16 TimeFormat::RelativeDate( | 363 base::string16 TimeFormat::RelativeDate( |
| 364 const Time& time, | 364 const Time& time, |
| 365 const Time* optional_midnight_today) { | 365 const Time* optional_midnight_today) { |
| 366 Time midnight_today = optional_midnight_today ? *optional_midnight_today : | 366 Time midnight_today = optional_midnight_today ? *optional_midnight_today : |
| 367 Time::Now().LocalMidnight(); | 367 Time::Now().LocalMidnight(); |
| 368 TimeDelta day = TimeDelta::FromMicroseconds(Time::kMicrosecondsPerDay); | 368 TimeDelta day = TimeDelta::FromMicroseconds(Time::kMicrosecondsPerDay); |
| 369 Time tomorrow = midnight_today + day; | 369 Time tomorrow = midnight_today + day; |
| 370 Time yesterday = midnight_today - day; | 370 Time yesterday = midnight_today - day; |
| 371 if (time >= tomorrow) | 371 if (time >= tomorrow) |
| 372 return string16(); | 372 return base::string16(); |
| 373 else if (time >= midnight_today) | 373 else if (time >= midnight_today) |
| 374 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); | 374 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); |
| 375 else if (time >= yesterday) | 375 else if (time >= yesterday) |
| 376 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); | 376 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); |
| 377 return string16(); | 377 return base::string16(); |
| 378 } | 378 } |
| 379 | 379 |
| 380 } // namespace ui | 380 } // namespace ui |
| OLD | NEW |