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

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

Issue 2734883003: base: Make TimeDurationFormat* report failures. (Closed)
Patch Set: log locale Created 3 years, 9 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
« no previous file with comments | « base/i18n/time_formatting.h ('k') | base/i18n/time_formatting_unittest.cc » ('j') | 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/common/unicode/utypes.h"
14 #include "third_party/icu/source/i18n/unicode/datefmt.h" 15 #include "third_party/icu/source/i18n/unicode/datefmt.h"
15 #include "third_party/icu/source/i18n/unicode/dtitvfmt.h" 16 #include "third_party/icu/source/i18n/unicode/dtitvfmt.h"
16 #include "third_party/icu/source/i18n/unicode/dtptngen.h" 17 #include "third_party/icu/source/i18n/unicode/dtptngen.h"
17 #include "third_party/icu/source/i18n/unicode/fmtable.h" 18 #include "third_party/icu/source/i18n/unicode/fmtable.h"
18 #include "third_party/icu/source/i18n/unicode/measfmt.h" 19 #include "third_party/icu/source/i18n/unicode/measfmt.h"
19 #include "third_party/icu/source/i18n/unicode/smpdtfmt.h" 20 #include "third_party/icu/source/i18n/unicode/smpdtfmt.h"
20 21
21 namespace base { 22 namespace base {
22 namespace { 23 namespace {
23 24
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 std::unique_ptr<icu::DateFormat> formatter( 170 std::unique_ptr<icu::DateFormat> formatter(
170 icu::DateFormat::createDateInstance(icu::DateFormat::kFull)); 171 icu::DateFormat::createDateInstance(icu::DateFormat::kFull));
171 return TimeFormat(formatter.get(), time); 172 return TimeFormat(formatter.get(), time);
172 } 173 }
173 174
174 string16 TimeFormatWithPattern(const Time& time, const char* pattern) { 175 string16 TimeFormatWithPattern(const Time& time, const char* pattern) {
175 icu::SimpleDateFormat formatter = CreateSimpleDateFormatter(pattern); 176 icu::SimpleDateFormat formatter = CreateSimpleDateFormatter(pattern);
176 return TimeFormat(&formatter, time); 177 return TimeFormat(&formatter, time);
177 } 178 }
178 179
179 string16 TimeDurationFormat(const TimeDelta time, 180 bool TimeDurationFormat(const TimeDelta time,
180 const DurationFormatWidth width) { 181 const DurationFormatWidth width,
182 string16* out) {
183 DCHECK(out);
181 UErrorCode status = U_ZERO_ERROR; 184 UErrorCode status = U_ZERO_ERROR;
182 const int total_minutes = static_cast<int>(time.InSecondsF() / 60 + 0.5); 185 const int total_minutes = static_cast<int>(time.InSecondsF() / 60 + 0.5);
183 const int hours = total_minutes / 60; 186 const int hours = total_minutes / 60;
184 const int minutes = total_minutes % 60; 187 const int minutes = total_minutes % 60;
185 UMeasureFormatWidth u_width = DurationWidthToMeasureWidth(width); 188 UMeasureFormatWidth u_width = DurationWidthToMeasureWidth(width);
186 189
190 // TODO(derat): Delete the |status| checks and LOG(ERROR) calls throughout
191 // this function once the cause of http://crbug.com/677043 is tracked down.
187 const icu::Measure measures[] = { 192 const icu::Measure measures[] = {
188 icu::Measure(hours, icu::MeasureUnit::createHour(status), status), 193 icu::Measure(hours, icu::MeasureUnit::createHour(status), status),
189 icu::Measure(minutes, icu::MeasureUnit::createMinute(status), status)}; 194 icu::Measure(minutes, icu::MeasureUnit::createMinute(status), status)};
195 if (U_FAILURE(status)) {
196 LOG(ERROR) << "Creating MeasureUnit or Measure for " << hours << "h"
197 << minutes << "m failed: " << u_errorName(status);
198 return false;
199 }
200
190 icu::MeasureFormat measure_format(icu::Locale::getDefault(), u_width, status); 201 icu::MeasureFormat measure_format(icu::Locale::getDefault(), u_width, status);
202 if (U_FAILURE(status)) {
203 LOG(ERROR) << "Creating MeasureFormat for "
204 << icu::Locale::getDefault().getName()
205 << " failed: " << u_errorName(status);
206 return false;
207 }
208
191 icu::UnicodeString formatted; 209 icu::UnicodeString formatted;
192 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE); 210 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE);
193 measure_format.formatMeasures(measures, 2, formatted, ignore, status); 211 measure_format.formatMeasures(measures, 2, formatted, ignore, status);
194 return base::string16(formatted.getBuffer(), formatted.length()); 212 if (U_FAILURE(status)) {
213 LOG(ERROR) << "formatMeasures failed: " << u_errorName(status);
214 return false;
215 }
216
217 *out = base::string16(formatted.getBuffer(), formatted.length());
218 return true;
195 } 219 }
196 220
197 string16 TimeDurationFormatWithSeconds(const TimeDelta time, 221 bool TimeDurationFormatWithSeconds(const TimeDelta time,
198 const DurationFormatWidth width) { 222 const DurationFormatWidth width,
223 string16* out) {
224 DCHECK(out);
199 UErrorCode status = U_ZERO_ERROR; 225 UErrorCode status = U_ZERO_ERROR;
200 const int64_t total_seconds = static_cast<int>(time.InSecondsF() + 0.5); 226 const int64_t total_seconds = static_cast<int>(time.InSecondsF() + 0.5);
201 const int hours = total_seconds / 3600; 227 const int hours = total_seconds / 3600;
202 const int minutes = (total_seconds - hours * 3600) / 60; 228 const int minutes = (total_seconds - hours * 3600) / 60;
203 const int seconds = total_seconds % 60; 229 const int seconds = total_seconds % 60;
204 UMeasureFormatWidth u_width = DurationWidthToMeasureWidth(width); 230 UMeasureFormatWidth u_width = DurationWidthToMeasureWidth(width);
205 231
206 const icu::Measure measures[] = { 232 const icu::Measure measures[] = {
207 icu::Measure(hours, icu::MeasureUnit::createHour(status), status), 233 icu::Measure(hours, icu::MeasureUnit::createHour(status), status),
208 icu::Measure(minutes, icu::MeasureUnit::createMinute(status), status), 234 icu::Measure(minutes, icu::MeasureUnit::createMinute(status), status),
209 icu::Measure(seconds, icu::MeasureUnit::createSecond(status), status)}; 235 icu::Measure(seconds, icu::MeasureUnit::createSecond(status), status)};
210 icu::MeasureFormat measure_format(icu::Locale::getDefault(), u_width, status); 236 icu::MeasureFormat measure_format(icu::Locale::getDefault(), u_width, status);
211 icu::UnicodeString formatted; 237 icu::UnicodeString formatted;
212 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE); 238 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE);
213 measure_format.formatMeasures(measures, 3, formatted, ignore, status); 239 measure_format.formatMeasures(measures, 3, formatted, ignore, status);
214 return base::string16(formatted.getBuffer(), formatted.length()); 240 *out = base::string16(formatted.getBuffer(), formatted.length());
241 return U_SUCCESS(status) == TRUE;
215 } 242 }
216 243
217 string16 DateIntervalFormat(const Time& begin_time, 244 string16 DateIntervalFormat(const Time& begin_time,
218 const Time& end_time, 245 const Time& end_time,
219 DateFormat format) { 246 DateFormat format) {
220 UErrorCode status = U_ZERO_ERROR; 247 UErrorCode status = U_ZERO_ERROR;
221 248
222 std::unique_ptr<icu::DateIntervalFormat> formatter( 249 std::unique_ptr<icu::DateIntervalFormat> formatter(
223 icu::DateIntervalFormat::createInstance(DateFormatToString(format), 250 icu::DateIntervalFormat::createInstance(DateFormatToString(format),
224 status)); 251 status));
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 // See http://userguide.icu-project.org/formatparse/datetime for details 294 // See http://userguide.icu-project.org/formatparse/datetime for details
268 // about the date/time format syntax. 295 // about the date/time format syntax.
269 if (pattern_unicode.indexOf('a') == -1) { 296 if (pattern_unicode.indexOf('a') == -1) {
270 return k24HourClock; 297 return k24HourClock;
271 } else { 298 } else {
272 return k12HourClock; 299 return k12HourClock;
273 } 300 }
274 } 301 }
275 302
276 } // namespace base 303 } // namespace base
OLDNEW
« no previous file with comments | « base/i18n/time_formatting.h ('k') | base/i18n/time_formatting_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698