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/i18n/rtl.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/test/icu_test_util.h"
11 #include "base/time/time.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/icu/source/common/unicode/uversion.h"
14 #include "third_party/icu/source/i18n/unicode/calendar.h"
15 #include "third_party/icu/source/i18n/unicode/timezone.h"
16 #include "third_party/icu/source/i18n/unicode/tzfmt.h"
21 const Time::Exploded kTestDateTimeExploded
= {
22 2011, 4, 6, 30, // Sat, Apr 30, 2011
23 15, 42, 7, 0 // 15:42:07.000
26 // Returns difference between the local time and GMT formatted as string.
27 // This function gets |time| because the difference depends on time,
28 // see https://en.wikipedia.org/wiki/Daylight_saving_time for details.
29 base::string16
GetShortTimeZone(const Time
& time
) {
30 UErrorCode status
= U_ZERO_ERROR
;
31 scoped_ptr
<icu::TimeZone
> zone(icu::TimeZone::createDefault());
32 scoped_ptr
<icu::TimeZoneFormat
> zone_formatter(
33 icu::TimeZoneFormat::createInstance(icu::Locale::getDefault(), status
));
34 EXPECT_TRUE(U_SUCCESS(status
));
35 icu::UnicodeString name
;
36 zone_formatter
->format(UTZFMT_STYLE_SPECIFIC_SHORT
, *zone
,
37 static_cast<UDate
>(time
.ToDoubleT() * 1000),
39 return base::string16(name
.getBuffer(), name
.length());
42 TEST(TimeFormattingTest
, TimeFormatTimeOfDayDefault12h
) {
43 // Test for a locale defaulted to 12h clock.
44 // As an instance, we use third_party/icu/source/data/locales/en.txt.
45 test::ScopedRestoreICUDefaultLocale restore_locale
;
46 i18n::SetICUDefaultLocale("en_US");
48 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
49 string16
clock24h(ASCIIToUTF16("15:42"));
50 string16
clock12h_pm(ASCIIToUTF16("3:42 PM"));
51 string16
clock12h(ASCIIToUTF16("3:42"));
52 string16
clock24h_millis(ASCIIToUTF16("15:42:07.000"));
54 // The default is 12h clock.
55 EXPECT_EQ(clock12h_pm
, TimeFormatTimeOfDay(time
));
56 EXPECT_EQ(clock24h_millis
, TimeFormatTimeOfDayWithMilliseconds(time
));
57 EXPECT_EQ(k12HourClock
, GetHourClockType());
58 // k{Keep,Drop}AmPm should not affect for 24h clock.
60 TimeFormatTimeOfDayWithHourClockType(time
,
64 TimeFormatTimeOfDayWithHourClockType(time
,
67 // k{Keep,Drop}AmPm affects for 12h clock.
68 EXPECT_EQ(clock12h_pm
,
69 TimeFormatTimeOfDayWithHourClockType(time
,
73 TimeFormatTimeOfDayWithHourClockType(time
,
78 TEST(TimeFormattingTest
, TimeFormatTimeOfDayDefault24h
) {
79 // Test for a locale defaulted to 24h clock.
80 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt.
81 test::ScopedRestoreICUDefaultLocale restore_locale
;
82 i18n::SetICUDefaultLocale("en_GB");
84 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
85 string16
clock24h(ASCIIToUTF16("15:42"));
86 string16
clock12h_pm(ASCIIToUTF16("3:42 pm"));
87 string16
clock12h(ASCIIToUTF16("3:42"));
88 string16
clock24h_millis(ASCIIToUTF16("15:42:07.000"));
90 // The default is 24h clock.
91 EXPECT_EQ(clock24h
, TimeFormatTimeOfDay(time
));
92 EXPECT_EQ(clock24h_millis
, TimeFormatTimeOfDayWithMilliseconds(time
));
93 EXPECT_EQ(k24HourClock
, GetHourClockType());
94 // k{Keep,Drop}AmPm should not affect for 24h clock.
96 TimeFormatTimeOfDayWithHourClockType(time
,
100 TimeFormatTimeOfDayWithHourClockType(time
,
103 // k{Keep,Drop}AmPm affects for 12h clock.
104 EXPECT_EQ(clock12h_pm
,
105 TimeFormatTimeOfDayWithHourClockType(time
,
109 TimeFormatTimeOfDayWithHourClockType(time
,
114 TEST(TimeFormattingTest
, TimeFormatTimeOfDayJP
) {
115 // Test for a locale that uses different mark than "AM" and "PM".
116 // As an instance, we use third_party/icu/source/data/locales/ja.txt.
117 test::ScopedRestoreICUDefaultLocale restore_locale
;
118 i18n::SetICUDefaultLocale("ja_JP");
120 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
121 string16
clock24h(ASCIIToUTF16("15:42"));
122 string16
clock12h_pm(WideToUTF16(L
"\x5348\x5f8c" L
"3:42"));
123 string16
clock12h(ASCIIToUTF16("3:42"));
125 // The default is 24h clock.
126 EXPECT_EQ(clock24h
, TimeFormatTimeOfDay(time
));
127 EXPECT_EQ(k24HourClock
, GetHourClockType());
128 // k{Keep,Drop}AmPm should not affect for 24h clock.
130 TimeFormatTimeOfDayWithHourClockType(time
,
134 TimeFormatTimeOfDayWithHourClockType(time
,
137 // k{Keep,Drop}AmPm affects for 12h clock.
138 EXPECT_EQ(clock12h_pm
,
139 TimeFormatTimeOfDayWithHourClockType(time
,
143 TimeFormatTimeOfDayWithHourClockType(time
,
148 TEST(TimeFormattingTest
, TimeFormatDateUS
) {
149 // See third_party/icu/source/data/locales/en.txt.
150 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy".
151 test::ScopedRestoreICUDefaultLocale restore_locale
;
152 i18n::SetICUDefaultLocale("en_US");
154 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
156 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time
));
157 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time
));
159 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"),
160 TimeFormatShortDateAndTime(time
));
161 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(time
),
162 TimeFormatShortDateAndTimeWithTimeZone(time
));
164 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"),
165 TimeFormatFriendlyDateAndTime(time
));
167 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"),
168 TimeFormatFriendlyDate(time
));
171 TEST(TimeFormattingTest
, TimeFormatDateGB
) {
172 // See third_party/icu/source/data/locales/en_GB.txt.
173 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy".
174 test::ScopedRestoreICUDefaultLocale restore_locale
;
175 i18n::SetICUDefaultLocale("en_GB");
177 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
179 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time
));
180 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time
));
181 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"),
182 TimeFormatShortDateAndTime(time
));
183 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(time
),
184 TimeFormatShortDateAndTimeWithTimeZone(time
));
185 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"),
186 TimeFormatFriendlyDateAndTime(time
));
187 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"),
188 TimeFormatFriendlyDate(time
));