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 // Test for a locale that uses different mark than "AM" and "PM".
191 // As an instance, we use third_party/icu/source/data/locales/de.txt.
192 test::ScopedRestoreICUDefaultLocale restore_locale;
193 i18n::SetICUDefaultLocale("de");
194 ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
197 EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
198 string16 clock24h(ASCIIToUTF16("15:42"));
199 string16 clock12h_pm(UTF8ToUTF16("3:42 nachm."));
200 string16 clock12h(ASCIIToUTF16("3:42"));
202 // The default is 24h clock.
203 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time));
204 EXPECT_EQ(k24HourClock, GetHourClockType());
205 // k{Keep,Drop}AmPm should not affect for 24h clock.
207 TimeFormatTimeOfDayWithHourClockType(time,
211 TimeFormatTimeOfDayWithHourClockType(time,
214 // k{Keep,Drop}AmPm affects for 12h clock.
215 EXPECT_EQ(clock12h_pm,
216 TimeFormatTimeOfDayWithHourClockType(time,
220 TimeFormatTimeOfDayWithHourClockType(time,
225 TEST(TimeFormattingTest, TimeFormatDateUS) {
226 // See third_party/icu/source/data/locales/en.txt.
227 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy".
228 test::ScopedRestoreICUDefaultLocale restore_locale;
229 i18n::SetICUDefaultLocale("en_US");
230 ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
233 EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
235 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time));
236 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time));
238 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"),
239 TimeFormatShortDateAndTime(time));
240 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(time),
241 TimeFormatShortDateAndTimeWithTimeZone(time));
243 EXPECT_EQ(ASCIIToUTF16("April 2011"), TimeFormatMonthAndYear(time));
245 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"),
246 TimeFormatFriendlyDateAndTime(time));
248 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"),
249 TimeFormatFriendlyDate(time));
252 TEST(TimeFormattingTest, TimeFormatDateGB) {
253 // See third_party/icu/source/data/locales/en_GB.txt.
254 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy".
255 test::ScopedRestoreICUDefaultLocale restore_locale;
256 i18n::SetICUDefaultLocale("en_GB");
257 ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
260 EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
262 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time));
263 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time));
264 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"),
265 TimeFormatShortDateAndTime(time));
266 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(time),
267 TimeFormatShortDateAndTimeWithTimeZone(time));
268 EXPECT_EQ(ASCIIToUTF16("April 2011"), TimeFormatMonthAndYear(time));
269 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"),
270 TimeFormatFriendlyDateAndTime(time));
271 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"),
272 TimeFormatFriendlyDate(time));
275 TEST(TimeFormattingTest, TimeFormatWithPattern) {
276 test::ScopedRestoreICUDefaultLocale restore_locale;
277 ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
280 EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
282 i18n::SetICUDefaultLocale("en_US");
283 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatWithPattern(time, "yMMMd"));
284 EXPECT_EQ(ASCIIToUTF16("April 30, 3:42:07 PM"),
285 TimeFormatWithPattern(time, "MMMMdjmmss"));
287 i18n::SetICUDefaultLocale("en_GB");
288 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatWithPattern(time, "yMMMd"));
289 EXPECT_EQ(ASCIIToUTF16("30 April, 15:42:07"),
290 TimeFormatWithPattern(time, "MMMMdjmmss"));
292 i18n::SetICUDefaultLocale("ja_JP");
293 EXPECT_EQ(UTF8ToUTF16(u8"2011年4月30日"),
294 TimeFormatWithPattern(time, "yMMMd"));
295 EXPECT_EQ(UTF8ToUTF16(u8"4月30日 15:42:07"),
296 TimeFormatWithPattern(time, "MMMMdjmmss"));
299 TEST(TimeFormattingTest, TimeDurationFormat) {
300 test::ScopedRestoreICUDefaultLocale restore_locale;
301 TimeDelta delta = TimeDelta::FromMinutes(15 * 60 + 42);
304 i18n::SetICUDefaultLocale("en_US");
305 EXPECT_EQ(ASCIIToUTF16("15 hours, 42 minutes"),
306 TimeDurationFormatString(delta, DURATION_WIDTH_WIDE));
307 EXPECT_EQ(ASCIIToUTF16("15 hr, 42 min"),
308 TimeDurationFormatString(delta, DURATION_WIDTH_SHORT));
309 EXPECT_EQ(ASCIIToUTF16("15h 42m"),
310 TimeDurationFormatString(delta, DURATION_WIDTH_NARROW));
311 EXPECT_EQ(ASCIIToUTF16("15:42"),
312 TimeDurationFormatString(delta, DURATION_WIDTH_NUMERIC));
314 // Danish, with Latin alphabet but different abbreviations and punctuation.
315 i18n::SetICUDefaultLocale("da");
316 EXPECT_EQ(ASCIIToUTF16("15 timer og 42 minutter"),
317 TimeDurationFormatString(delta, DURATION_WIDTH_WIDE));
318 EXPECT_EQ(ASCIIToUTF16("15 t og 42 min."),
319 TimeDurationFormatString(delta, DURATION_WIDTH_SHORT));
320 EXPECT_EQ(ASCIIToUTF16("15 t og 42 min"),
321 TimeDurationFormatString(delta, DURATION_WIDTH_NARROW));
322 EXPECT_EQ(ASCIIToUTF16("15.42"),
323 TimeDurationFormatString(delta, DURATION_WIDTH_NUMERIC));
325 // Persian, with non-Arabic numbers.
326 i18n::SetICUDefaultLocale("fa");
327 string16 fa_wide = UTF8ToUTF16(
328 u8"\u06f1\u06f5 \u0633\u0627\u0639\u062a \u0648 \u06f4\u06f2 \u062f\u0642"
329 u8"\u06cc\u0642\u0647");
330 string16 fa_short = UTF8ToUTF16(
331 u8"\u06f1\u06f5 \u0633\u0627\u0639\u062a\u060c\u200f \u06f4\u06f2 \u062f"
332 u8"\u0642\u06cc\u0642\u0647");
333 string16 fa_narrow = UTF8ToUTF16(
334 u8"\u06f1\u06f5 \u0633\u0627\u0639\u062a \u06f4\u06f2 \u062f\u0642\u06cc"
336 string16 fa_numeric = UTF8ToUTF16(u8"\u06f1\u06f5:\u06f4\u06f2");
337 EXPECT_EQ(fa_wide, TimeDurationFormatString(delta, DURATION_WIDTH_WIDE));
338 EXPECT_EQ(fa_short, TimeDurationFormatString(delta, DURATION_WIDTH_SHORT));
339 EXPECT_EQ(fa_narrow, TimeDurationFormatString(delta, DURATION_WIDTH_NARROW));
340 EXPECT_EQ(fa_numeric,
341 TimeDurationFormatString(delta, DURATION_WIDTH_NUMERIC));
344 TEST(TimeFormattingTest, TimeDurationFormatWithSeconds) {
345 test::ScopedRestoreICUDefaultLocale restore_locale;
348 i18n::SetICUDefaultLocale("en_US");
350 // Test different formats.
351 TimeDelta delta = TimeDelta::FromSeconds(15 * 3600 + 42 * 60 + 30);
352 EXPECT_EQ(ASCIIToUTF16("15 hours, 42 minutes, 30 seconds"),
353 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_WIDE));
354 EXPECT_EQ(ASCIIToUTF16("15 hr, 42 min, 30 sec"),
355 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_SHORT));
356 EXPECT_EQ(ASCIIToUTF16("15h 42m 30s"),
357 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NARROW));
358 EXPECT_EQ(ASCIIToUTF16("15:42:30"),
359 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NUMERIC));
361 // Test edge case when hour >= 100.
362 delta = TimeDelta::FromSeconds(125 * 3600 + 42 * 60 + 30);
363 EXPECT_EQ(ASCIIToUTF16("125 hours, 42 minutes, 30 seconds"),
364 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_WIDE));
365 EXPECT_EQ(ASCIIToUTF16("125 hr, 42 min, 30 sec"),
366 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_SHORT));
367 EXPECT_EQ(ASCIIToUTF16("125h 42m 30s"),
368 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NARROW));
370 // Test edge case when minute = 0.
371 delta = TimeDelta::FromSeconds(15 * 3600 + 0 * 60 + 30);
372 EXPECT_EQ(ASCIIToUTF16("15 hours, 0 minutes, 30 seconds"),
373 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_WIDE));
374 EXPECT_EQ(ASCIIToUTF16("15 hr, 0 min, 30 sec"),
375 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_SHORT));
376 EXPECT_EQ(ASCIIToUTF16("15h 0m 30s"),
377 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NARROW));
378 EXPECT_EQ(ASCIIToUTF16("15:00:30"),
379 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NUMERIC));
381 // Test edge case when second = 0.
382 delta = TimeDelta::FromSeconds(15 * 3600 + 42 * 60 + 0);
383 EXPECT_EQ(ASCIIToUTF16("15 hours, 42 minutes, 0 seconds"),
384 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_WIDE));
385 EXPECT_EQ(ASCIIToUTF16("15 hr, 42 min, 0 sec"),
386 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_SHORT));
387 EXPECT_EQ(ASCIIToUTF16("15h 42m 0s"),
388 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NARROW));
389 EXPECT_EQ(ASCIIToUTF16("15:42:00"),
390 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NUMERIC));
393 TEST(TimeFormattingTest, TimeIntervalFormat) {
394 test::ScopedRestoreICUDefaultLocale restore_locale;
395 i18n::SetICUDefaultLocale("en_US");
396 ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
398 const Time::Exploded kTestIntervalEndTimeExploded = {
399 2011, 5, 6, 28, // Sat, May 28, 2012
400 22, 42, 7, 0 // 22:42:07.000
404 EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &begin_time));
406 EXPECT_TRUE(Time::FromUTCExploded(kTestIntervalEndTimeExploded, &end_time));
409 UTF8ToUTF16(u8"Saturday, April 30 – Saturday, May 28"),
410 DateIntervalFormat(begin_time, end_time, DATE_FORMAT_MONTH_WEEKDAY_DAY));
412 const Time::Exploded kTestIntervalBeginTimeExploded = {
413 2011, 5, 1, 16, // Mon, May 16, 2012
414 22, 42, 7, 0 // 22:42:07.000
417 Time::FromUTCExploded(kTestIntervalBeginTimeExploded, &begin_time));
419 UTF8ToUTF16(u8"Monday, May 16 – Saturday, May 28"),
420 DateIntervalFormat(begin_time, end_time, DATE_FORMAT_MONTH_WEEKDAY_DAY));
422 i18n::SetICUDefaultLocale("en_GB");
424 UTF8ToUTF16(u8"Monday 16 – Saturday 28 May"),
425 DateIntervalFormat(begin_time, end_time, DATE_FORMAT_MONTH_WEEKDAY_DAY));
427 i18n::SetICUDefaultLocale("ja");
429 UTF8ToUTF16(u8"5月16日(月曜日)~28日(土曜日)"),
430 DateIntervalFormat(begin_time, end_time, DATE_FORMAT_MONTH_WEEKDAY_DAY));