Chromium Code Reviews| 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 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 base::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 < TimeDelta::FromSeconds(0)) { |
|
bartfab (slow)
2014/01/29 15:40:12
Nit: The TimeDelta() constructor actually produces
Thiemo Nagel
2014/01/29 17:40:12
I think "FromSeconds(0)" is clearer than both "Tim
| |
| 286 NOTREACHED() << "Negative duration"; | 286 NOTREACHED() << "Negative duration"; |
| 287 return base::string16(); | 287 return base::string16(); |
| 288 } | 288 } |
| 289 | 289 |
| 290 int number; | |
| 291 | |
| 292 const std::vector<icu::PluralFormat*>& formatters = | 290 const std::vector<icu::PluralFormat*>& formatters = |
| 293 g_time_formatter.Get().formatter(format_type); | 291 g_time_formatter.Get().formatter(format_type); |
| 294 | 292 |
| 295 UErrorCode error = U_ZERO_ERROR; | 293 UErrorCode error = U_ZERO_ERROR; |
| 296 icu::UnicodeString time_string; | 294 icu::UnicodeString time_string; |
| 297 // Less than a minute gets "X seconds left" | |
| 298 if (delta.ToInternalValue() < Time::kMicrosecondsPerMinute) { | |
| 299 number = static_cast<int>(delta.ToInternalValue() / | |
| 300 Time::kMicrosecondsPerSecond); | |
| 301 time_string = formatters[0]->format(number, error); | |
| 302 | 295 |
| 303 // Less than 1 hour gets "X minutes left". | 296 const TimeDelta minute (TimeDelta::FromMinutes(1)); |
|
bartfab (slow)
2014/01/29 15:40:12
Chrome code does not use this kind of formatting t
Thiemo Nagel
2014/01/29 17:40:12
Done.
| |
| 304 } else if (delta.ToInternalValue() < Time::kMicrosecondsPerHour) { | 297 const TimeDelta hour (TimeDelta::FromHours(1)); |
| 305 number = static_cast<int>(delta.ToInternalValue() / | 298 const TimeDelta day (TimeDelta::FromDays(1)); |
| 306 Time::kMicrosecondsPerMinute); | |
| 307 time_string = formatters[1]->format(number, error); | |
| 308 | 299 |
| 309 // Less than 1 day remaining gets "X hours left" | 300 const TimeDelta half_second(TimeDelta::FromMilliseconds(500)); |
| 310 } else if (delta.ToInternalValue() < Time::kMicrosecondsPerDay) { | 301 const TimeDelta half_minute(TimeDelta::FromSeconds(30)); |
| 311 number = static_cast<int>(delta.ToInternalValue() / | 302 const TimeDelta half_hour (TimeDelta::FromMinutes(30)); |
| 312 Time::kMicrosecondsPerHour); | 303 const TimeDelta half_day (TimeDelta::FromHours(12)); |
| 313 time_string = formatters[2]->format(number, error); | 304 |
| 305 // Less than 59.5 seconds gets "X seconds left" | |
|
bartfab (slow)
2014/01/29 15:40:12
Nit: Could you add a comment explaining that the .
Thiemo Nagel
2014/01/29 17:40:12
Done.
| |
| 306 if (delta < minute - half_second) { | |
| 307 const int64 seconds64 = (delta+half_second).InSeconds(); | |
|
bartfab (slow)
2014/01/29 15:40:12
Here and further down: You are missing spaces arou
Thiemo Nagel
2014/01/29 17:40:12
Done.
| |
| 308 const int seconds = static_cast<int>(seconds64); | |
|
bartfab (slow)
2014/01/29 15:40:12
Nit: Why the intermediary int64 variable?
Thiemo Nagel
2014/01/29 17:40:12
Done.
| |
| 309 time_string = formatters[0]->format(seconds, error); | |
| 310 | |
| 311 // Less than 59.5 minutes gets "X minutes left". | |
| 312 } else if (delta < hour - half_minute) { | |
| 313 const int minutes = (delta+half_minute).InMinutes(); | |
| 314 time_string = formatters[1]->format(minutes, error); | |
| 315 | |
| 316 // Less than 23.5 hours gets "X hours left" | |
| 317 } else if (delta < day - half_hour) { | |
| 318 const int hours = (delta+half_hour).InHours(); | |
| 319 time_string = formatters[2]->format(hours, error); | |
| 314 | 320 |
| 315 // Anything bigger gets "X days left" | 321 // Anything bigger gets "X days left" |
| 316 } else { | 322 } else { |
| 317 number = static_cast<int>(delta.ToInternalValue() / | 323 const int days = (delta+half_day).InDays(); |
| 318 Time::kMicrosecondsPerDay); | 324 time_string = formatters[3]->format(days, error); |
| 319 time_string = formatters[3]->format(number, error); | |
| 320 } | 325 } |
| 321 | 326 |
| 322 // With the fallback added, this should never fail. | 327 // With the fallback added, this should never fail. |
| 323 DCHECK(U_SUCCESS(error)); | 328 DCHECK(U_SUCCESS(error)); |
| 324 int capacity = time_string.length() + 1; | 329 int capacity = time_string.length() + 1; |
| 325 DCHECK_GT(capacity, 1); | 330 DCHECK_GT(capacity, 1); |
| 326 base::string16 result; | 331 base::string16 result; |
| 327 time_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)), | 332 time_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)), |
| 328 capacity, error); | 333 capacity, error); |
| 329 DCHECK(U_SUCCESS(error)); | 334 DCHECK(U_SUCCESS(error)); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 371 if (time >= tomorrow) | 376 if (time >= tomorrow) |
| 372 return base::string16(); | 377 return base::string16(); |
| 373 else if (time >= midnight_today) | 378 else if (time >= midnight_today) |
| 374 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); | 379 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); |
| 375 else if (time >= yesterday) | 380 else if (time >= yesterday) |
| 376 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); | 381 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); |
| 377 return base::string16(); | 382 return base::string16(); |
| 378 } | 383 } |
| 379 | 384 |
| 380 } // namespace ui | 385 } // namespace ui |
| OLD | NEW |