Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(554)

Unified Diff: ui/base/l10n/time_format.cc

Issue 143633003: Fix rounding of time interval strings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Re-formatted code according to Bartosz' suggestions Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | ui/base/l10n/time_format_unittest.cc » ('j') | ui/base/l10n/time_format_unittest.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/base/l10n/time_format.cc
diff --git a/ui/base/l10n/time_format.cc b/ui/base/l10n/time_format.cc
index f3b16a3f969ac1115b642550d5146660ce1b7ea3..c7dc5865ceec0876a4df1cf21b201ce7bb9e728f 100644
--- a/ui/base/l10n/time_format.cc
+++ b/ui/base/l10n/time_format.cc
@@ -282,41 +282,48 @@ icu::PluralFormat* TimeFormatter::createFallbackFormat(
}
base::string16 FormatTimeImpl(const TimeDelta& delta, FormatType format_type) {
- if (delta.ToInternalValue() < 0) {
+ if (delta < TimeDelta::FromSeconds(0)) {
NOTREACHED() << "Negative duration";
return base::string16();
}
- int number;
-
const std::vector<icu::PluralFormat*>& formatters =
g_time_formatter.Get().formatter(format_type);
UErrorCode error = U_ZERO_ERROR;
icu::UnicodeString time_string;
- // Less than a minute gets "X seconds left"
- if (delta.ToInternalValue() < Time::kMicrosecondsPerMinute) {
- number = static_cast<int>(delta.ToInternalValue() /
- Time::kMicrosecondsPerSecond);
- time_string = formatters[0]->format(number, error);
-
- // Less than 1 hour gets "X minutes left".
- } else if (delta.ToInternalValue() < Time::kMicrosecondsPerHour) {
- number = static_cast<int>(delta.ToInternalValue() /
- Time::kMicrosecondsPerMinute);
- time_string = formatters[1]->format(number, error);
-
- // Less than 1 day remaining gets "X hours left"
- } else if (delta.ToInternalValue() < Time::kMicrosecondsPerDay) {
- number = static_cast<int>(delta.ToInternalValue() /
- Time::kMicrosecondsPerHour);
- time_string = formatters[2]->format(number, error);
-
- // Anything bigger gets "X days left"
+
+ const TimeDelta one_minute(TimeDelta::FromMinutes(1));
+ const TimeDelta one_hour(TimeDelta::FromHours(1));
+ const TimeDelta one_day(TimeDelta::FromDays(1));
+
+ const TimeDelta half_second(TimeDelta::FromMilliseconds(500));
+ const TimeDelta half_minute(TimeDelta::FromSeconds(30));
+ const TimeDelta half_hour(TimeDelta::FromMinutes(30));
+ const TimeDelta half_day(TimeDelta::FromHours(12));
+
+ // Less than 59.5 seconds gets "X seconds left", anything larger is
+ // rounded to minutes.
+ if (delta < one_minute - half_second) {
+ 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
+ time_string = formatters[0]->format(seconds, error);
+
+ // Less than 59.5 minutes gets "X minutes left", anything larger is
+ // rounded to hours.
+ } else if (delta < one_hour - half_minute) {
+ const int minutes = (delta + half_minute).InMinutes();
+ time_string = formatters[1]->format(minutes, error);
+
+ // Less than 23.5 hours gets "X hours left", anything larger is
+ // rounded to days.
+ } else if (delta < one_day - half_hour) {
+ const int hours = (delta + half_hour).InHours();
+ time_string = formatters[2]->format(hours, error);
+
+ // Anything bigger gets "X days left".
} else {
- number = static_cast<int>(delta.ToInternalValue() /
- Time::kMicrosecondsPerDay);
- time_string = formatters[3]->format(number, error);
+ const int days = (delta + half_day).InDays();
+ time_string = formatters[3]->format(days, error);
}
// With the fallback added, this should never fail.
« no previous file with comments | « no previous file | ui/base/l10n/time_format_unittest.cc » ('j') | ui/base/l10n/time_format_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698