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

Side by Side Diff: base/i18n/time_formatting.cc

Issue 1951493002: Fix i18n number formats in tray power displays (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move time formatting into TimeDurationFormat() Created 4 years, 7 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
« base/i18n/time_formatting.h ('K') | « base/i18n/time_formatting.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/i18n/time_formatting.h" 5 #include "base/i18n/time_formatting.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "third_party/icu/source/i18n/unicode/datefmt.h" 14 #include "third_party/icu/source/i18n/unicode/datefmt.h"
15 #include "third_party/icu/source/i18n/unicode/dtptngen.h" 15 #include "third_party/icu/source/i18n/unicode/dtptngen.h"
16 #include "third_party/icu/source/i18n/unicode/fmtable.h"
17 #include "third_party/icu/source/i18n/unicode/measfmt.h"
16 #include "third_party/icu/source/i18n/unicode/smpdtfmt.h" 18 #include "third_party/icu/source/i18n/unicode/smpdtfmt.h"
17 19
18 namespace base { 20 namespace base {
19 namespace { 21 namespace {
20 22
21 string16 TimeFormat(const icu::DateFormat* formatter, 23 string16 TimeFormat(const icu::DateFormat* formatter,
22 const Time& time) { 24 const Time& time) {
23 DCHECK(formatter); 25 DCHECK(formatter);
24 icu::UnicodeString date_string; 26 icu::UnicodeString date_string;
25 27
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 generator->getBestPattern(icu::UnicodeString(pattern), status); 63 generator->getBestPattern(icu::UnicodeString(pattern), status);
62 DCHECK(U_SUCCESS(status)); 64 DCHECK(U_SUCCESS(status));
63 65
64 // Then, format the time using the generated pattern. 66 // Then, format the time using the generated pattern.
65 icu::SimpleDateFormat formatter(generated_pattern, status); 67 icu::SimpleDateFormat formatter(generated_pattern, status);
66 DCHECK(U_SUCCESS(status)); 68 DCHECK(U_SUCCESS(status));
67 69
68 return formatter; 70 return formatter;
69 } 71 }
70 72
73 UMeasureFormatWidth DurationWidthToMeasureWidth(DurationFormatWidth width) {
74 switch (width) {
75 case DURATION_WIDTH_WIDE: return UMEASFMT_WIDTH_WIDE;
76 case DURATION_WIDTH_SHORT: return UMEASFMT_WIDTH_SHORT;
77 case DURATION_WIDTH_NARROW: return UMEASFMT_WIDTH_NARROW;
78 case DURATION_WIDTH_NUMERIC: return UMEASFMT_WIDTH_NUMERIC;
79 }
80 NOTREACHED();
81 return UMEASFMT_WIDTH_COUNT;
82 }
83
71 } // namespace 84 } // namespace
72 85
73 string16 TimeFormatTimeOfDay(const Time& time) { 86 string16 TimeFormatTimeOfDay(const Time& time) {
74 // We can omit the locale parameter because the default should match 87 // We can omit the locale parameter because the default should match
75 // Chrome's application locale. 88 // Chrome's application locale.
76 std::unique_ptr<icu::DateFormat> formatter( 89 std::unique_ptr<icu::DateFormat> formatter(
77 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort)); 90 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort));
78 return TimeFormat(formatter.get(), time); 91 return TimeFormat(formatter.get(), time);
79 } 92 }
80 93
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 icu::DateFormat::createDateTimeInstance(icu::DateFormat::kFull)); 146 icu::DateFormat::createDateTimeInstance(icu::DateFormat::kFull));
134 return TimeFormat(formatter.get(), time); 147 return TimeFormat(formatter.get(), time);
135 } 148 }
136 149
137 string16 TimeFormatFriendlyDate(const Time& time) { 150 string16 TimeFormatFriendlyDate(const Time& time) {
138 std::unique_ptr<icu::DateFormat> formatter( 151 std::unique_ptr<icu::DateFormat> formatter(
139 icu::DateFormat::createDateInstance(icu::DateFormat::kFull)); 152 icu::DateFormat::createDateInstance(icu::DateFormat::kFull));
140 return TimeFormat(formatter.get(), time); 153 return TimeFormat(formatter.get(), time);
141 } 154 }
142 155
156 string16 TimeDurationFormat(const TimeDelta& time,
157 const DurationFormatWidth width) {
158 UErrorCode status = U_ZERO_ERROR;
159 const int total_minutes = static_cast<int>(time.InSecondsF() / 60 + 0.5);
160 // Using doubles instead of ints, and the 0.0001, causes formatMeasures() to
161 // 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
162 double hours = total_minutes / 60;
163 double minutes = total_minutes % 60 + 0.0001;
164 UMeasureFormatWidth u_width = DurationWidthToMeasureWidth(width);
165
166 const icu::Measure measures[] = {
167 icu::Measure(hours, icu::MeasureUnit::createHour(status), status),
168 icu::Measure(minutes, icu::MeasureUnit::createMinute(status), status)};
169 icu::MeasureFormat measure_format(icu::Locale::getDefault(), u_width, status);
170 icu::UnicodeString formatted;
171 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE);
172 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
173 ignore, status);
174 return base::string16(formatted.getBuffer(), formatted.length());
175 }
176
143 HourClockType GetHourClockType() { 177 HourClockType GetHourClockType() {
144 // TODO(satorux,jshin): Rework this with ures_getByKeyWithFallback() 178 // TODO(satorux,jshin): Rework this with ures_getByKeyWithFallback()
145 // once it becomes public. The short time format can be found at 179 // once it becomes public. The short time format can be found at
146 // "calendar/gregorian/DateTimePatterns/3" in the resources. 180 // "calendar/gregorian/DateTimePatterns/3" in the resources.
147 std::unique_ptr<icu::SimpleDateFormat> formatter( 181 std::unique_ptr<icu::SimpleDateFormat> formatter(
148 static_cast<icu::SimpleDateFormat*>( 182 static_cast<icu::SimpleDateFormat*>(
149 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort))); 183 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort)));
150 // Retrieve the short time format. 184 // Retrieve the short time format.
151 icu::UnicodeString pattern_unicode; 185 icu::UnicodeString pattern_unicode;
152 formatter->toPattern(pattern_unicode); 186 formatter->toPattern(pattern_unicode);
(...skipping 21 matching lines...) Expand all
174 // See http://userguide.icu-project.org/formatparse/datetime for details 208 // See http://userguide.icu-project.org/formatparse/datetime for details
175 // about the date/time format syntax. 209 // about the date/time format syntax.
176 if (pattern_unicode.indexOf('a') == -1) { 210 if (pattern_unicode.indexOf('a') == -1) {
177 return k24HourClock; 211 return k24HourClock;
178 } else { 212 } else {
179 return k12HourClock; 213 return k12HourClock;
180 } 214 }
181 } 215 }
182 216
183 } // namespace base 217 } // namespace base
OLDNEW
« base/i18n/time_formatting.h ('K') | « base/i18n/time_formatting.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698