[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "base/i18n/time_formatting.h" |
| 6 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 9 | #include "base/i18n/rtl.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
olli.raula | 3be75bf7 | 2015-08-20 08:48:45 | [diff] [blame] | 11 | #include "base/test/icu_test_util.h" |
[email protected] | 99084f6 | 2013-06-28 00:49:07 | [diff] [blame] | 12 | #include "base/time/time.h" |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 8bbf619 | 2013-07-18 11:14:04 | [diff] [blame] | 14 | #include "third_party/icu/source/common/unicode/uversion.h" |
jww | 62c6b3e | 2015-02-08 19:35:22 | [diff] [blame] | 15 | #include "third_party/icu/source/i18n/unicode/calendar.h" |
| 16 | #include "third_party/icu/source/i18n/unicode/timezone.h" |
vitbar | 846dea25 | 2015-07-01 15:02:24 | [diff] [blame] | 17 | #include "third_party/icu/source/i18n/unicode/tzfmt.h" |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 18 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 19 | namespace base { |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 20 | namespace { |
| 21 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 22 | const Time::Exploded kTestDateTimeExploded = { |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 23 | 2011, 4, 6, 30, // Sat, Apr 30, 2011 |
| 24 | 15, 42, 7, 0 // 15:42:07.000 |
| 25 | }; |
| 26 | |
vitbar | 846dea25 | 2015-07-01 15:02:24 | [diff] [blame] | 27 | // Returns difference between the local time and GMT formatted as string. |
| 28 | // This function gets |time| because the difference depends on time, |
| 29 | // see https://en.wikipedia.org/wiki/Daylight_saving_time for details. |
| 30 | base::string16 GetShortTimeZone(const Time& time) { |
| 31 | UErrorCode status = U_ZERO_ERROR; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 32 | std::unique_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); |
| 33 | std::unique_ptr<icu::TimeZoneFormat> zone_formatter( |
vitbar | 846dea25 | 2015-07-01 15:02:24 | [diff] [blame] | 34 | icu::TimeZoneFormat::createInstance(icu::Locale::getDefault(), status)); |
| 35 | EXPECT_TRUE(U_SUCCESS(status)); |
jww | 62c6b3e | 2015-02-08 19:35:22 | [diff] [blame] | 36 | icu::UnicodeString name; |
vitbar | 846dea25 | 2015-07-01 15:02:24 | [diff] [blame] | 37 | zone_formatter->format(UTZFMT_STYLE_SPECIFIC_SHORT, *zone, |
| 38 | static_cast<UDate>(time.ToDoubleT() * 1000), |
| 39 | name, nullptr); |
jww | 62c6b3e | 2015-02-08 19:35:22 | [diff] [blame] | 40 | return base::string16(name.getBuffer(), name.length()); |
| 41 | } |
| 42 | |
clamy | a5c5f94 | 2016-02-01 13:28:02 | [diff] [blame] | 43 | #if defined(OS_ANDROID) |
| 44 | #define MAYBE_TimeFormatTimeOfDayDefault12h \ |
| 45 | DISABLED_TimeFormatTimeOfDayDefault12h |
| 46 | #else |
| 47 | #define MAYBE_TimeFormatTimeOfDayDefault12h TimeFormatTimeOfDayDefault12h |
| 48 | #endif |
| 49 | TEST(TimeFormattingTest, MAYBE_TimeFormatTimeOfDayDefault12h) { |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 50 | // Test for a locale defaulted to 12h clock. |
| 51 | // As an instance, we use third_party/icu/source/data/locales/en.txt. |
olli.raula | 3be75bf7 | 2015-08-20 08:48:45 | [diff] [blame] | 52 | test::ScopedRestoreICUDefaultLocale restore_locale; |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 53 | i18n::SetICUDefaultLocale("en_US"); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 54 | |
maksim.sisov | c9e6026 | 2016-06-29 05:12:59 | [diff] [blame] | 55 | Time time; |
| 56 | EXPECT_TRUE(Time::FromLocalExploded(kTestDateTimeExploded, &time)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 57 | string16 clock24h(ASCIIToUTF16("15:42")); |
| 58 | string16 clock12h_pm(ASCIIToUTF16("3:42 PM")); |
| 59 | string16 clock12h(ASCIIToUTF16("3:42")); |
tengs | 4b5116d | 2015-06-24 22:58:18 | [diff] [blame] | 60 | string16 clock24h_millis(ASCIIToUTF16("15:42:07.000")); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 61 | |
| 62 | // The default is 12h clock. |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 63 | EXPECT_EQ(clock12h_pm, TimeFormatTimeOfDay(time)); |
tengs | 4b5116d | 2015-06-24 22:58:18 | [diff] [blame] | 64 | EXPECT_EQ(clock24h_millis, TimeFormatTimeOfDayWithMilliseconds(time)); |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 65 | EXPECT_EQ(k12HourClock, GetHourClockType()); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 66 | // k{Keep,Drop}AmPm should not affect for 24h clock. |
| 67 | EXPECT_EQ(clock24h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 68 | TimeFormatTimeOfDayWithHourClockType(time, |
| 69 | k24HourClock, |
| 70 | kKeepAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 71 | EXPECT_EQ(clock24h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 72 | TimeFormatTimeOfDayWithHourClockType(time, |
| 73 | k24HourClock, |
| 74 | kDropAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 75 | // k{Keep,Drop}AmPm affects for 12h clock. |
| 76 | EXPECT_EQ(clock12h_pm, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 77 | TimeFormatTimeOfDayWithHourClockType(time, |
| 78 | k12HourClock, |
| 79 | kKeepAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 80 | EXPECT_EQ(clock12h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 81 | TimeFormatTimeOfDayWithHourClockType(time, |
| 82 | k12HourClock, |
| 83 | kDropAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 84 | } |
| 85 | |
clamy | a5c5f94 | 2016-02-01 13:28:02 | [diff] [blame] | 86 | #if defined(OS_ANDROID) |
| 87 | #define MAYBE_TimeFormatTimeOfDayDefault24h \ |
| 88 | DISABLED_TimeFormatTimeOfDayDefault24h |
| 89 | #else |
| 90 | #define MAYBE_TimeFormatTimeOfDayDefault24h TimeFormatTimeOfDayDefault24h |
| 91 | #endif |
| 92 | TEST(TimeFormattingTest, MAYBE_TimeFormatTimeOfDayDefault24h) { |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 93 | // Test for a locale defaulted to 24h clock. |
| 94 | // As an instance, we use third_party/icu/source/data/locales/en_GB.txt. |
olli.raula | 3be75bf7 | 2015-08-20 08:48:45 | [diff] [blame] | 95 | test::ScopedRestoreICUDefaultLocale restore_locale; |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 96 | i18n::SetICUDefaultLocale("en_GB"); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 97 | |
maksim.sisov | c9e6026 | 2016-06-29 05:12:59 | [diff] [blame] | 98 | Time time; |
| 99 | EXPECT_TRUE(Time::FromLocalExploded(kTestDateTimeExploded, &time)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 100 | string16 clock24h(ASCIIToUTF16("15:42")); |
[email protected] | 5ece804 | 2013-01-03 19:00:29 | [diff] [blame] | 101 | string16 clock12h_pm(ASCIIToUTF16("3:42 pm")); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 102 | string16 clock12h(ASCIIToUTF16("3:42")); |
tengs | 4b5116d | 2015-06-24 22:58:18 | [diff] [blame] | 103 | string16 clock24h_millis(ASCIIToUTF16("15:42:07.000")); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 104 | |
| 105 | // The default is 24h clock. |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 106 | EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time)); |
tengs | 4b5116d | 2015-06-24 22:58:18 | [diff] [blame] | 107 | EXPECT_EQ(clock24h_millis, TimeFormatTimeOfDayWithMilliseconds(time)); |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 108 | EXPECT_EQ(k24HourClock, GetHourClockType()); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 109 | // k{Keep,Drop}AmPm should not affect for 24h clock. |
| 110 | EXPECT_EQ(clock24h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 111 | TimeFormatTimeOfDayWithHourClockType(time, |
| 112 | k24HourClock, |
| 113 | kKeepAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 114 | EXPECT_EQ(clock24h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 115 | TimeFormatTimeOfDayWithHourClockType(time, |
| 116 | k24HourClock, |
| 117 | kDropAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 118 | // k{Keep,Drop}AmPm affects for 12h clock. |
| 119 | EXPECT_EQ(clock12h_pm, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 120 | TimeFormatTimeOfDayWithHourClockType(time, |
| 121 | k12HourClock, |
| 122 | kKeepAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 123 | EXPECT_EQ(clock12h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 124 | TimeFormatTimeOfDayWithHourClockType(time, |
| 125 | k12HourClock, |
| 126 | kDropAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 127 | } |
| 128 | |
clamy | a5c5f94 | 2016-02-01 13:28:02 | [diff] [blame] | 129 | #if defined(OS_ANDROID) |
| 130 | #define MAYBE_TimeFormatTimeOfDayJP DISABLED_TimeFormatTimeOfDayJP |
| 131 | #else |
| 132 | #define MAYBE_TimeFormatTimeOfDayJP TimeFormatTimeOfDayJP |
| 133 | #endif |
| 134 | TEST(TimeFormattingTest, MAYBE_TimeFormatTimeOfDayJP) { |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 135 | // Test for a locale that uses different mark than "AM" and "PM". |
| 136 | // As an instance, we use third_party/icu/source/data/locales/ja.txt. |
olli.raula | 3be75bf7 | 2015-08-20 08:48:45 | [diff] [blame] | 137 | test::ScopedRestoreICUDefaultLocale restore_locale; |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 138 | i18n::SetICUDefaultLocale("ja_JP"); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 139 | |
maksim.sisov | c9e6026 | 2016-06-29 05:12:59 | [diff] [blame] | 140 | Time time; |
| 141 | EXPECT_TRUE(Time::FromLocalExploded(kTestDateTimeExploded, &time)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 142 | string16 clock24h(ASCIIToUTF16("15:42")); |
[email protected] | fdbb5c7e | 2013-03-15 15:48:43 | [diff] [blame] | 143 | string16 clock12h_pm(WideToUTF16(L"\x5348\x5f8c" L"3:42")); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 144 | string16 clock12h(ASCIIToUTF16("3:42")); |
| 145 | |
| 146 | // The default is 24h clock. |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 147 | EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time)); |
| 148 | EXPECT_EQ(k24HourClock, GetHourClockType()); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 149 | // k{Keep,Drop}AmPm should not affect for 24h clock. |
| 150 | EXPECT_EQ(clock24h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 151 | TimeFormatTimeOfDayWithHourClockType(time, |
| 152 | k24HourClock, |
| 153 | kKeepAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 154 | EXPECT_EQ(clock24h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 155 | TimeFormatTimeOfDayWithHourClockType(time, |
| 156 | k24HourClock, |
| 157 | kDropAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 158 | // k{Keep,Drop}AmPm affects for 12h clock. |
| 159 | EXPECT_EQ(clock12h_pm, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 160 | TimeFormatTimeOfDayWithHourClockType(time, |
| 161 | k12HourClock, |
| 162 | kKeepAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 163 | EXPECT_EQ(clock12h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 164 | TimeFormatTimeOfDayWithHourClockType(time, |
| 165 | k12HourClock, |
| 166 | kDropAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 167 | } |
| 168 | |
clamy | a5c5f94 | 2016-02-01 13:28:02 | [diff] [blame] | 169 | #if defined(OS_ANDROID) |
| 170 | #define MAYBE_TimeFormatDateUS DISABLED_TimeFormatDateUS |
| 171 | #else |
| 172 | #define MAYBE_TimeFormatDateUS TimeFormatDateUS |
| 173 | #endif |
| 174 | TEST(TimeFormattingTest, MAYBE_TimeFormatDateUS) { |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 175 | // See third_party/icu/source/data/locales/en.txt. |
| 176 | // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy". |
olli.raula | 3be75bf7 | 2015-08-20 08:48:45 | [diff] [blame] | 177 | test::ScopedRestoreICUDefaultLocale restore_locale; |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 178 | i18n::SetICUDefaultLocale("en_US"); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 179 | |
maksim.sisov | c9e6026 | 2016-06-29 05:12:59 | [diff] [blame] | 180 | Time time; |
| 181 | EXPECT_TRUE(Time::FromLocalExploded(kTestDateTimeExploded, &time)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 182 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 183 | EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time)); |
| 184 | EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time)); |
[email protected] | 5ece804 | 2013-01-03 19:00:29 | [diff] [blame] | 185 | |
[email protected] | 5ece804 | 2013-01-03 19:00:29 | [diff] [blame] | 186 | EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"), |
| 187 | TimeFormatShortDateAndTime(time)); |
vitbar | 846dea25 | 2015-07-01 15:02:24 | [diff] [blame] | 188 | EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(time), |
jww | 62c6b3e | 2015-02-08 19:35:22 | [diff] [blame] | 189 | TimeFormatShortDateAndTimeWithTimeZone(time)); |
[email protected] | 5ece804 | 2013-01-03 19:00:29 | [diff] [blame] | 190 | |
[email protected] | 5ece804 | 2013-01-03 19:00:29 | [diff] [blame] | 191 | EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"), |
| 192 | TimeFormatFriendlyDateAndTime(time)); |
[email protected] | 5ece804 | 2013-01-03 19:00:29 | [diff] [blame] | 193 | |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 194 | EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"), |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 195 | TimeFormatFriendlyDate(time)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 196 | } |
| 197 | |
clamy | a5c5f94 | 2016-02-01 13:28:02 | [diff] [blame] | 198 | #if defined(OS_ANDROID) |
| 199 | #define MAYBE_TimeFormatDateGB DISABLED_TimeFormatDateGB |
| 200 | #else |
| 201 | #define MAYBE_TimeFormatDateGB TimeFormatDateGB |
| 202 | #endif |
| 203 | TEST(TimeFormattingTest, MAYBE_TimeFormatDateGB) { |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 204 | // See third_party/icu/source/data/locales/en_GB.txt. |
| 205 | // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy". |
olli.raula | 3be75bf7 | 2015-08-20 08:48:45 | [diff] [blame] | 206 | test::ScopedRestoreICUDefaultLocale restore_locale; |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 207 | i18n::SetICUDefaultLocale("en_GB"); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 208 | |
maksim.sisov | c9e6026 | 2016-06-29 05:12:59 | [diff] [blame] | 209 | Time time; |
| 210 | EXPECT_TRUE(Time::FromLocalExploded(kTestDateTimeExploded, &time)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 211 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 212 | EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time)); |
| 213 | EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time)); |
jshin | 8fe2fe9a | 2015-02-20 01:37:58 | [diff] [blame] | 214 | EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"), |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 215 | TimeFormatShortDateAndTime(time)); |
vitbar | 846dea25 | 2015-07-01 15:02:24 | [diff] [blame] | 216 | EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(time), |
jww | 62c6b3e | 2015-02-08 19:35:22 | [diff] [blame] | 217 | TimeFormatShortDateAndTimeWithTimeZone(time)); |
jshin | 8fe2fe9a | 2015-02-20 01:37:58 | [diff] [blame] | 218 | EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"), |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 219 | TimeFormatFriendlyDateAndTime(time)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 220 | EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"), |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 221 | TimeFormatFriendlyDate(time)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 222 | } |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 223 | |
glevin | 5cff4d1 | 2016-05-19 19:22:04 | [diff] [blame] | 224 | TEST(TimeFormattingTest, TimeDurationFormat) { |
| 225 | test::ScopedRestoreICUDefaultLocale restore_locale; |
| 226 | TimeDelta delta = TimeDelta::FromMinutes(15 * 60 + 42); |
| 227 | |
| 228 | // US English. |
| 229 | i18n::SetICUDefaultLocale("en_US"); |
| 230 | EXPECT_EQ(ASCIIToUTF16("15 hours, 42 minutes"), |
| 231 | TimeDurationFormat(delta, DURATION_WIDTH_WIDE)); |
| 232 | EXPECT_EQ(ASCIIToUTF16("15 hr, 42 min"), |
| 233 | TimeDurationFormat(delta, DURATION_WIDTH_SHORT)); |
| 234 | EXPECT_EQ(ASCIIToUTF16("15h 42m"), |
| 235 | TimeDurationFormat(delta, DURATION_WIDTH_NARROW)); |
| 236 | EXPECT_EQ(ASCIIToUTF16("15:42"), |
| 237 | TimeDurationFormat(delta, DURATION_WIDTH_NUMERIC)); |
| 238 | |
| 239 | // Danish, with Latin alphabet but different abbreviations and punctuation. |
| 240 | i18n::SetICUDefaultLocale("da"); |
| 241 | EXPECT_EQ(ASCIIToUTF16("15 timer og 42 minutter"), |
| 242 | TimeDurationFormat(delta, DURATION_WIDTH_WIDE)); |
| 243 | EXPECT_EQ(ASCIIToUTF16("15 t og 42 min."), |
| 244 | TimeDurationFormat(delta, DURATION_WIDTH_SHORT)); |
| 245 | EXPECT_EQ(ASCIIToUTF16("15 t og 42 min"), |
| 246 | TimeDurationFormat(delta, DURATION_WIDTH_NARROW)); |
| 247 | EXPECT_EQ(ASCIIToUTF16("15.42"), |
| 248 | TimeDurationFormat(delta, DURATION_WIDTH_NUMERIC)); |
| 249 | |
| 250 | // Persian, with non-Arabic numbers. |
| 251 | i18n::SetICUDefaultLocale("fa"); |
| 252 | string16 fa_wide = WideToUTF16( |
| 253 | L"\x6f1\x6f5\x20\x633\x627\x639\x62a\x20\x648\x20\x6f4\x6f2\x20\x62f\x642" |
| 254 | L"\x6cc\x642\x647"); |
| 255 | string16 fa_short = WideToUTF16( |
| 256 | L"\x6f1\x6f5\x20\x633\x627\x639\x62a\x60c\x200f\x20\x6f4\x6f2\x20\x62f" |
| 257 | L"\x642\x6cc\x642\x647"); |
| 258 | string16 fa_narrow = WideToUTF16( |
| 259 | L"\x6f1\x6f5\x20\x633\x627\x639\x62a\x20\x6f4\x6f2\x20\x62f\x642\x6cc" |
| 260 | L"\x642\x647"); |
| 261 | string16 fa_numeric = WideToUTF16(L"\x6f1\x6f5\x3a\x6f4\x6f2"); |
| 262 | EXPECT_EQ(fa_wide, TimeDurationFormat(delta, DURATION_WIDTH_WIDE)); |
| 263 | EXPECT_EQ(fa_short, TimeDurationFormat(delta, DURATION_WIDTH_SHORT)); |
| 264 | EXPECT_EQ(fa_narrow, TimeDurationFormat(delta, DURATION_WIDTH_NARROW)); |
| 265 | EXPECT_EQ(fa_numeric, TimeDurationFormat(delta, DURATION_WIDTH_NUMERIC)); |
| 266 | } |
| 267 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 268 | } // namespace |
| 269 | } // namespace base |