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), name, |
| 37 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")); |
38 string16 clock12h_pm(ASCIIToUTF16("3:42 PM")); | 48 string16 clock12h_pm(ASCIIToUTF16("3:42 PM")); |
39 string16 clock12h(ASCIIToUTF16("3:42")); | 49 string16 clock12h(ASCIIToUTF16("3:42")); |
| 50 string16 clock24h_millis(ASCIIToUTF16("15:42:07.000")); |
40 | 51 |
41 // The default is 12h clock. | 52 // The default is 12h clock. |
42 EXPECT_EQ(clock12h_pm, TimeFormatTimeOfDay(time)); | 53 EXPECT_EQ(clock12h_pm, TimeFormatTimeOfDay(time)); |
| 54 EXPECT_EQ(clock24h_millis, TimeFormatTimeOfDayWithMilliseconds(time)); |
43 EXPECT_EQ(k12HourClock, GetHourClockType()); | 55 EXPECT_EQ(k12HourClock, GetHourClockType()); |
44 // k{Keep,Drop}AmPm should not affect for 24h clock. | 56 // k{Keep,Drop}AmPm should not affect for 24h clock. |
45 EXPECT_EQ(clock24h, | 57 EXPECT_EQ(clock24h, |
46 TimeFormatTimeOfDayWithHourClockType(time, | 58 TimeFormatTimeOfDayWithHourClockType(time, |
47 k24HourClock, | 59 k24HourClock, |
48 kKeepAmPm)); | 60 kKeepAmPm)); |
49 EXPECT_EQ(clock24h, | 61 EXPECT_EQ(clock24h, |
50 TimeFormatTimeOfDayWithHourClockType(time, | 62 TimeFormatTimeOfDayWithHourClockType(time, |
51 k24HourClock, | 63 k24HourClock, |
52 kDropAmPm)); | 64 kDropAmPm)); |
(...skipping 10 matching lines...) Expand all Loading... |
63 | 75 |
64 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) { | 76 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) { |
65 // Test for a locale defaulted to 24h clock. | 77 // Test for a locale defaulted to 24h clock. |
66 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt. | 78 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt. |
67 i18n::SetICUDefaultLocale("en_GB"); | 79 i18n::SetICUDefaultLocale("en_GB"); |
68 | 80 |
69 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 81 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
70 string16 clock24h(ASCIIToUTF16("15:42")); | 82 string16 clock24h(ASCIIToUTF16("15:42")); |
71 string16 clock12h_pm(ASCIIToUTF16("3:42 pm")); | 83 string16 clock12h_pm(ASCIIToUTF16("3:42 pm")); |
72 string16 clock12h(ASCIIToUTF16("3:42")); | 84 string16 clock12h(ASCIIToUTF16("3:42")); |
| 85 string16 clock24h_millis(ASCIIToUTF16("15:42:07.000")); |
73 | 86 |
74 // The default is 24h clock. | 87 // The default is 24h clock. |
75 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time)); | 88 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time)); |
| 89 EXPECT_EQ(clock24h_millis, TimeFormatTimeOfDayWithMilliseconds(time)); |
76 EXPECT_EQ(k24HourClock, GetHourClockType()); | 90 EXPECT_EQ(k24HourClock, GetHourClockType()); |
77 // k{Keep,Drop}AmPm should not affect for 24h clock. | 91 // k{Keep,Drop}AmPm should not affect for 24h clock. |
78 EXPECT_EQ(clock24h, | 92 EXPECT_EQ(clock24h, |
79 TimeFormatTimeOfDayWithHourClockType(time, | 93 TimeFormatTimeOfDayWithHourClockType(time, |
80 k24HourClock, | 94 k24HourClock, |
81 kKeepAmPm)); | 95 kKeepAmPm)); |
82 EXPECT_EQ(clock24h, | 96 EXPECT_EQ(clock24h, |
83 TimeFormatTimeOfDayWithHourClockType(time, | 97 TimeFormatTimeOfDayWithHourClockType(time, |
84 k24HourClock, | 98 k24HourClock, |
85 kDropAmPm)); | 99 kDropAmPm)); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 // 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". |
133 i18n::SetICUDefaultLocale("en_US"); | 147 i18n::SetICUDefaultLocale("en_US"); |
134 | 148 |
135 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 149 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
136 | 150 |
137 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time)); | 151 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time)); |
138 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time)); | 152 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time)); |
139 | 153 |
140 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"), | 154 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"), |
141 TimeFormatShortDateAndTime(time)); | 155 TimeFormatShortDateAndTime(time)); |
142 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(), | 156 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(time), |
143 TimeFormatShortDateAndTimeWithTimeZone(time)); | 157 TimeFormatShortDateAndTimeWithTimeZone(time)); |
144 | 158 |
145 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"), |
146 TimeFormatFriendlyDateAndTime(time)); | 160 TimeFormatFriendlyDateAndTime(time)); |
147 | 161 |
148 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"), | 162 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"), |
149 TimeFormatFriendlyDate(time)); | 163 TimeFormatFriendlyDate(time)); |
150 } | 164 } |
151 | 165 |
152 TEST(TimeFormattingTest, TimeFormatDateGB) { | 166 TEST(TimeFormattingTest, TimeFormatDateGB) { |
153 // See third_party/icu/source/data/locales/en_GB.txt. | 167 // 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". | 168 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy". |
155 i18n::SetICUDefaultLocale("en_GB"); | 169 i18n::SetICUDefaultLocale("en_GB"); |
156 | 170 |
157 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 171 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
158 | 172 |
159 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time)); | 173 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time)); |
160 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time)); | 174 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time)); |
161 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"), | 175 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"), |
162 TimeFormatShortDateAndTime(time)); | 176 TimeFormatShortDateAndTime(time)); |
163 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(), | 177 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(time), |
164 TimeFormatShortDateAndTimeWithTimeZone(time)); | 178 TimeFormatShortDateAndTimeWithTimeZone(time)); |
165 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"), | 179 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"), |
166 TimeFormatFriendlyDateAndTime(time)); | 180 TimeFormatFriendlyDateAndTime(time)); |
167 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"), | 181 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"), |
168 TimeFormatFriendlyDate(time)); | 182 TimeFormatFriendlyDate(time)); |
169 } | 183 } |
170 | 184 |
171 } // namespace | 185 } // namespace |
172 } // namespace base | 186 } // namespace base |
OLD | NEW |