| 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 #include "third_party/icu/source/i18n/unicode/tzfmt.h" |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class ScopedSetICUDefaultLocale { |
| 21 std::string default_locale; |
| 22 |
| 23 public: |
| 24 ScopedSetICUDefaultLocale(std::string locale); |
| 25 ~ScopedSetICUDefaultLocale(); |
| 26 }; |
| 27 |
| 28 ScopedSetICUDefaultLocale::ScopedSetICUDefaultLocale(std::string locale) |
| 29 : default_locale(locale) {} |
| 30 |
| 31 ScopedSetICUDefaultLocale::~ScopedSetICUDefaultLocale() { |
| 32 i18n::SetICUDefaultLocale(default_locale.data()); |
| 33 } |
| 34 |
| 20 const Time::Exploded kTestDateTimeExploded = { | 35 const Time::Exploded kTestDateTimeExploded = { |
| 21 2011, 4, 6, 30, // Sat, Apr 30, 2011 | 36 2011, 4, 6, 30, // Sat, Apr 30, 2011 |
| 22 15, 42, 7, 0 // 15:42:07.000 | 37 15, 42, 7, 0 // 15:42:07.000 |
| 23 }; | 38 }; |
| 24 | 39 |
| 25 // Returns difference between the local time and GMT formatted as string. | 40 // Returns difference between the local time and GMT formatted as string. |
| 26 // This function gets |time| because the difference depends on time, | 41 // This function gets |time| because the difference depends on time, |
| 27 // see https://en.wikipedia.org/wiki/Daylight_saving_time for details. | 42 // see https://en.wikipedia.org/wiki/Daylight_saving_time for details. |
| 28 base::string16 GetShortTimeZone(const Time& time) { | 43 base::string16 GetShortTimeZone(const Time& time) { |
| 29 UErrorCode status = U_ZERO_ERROR; | 44 UErrorCode status = U_ZERO_ERROR; |
| 30 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); | 45 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); |
| 31 scoped_ptr<icu::TimeZoneFormat> zone_formatter( | 46 scoped_ptr<icu::TimeZoneFormat> zone_formatter( |
| 32 icu::TimeZoneFormat::createInstance(icu::Locale::getDefault(), status)); | 47 icu::TimeZoneFormat::createInstance(icu::Locale::getDefault(), status)); |
| 33 EXPECT_TRUE(U_SUCCESS(status)); | 48 EXPECT_TRUE(U_SUCCESS(status)); |
| 34 icu::UnicodeString name; | 49 icu::UnicodeString name; |
| 35 zone_formatter->format(UTZFMT_STYLE_SPECIFIC_SHORT, *zone, | 50 zone_formatter->format(UTZFMT_STYLE_SPECIFIC_SHORT, *zone, |
| 36 static_cast<UDate>(time.ToDoubleT() * 1000), | 51 static_cast<UDate>(time.ToDoubleT() * 1000), |
| 37 name, nullptr); | 52 name, nullptr); |
| 38 return base::string16(name.getBuffer(), name.length()); | 53 return base::string16(name.getBuffer(), name.length()); |
| 39 } | 54 } |
| 40 | 55 |
| 41 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) { | 56 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) { |
| 42 // Test for a locale defaulted to 12h clock. | 57 // Test for a locale defaulted to 12h clock. |
| 43 // As an instance, we use third_party/icu/source/data/locales/en.txt. | 58 // As an instance, we use third_party/icu/source/data/locales/en.txt. |
| 59 ScopedSetICUDefaultLocale restore_object(uloc_getDefault()); |
| 44 i18n::SetICUDefaultLocale("en_US"); | 60 i18n::SetICUDefaultLocale("en_US"); |
| 45 | 61 |
| 46 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 62 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
| 47 string16 clock24h(ASCIIToUTF16("15:42")); | 63 string16 clock24h(ASCIIToUTF16("15:42")); |
| 48 string16 clock12h_pm(ASCIIToUTF16("3:42 PM")); | 64 string16 clock12h_pm(ASCIIToUTF16("3:42 PM")); |
| 49 string16 clock12h(ASCIIToUTF16("3:42")); | 65 string16 clock12h(ASCIIToUTF16("3:42")); |
| 50 string16 clock24h_millis(ASCIIToUTF16("15:42:07.000")); | 66 string16 clock24h_millis(ASCIIToUTF16("15:42:07.000")); |
| 51 | 67 |
| 52 // The default is 12h clock. | 68 // The default is 12h clock. |
| 53 EXPECT_EQ(clock12h_pm, TimeFormatTimeOfDay(time)); | 69 EXPECT_EQ(clock12h_pm, TimeFormatTimeOfDay(time)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 69 kKeepAmPm)); | 85 kKeepAmPm)); |
| 70 EXPECT_EQ(clock12h, | 86 EXPECT_EQ(clock12h, |
| 71 TimeFormatTimeOfDayWithHourClockType(time, | 87 TimeFormatTimeOfDayWithHourClockType(time, |
| 72 k12HourClock, | 88 k12HourClock, |
| 73 kDropAmPm)); | 89 kDropAmPm)); |
| 74 } | 90 } |
| 75 | 91 |
| 76 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) { | 92 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) { |
| 77 // Test for a locale defaulted to 24h clock. | 93 // Test for a locale defaulted to 24h clock. |
| 78 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt. | 94 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt. |
| 95 ScopedSetICUDefaultLocale restore_object(uloc_getDefault()); |
| 79 i18n::SetICUDefaultLocale("en_GB"); | 96 i18n::SetICUDefaultLocale("en_GB"); |
| 80 | 97 |
| 81 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 98 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
| 82 string16 clock24h(ASCIIToUTF16("15:42")); | 99 string16 clock24h(ASCIIToUTF16("15:42")); |
| 83 string16 clock12h_pm(ASCIIToUTF16("3:42 pm")); | 100 string16 clock12h_pm(ASCIIToUTF16("3:42 pm")); |
| 84 string16 clock12h(ASCIIToUTF16("3:42")); | 101 string16 clock12h(ASCIIToUTF16("3:42")); |
| 85 string16 clock24h_millis(ASCIIToUTF16("15:42:07.000")); | 102 string16 clock24h_millis(ASCIIToUTF16("15:42:07.000")); |
| 86 | 103 |
| 87 // The default is 24h clock. | 104 // The default is 24h clock. |
| 88 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time)); | 105 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 104 kKeepAmPm)); | 121 kKeepAmPm)); |
| 105 EXPECT_EQ(clock12h, | 122 EXPECT_EQ(clock12h, |
| 106 TimeFormatTimeOfDayWithHourClockType(time, | 123 TimeFormatTimeOfDayWithHourClockType(time, |
| 107 k12HourClock, | 124 k12HourClock, |
| 108 kDropAmPm)); | 125 kDropAmPm)); |
| 109 } | 126 } |
| 110 | 127 |
| 111 TEST(TimeFormattingTest, TimeFormatTimeOfDayJP) { | 128 TEST(TimeFormattingTest, TimeFormatTimeOfDayJP) { |
| 112 // Test for a locale that uses different mark than "AM" and "PM". | 129 // Test for a locale that uses different mark than "AM" and "PM". |
| 113 // As an instance, we use third_party/icu/source/data/locales/ja.txt. | 130 // As an instance, we use third_party/icu/source/data/locales/ja.txt. |
| 131 ScopedSetICUDefaultLocale restore_object(uloc_getDefault()); |
| 114 i18n::SetICUDefaultLocale("ja_JP"); | 132 i18n::SetICUDefaultLocale("ja_JP"); |
| 115 | 133 |
| 116 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 134 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
| 117 string16 clock24h(ASCIIToUTF16("15:42")); | 135 string16 clock24h(ASCIIToUTF16("15:42")); |
| 118 string16 clock12h_pm(WideToUTF16(L"\x5348\x5f8c" L"3:42")); | 136 string16 clock12h_pm(WideToUTF16(L"\x5348\x5f8c" L"3:42")); |
| 119 string16 clock12h(ASCIIToUTF16("3:42")); | 137 string16 clock12h(ASCIIToUTF16("3:42")); |
| 120 | 138 |
| 121 // The default is 24h clock. | 139 // The default is 24h clock. |
| 122 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time)); | 140 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time)); |
| 123 EXPECT_EQ(k24HourClock, GetHourClockType()); | 141 EXPECT_EQ(k24HourClock, GetHourClockType()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 137 kKeepAmPm)); | 155 kKeepAmPm)); |
| 138 EXPECT_EQ(clock12h, | 156 EXPECT_EQ(clock12h, |
| 139 TimeFormatTimeOfDayWithHourClockType(time, | 157 TimeFormatTimeOfDayWithHourClockType(time, |
| 140 k12HourClock, | 158 k12HourClock, |
| 141 kDropAmPm)); | 159 kDropAmPm)); |
| 142 } | 160 } |
| 143 | 161 |
| 144 TEST(TimeFormattingTest, TimeFormatDateUS) { | 162 TEST(TimeFormattingTest, TimeFormatDateUS) { |
| 145 // See third_party/icu/source/data/locales/en.txt. | 163 // See third_party/icu/source/data/locales/en.txt. |
| 146 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy". | 164 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy". |
| 165 ScopedSetICUDefaultLocale restore_object(uloc_getDefault()); |
| 147 i18n::SetICUDefaultLocale("en_US"); | 166 i18n::SetICUDefaultLocale("en_US"); |
| 148 | 167 |
| 149 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 168 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
| 150 | 169 |
| 151 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time)); | 170 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time)); |
| 152 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time)); | 171 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time)); |
| 153 | 172 |
| 154 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"), | 173 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"), |
| 155 TimeFormatShortDateAndTime(time)); | 174 TimeFormatShortDateAndTime(time)); |
| 156 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(time), | 175 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(time), |
| 157 TimeFormatShortDateAndTimeWithTimeZone(time)); | 176 TimeFormatShortDateAndTimeWithTimeZone(time)); |
| 158 | 177 |
| 159 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"), | 178 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"), |
| 160 TimeFormatFriendlyDateAndTime(time)); | 179 TimeFormatFriendlyDateAndTime(time)); |
| 161 | 180 |
| 162 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"), | 181 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"), |
| 163 TimeFormatFriendlyDate(time)); | 182 TimeFormatFriendlyDate(time)); |
| 164 } | 183 } |
| 165 | 184 |
| 166 TEST(TimeFormattingTest, TimeFormatDateGB) { | 185 TEST(TimeFormattingTest, TimeFormatDateGB) { |
| 167 // See third_party/icu/source/data/locales/en_GB.txt. | 186 // See third_party/icu/source/data/locales/en_GB.txt. |
| 168 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy". | 187 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy". |
| 188 ScopedSetICUDefaultLocale restore_object(uloc_getDefault()); |
| 169 i18n::SetICUDefaultLocale("en_GB"); | 189 i18n::SetICUDefaultLocale("en_GB"); |
| 170 | 190 |
| 171 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); | 191 Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
| 172 | 192 |
| 173 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time)); | 193 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time)); |
| 174 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time)); | 194 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time)); |
| 175 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"), | 195 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"), |
| 176 TimeFormatShortDateAndTime(time)); | 196 TimeFormatShortDateAndTime(time)); |
| 177 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(time), | 197 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(time), |
| 178 TimeFormatShortDateAndTimeWithTimeZone(time)); | 198 TimeFormatShortDateAndTimeWithTimeZone(time)); |
| 179 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"), | 199 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"), |
| 180 TimeFormatFriendlyDateAndTime(time)); | 200 TimeFormatFriendlyDateAndTime(time)); |
| 181 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"), | 201 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"), |
| 182 TimeFormatFriendlyDate(time)); | 202 TimeFormatFriendlyDate(time)); |
| 183 } | 203 } |
| 184 | 204 |
| 185 } // namespace | 205 } // namespace |
| 186 } // namespace base | 206 } // namespace base |
| OLD | NEW |