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

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

Issue 147443007: Add support for localized time strings with two units, eg. "2 hours 17 minutes" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Bartosz' comments 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 side-by-side diff with in-line comments
Download patch
Index: ui/base/l10n/time_format.h
diff --git a/ui/base/l10n/time_format.h b/ui/base/l10n/time_format.h
index 1306bf00051e623e920cbbb0ad0ed0cb57130287..8201fb7616a4c8df73399a590c38e4a8236ff77b 100644
--- a/ui/base/l10n/time_format.h
+++ b/ui/base/l10n/time_format.h
@@ -19,23 +19,57 @@ namespace ui {
// Methods to format time values as strings.
class UI_BASE_EXPORT TimeFormat {
public:
- // TimeElapsed, TimeRemaining* and TimeDuration*:
- // These functions return a localized string of approximate time duration.
+ enum Format {
+ FORMAT_DURATION, // Plain duration, e.g. in English: "2 minutes".
+ FORMAT_REMAINING, // Remaining time, e.g. in English: "2 minutes left".
+ FORMAT_ELAPSED, // Elapsed time, e.g. in English: "2 minutes ago".
+ FORMAT_COUNT // Enum size counter, not a format. Must be last.
+ };
- // Returns times in elapsed-format: "3 mins ago", "2 days ago".
- static base::string16 TimeElapsed(const base::TimeDelta& delta);
+ enum Length {
+ LENGTH_SHORT, // Short format, e.g. in English: sec/min/hour/day.
+ LENGTH_LONG, // Long format, e.g. in English: second/minute/hour/day.
+ LENGTH_COUNT // Enum size counter, not a length. Must be last.
+ };
- // Returns times in remaining-format: "3 mins left", "2 days left".
- static base::string16 TimeRemaining(const base::TimeDelta& delta);
+ // Return a localized string of approximate time duration, formatted as a
+ // single number, e.g. in English "2 hours ago". Currently, all combinations
+ // of format and length except (FORMAT_ELAPSED, LENGTH_LONG) are implemented
+ // but it's easy to add this, if required.
+ static base::string16 Simple(Format format,
+ Length length,
+ const base::TimeDelta& delta);
- // Returns times in remaining-long-format: "3 minutes left", "2 days left".
- static base::string16 TimeRemainingLong(const base::TimeDelta& delta);
-
- // Returns times in short-format: "3 mins", "2 days".
- static base::string16 TimeDurationShort(const base::TimeDelta& delta);
-
- // Return times in long-format: "2 hours", "25 minutes".
- static base::string16 TimeDurationLong(const base::TimeDelta& delta);
+ // Return a localized string of more precise time duration, either formatted
+ // as a single number or as two numbers: for a time delta of e.g. 2h19m either
+ // "2 hours" or "2 hours and 19 minutes" could be returned in English.
+ // Two-number output can be forced by setting |cutoff| to -1. Single-number
+ // output can be forced by using Simple() or setting |cutoff| to 0.
+ // Otherwise, choice of format happens automatically and the value of |cutoff|
+ // determines the largest numeric value that may appear in a single-valued
+ // format -- for lower numeric values, a second unit is added to increase
+ // precision. (Applied to the examples above, a |cutoff| of 2 or smaller
+ // would yield the first string and a |cutoff| of 3 or larger would return the
+ // second string.)
+ //
+ // The aim of this logic is to reduce rounding errors (that can amount up to
+ // 50% in single-valued representation) while at the same time avoiding
+ // over-precise time outputs such as e.g. "56 minutes 29 seconds". The
+ // relative rounding error is guaranteed to be less than approximately 0.5 /
bartfab (slow) 2014/02/20 16:31:23 I like the combination of "guaranteed to" and "app
Thiemo Nagel 2014/02/22 21:44:10 It was tempting to write it that way, but I've cha
+ // |cutoff| (e.g. 5% for a |cutoff| of 10) and a second unit is only used when
+ // necessary to achieve the precision guarantee.
+ //
+ // Currently, the only combination of format and length that is implemented is
+ // (FORMAT_DURATION, LENGTH_LONG), but it's easy to add others if required.
+ //
+ // Note: This is implemented with two translation strings (IDS_TIME_*_1ST and
+ // IDS_TIME_*_2ND) per [plurality x pair of units] which are concatenated
+ // after having been formatted individually. The separator between first unit
+ // and second unit (a blank in English) is included in IDS_TIME_*_1ST.
bartfab (slow) 2014/02/20 16:31:23 Nit: The comment is excellent. It may make sense t
Thiemo Nagel 2014/02/22 21:44:10 Done.
+ static base::string16 Detailed(Format format,
+ Length length,
+ int cutoff,
+ const base::TimeDelta& delta);
// For displaying a relative time in the past. This method returns either
// "Today", "Yesterday", or an empty string if it's older than that. Returns

Powered by Google App Engine
This is Rietveld 408576698