Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: base/i18n/time_formatting.cc

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/i18n/time_formatting.h ('k') | base/i18n/time_formatting_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/i18n/time_formatting.h"
6
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"
14
15 namespace base {
16 namespace {
17
18 string16 TimeFormat(const icu::DateFormat* formatter,
19 const Time& time) {
20 DCHECK(formatter);
21 icu::UnicodeString date_string;
22
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()));
26 }
27
28 string16 TimeFormatWithoutAmPm(const icu::DateFormat* formatter,
29 const Time& time) {
30 DCHECK(formatter);
31 icu::UnicodeString time_string;
32
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());
43 }
44 return string16(time_string.getBuffer(),
45 static_cast<size_t>(time_string.length()));
46 }
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
68 } // namespace
69
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);
76 }
77
78 string16 TimeFormatTimeOfDayWithMilliseconds(const Time& time) {
79 icu::SimpleDateFormat formatter = CreateSimpleDateFormatter("HmsSSS");
80 return TimeFormatWithoutAmPm(&formatter, time);
81 }
82
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);
91 }
92
93 const char* base_pattern = (type == k12HourClock ? "ahm" : "Hm");
94 icu::SimpleDateFormat formatter = CreateSimpleDateFormatter(base_pattern);
95
96 if (ampm == kKeepAmPm) {
97 return TimeFormat(&formatter, time);
98 } else {
99 return TimeFormatWithoutAmPm(&formatter, time);
100 }
101 }
102
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);
107 }
108
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);
113 }
114
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);
119 }
120
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);
125 }
126
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);
131 }
132
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);
137 }
138
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);
149
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.
154 //
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
158 //
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
169 //
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;
176 }
177 }
178
179 } // namespace base
OLDNEW
« no previous file with comments | « base/i18n/time_formatting.h ('k') | base/i18n/time_formatting_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698