Refactored not to expose raw pointers on ProxyList class.
[chromium-blink-merge.git] / base / i18n / time_formatting.cc
blob55121115fa5387e5a9d0e629aa26ec2e9da539da
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
3 // found in the LICENSE file.
5 #include "base/i18n/time_formatting.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h"
11 #include "third_party/icu/source/i18n/unicode/datefmt.h"
12 #include "third_party/icu/source/i18n/unicode/dtptngen.h"
13 #include "third_party/icu/source/i18n/unicode/smpdtfmt.h"
15 namespace base {
16 namespace {
18 string16 TimeFormat(const icu::DateFormat* formatter,
19 const Time& time) {
20 DCHECK(formatter);
21 icu::UnicodeString date_string;
23 formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), date_string);
24 return string16(date_string.getBuffer(),
25 static_cast<size_t>(date_string.length()));
28 string16 TimeFormatWithoutAmPm(const icu::DateFormat* formatter,
29 const Time& time) {
30 DCHECK(formatter);
31 icu::UnicodeString time_string;
33 icu::FieldPosition ampm_field(icu::DateFormat::kAmPmField);
34 formatter->format(
35 static_cast<UDate>(time.ToDoubleT() * 1000), time_string, ampm_field);
36 int ampm_length = ampm_field.getEndIndex() - ampm_field.getBeginIndex();
37 if (ampm_length) {
38 int begin = ampm_field.getBeginIndex();
39 // Doesn't include any spacing before the field.
40 if (begin)
41 begin--;
42 time_string.removeBetween(begin, ampm_field.getEndIndex());
44 return string16(time_string.getBuffer(),
45 static_cast<size_t>(time_string.length()));
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));
61 // Then, format the time using the generated pattern.
62 icu::SimpleDateFormat formatter(generated_pattern, status);
63 DCHECK(U_SUCCESS(status));
65 return formatter;
68 } // namespace
70 string16 TimeFormatTimeOfDay(const Time& time) {
71 // We can omit the locale parameter because the default should match
72 // Chrome's application locale.
73 scoped_ptr<icu::DateFormat> formatter(
74 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort));
75 return TimeFormat(formatter.get(), time);
78 string16 TimeFormatTimeOfDayWithMilliseconds(const Time& time) {
79 icu::SimpleDateFormat formatter = CreateSimpleDateFormatter("HmsSSS");
80 return TimeFormatWithoutAmPm(&formatter, time);
83 string16 TimeFormatTimeOfDayWithHourClockType(const Time& time,
84 HourClockType type,
85 AmPmClockType ampm) {
86 // Just redirect to the normal function if the default type matches the
87 // given type.
88 HourClockType default_type = GetHourClockType();
89 if (default_type == type && (type == k24HourClock || ampm == kKeepAmPm)) {
90 return TimeFormatTimeOfDay(time);
93 const char* base_pattern = (type == k12HourClock ? "ahm" : "Hm");
94 icu::SimpleDateFormat formatter = CreateSimpleDateFormatter(base_pattern);
96 if (ampm == kKeepAmPm) {
97 return TimeFormat(&formatter, time);
98 } else {
99 return TimeFormatWithoutAmPm(&formatter, time);
103 string16 TimeFormatShortDate(const Time& time) {
104 scoped_ptr<icu::DateFormat> formatter(
105 icu::DateFormat::createDateInstance(icu::DateFormat::kMedium));
106 return TimeFormat(formatter.get(), time);
109 string16 TimeFormatShortDateNumeric(const Time& time) {
110 scoped_ptr<icu::DateFormat> formatter(
111 icu::DateFormat::createDateInstance(icu::DateFormat::kShort));
112 return TimeFormat(formatter.get(), time);
115 string16 TimeFormatShortDateAndTime(const Time& time) {
116 scoped_ptr<icu::DateFormat> formatter(
117 icu::DateFormat::createDateTimeInstance(icu::DateFormat::kShort));
118 return TimeFormat(formatter.get(), time);
121 string16 TimeFormatShortDateAndTimeWithTimeZone(const Time& time) {
122 scoped_ptr<icu::DateFormat> formatter(icu::DateFormat::createDateTimeInstance(
123 icu::DateFormat::kShort, icu::DateFormat::kLong));
124 return TimeFormat(formatter.get(), time);
127 string16 TimeFormatFriendlyDateAndTime(const Time& time) {
128 scoped_ptr<icu::DateFormat> formatter(
129 icu::DateFormat::createDateTimeInstance(icu::DateFormat::kFull));
130 return TimeFormat(formatter.get(), time);
133 string16 TimeFormatFriendlyDate(const Time& time) {
134 scoped_ptr<icu::DateFormat> formatter(icu::DateFormat::createDateInstance(
135 icu::DateFormat::kFull));
136 return TimeFormat(formatter.get(), time);
139 HourClockType GetHourClockType() {
140 // TODO(satorux,jshin): Rework this with ures_getByKeyWithFallback()
141 // once it becomes public. The short time format can be found at
142 // "calendar/gregorian/DateTimePatterns/3" in the resources.
143 scoped_ptr<icu::SimpleDateFormat> formatter(
144 static_cast<icu::SimpleDateFormat*>(
145 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort)));
146 // Retrieve the short time format.
147 icu::UnicodeString pattern_unicode;
148 formatter->toPattern(pattern_unicode);
150 // Determine what hour clock type the current locale uses, by checking
151 // "a" (am/pm marker) in the short time format. This is reliable as "a"
152 // is used by all of 12-hour clock formats, but not any of 24-hour clock
153 // formats, as shown below.
155 // % grep -A4 DateTimePatterns third_party/icu/source/data/locales/*.txt |
156 // grep -B1 -- -- |grep -v -- '--' |
157 // perl -nle 'print $1 if /^\S+\s+"(.*)"/' |sort -u
159 // H.mm
160 // H:mm
161 // HH.mm
162 // HH:mm
163 // a h:mm
164 // ah:mm
165 // ahh:mm
166 // h-mm a
167 // h:mm a
168 // hh:mm a
170 // See http://userguide.icu-project.org/formatparse/datetime for details
171 // about the date/time format syntax.
172 if (pattern_unicode.indexOf('a') == -1) {
173 return k24HourClock;
174 } else {
175 return k12HourClock;
179 } // namespace base