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/time/time.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/icu/source/common/unicode/uversion.h"
13 #include "third_party/icu/source/i18n/unicode/calendar.h"
14 #include "third_party/icu/source/i18n/unicode/timezone.h"
19 const Time::Exploded kTestDateTimeExploded
= {
20 2011, 4, 6, 30, // Sat, Apr 30, 2011
21 15, 42, 7, 0 // 15:42:07.000
24 base::string16
GetShortTimeZone() {
25 scoped_ptr
<icu::TimeZone
> zone(icu::TimeZone::createDefault());
26 icu::UnicodeString name
;
27 zone
->getDisplayName(true, icu::TimeZone::SHORT
, name
);
28 return base::string16(name
.getBuffer(), name
.length());
31 TEST(TimeFormattingTest
, TimeFormatTimeOfDayDefault12h
) {
32 // Test for a locale defaulted to 12h clock.
33 // As an instance, we use third_party/icu/source/data/locales/en.txt.
34 i18n::SetICUDefaultLocale("en_US");
36 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
37 string16
clock24h(ASCIIToUTF16("15:42"));
38 string16
clock12h_pm(ASCIIToUTF16("3:42 PM"));
39 string16
clock12h(ASCIIToUTF16("3:42"));
40 string16
clock24h_millis(ASCIIToUTF16("15:42:07.000"));
42 // The default is 12h clock.
43 EXPECT_EQ(clock12h_pm
, TimeFormatTimeOfDay(time
));
44 EXPECT_EQ(clock24h_millis
, TimeFormatTimeOfDayWithMilliseconds(time
));
45 EXPECT_EQ(k12HourClock
, GetHourClockType());
46 // k{Keep,Drop}AmPm should not affect for 24h clock.
48 TimeFormatTimeOfDayWithHourClockType(time
,
52 TimeFormatTimeOfDayWithHourClockType(time
,
55 // k{Keep,Drop}AmPm affects for 12h clock.
56 EXPECT_EQ(clock12h_pm
,
57 TimeFormatTimeOfDayWithHourClockType(time
,
61 TimeFormatTimeOfDayWithHourClockType(time
,
66 TEST(TimeFormattingTest
, TimeFormatTimeOfDayDefault24h
) {
67 // Test for a locale defaulted to 24h clock.
68 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt.
69 i18n::SetICUDefaultLocale("en_GB");
71 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
72 string16
clock24h(ASCIIToUTF16("15:42"));
73 string16
clock12h_pm(ASCIIToUTF16("3:42 pm"));
74 string16
clock12h(ASCIIToUTF16("3:42"));
75 string16
clock24h_millis(ASCIIToUTF16("15:42:07.000"));
77 // The default is 24h clock.
78 EXPECT_EQ(clock24h
, TimeFormatTimeOfDay(time
));
79 EXPECT_EQ(clock24h_millis
, TimeFormatTimeOfDayWithMilliseconds(time
));
80 EXPECT_EQ(k24HourClock
, GetHourClockType());
81 // k{Keep,Drop}AmPm should not affect for 24h clock.
83 TimeFormatTimeOfDayWithHourClockType(time
,
87 TimeFormatTimeOfDayWithHourClockType(time
,
90 // k{Keep,Drop}AmPm affects for 12h clock.
91 EXPECT_EQ(clock12h_pm
,
92 TimeFormatTimeOfDayWithHourClockType(time
,
96 TimeFormatTimeOfDayWithHourClockType(time
,
101 TEST(TimeFormattingTest
, TimeFormatTimeOfDayJP
) {
102 // Test for a locale that uses different mark than "AM" and "PM".
103 // As an instance, we use third_party/icu/source/data/locales/ja.txt.
104 i18n::SetICUDefaultLocale("ja_JP");
106 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
107 string16
clock24h(ASCIIToUTF16("15:42"));
108 string16
clock12h_pm(WideToUTF16(L
"\x5348\x5f8c" L
"3:42"));
109 string16
clock12h(ASCIIToUTF16("3:42"));
111 // The default is 24h clock.
112 EXPECT_EQ(clock24h
, TimeFormatTimeOfDay(time
));
113 EXPECT_EQ(k24HourClock
, GetHourClockType());
114 // k{Keep,Drop}AmPm should not affect for 24h clock.
116 TimeFormatTimeOfDayWithHourClockType(time
,
120 TimeFormatTimeOfDayWithHourClockType(time
,
123 // k{Keep,Drop}AmPm affects for 12h clock.
124 EXPECT_EQ(clock12h_pm
,
125 TimeFormatTimeOfDayWithHourClockType(time
,
129 TimeFormatTimeOfDayWithHourClockType(time
,
134 TEST(TimeFormattingTest
, TimeFormatDateUS
) {
135 // See third_party/icu/source/data/locales/en.txt.
136 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy".
137 i18n::SetICUDefaultLocale("en_US");
139 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
141 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time
));
142 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time
));
144 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"),
145 TimeFormatShortDateAndTime(time
));
146 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(),
147 TimeFormatShortDateAndTimeWithTimeZone(time
));
149 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"),
150 TimeFormatFriendlyDateAndTime(time
));
152 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"),
153 TimeFormatFriendlyDate(time
));
156 TEST(TimeFormattingTest
, TimeFormatDateGB
) {
157 // See third_party/icu/source/data/locales/en_GB.txt.
158 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy".
159 i18n::SetICUDefaultLocale("en_GB");
161 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
163 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time
));
164 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time
));
165 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"),
166 TimeFormatShortDateAndTime(time
));
167 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(),
168 TimeFormatShortDateAndTimeWithTimeZone(time
));
169 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"),
170 TimeFormatFriendlyDateAndTime(time
));
171 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"),
172 TimeFormatFriendlyDate(time
));