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"));
41 // The default is 12h clock.
42 EXPECT_EQ(clock12h_pm
, TimeFormatTimeOfDay(time
));
43 EXPECT_EQ(k12HourClock
, GetHourClockType());
44 // k{Keep,Drop}AmPm should not affect for 24h clock.
46 TimeFormatTimeOfDayWithHourClockType(time
,
50 TimeFormatTimeOfDayWithHourClockType(time
,
53 // k{Keep,Drop}AmPm affects for 12h clock.
54 EXPECT_EQ(clock12h_pm
,
55 TimeFormatTimeOfDayWithHourClockType(time
,
59 TimeFormatTimeOfDayWithHourClockType(time
,
64 TEST(TimeFormattingTest
, TimeFormatTimeOfDayDefault24h
) {
65 // Test for a locale defaulted to 24h clock.
66 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt.
67 i18n::SetICUDefaultLocale("en_GB");
69 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
70 string16
clock24h(ASCIIToUTF16("15:42"));
71 string16
clock12h_pm(ASCIIToUTF16("3:42 pm"));
72 string16
clock12h(ASCIIToUTF16("3:42"));
74 // The default is 24h clock.
75 EXPECT_EQ(clock24h
, TimeFormatTimeOfDay(time
));
76 EXPECT_EQ(k24HourClock
, GetHourClockType());
77 // k{Keep,Drop}AmPm should not affect for 24h clock.
79 TimeFormatTimeOfDayWithHourClockType(time
,
83 TimeFormatTimeOfDayWithHourClockType(time
,
86 // k{Keep,Drop}AmPm affects for 12h clock.
87 EXPECT_EQ(clock12h_pm
,
88 TimeFormatTimeOfDayWithHourClockType(time
,
92 TimeFormatTimeOfDayWithHourClockType(time
,
97 TEST(TimeFormattingTest
, TimeFormatTimeOfDayJP
) {
98 // Test for a locale that uses different mark than "AM" and "PM".
99 // As an instance, we use third_party/icu/source/data/locales/ja.txt.
100 i18n::SetICUDefaultLocale("ja_JP");
102 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
103 string16
clock24h(ASCIIToUTF16("15:42"));
104 string16
clock12h_pm(WideToUTF16(L
"\x5348\x5f8c" L
"3:42"));
105 string16
clock12h(ASCIIToUTF16("3:42"));
107 // The default is 24h clock.
108 EXPECT_EQ(clock24h
, TimeFormatTimeOfDay(time
));
109 EXPECT_EQ(k24HourClock
, GetHourClockType());
110 // k{Keep,Drop}AmPm should not affect for 24h clock.
112 TimeFormatTimeOfDayWithHourClockType(time
,
116 TimeFormatTimeOfDayWithHourClockType(time
,
119 // k{Keep,Drop}AmPm affects for 12h clock.
120 EXPECT_EQ(clock12h_pm
,
121 TimeFormatTimeOfDayWithHourClockType(time
,
125 TimeFormatTimeOfDayWithHourClockType(time
,
130 TEST(TimeFormattingTest
, TimeFormatDateUS
) {
131 // See third_party/icu/source/data/locales/en.txt.
132 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy".
133 i18n::SetICUDefaultLocale("en_US");
135 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
137 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time
));
138 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time
));
140 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"),
141 TimeFormatShortDateAndTime(time
));
142 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(),
143 TimeFormatShortDateAndTimeWithTimeZone(time
));
145 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"),
146 TimeFormatFriendlyDateAndTime(time
));
148 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"),
149 TimeFormatFriendlyDate(time
));
152 TEST(TimeFormattingTest
, TimeFormatDateGB
) {
153 // See third_party/icu/source/data/locales/en_GB.txt.
154 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy".
155 i18n::SetICUDefaultLocale("en_GB");
157 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
159 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time
));
160 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time
));
161 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"),
162 TimeFormatShortDateAndTime(time
));
163 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(),
164 TimeFormatShortDateAndTimeWithTimeZone(time
));
165 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"),
166 TimeFormatFriendlyDateAndTime(time
));
167 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"),
168 TimeFormatFriendlyDate(time
));