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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 int begin = ampm_field.getBeginIndex(); | 38 int begin = ampm_field.getBeginIndex(); |
39 // Doesn't include any spacing before the field. | 39 // Doesn't include any spacing before the field. |
40 if (begin) | 40 if (begin) |
41 begin--; | 41 begin--; |
42 time_string.removeBetween(begin, ampm_field.getEndIndex()); | 42 time_string.removeBetween(begin, ampm_field.getEndIndex()); |
43 } | 43 } |
44 return string16(time_string.getBuffer(), | 44 return string16(time_string.getBuffer(), |
45 static_cast<size_t>(time_string.length())); | 45 static_cast<size_t>(time_string.length())); |
46 } | 46 } |
47 | 47 |
| 48 icu::SimpleDateFormat CreateSimpleDateFormatter(const char* pattern) { |
| 49 // Generate a locale-dependent format pattern. The generator will take |
| 50 // care of locale-dependent formatting issues like which separator to |
| 51 // use (some locales use '.' instead of ':'), and where to put the am/pm |
| 52 // marker. |
| 53 UErrorCode status = U_ZERO_ERROR; |
| 54 scoped_ptr<icu::DateTimePatternGenerator> generator( |
| 55 icu::DateTimePatternGenerator::createInstance(status)); |
| 56 DCHECK(U_SUCCESS(status)); |
| 57 icu::UnicodeString generated_pattern = |
| 58 generator->getBestPattern(icu::UnicodeString(pattern), status); |
| 59 DCHECK(U_SUCCESS(status)); |
| 60 |
| 61 // Then, format the time using the generated pattern. |
| 62 icu::SimpleDateFormat formatter(generated_pattern, status); |
| 63 DCHECK(U_SUCCESS(status)); |
| 64 |
| 65 return formatter; |
| 66 } |
| 67 |
48 } // namespace | 68 } // namespace |
49 | 69 |
50 string16 TimeFormatTimeOfDay(const Time& time) { | 70 string16 TimeFormatTimeOfDay(const Time& time) { |
51 // We can omit the locale parameter because the default should match | 71 // We can omit the locale parameter because the default should match |
52 // Chrome's application locale. | 72 // Chrome's application locale. |
53 scoped_ptr<icu::DateFormat> formatter( | 73 scoped_ptr<icu::DateFormat> formatter( |
54 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort)); | 74 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort)); |
55 return TimeFormat(formatter.get(), time); | 75 return TimeFormat(formatter.get(), time); |
56 } | 76 } |
57 | 77 |
| 78 string16 TimeFormatTimeOfDayWithMilliseconds(const Time& time) { |
| 79 icu::SimpleDateFormat formatter = CreateSimpleDateFormatter("HmsSSS"); |
| 80 return TimeFormatWithoutAmPm(&formatter, time); |
| 81 } |
| 82 |
58 string16 TimeFormatTimeOfDayWithHourClockType(const Time& time, | 83 string16 TimeFormatTimeOfDayWithHourClockType(const Time& time, |
59 HourClockType type, | 84 HourClockType type, |
60 AmPmClockType ampm) { | 85 AmPmClockType ampm) { |
61 // Just redirect to the normal function if the default type matches the | 86 // Just redirect to the normal function if the default type matches the |
62 // given type. | 87 // given type. |
63 HourClockType default_type = GetHourClockType(); | 88 HourClockType default_type = GetHourClockType(); |
64 if (default_type == type && (type == k24HourClock || ampm == kKeepAmPm)) { | 89 if (default_type == type && (type == k24HourClock || ampm == kKeepAmPm)) { |
65 return TimeFormatTimeOfDay(time); | 90 return TimeFormatTimeOfDay(time); |
66 } | 91 } |
67 | 92 |
68 // Generate a locale-dependent format pattern. The generator will take | |
69 // care of locale-dependent formatting issues like which separator to | |
70 // use (some locales use '.' instead of ':'), and where to put the am/pm | |
71 // marker. | |
72 UErrorCode status = U_ZERO_ERROR; | |
73 scoped_ptr<icu::DateTimePatternGenerator> generator( | |
74 icu::DateTimePatternGenerator::createInstance(status)); | |
75 DCHECK(U_SUCCESS(status)); | |
76 const char* base_pattern = (type == k12HourClock ? "ahm" : "Hm"); | 93 const char* base_pattern = (type == k12HourClock ? "ahm" : "Hm"); |
77 icu::UnicodeString generated_pattern = | 94 icu::SimpleDateFormat formatter = CreateSimpleDateFormatter(base_pattern); |
78 generator->getBestPattern(icu::UnicodeString(base_pattern), status); | |
79 DCHECK(U_SUCCESS(status)); | |
80 | 95 |
81 // Then, format the time using the generated pattern. | |
82 icu::SimpleDateFormat formatter(generated_pattern, status); | |
83 DCHECK(U_SUCCESS(status)); | |
84 if (ampm == kKeepAmPm) { | 96 if (ampm == kKeepAmPm) { |
85 return TimeFormat(&formatter, time); | 97 return TimeFormat(&formatter, time); |
86 } else { | 98 } else { |
87 return TimeFormatWithoutAmPm(&formatter, time); | 99 return TimeFormatWithoutAmPm(&formatter, time); |
88 } | 100 } |
89 } | 101 } |
90 | 102 |
91 string16 TimeFormatShortDate(const Time& time) { | 103 string16 TimeFormatShortDate(const Time& time) { |
92 scoped_ptr<icu::DateFormat> formatter( | 104 scoped_ptr<icu::DateFormat> formatter( |
93 icu::DateFormat::createDateInstance(icu::DateFormat::kMedium)); | 105 icu::DateFormat::createDateInstance(icu::DateFormat::kMedium)); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 // See http://userguide.icu-project.org/formatparse/datetime for details | 170 // See http://userguide.icu-project.org/formatparse/datetime for details |
159 // about the date/time format syntax. | 171 // about the date/time format syntax. |
160 if (pattern_unicode.indexOf('a') == -1) { | 172 if (pattern_unicode.indexOf('a') == -1) { |
161 return k24HourClock; | 173 return k24HourClock; |
162 } else { | 174 } else { |
163 return k12HourClock; | 175 return k12HourClock; |
164 } | 176 } |
165 } | 177 } |
166 | 178 |
167 } // namespace base | 179 } // namespace base |
OLD | NEW |