| OLD | NEW |
| 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/i18n/unicodestring.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 14 #include "third_party/icu/source/common/unicode/utypes.h" | 15 #include "third_party/icu/source/common/unicode/utypes.h" |
| 15 #include "third_party/icu/source/i18n/unicode/datefmt.h" | 16 #include "third_party/icu/source/i18n/unicode/datefmt.h" |
| 16 #include "third_party/icu/source/i18n/unicode/dtitvfmt.h" | 17 #include "third_party/icu/source/i18n/unicode/dtitvfmt.h" |
| 17 #include "third_party/icu/source/i18n/unicode/dtptngen.h" | 18 #include "third_party/icu/source/i18n/unicode/dtptngen.h" |
| 18 #include "third_party/icu/source/i18n/unicode/fmtable.h" | 19 #include "third_party/icu/source/i18n/unicode/fmtable.h" |
| 19 #include "third_party/icu/source/i18n/unicode/measfmt.h" | 20 #include "third_party/icu/source/i18n/unicode/measfmt.h" |
| 20 #include "third_party/icu/source/i18n/unicode/smpdtfmt.h" | 21 #include "third_party/icu/source/i18n/unicode/smpdtfmt.h" |
| 21 | 22 |
| 22 namespace base { | 23 namespace base { |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 string16 TimeFormat(const icu::DateFormat* formatter, | 26 string16 TimeFormat(const icu::DateFormat* formatter, |
| 26 const Time& time) { | 27 const Time& time) { |
| 27 DCHECK(formatter); | 28 DCHECK(formatter); |
| 28 icu::UnicodeString date_string; | 29 icu::UnicodeString date_string; |
| 29 | 30 |
| 30 formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), date_string); | 31 formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), date_string); |
| 31 return string16(date_string.getBuffer(), | 32 return i18n::UnicodeStringToString16(date_string); |
| 32 static_cast<size_t>(date_string.length())); | |
| 33 } | 33 } |
| 34 | 34 |
| 35 string16 TimeFormatWithoutAmPm(const icu::DateFormat* formatter, | 35 string16 TimeFormatWithoutAmPm(const icu::DateFormat* formatter, |
| 36 const Time& time) { | 36 const Time& time) { |
| 37 DCHECK(formatter); | 37 DCHECK(formatter); |
| 38 icu::UnicodeString time_string; | 38 icu::UnicodeString time_string; |
| 39 | 39 |
| 40 icu::FieldPosition ampm_field(icu::DateFormat::kAmPmField); | 40 icu::FieldPosition ampm_field(icu::DateFormat::kAmPmField); |
| 41 formatter->format( | 41 formatter->format( |
| 42 static_cast<UDate>(time.ToDoubleT() * 1000), time_string, ampm_field); | 42 static_cast<UDate>(time.ToDoubleT() * 1000), time_string, ampm_field); |
| 43 int ampm_length = ampm_field.getEndIndex() - ampm_field.getBeginIndex(); | 43 int ampm_length = ampm_field.getEndIndex() - ampm_field.getBeginIndex(); |
| 44 if (ampm_length) { | 44 if (ampm_length) { |
| 45 int begin = ampm_field.getBeginIndex(); | 45 int begin = ampm_field.getBeginIndex(); |
| 46 // Doesn't include any spacing before the field. | 46 // Doesn't include any spacing before the field. |
| 47 if (begin) | 47 if (begin) |
| 48 begin--; | 48 begin--; |
| 49 time_string.removeBetween(begin, ampm_field.getEndIndex()); | 49 time_string.removeBetween(begin, ampm_field.getEndIndex()); |
| 50 } | 50 } |
| 51 return string16(time_string.getBuffer(), | 51 return i18n::UnicodeStringToString16(time_string); |
| 52 static_cast<size_t>(time_string.length())); | |
| 53 } | 52 } |
| 54 | 53 |
| 55 icu::SimpleDateFormat CreateSimpleDateFormatter(const char* pattern) { | 54 icu::SimpleDateFormat CreateSimpleDateFormatter(const char* pattern) { |
| 56 // Generate a locale-dependent format pattern. The generator will take | 55 // Generate a locale-dependent format pattern. The generator will take |
| 57 // care of locale-dependent formatting issues like which separator to | 56 // care of locale-dependent formatting issues like which separator to |
| 58 // use (some locales use '.' instead of ':'), and where to put the am/pm | 57 // use (some locales use '.' instead of ':'), and where to put the am/pm |
| 59 // marker. | 58 // marker. |
| 60 UErrorCode status = U_ZERO_ERROR; | 59 UErrorCode status = U_ZERO_ERROR; |
| 61 std::unique_ptr<icu::DateTimePatternGenerator> generator( | 60 std::unique_ptr<icu::DateTimePatternGenerator> generator( |
| 62 icu::DateTimePatternGenerator::createInstance(status)); | 61 icu::DateTimePatternGenerator::createInstance(status)); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 } | 206 } |
| 208 | 207 |
| 209 icu::UnicodeString formatted; | 208 icu::UnicodeString formatted; |
| 210 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE); | 209 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE); |
| 211 measure_format.formatMeasures(measures, 2, formatted, ignore, status); | 210 measure_format.formatMeasures(measures, 2, formatted, ignore, status); |
| 212 if (U_FAILURE(status)) { | 211 if (U_FAILURE(status)) { |
| 213 LOG(ERROR) << "formatMeasures failed: " << u_errorName(status); | 212 LOG(ERROR) << "formatMeasures failed: " << u_errorName(status); |
| 214 return false; | 213 return false; |
| 215 } | 214 } |
| 216 | 215 |
| 217 *out = base::string16(formatted.getBuffer(), formatted.length()); | 216 *out = i18n::UnicodeStringToString16(formatted); |
| 218 return true; | 217 return true; |
| 219 } | 218 } |
| 220 | 219 |
| 221 bool TimeDurationFormatWithSeconds(const TimeDelta time, | 220 bool TimeDurationFormatWithSeconds(const TimeDelta time, |
| 222 const DurationFormatWidth width, | 221 const DurationFormatWidth width, |
| 223 string16* out) { | 222 string16* out) { |
| 224 DCHECK(out); | 223 DCHECK(out); |
| 225 UErrorCode status = U_ZERO_ERROR; | 224 UErrorCode status = U_ZERO_ERROR; |
| 226 const int64_t total_seconds = static_cast<int>(time.InSecondsF() + 0.5); | 225 const int64_t total_seconds = static_cast<int>(time.InSecondsF() + 0.5); |
| 227 const int hours = total_seconds / 3600; | 226 const int hours = total_seconds / 3600; |
| 228 const int minutes = (total_seconds - hours * 3600) / 60; | 227 const int minutes = (total_seconds - hours * 3600) / 60; |
| 229 const int seconds = total_seconds % 60; | 228 const int seconds = total_seconds % 60; |
| 230 UMeasureFormatWidth u_width = DurationWidthToMeasureWidth(width); | 229 UMeasureFormatWidth u_width = DurationWidthToMeasureWidth(width); |
| 231 | 230 |
| 232 const icu::Measure measures[] = { | 231 const icu::Measure measures[] = { |
| 233 icu::Measure(hours, icu::MeasureUnit::createHour(status), status), | 232 icu::Measure(hours, icu::MeasureUnit::createHour(status), status), |
| 234 icu::Measure(minutes, icu::MeasureUnit::createMinute(status), status), | 233 icu::Measure(minutes, icu::MeasureUnit::createMinute(status), status), |
| 235 icu::Measure(seconds, icu::MeasureUnit::createSecond(status), status)}; | 234 icu::Measure(seconds, icu::MeasureUnit::createSecond(status), status)}; |
| 236 icu::MeasureFormat measure_format(icu::Locale::getDefault(), u_width, status); | 235 icu::MeasureFormat measure_format(icu::Locale::getDefault(), u_width, status); |
| 237 icu::UnicodeString formatted; | 236 icu::UnicodeString formatted; |
| 238 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE); | 237 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE); |
| 239 measure_format.formatMeasures(measures, 3, formatted, ignore, status); | 238 measure_format.formatMeasures(measures, 3, formatted, ignore, status); |
| 240 *out = base::string16(formatted.getBuffer(), formatted.length()); | 239 *out = i18n::UnicodeStringToString16(formatted); |
| 241 return U_SUCCESS(status) == TRUE; | 240 return U_SUCCESS(status) == TRUE; |
| 242 } | 241 } |
| 243 | 242 |
| 244 string16 DateIntervalFormat(const Time& begin_time, | 243 string16 DateIntervalFormat(const Time& begin_time, |
| 245 const Time& end_time, | 244 const Time& end_time, |
| 246 DateFormat format) { | 245 DateFormat format) { |
| 247 UErrorCode status = U_ZERO_ERROR; | 246 UErrorCode status = U_ZERO_ERROR; |
| 248 | 247 |
| 249 std::unique_ptr<icu::DateIntervalFormat> formatter( | 248 std::unique_ptr<icu::DateIntervalFormat> formatter( |
| 250 icu::DateIntervalFormat::createInstance(DateFormatToString(format), | 249 icu::DateIntervalFormat::createInstance(DateFormatToString(format), |
| 251 status)); | 250 status)); |
| 252 | 251 |
| 253 icu::FieldPosition pos = 0; | 252 icu::FieldPosition pos = 0; |
| 254 UDate start_date = static_cast<UDate>(begin_time.ToDoubleT() * 1000); | 253 UDate start_date = static_cast<UDate>(begin_time.ToDoubleT() * 1000); |
| 255 UDate end_date = static_cast<UDate>(end_time.ToDoubleT() * 1000); | 254 UDate end_date = static_cast<UDate>(end_time.ToDoubleT() * 1000); |
| 256 icu::DateInterval interval(start_date, end_date); | 255 icu::DateInterval interval(start_date, end_date); |
| 257 icu::UnicodeString formatted; | 256 icu::UnicodeString formatted; |
| 258 formatter->format(&interval, formatted, pos, status); | 257 formatter->format(&interval, formatted, pos, status); |
| 259 return string16(formatted.getBuffer(), | 258 return i18n::UnicodeStringToString16(formatted); |
| 260 static_cast<size_t>(formatted.length())); | |
| 261 } | 259 } |
| 262 | 260 |
| 263 HourClockType GetHourClockType() { | 261 HourClockType GetHourClockType() { |
| 264 // TODO(satorux,jshin): Rework this with ures_getByKeyWithFallback() | 262 // TODO(satorux,jshin): Rework this with ures_getByKeyWithFallback() |
| 265 // once it becomes public. The short time format can be found at | 263 // once it becomes public. The short time format can be found at |
| 266 // "calendar/gregorian/DateTimePatterns/3" in the resources. | 264 // "calendar/gregorian/DateTimePatterns/3" in the resources. |
| 267 std::unique_ptr<icu::SimpleDateFormat> formatter( | 265 std::unique_ptr<icu::SimpleDateFormat> formatter( |
| 268 static_cast<icu::SimpleDateFormat*>( | 266 static_cast<icu::SimpleDateFormat*>( |
| 269 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort))); | 267 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort))); |
| 270 // Retrieve the short time format. | 268 // Retrieve the short time format. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 294 // See http://userguide.icu-project.org/formatparse/datetime for details | 292 // See http://userguide.icu-project.org/formatparse/datetime for details |
| 295 // about the date/time format syntax. | 293 // about the date/time format syntax. |
| 296 if (pattern_unicode.indexOf('a') == -1) { | 294 if (pattern_unicode.indexOf('a') == -1) { |
| 297 return k24HourClock; | 295 return k24HourClock; |
| 298 } else { | 296 } else { |
| 299 return k12HourClock; | 297 return k12HourClock; |
| 300 } | 298 } |
| 301 } | 299 } |
| 302 | 300 |
| 303 } // namespace base | 301 } // namespace base |
| OLD | NEW |