Index: base/i18n/time_formatting.cc |
diff --git a/base/i18n/time_formatting.cc b/base/i18n/time_formatting.cc |
index 666abd44dd6c03decc9ad5fce4228401fa31a31f..63bee433ff78e434c9a73755b08d9d5f4f78ceb3 100644 |
--- a/base/i18n/time_formatting.cc |
+++ b/base/i18n/time_formatting.cc |
@@ -13,6 +13,8 @@ |
#include "base/time/time.h" |
#include "third_party/icu/source/i18n/unicode/datefmt.h" |
#include "third_party/icu/source/i18n/unicode/dtptngen.h" |
+#include "third_party/icu/source/i18n/unicode/fmtable.h" |
+#include "third_party/icu/source/i18n/unicode/measfmt.h" |
#include "third_party/icu/source/i18n/unicode/smpdtfmt.h" |
namespace base { |
@@ -68,6 +70,17 @@ icu::SimpleDateFormat CreateSimpleDateFormatter(const char* pattern) { |
return formatter; |
} |
+UMeasureFormatWidth DurationWidthToMeasureWidth(DurationFormatWidth width) { |
+ switch (width) { |
+ case DURATION_WIDTH_WIDE: return UMEASFMT_WIDTH_WIDE; |
+ case DURATION_WIDTH_SHORT: return UMEASFMT_WIDTH_SHORT; |
+ case DURATION_WIDTH_NARROW: return UMEASFMT_WIDTH_NARROW; |
+ case DURATION_WIDTH_NUMERIC: return UMEASFMT_WIDTH_NUMERIC; |
+ } |
+ NOTREACHED(); |
+ return UMEASFMT_WIDTH_COUNT; |
+} |
+ |
} // namespace |
string16 TimeFormatTimeOfDay(const Time& time) { |
@@ -140,6 +153,27 @@ string16 TimeFormatFriendlyDate(const Time& time) { |
return TimeFormat(formatter.get(), time); |
} |
+string16 TimeDurationFormat(const TimeDelta& time, |
+ const DurationFormatWidth width) { |
+ UErrorCode status = U_ZERO_ERROR; |
+ const int total_minutes = static_cast<int>(time.InSecondsF() / 60 + 0.5); |
+ // Using doubles instead of ints, and the 0.0001, causes formatMeasures() to |
+ // write "1:00", instead of "1h", when using NUMERIC width. |
Greg Levin
2016/05/11 19:43:02
Yeah, this is hacky. I'd rather have "1:00" than
jungshik at Google
2016/05/11 21:11:45
A potential issue with plural is why I just left
Greg Levin
2016/05/12 19:31:44
I looked at it awhile, and will give it a shot if
jungshik at Google
2016/05/13 23:00:35
Yeah... let's just leave it as it is for now.
IC
|
+ double hours = total_minutes / 60; |
+ double minutes = total_minutes % 60 + 0.0001; |
+ UMeasureFormatWidth u_width = DurationWidthToMeasureWidth(width); |
+ |
+ const icu::Measure measures[] = { |
+ icu::Measure(hours, icu::MeasureUnit::createHour(status), status), |
+ icu::Measure(minutes, icu::MeasureUnit::createMinute(status), status)}; |
+ icu::MeasureFormat measure_format(icu::Locale::getDefault(), u_width, status); |
+ icu::UnicodeString formatted; |
+ icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE); |
+ measure_format.formatMeasures(measures, minutes == 0 ? 1 : 2, formatted, |
jungshik at Google
2016/05/11 21:11:45
If you always use '2' for the 2nd argument, do you
Greg Levin
2016/05/12 19:31:44
<head desk> S'what I get for not reading carefull
|
+ ignore, status); |
+ return base::string16(formatted.getBuffer(), formatted.length()); |
+} |
+ |
HourClockType GetHourClockType() { |
// TODO(satorux,jshin): Rework this with ures_getByKeyWithFallback() |
// once it becomes public. The short time format can be found at |