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/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 Loading... |
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 Loading... |
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 int hours = total_minutes / 60; |
| 161 int minutes = total_minutes % 60; |
| 162 UMeasureFormatWidth u_width = DurationWidthToMeasureWidth(width); |
| 163 |
| 164 const icu::Measure measures[] = { |
| 165 icu::Measure(hours, icu::MeasureUnit::createHour(status), status), |
| 166 icu::Measure(minutes, icu::MeasureUnit::createMinute(status), status)}; |
| 167 icu::MeasureFormat measure_format(icu::Locale::getDefault(), u_width, status); |
| 168 icu::UnicodeString formatted; |
| 169 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE); |
| 170 measure_format.formatMeasures(measures, 2, formatted, ignore, status); |
| 171 return base::string16(formatted.getBuffer(), formatted.length()); |
| 172 } |
| 173 |
143 HourClockType GetHourClockType() { | 174 HourClockType GetHourClockType() { |
144 // TODO(satorux,jshin): Rework this with ures_getByKeyWithFallback() | 175 // TODO(satorux,jshin): Rework this with ures_getByKeyWithFallback() |
145 // once it becomes public. The short time format can be found at | 176 // once it becomes public. The short time format can be found at |
146 // "calendar/gregorian/DateTimePatterns/3" in the resources. | 177 // "calendar/gregorian/DateTimePatterns/3" in the resources. |
147 std::unique_ptr<icu::SimpleDateFormat> formatter( | 178 std::unique_ptr<icu::SimpleDateFormat> formatter( |
148 static_cast<icu::SimpleDateFormat*>( | 179 static_cast<icu::SimpleDateFormat*>( |
149 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort))); | 180 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort))); |
150 // Retrieve the short time format. | 181 // Retrieve the short time format. |
151 icu::UnicodeString pattern_unicode; | 182 icu::UnicodeString pattern_unicode; |
152 formatter->toPattern(pattern_unicode); | 183 formatter->toPattern(pattern_unicode); |
(...skipping 21 matching lines...) Expand all Loading... |
174 // See http://userguide.icu-project.org/formatparse/datetime for details | 205 // See http://userguide.icu-project.org/formatparse/datetime for details |
175 // about the date/time format syntax. | 206 // about the date/time format syntax. |
176 if (pattern_unicode.indexOf('a') == -1) { | 207 if (pattern_unicode.indexOf('a') == -1) { |
177 return k24HourClock; | 208 return k24HourClock; |
178 } else { | 209 } else { |
179 return k12HourClock; | 210 return k12HourClock; |
180 } | 211 } |
181 } | 212 } |
182 | 213 |
183 } // namespace base | 214 } // namespace base |
OLD | NEW |