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/strings/utf_string_conversions.h"
9 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/icu/source/common/unicode/uversion.h"
16 const Time::Exploded kTestDateTimeExploded
= {
17 2011, 4, 6, 30, // Sat, Apr 30, 2011
18 15, 42, 7, 0 // 15:42:07.000
21 TEST(TimeFormattingTest
, TimeFormatTimeOfDayDefault12h
) {
22 // Test for a locale defaulted to 12h clock.
23 // As an instance, we use third_party/icu/source/data/locales/en.txt.
24 i18n::SetICUDefaultLocale("en_US");
26 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
27 string16
clock24h(ASCIIToUTF16("15:42"));
28 string16
clock12h_pm(ASCIIToUTF16("3:42 PM"));
29 string16
clock12h(ASCIIToUTF16("3:42"));
31 // The default is 12h clock.
32 EXPECT_EQ(clock12h_pm
, TimeFormatTimeOfDay(time
));
33 EXPECT_EQ(k12HourClock
, GetHourClockType());
34 // k{Keep,Drop}AmPm should not affect for 24h clock.
36 TimeFormatTimeOfDayWithHourClockType(time
,
40 TimeFormatTimeOfDayWithHourClockType(time
,
43 // k{Keep,Drop}AmPm affects for 12h clock.
44 EXPECT_EQ(clock12h_pm
,
45 TimeFormatTimeOfDayWithHourClockType(time
,
49 TimeFormatTimeOfDayWithHourClockType(time
,
54 TEST(TimeFormattingTest
, TimeFormatTimeOfDayDefault24h
) {
55 // Test for a locale defaulted to 24h clock.
56 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt.
57 i18n::SetICUDefaultLocale("en_GB");
59 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
60 string16
clock24h(ASCIIToUTF16("15:42"));
61 #if U_ICU_VERSION_MAJOR_NUM >= 50
62 string16
clock12h_pm(ASCIIToUTF16("3:42 pm"));
64 // TODO(phajdan.jr): Clean up after bundled ICU gets updated to 50.
65 string16
clock12h_pm(ASCIIToUTF16("3:42 PM"));
67 string16
clock12h(ASCIIToUTF16("3:42"));
69 // The default is 24h clock.
70 EXPECT_EQ(clock24h
, TimeFormatTimeOfDay(time
));
71 EXPECT_EQ(k24HourClock
, GetHourClockType());
72 // k{Keep,Drop}AmPm should not affect for 24h clock.
74 TimeFormatTimeOfDayWithHourClockType(time
,
78 TimeFormatTimeOfDayWithHourClockType(time
,
81 // k{Keep,Drop}AmPm affects for 12h clock.
82 EXPECT_EQ(clock12h_pm
,
83 TimeFormatTimeOfDayWithHourClockType(time
,
87 TimeFormatTimeOfDayWithHourClockType(time
,
92 TEST(TimeFormattingTest
, TimeFormatTimeOfDayJP
) {
93 // Test for a locale that uses different mark than "AM" and "PM".
94 // As an instance, we use third_party/icu/source/data/locales/ja.txt.
95 i18n::SetICUDefaultLocale("ja_JP");
97 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
98 string16
clock24h(ASCIIToUTF16("15:42"));
99 string16
clock12h_pm(WideToUTF16(L
"\x5348\x5f8c" L
"3:42"));
100 string16
clock12h(ASCIIToUTF16("3:42"));
102 // The default is 24h clock.
103 EXPECT_EQ(clock24h
, TimeFormatTimeOfDay(time
));
104 EXPECT_EQ(k24HourClock
, GetHourClockType());
105 // k{Keep,Drop}AmPm should not affect for 24h clock.
107 TimeFormatTimeOfDayWithHourClockType(time
,
111 TimeFormatTimeOfDayWithHourClockType(time
,
114 // k{Keep,Drop}AmPm affects for 12h clock.
115 EXPECT_EQ(clock12h_pm
,
116 TimeFormatTimeOfDayWithHourClockType(time
,
120 TimeFormatTimeOfDayWithHourClockType(time
,
125 TEST(TimeFormattingTest
, TimeFormatDateUS
) {
126 // See third_party/icu/source/data/locales/en.txt.
127 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy".
128 i18n::SetICUDefaultLocale("en_US");
130 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
132 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time
));
133 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time
));
135 #if U_ICU_VERSION_MAJOR_NUM >= 50
136 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"),
137 TimeFormatShortDateAndTime(time
));
139 // TODO(phajdan.jr): Clean up after bundled ICU gets updated to 50.
140 EXPECT_EQ(ASCIIToUTF16("4/30/11 3:42:07 PM"),
141 TimeFormatShortDateAndTime(time
));
144 #if U_ICU_VERSION_MAJOR_NUM >= 50
145 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"),
146 TimeFormatFriendlyDateAndTime(time
));
148 // TODO(phajdan.jr): Clean up after bundled ICU gets updated to 50.
149 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 3:42:07 PM"),
150 TimeFormatFriendlyDateAndTime(time
));
153 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"),
154 TimeFormatFriendlyDate(time
));
157 TEST(TimeFormattingTest
, TimeFormatDateGB
) {
158 // See third_party/icu/source/data/locales/en_GB.txt.
159 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy".
160 i18n::SetICUDefaultLocale("en_GB");
162 Time
time(Time::FromLocalExploded(kTestDateTimeExploded
));
164 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time
));
165 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time
));
166 EXPECT_EQ(ASCIIToUTF16("30/04/2011 15:42:07"),
167 TimeFormatShortDateAndTime(time
));
168 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 15:42:07"),
169 TimeFormatFriendlyDateAndTime(time
));
170 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"),
171 TimeFormatFriendlyDate(time
));