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"
18 string16
TimeFormat(const icu::DateFormat
* 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
,
31 icu::UnicodeString time_string
;
33 icu::FieldPosition
ampm_field(icu::DateFormat::kAmPmField
);
35 static_cast<UDate
>(time
.ToDoubleT() * 1000), time_string
, ampm_field
);
36 int ampm_length
= ampm_field
.getEndIndex() - ampm_field
.getBeginIndex();
38 int begin
= ampm_field
.getBeginIndex();
39 // Doesn't include any spacing before the field.
42 time_string
.removeBetween(begin
, ampm_field
.getEndIndex());
44 return string16(time_string
.getBuffer(),
45 static_cast<size_t>(time_string
.length()));
50 string16
TimeFormatTimeOfDay(const Time
& time
) {
51 // We can omit the locale parameter because the default should match
52 // Chrome's application locale.
53 scoped_ptr
<icu::DateFormat
> formatter(
54 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort
));
55 return TimeFormat(formatter
.get(), time
);
58 string16
TimeFormatTimeOfDayWithHourClockType(const Time
& time
,
61 // Just redirect to the normal function if the default type matches the
63 HourClockType default_type
= GetHourClockType();
64 if (default_type
== type
&& (type
== k24HourClock
|| ampm
== kKeepAmPm
)) {
65 return TimeFormatTimeOfDay(time
);
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
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");
77 icu::UnicodeString generated_pattern
=
78 generator
->getBestPattern(icu::UnicodeString(base_pattern
), status
);
79 DCHECK(U_SUCCESS(status
));
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
) {
85 return TimeFormat(&formatter
, time
);
87 return TimeFormatWithoutAmPm(&formatter
, time
);
91 string16
TimeFormatShortDate(const Time
& time
) {
92 scoped_ptr
<icu::DateFormat
> formatter(
93 icu::DateFormat::createDateInstance(icu::DateFormat::kMedium
));
94 return TimeFormat(formatter
.get(), time
);
97 string16
TimeFormatShortDateNumeric(const Time
& time
) {
98 scoped_ptr
<icu::DateFormat
> formatter(
99 icu::DateFormat::createDateInstance(icu::DateFormat::kShort
));
100 return TimeFormat(formatter
.get(), time
);
103 string16
TimeFormatShortDateAndTime(const Time
& time
) {
104 scoped_ptr
<icu::DateFormat
> formatter(
105 icu::DateFormat::createDateTimeInstance(icu::DateFormat::kShort
));
106 return TimeFormat(formatter
.get(), time
);
109 string16
TimeFormatShortDateAndTimeWithTimeZone(const Time
& time
) {
110 scoped_ptr
<icu::DateFormat
> formatter(icu::DateFormat::createDateTimeInstance(
111 icu::DateFormat::kShort
, icu::DateFormat::kLong
));
112 return TimeFormat(formatter
.get(), time
);
115 string16
TimeFormatFriendlyDateAndTime(const Time
& time
) {
116 scoped_ptr
<icu::DateFormat
> formatter(
117 icu::DateFormat::createDateTimeInstance(icu::DateFormat::kFull
));
118 return TimeFormat(formatter
.get(), time
);
121 string16
TimeFormatFriendlyDate(const Time
& time
) {
122 scoped_ptr
<icu::DateFormat
> formatter(icu::DateFormat::createDateInstance(
123 icu::DateFormat::kFull
));
124 return TimeFormat(formatter
.get(), time
);
127 HourClockType
GetHourClockType() {
128 // TODO(satorux,jshin): Rework this with ures_getByKeyWithFallback()
129 // once it becomes public. The short time format can be found at
130 // "calendar/gregorian/DateTimePatterns/3" in the resources.
131 scoped_ptr
<icu::SimpleDateFormat
> formatter(
132 static_cast<icu::SimpleDateFormat
*>(
133 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort
)));
134 // Retrieve the short time format.
135 icu::UnicodeString pattern_unicode
;
136 formatter
->toPattern(pattern_unicode
);
138 // Determine what hour clock type the current locale uses, by checking
139 // "a" (am/pm marker) in the short time format. This is reliable as "a"
140 // is used by all of 12-hour clock formats, but not any of 24-hour clock
141 // formats, as shown below.
143 // % grep -A4 DateTimePatterns third_party/icu/source/data/locales/*.txt |
144 // grep -B1 -- -- |grep -v -- '--' |
145 // perl -nle 'print $1 if /^\S+\s+"(.*)"/' |sort -u
158 // See http://userguide.icu-project.org/formatparse/datetime for details
159 // about the date/time format syntax.
160 if (pattern_unicode
.indexOf('a') == -1) {