OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/i18n/time_formatting.h" | 5 #include "base/i18n/time_formatting.h" |
6 | 6 |
7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
| 8 #include "base/memory/scoped_ptr.h" |
8 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "third_party/icu/source/common/unicode/uversion.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" |
12 | 15 |
13 namespace base { | 16 namespace base { |
14 namespace { | 17 namespace { |
15 | 18 |
16 const Time::Exploded kTestDateTimeExploded = { | 19 const Time::Exploded kTestDateTimeExploded = { |
17 2011, 4, 6, 30, // Sat, Apr 30, 2011 | 20 2011, 4, 6, 30, // Sat, Apr 30, 2011 |
18 15, 42, 7, 0 // 15:42:07.000 | 21 15, 42, 7, 0 // 15:42:07.000 |
19 }; | 22 }; |
20 | 23 |
| 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()); |
| 29 } |
| 30 |
21 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) { | 31 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) { |
22 // Test for a locale defaulted to 12h clock. | 32 // Test for a locale defaulted to 12h clock. |
23 // As an instance, we use third_party/icu/source/data/locales/en.txt. | 33 // As an instance, we use third_party/icu/source/data/locales/en.txt. |
24 i18n::SetICUDefaultLocale("en_US"); | 34 i18n::SetICUDefaultLocale("en_US"); |
25 | 35 |
26 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 36 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
27 string16 clock24h(ASCIIToUTF16("15:42")); | 37 string16 clock24h(ASCIIToUTF16("15:42")); |
28 string16 clock12h_pm(ASCIIToUTF16("3:42 PM")); | 38 string16 clock12h_pm(ASCIIToUTF16("3:42 PM")); |
29 string16 clock12h(ASCIIToUTF16("3:42")); | 39 string16 clock12h(ASCIIToUTF16("3:42")); |
30 | 40 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy". | 132 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy". |
123 i18n::SetICUDefaultLocale("en_US"); | 133 i18n::SetICUDefaultLocale("en_US"); |
124 | 134 |
125 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 135 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
126 | 136 |
127 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time)); | 137 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time)); |
128 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time)); | 138 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time)); |
129 | 139 |
130 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"), | 140 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"), |
131 TimeFormatShortDateAndTime(time)); | 141 TimeFormatShortDateAndTime(time)); |
| 142 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(), |
| 143 TimeFormatShortDateAndTimeWithTimeZone(time)); |
132 | 144 |
133 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"), | 145 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"), |
134 TimeFormatFriendlyDateAndTime(time)); | 146 TimeFormatFriendlyDateAndTime(time)); |
135 | 147 |
136 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"), | 148 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"), |
137 TimeFormatFriendlyDate(time)); | 149 TimeFormatFriendlyDate(time)); |
138 } | 150 } |
139 | 151 |
140 TEST(TimeFormattingTest, TimeFormatDateGB) { | 152 TEST(TimeFormattingTest, TimeFormatDateGB) { |
141 // See third_party/icu/source/data/locales/en_GB.txt. | 153 // See third_party/icu/source/data/locales/en_GB.txt. |
142 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy". | 154 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy". |
143 i18n::SetICUDefaultLocale("en_GB"); | 155 i18n::SetICUDefaultLocale("en_GB"); |
144 | 156 |
145 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 157 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
146 | 158 |
147 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time)); | 159 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time)); |
148 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time)); | 160 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time)); |
149 EXPECT_EQ(ASCIIToUTF16("30/04/2011 15:42:07"), | 161 EXPECT_EQ(ASCIIToUTF16("30/04/2011 15:42:07"), |
150 TimeFormatShortDateAndTime(time)); | 162 TimeFormatShortDateAndTime(time)); |
| 163 EXPECT_EQ(ASCIIToUTF16("30/04/2011 15:42:07 ") + GetShortTimeZone(), |
| 164 TimeFormatShortDateAndTimeWithTimeZone(time)); |
151 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 15:42:07"), | 165 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 15:42:07"), |
152 TimeFormatFriendlyDateAndTime(time)); | 166 TimeFormatFriendlyDateAndTime(time)); |
153 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"), | 167 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"), |
154 TimeFormatFriendlyDate(time)); | 168 TimeFormatFriendlyDate(time)); |
155 } | 169 } |
156 | 170 |
157 } // namespace | 171 } // namespace |
158 } // namespace base | 172 } // namespace base |
OLD | NEW |