 Chromium Code Reviews
 Chromium Code Reviews Issue 143633003:
  Fix rounding of time interval strings  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 143633003:
  Fix rounding of time interval strings  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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)) { | 
| 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 one_minute(TimeDelta::FromMinutes(1)); | 
| 304 } else if (delta.ToInternalValue() < Time::kMicrosecondsPerHour) { | 297 const TimeDelta one_hour(TimeDelta::FromHours(1)); | 
| 305 number = static_cast<int>(delta.ToInternalValue() / | 298 const TimeDelta one_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); | |
| 314 | 304 | 
| 315 // Anything bigger gets "X days left" | 305 // Less than 59.5 seconds gets "X seconds left", anything larger is | 
| 306 // rounded to minutes. | |
| 307 if (delta < one_minute - half_second) { | |
| 308 const int seconds = static_cast<int>((delta + half_second).InSeconds()); | |
| 
bartfab (slow)
2014/01/31 13:17:09
Nit: Is the cast needed? Will the compiler not imp
 | |
| 309 time_string = formatters[0]->format(seconds, error); | |
| 310 | |
| 311 // Less than 59.5 minutes gets "X minutes left", anything larger is | |
| 312 // rounded to hours. | |
| 313 } else if (delta < one_hour - half_minute) { | |
| 314 const int minutes = (delta + half_minute).InMinutes(); | |
| 315 time_string = formatters[1]->format(minutes, error); | |
| 316 | |
| 317 // Less than 23.5 hours gets "X hours left", anything larger is | |
| 318 // rounded to days. | |
| 319 } else if (delta < one_day - half_hour) { | |
| 320 const int hours = (delta + half_hour).InHours(); | |
| 321 time_string = formatters[2]->format(hours, error); | |
| 322 | |
| 323 // Anything bigger gets "X days left". | |
| 316 } else { | 324 } else { | 
| 317 number = static_cast<int>(delta.ToInternalValue() / | 325 const int days = (delta + half_day).InDays(); | 
| 318 Time::kMicrosecondsPerDay); | 326 time_string = formatters[3]->format(days, error); | 
| 319 time_string = formatters[3]->format(number, error); | |
| 320 } | 327 } | 
| 321 | 328 | 
| 322 // With the fallback added, this should never fail. | 329 // With the fallback added, this should never fail. | 
| 323 DCHECK(U_SUCCESS(error)); | 330 DCHECK(U_SUCCESS(error)); | 
| 324 int capacity = time_string.length() + 1; | 331 int capacity = time_string.length() + 1; | 
| 325 DCHECK_GT(capacity, 1); | 332 DCHECK_GT(capacity, 1); | 
| 326 base::string16 result; | 333 base::string16 result; | 
| 327 time_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)), | 334 time_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)), | 
| 328 capacity, error); | 335 capacity, error); | 
| 329 DCHECK(U_SUCCESS(error)); | 336 DCHECK(U_SUCCESS(error)); | 
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 371 if (time >= tomorrow) | 378 if (time >= tomorrow) | 
| 372 return base::string16(); | 379 return base::string16(); | 
| 373 else if (time >= midnight_today) | 380 else if (time >= midnight_today) | 
| 374 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); | 381 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); | 
| 375 else if (time >= yesterday) | 382 else if (time >= yesterday) | 
| 376 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); | 383 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); | 
| 377 return base::string16(); | 384 return base::string16(); | 
| 378 } | 385 } | 
| 379 | 386 | 
| 380 } // namespace ui | 387 } // namespace ui | 
| OLD | NEW |