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"
9 #include "base/i18n/rtl.h"
10 #include "base/i18n/unicodestring.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/test/icu_test_util.h"
13 #include "base/time/time.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/icu/source/common/unicode/uversion.h"
16 #include "third_party/icu/source/i18n/unicode/calendar.h"
17 #include "third_party/icu/source/i18n/unicode/timezone.h"
18 #include "third_party/icu/source/i18n/unicode/tzfmt.h"
23 const Time::Exploded kTestDateTimeExploded = {
24 2011, 4, 6, 30, // Sat, Apr 30, 2011
25 22, 42, 7, 0 // 22:42:07.000 in UTC = 15:42:07 in US PDT.
28 // Returns difference between the local time and GMT formatted as string.
29 // This function gets |time| because the difference depends on time,
30 // see https://en.wikipedia.org/wiki/Daylight_saving_time for details.
31 string16 GetShortTimeZone(const Time& time) {
32 UErrorCode status = U_ZERO_ERROR;
33 std::unique_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
34 std::unique_ptr<icu::TimeZoneFormat> zone_formatter(
35 icu::TimeZoneFormat::createInstance(icu::Locale::getDefault(), status));
36 EXPECT_TRUE(U_SUCCESS(status));
37 icu::UnicodeString name;
38 zone_formatter->format(UTZFMT_STYLE_SPECIFIC_SHORT, *zone,
39 static_cast<UDate>(time.ToDoubleT() * 1000),
41 return i18n::UnicodeStringToString16(name);
44 // Calls TimeDurationFormat() with |delta| and |width| and returns the resulting
45 // string. On failure, adds a failed expectation and returns an empty string.
46 string16 TimeDurationFormatString(const TimeDelta& delta,
47 DurationFormatWidth width) {
49 EXPECT_TRUE(TimeDurationFormat(delta, width, &str))
50 << "Failed to format " << delta.ToInternalValue() << " with width "
55 // Calls TimeDurationFormatWithSeconds() with |delta| and |width| and returns
56 // the resulting string. On failure, adds a failed expectation and returns an
58 string16 TimeDurationFormatWithSecondsString(const TimeDelta& delta,
59 DurationFormatWidth width) {
61 EXPECT_TRUE(TimeDurationFormatWithSeconds(delta, width, &str))
62 << "Failed to format " << delta.ToInternalValue() << " with width "
67 class ScopedRestoreDefaultTimezone {
69 ScopedRestoreDefaultTimezone(const char* zoneid) {
70 original_zone_.reset(icu::TimeZone::createDefault());
71 icu::TimeZone::adoptDefault(icu::TimeZone::createTimeZone(zoneid));
73 ~ScopedRestoreDefaultTimezone() {
74 icu::TimeZone::adoptDefault(original_zone_.release());
77 ScopedRestoreDefaultTimezone(const ScopedRestoreDefaultTimezone&) = delete;
78 ScopedRestoreDefaultTimezone& operator=(const ScopedRestoreDefaultTimezone&) =
82 std::unique_ptr<icu::TimeZone> original_zone_;
85 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) {
86 // Test for a locale defaulted to 12h clock.
87 // As an instance, we use third_party/icu/source/data/locales/en.txt.
88 test::ScopedRestoreICUDefaultLocale restore_locale;
89 i18n::SetICUDefaultLocale("en_US");
90 ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
93 EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
94 string16 clock24h(ASCIIToUTF16("15:42"));
95 string16 clock12h_pm(ASCIIToUTF16("3:42 PM"));
96 string16 clock12h(ASCIIToUTF16("3:42"));
97 string16 clock24h_millis(ASCIIToUTF16("15:42:07.000"));
99 // The default is 12h clock.
100 EXPECT_EQ(clock12h_pm, TimeFormatTimeOfDay(time));
101 EXPECT_EQ(clock24h_millis, TimeFormatTimeOfDayWithMilliseconds(time));
102 EXPECT_EQ(k12HourClock, GetHourClockType());
103 // k{Keep,Drop}AmPm should not affect for 24h clock.
105 TimeFormatTimeOfDayWithHourClockType(time,
109 TimeFormatTimeOfDayWithHourClockType(time,
112 // k{Keep,Drop}AmPm affects for 12h clock.
113 EXPECT_EQ(clock12h_pm,
114 TimeFormatTimeOfDayWithHourClockType(time,
118 TimeFormatTimeOfDayWithHourClockType(time,
123 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) {
124 // Test for a locale defaulted to 24h clock.
125 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt.
126 test::ScopedRestoreICUDefaultLocale restore_locale;
127 i18n::SetICUDefaultLocale("en_GB");
128 ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
131 EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
132 string16 clock24h(ASCIIToUTF16("15:42"));
133 string16 clock12h_pm(ASCIIToUTF16("3:42 pm"));
134 string16 clock12h(ASCIIToUTF16("3:42"));
135 string16 clock24h_millis(ASCIIToUTF16("15:42:07.000"));
137 // The default is 24h clock.
138 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time));
139 EXPECT_EQ(clock24h_millis, TimeFormatTimeOfDayWithMilliseconds(time));
140 EXPECT_EQ(k24HourClock, GetHourClockType());
141 // k{Keep,Drop}AmPm should not affect for 24h clock.
143 TimeFormatTimeOfDayWithHourClockType(time,
147 TimeFormatTimeOfDayWithHourClockType(time,
150 // k{Keep,Drop}AmPm affects for 12h clock.
151 EXPECT_EQ(clock12h_pm,
152 TimeFormatTimeOfDayWithHourClockType(time,
156 TimeFormatTimeOfDayWithHourClockType(time,
161 TEST(TimeFormattingTest, TimeFormatTimeOfDayJP) {
162 // Test for a locale that uses different mark than "AM" and "PM".
163 // As an instance, we use third_party/icu/source/data/locales/ja.txt.
164 test::ScopedRestoreICUDefaultLocale restore_locale;
165 i18n::SetICUDefaultLocale("ja_JP");
166 ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
169 EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
170 string16 clock24h(ASCIIToUTF16("15:42"));
171 string16 clock12h_pm(UTF8ToUTF16(u8"午後3:42"));
172 string16 clock12h(ASCIIToUTF16("3:42"));
174 // The default is 24h clock.
175 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time));
176 EXPECT_EQ(k24HourClock, GetHourClockType());
177 // k{Keep,Drop}AmPm should not affect for 24h clock.
178 EXPECT_EQ(clock24h, TimeFormatTimeOfDayWithHourClockType(time, k24HourClock,
180 EXPECT_EQ(clock24h, TimeFormatTimeOfDayWithHourClockType(time, k24HourClock,
182 // k{Keep,Drop}AmPm affects for 12h clock.
183 EXPECT_EQ(clock12h_pm, TimeFormatTimeOfDayWithHourClockType(
184 time, k12HourClock, kKeepAmPm));
185 EXPECT_EQ(clock12h, TimeFormatTimeOfDayWithHourClockType(time, k12HourClock,
189 TEST(TimeFormattingTest, TimeFormatTimeOfDayDE) {
190 // German uses 24h by default, but uses 'AM', 'PM' for 12h format.
191 test::ScopedRestoreICUDefaultLocale restore_locale;
192 i18n::SetICUDefaultLocale("de");
193 ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
196 EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
197 string16 clock24h(ASCIIToUTF16("15:42"));
198 string16 clock12h_pm(UTF8ToUTF16("3:42 PM"));
199 string16 clock12h(ASCIIToUTF16("3:42"));
201 // The default is 24h clock.
202 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time));
203 EXPECT_EQ(k24HourClock, GetHourClockType());
204 // k{Keep,Drop}AmPm should not affect for 24h clock.
206 TimeFormatTimeOfDayWithHourClockType(time,
210 TimeFormatTimeOfDayWithHourClockType(time,
213 // k{Keep,Drop}AmPm affects for 12h clock.
214 EXPECT_EQ(clock12h_pm,
215 TimeFormatTimeOfDayWithHourClockType(time,
219 TimeFormatTimeOfDayWithHourClockType(time,
224 TEST(TimeFormattingTest, TimeFormatDateUS) {
225 // See third_party/icu/source/data/locales/en.txt.
226 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy".
227 test::ScopedRestoreICUDefaultLocale restore_locale;
228 i18n::SetICUDefaultLocale("en_US");
229 ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
232 EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
234 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time));
235 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time));
237 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"),
238 TimeFormatShortDateAndTime(time));
239 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(time),
240 TimeFormatShortDateAndTimeWithTimeZone(time));
242 EXPECT_EQ(ASCIIToUTF16("April 2011"), TimeFormatMonthAndYear(time));
244 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"),
245 TimeFormatFriendlyDateAndTime(time));
247 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"),
248 TimeFormatFriendlyDate(time));
251 TEST(TimeFormattingTest, TimeFormatDateGB) {
252 // See third_party/icu/source/data/locales/en_GB.txt.
253 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy".
254 test::ScopedRestoreICUDefaultLocale restore_locale;
255 i18n::SetICUDefaultLocale("en_GB");
256 ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
259 EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
261 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time));
262 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time));
263 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"),
264 TimeFormatShortDateAndTime(time));
265 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(time),
266 TimeFormatShortDateAndTimeWithTimeZone(time));
267 EXPECT_EQ(ASCIIToUTF16("April 2011"), TimeFormatMonthAndYear(time));
268 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"),
269 TimeFormatFriendlyDateAndTime(time));
270 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"),
271 TimeFormatFriendlyDate(time));
274 TEST(TimeFormattingTest, TimeFormatWithPattern) {
275 test::ScopedRestoreICUDefaultLocale restore_locale;
276 ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
279 EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
281 i18n::SetICUDefaultLocale("en_US");
282 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatWithPattern(time, "yMMMd"));
283 EXPECT_EQ(ASCIIToUTF16("April 30, 3:42:07 PM"),
284 TimeFormatWithPattern(time, "MMMMdjmmss"));
286 i18n::SetICUDefaultLocale("en_GB");
287 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatWithPattern(time, "yMMMd"));
288 EXPECT_EQ(ASCIIToUTF16("30 April, 15:42:07"),
289 TimeFormatWithPattern(time, "MMMMdjmmss"));
291 i18n::SetICUDefaultLocale("ja_JP");
292 EXPECT_EQ(UTF8ToUTF16(u8"2011年4月30日"),
293 TimeFormatWithPattern(time, "yMMMd"));
294 EXPECT_EQ(UTF8ToUTF16(u8"4月30日 15:42:07"),
295 TimeFormatWithPattern(time, "MMMMdjmmss"));
298 TEST(TimeFormattingTest, TimeDurationFormat) {
299 test::ScopedRestoreICUDefaultLocale restore_locale;
300 TimeDelta delta = TimeDelta::FromMinutes(15 * 60 + 42);
303 i18n::SetICUDefaultLocale("en_US");
304 EXPECT_EQ(ASCIIToUTF16("15 hours, 42 minutes"),
305 TimeDurationFormatString(delta, DURATION_WIDTH_WIDE));
306 EXPECT_EQ(ASCIIToUTF16("15 hr, 42 min"),
307 TimeDurationFormatString(delta, DURATION_WIDTH_SHORT));
308 EXPECT_EQ(ASCIIToUTF16("15h 42m"),
309 TimeDurationFormatString(delta, DURATION_WIDTH_NARROW));
310 EXPECT_EQ(ASCIIToUTF16("15:42"),
311 TimeDurationFormatString(delta, DURATION_WIDTH_NUMERIC));
313 // Danish, with Latin alphabet but different abbreviations and punctuation.
314 i18n::SetICUDefaultLocale("da");
315 EXPECT_EQ(ASCIIToUTF16("15 timer og 42 minutter"),
316 TimeDurationFormatString(delta, DURATION_WIDTH_WIDE));
317 EXPECT_EQ(ASCIIToUTF16("15 t og 42 min."),
318 TimeDurationFormatString(delta, DURATION_WIDTH_SHORT));
319 EXPECT_EQ(ASCIIToUTF16("15 t og 42 min"),
320 TimeDurationFormatString(delta, DURATION_WIDTH_NARROW));
321 EXPECT_EQ(ASCIIToUTF16("15.42"),
322 TimeDurationFormatString(delta, DURATION_WIDTH_NUMERIC));
324 // Persian, with non-Arabic numbers.
325 i18n::SetICUDefaultLocale("fa");
326 string16 fa_wide = UTF8ToUTF16(
327 u8"\u06f1\u06f5 \u0633\u0627\u0639\u062a \u0648 \u06f4\u06f2 \u062f\u0642"
328 u8"\u06cc\u0642\u0647");
329 string16 fa_short = UTF8ToUTF16(
330 u8"\u06f1\u06f5 \u0633\u0627\u0639\u062a\u060c\u200f \u06f4\u06f2 \u062f"
331 u8"\u0642\u06cc\u0642\u0647");
332 string16 fa_narrow = UTF8ToUTF16(
333 u8"\u06f1\u06f5 \u0633\u0627\u0639\u062a \u06f4\u06f2 \u062f\u0642\u06cc"
335 string16 fa_numeric = UTF8ToUTF16(u8"\u06f1\u06f5:\u06f4\u06f2");
336 EXPECT_EQ(fa_wide, TimeDurationFormatString(delta, DURATION_WIDTH_WIDE));
337 EXPECT_EQ(fa_short, TimeDurationFormatString(delta, DURATION_WIDTH_SHORT));
338 EXPECT_EQ(fa_narrow, TimeDurationFormatString(delta, DURATION_WIDTH_NARROW));
339 EXPECT_EQ(fa_numeric,
340 TimeDurationFormatString(delta, DURATION_WIDTH_NUMERIC));
343 TEST(TimeFormattingTest, TimeDurationFormatWithSeconds) {
344 test::ScopedRestoreICUDefaultLocale restore_locale;
347 i18n::SetICUDefaultLocale("en_US");
349 // Test different formats.
350 TimeDelta delta = TimeDelta::FromSeconds(15 * 3600 + 42 * 60 + 30);
351 EXPECT_EQ(ASCIIToUTF16("15 hours, 42 minutes, 30 seconds"),
352 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_WIDE));
353 EXPECT_EQ(ASCIIToUTF16("15 hr, 42 min, 30 sec"),
354 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_SHORT));
355 EXPECT_EQ(ASCIIToUTF16("15h 42m 30s"),
356 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NARROW));
357 EXPECT_EQ(ASCIIToUTF16("15:42:30"),
358 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NUMERIC));
360 // Test edge case when hour >= 100.
361 delta = TimeDelta::FromSeconds(125 * 3600 + 42 * 60 + 30);
362 EXPECT_EQ(ASCIIToUTF16("125 hours, 42 minutes, 30 seconds"),
363 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_WIDE));
364 EXPECT_EQ(ASCIIToUTF16("125 hr, 42 min, 30 sec"),
365 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_SHORT));
366 EXPECT_EQ(ASCIIToUTF16("125h 42m 30s"),
367 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NARROW));
369 // Test edge case when minute = 0.
370 delta = TimeDelta::FromSeconds(15 * 3600 + 0 * 60 + 30);
371 EXPECT_EQ(ASCIIToUTF16("15 hours, 0 minutes, 30 seconds"),
372 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_WIDE));
373 EXPECT_EQ(ASCIIToUTF16("15 hr, 0 min, 30 sec"),
374 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_SHORT));
375 EXPECT_EQ(ASCIIToUTF16("15h 0m 30s"),
376 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NARROW));
377 EXPECT_EQ(ASCIIToUTF16("15:00:30"),
378 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NUMERIC));
380 // Test edge case when second = 0.
381 delta = TimeDelta::FromSeconds(15 * 3600 + 42 * 60 + 0);
382 EXPECT_EQ(ASCIIToUTF16("15 hours, 42 minutes, 0 seconds"),
383 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_WIDE));
384 EXPECT_EQ(ASCIIToUTF16("15 hr, 42 min, 0 sec"),
385 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_SHORT));
386 EXPECT_EQ(ASCIIToUTF16("15h 42m 0s"),
387 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NARROW));
388 EXPECT_EQ(ASCIIToUTF16("15:42:00"),
389 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NUMERIC));
392 TEST(TimeFormattingTest, TimeIntervalFormat) {
393 test::ScopedRestoreICUDefaultLocale restore_locale;
394 i18n::SetICUDefaultLocale("en_US");
395 ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
397 const Time::Exploded kTestIntervalEndTimeExploded = {
398 2011, 5, 6, 28, // Sat, May 28, 2012
399 22, 42, 7, 0 // 22:42:07.000
403 EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &begin_time));
405 EXPECT_TRUE(Time::FromUTCExploded(kTestIntervalEndTimeExploded, &end_time));
408 UTF8ToUTF16(u8"Saturday, April 30 – Saturday, May 28"),
409 DateIntervalFormat(begin_time, end_time, DATE_FORMAT_MONTH_WEEKDAY_DAY));
411 const Time::Exploded kTestIntervalBeginTimeExploded = {
412 2011, 5, 1, 16, // Mon, May 16, 2012
413 22, 42, 7, 0 // 22:42:07.000
416 Time::FromUTCExploded(kTestIntervalBeginTimeExploded, &begin_time));
418 UTF8ToUTF16(u8"Monday, May 16 – Saturday, May 28"),
419 DateIntervalFormat(begin_time, end_time, DATE_FORMAT_MONTH_WEEKDAY_DAY));
421 i18n::SetICUDefaultLocale("en_GB");
423 UTF8ToUTF16(u8"Monday 16 – Saturday 28 May"),
424 DateIntervalFormat(begin_time, end_time, DATE_FORMAT_MONTH_WEEKDAY_DAY));
426 i18n::SetICUDefaultLocale("ja");
428 UTF8ToUTF16(u8"5月16日(月曜日)~28日(土曜日)"),
429 DateIntervalFormat(begin_time, end_time, DATE_FORMAT_MONTH_WEEKDAY_DAY));