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

Side by Side 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: Created 6 years, 10 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 unified diff | Download patch
OLDNEW
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 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 return base::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 const int64 d2i = delta.ToInternalValue();
bartfab (slow) 2014/01/28 15:16:46 Nit: Wherever possible, we avoid abbreviations. |d
298
297 // Less than a minute gets "X seconds left" 299 // Less than a minute gets "X seconds left"
298 if (delta.ToInternalValue() < Time::kMicrosecondsPerMinute) { 300 if (d2i < Time::kMicrosecondsPerMinute-Time::kMicrosecondsPerSecond/2) {
bartfab (slow) 2014/01/28 15:16:46 1) Here and elsewhere in the file: Add spaces arou
299 number = static_cast<int>(delta.ToInternalValue() / 301 number = static_cast<int>((d2i+Time::kMicrosecondsPerSecond/2) /
300 Time::kMicrosecondsPerSecond); 302 Time::kMicrosecondsPerSecond);
301 time_string = formatters[0]->format(number, error); 303 time_string = formatters[0]->format(number, error);
302 304
303 // Less than 1 hour gets "X minutes left". 305 // Less than 1 hour gets "X minutes left".
304 } else if (delta.ToInternalValue() < Time::kMicrosecondsPerHour) { 306 } else if (d2i < Time::kMicrosecondsPerHour-Time::kMicrosecondsPerMinute/2) {
305 number = static_cast<int>(delta.ToInternalValue() / 307 number = static_cast<int>((d2i+Time::kMicrosecondsPerMinute/2) /
306 Time::kMicrosecondsPerMinute); 308 Time::kMicrosecondsPerMinute);
307 time_string = formatters[1]->format(number, error); 309 time_string = formatters[1]->format(number, error);
308 310
309 // Less than 1 day remaining gets "X hours left" 311 // Less than 1 day remaining gets "X hours left"
310 } else if (delta.ToInternalValue() < Time::kMicrosecondsPerDay) { 312 } else if (d2i < Time::kMicrosecondsPerDay-Time::kMicrosecondsPerHour/2) {
311 number = static_cast<int>(delta.ToInternalValue() / 313 number = static_cast<int>((d2i+Time::kMicrosecondsPerHour/2) /
312 Time::kMicrosecondsPerHour); 314 Time::kMicrosecondsPerHour);
313 time_string = formatters[2]->format(number, error); 315 time_string = formatters[2]->format(number, error);
314 316
315 // Anything bigger gets "X days left" 317 // Anything bigger gets "X days left"
316 } else { 318 } else {
317 number = static_cast<int>(delta.ToInternalValue() / 319 number = static_cast<int>((d2i+Time::kMicrosecondsPerDay/2) /
318 Time::kMicrosecondsPerDay); 320 Time::kMicrosecondsPerDay);
319 time_string = formatters[3]->format(number, error); 321 time_string = formatters[3]->format(number, error);
320 } 322 }
321 323
322 // With the fallback added, this should never fail. 324 // With the fallback added, this should never fail.
323 DCHECK(U_SUCCESS(error)); 325 DCHECK(U_SUCCESS(error));
324 int capacity = time_string.length() + 1; 326 int capacity = time_string.length() + 1;
325 DCHECK_GT(capacity, 1); 327 DCHECK_GT(capacity, 1);
326 base::string16 result; 328 base::string16 result;
327 time_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)), 329 time_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)),
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 if (time >= tomorrow) 373 if (time >= tomorrow)
372 return base::string16(); 374 return base::string16();
373 else if (time >= midnight_today) 375 else if (time >= midnight_today)
374 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); 376 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY);
375 else if (time >= yesterday) 377 else if (time >= yesterday)
376 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); 378 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY);
377 return base::string16(); 379 return base::string16();
378 } 380 }
379 381
380 } // namespace ui 382 } // namespace ui
OLDNEW
« 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