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/memory/scoped_ptr.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #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" | 13 #include "third_party/icu/source/i18n/unicode/calendar.h" |
14 #include "third_party/icu/source/i18n/unicode/timezone.h" | 14 #include "third_party/icu/source/i18n/unicode/timezone.h" |
| 15 #include "third_party/icu/source/i18n/unicode/tzfmt.h" |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 namespace { | 18 namespace { |
18 | 19 |
19 const Time::Exploded kTestDateTimeExploded = { | 20 const Time::Exploded kTestDateTimeExploded = { |
20 2011, 4, 6, 30, // Sat, Apr 30, 2011 | 21 2011, 4, 6, 30, // Sat, Apr 30, 2011 |
21 15, 42, 7, 0 // 15:42:07.000 | 22 15, 42, 7, 0 // 15:42:07.000 |
22 }; | 23 }; |
23 | 24 |
24 base::string16 GetShortTimeZone() { | 25 // Returns difference between the local time and GMT formatted as string. |
| 26 // This function gets |time| because the difference depends on time, |
| 27 // see https://en.wikipedia.org/wiki/Daylight_saving_time for details. |
| 28 base::string16 GetShortTimeZone(const Time& time) { |
| 29 UErrorCode status = U_ZERO_ERROR; |
25 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); | 30 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); |
| 31 scoped_ptr<icu::TimeZoneFormat> zone_formatter( |
| 32 icu::TimeZoneFormat::createInstance(icu::Locale::getDefault(), status)); |
| 33 EXPECT_TRUE(U_SUCCESS(status)); |
26 icu::UnicodeString name; | 34 icu::UnicodeString name; |
27 zone->getDisplayName(true, icu::TimeZone::SHORT, name); | 35 zone_formatter->format(UTZFMT_STYLE_SPECIFIC_SHORT, *zone, |
| 36 static_cast<UDate>(time.ToDoubleT() * 1000), |
| 37 name, nullptr); |
28 return base::string16(name.getBuffer(), name.length()); | 38 return base::string16(name.getBuffer(), name.length()); |
29 } | 39 } |
30 | 40 |
31 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) { | 41 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) { |
32 // Test for a locale defaulted to 12h clock. | 42 // Test for a locale defaulted to 12h clock. |
33 // As an instance, we use third_party/icu/source/data/locales/en.txt. | 43 // As an instance, we use third_party/icu/source/data/locales/en.txt. |
34 i18n::SetICUDefaultLocale("en_US"); | 44 i18n::SetICUDefaultLocale("en_US"); |
35 | 45 |
36 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 46 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
37 string16 clock24h(ASCIIToUTF16("15:42")); | 47 string16 clock24h(ASCIIToUTF16("15:42")); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy". | 146 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy". |
137 i18n::SetICUDefaultLocale("en_US"); | 147 i18n::SetICUDefaultLocale("en_US"); |
138 | 148 |
139 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 149 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
140 | 150 |
141 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time)); | 151 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time)); |
142 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time)); | 152 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time)); |
143 | 153 |
144 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"), | 154 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"), |
145 TimeFormatShortDateAndTime(time)); | 155 TimeFormatShortDateAndTime(time)); |
146 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(), | 156 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(time), |
147 TimeFormatShortDateAndTimeWithTimeZone(time)); | 157 TimeFormatShortDateAndTimeWithTimeZone(time)); |
148 | 158 |
149 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"), | 159 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"), |
150 TimeFormatFriendlyDateAndTime(time)); | 160 TimeFormatFriendlyDateAndTime(time)); |
151 | 161 |
152 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"), | 162 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"), |
153 TimeFormatFriendlyDate(time)); | 163 TimeFormatFriendlyDate(time)); |
154 } | 164 } |
155 | 165 |
156 TEST(TimeFormattingTest, TimeFormatDateGB) { | 166 TEST(TimeFormattingTest, TimeFormatDateGB) { |
157 // See third_party/icu/source/data/locales/en_GB.txt. | 167 // 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". | 168 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy". |
159 i18n::SetICUDefaultLocale("en_GB"); | 169 i18n::SetICUDefaultLocale("en_GB"); |
160 | 170 |
161 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 171 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
162 | 172 |
163 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time)); | 173 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time)); |
164 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time)); | 174 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time)); |
165 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"), | 175 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"), |
166 TimeFormatShortDateAndTime(time)); | 176 TimeFormatShortDateAndTime(time)); |
167 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(), | 177 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(time), |
168 TimeFormatShortDateAndTimeWithTimeZone(time)); | 178 TimeFormatShortDateAndTimeWithTimeZone(time)); |
169 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"), | 179 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"), |
170 TimeFormatFriendlyDateAndTime(time)); | 180 TimeFormatFriendlyDateAndTime(time)); |
171 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"), | 181 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"), |
172 TimeFormatFriendlyDate(time)); | 182 TimeFormatFriendlyDate(time)); |
173 } | 183 } |
174 | 184 |
175 } // namespace | 185 } // namespace |
176 } // namespace base | 186 } // namespace base |
OLD | NEW |