[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 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 7 | #include "base/i18n/rtl.h" |
jww | 62c6b3e | 2015-02-08 19:35:22 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 99084f6 | 2013-06-28 00:49:07 | [diff] [blame] | 10 | #include "base/time/time.h" |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 8bbf619 | 2013-07-18 11:14:04 | [diff] [blame] | 12 | #include "third_party/icu/source/common/unicode/uversion.h" |
jww | 62c6b3e | 2015-02-08 19:35:22 | [diff] [blame] | 13 | #include "third_party/icu/source/i18n/unicode/calendar.h" |
| 14 | #include "third_party/icu/source/i18n/unicode/timezone.h" |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 15 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 16 | namespace base { |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 17 | namespace { |
| 18 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 19 | const Time::Exploded kTestDateTimeExploded = { |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 20 | 2011, 4, 6, 30, // Sat, Apr 30, 2011 |
| 21 | 15, 42, 7, 0 // 15:42:07.000 |
| 22 | }; |
| 23 | |
jww | 62c6b3e | 2015-02-08 19:35:22 | [diff] [blame] | 24 | base::string16 GetShortTimeZone() { |
| 25 | scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); |
jww | 62c6b3e | 2015-02-08 19:35:22 | [diff] [blame] | 26 | icu::UnicodeString name; |
| 27 | zone->getDisplayName(true, icu::TimeZone::SHORT, name); |
| 28 | return base::string16(name.getBuffer(), name.length()); |
| 29 | } |
| 30 | |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 31 | TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) { |
| 32 | // Test for a locale defaulted to 12h clock. |
| 33 | // As an instance, we use third_party/icu/source/data/locales/en.txt. |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 34 | i18n::SetICUDefaultLocale("en_US"); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 35 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 36 | Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 37 | string16 clock24h(ASCIIToUTF16("15:42")); |
| 38 | string16 clock12h_pm(ASCIIToUTF16("3:42 PM")); |
| 39 | string16 clock12h(ASCIIToUTF16("3:42")); |
| 40 | |
| 41 | // The default is 12h clock. |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 42 | EXPECT_EQ(clock12h_pm, TimeFormatTimeOfDay(time)); |
| 43 | EXPECT_EQ(k12HourClock, GetHourClockType()); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 44 | // k{Keep,Drop}AmPm should not affect for 24h clock. |
| 45 | EXPECT_EQ(clock24h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 46 | TimeFormatTimeOfDayWithHourClockType(time, |
| 47 | k24HourClock, |
| 48 | kKeepAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 49 | EXPECT_EQ(clock24h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 50 | TimeFormatTimeOfDayWithHourClockType(time, |
| 51 | k24HourClock, |
| 52 | kDropAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 53 | // k{Keep,Drop}AmPm affects for 12h clock. |
| 54 | EXPECT_EQ(clock12h_pm, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 55 | TimeFormatTimeOfDayWithHourClockType(time, |
| 56 | k12HourClock, |
| 57 | kKeepAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 58 | EXPECT_EQ(clock12h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 59 | TimeFormatTimeOfDayWithHourClockType(time, |
| 60 | k12HourClock, |
| 61 | kDropAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 64 | TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) { |
| 65 | // Test for a locale defaulted to 24h clock. |
| 66 | // As an instance, we use third_party/icu/source/data/locales/en_GB.txt. |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 67 | i18n::SetICUDefaultLocale("en_GB"); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 68 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 69 | Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 70 | string16 clock24h(ASCIIToUTF16("15:42")); |
[email protected] | 5ece804 | 2013-01-03 19:00:29 | [diff] [blame] | 71 | string16 clock12h_pm(ASCIIToUTF16("3:42 pm")); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 72 | string16 clock12h(ASCIIToUTF16("3:42")); |
| 73 | |
| 74 | // The default is 24h clock. |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 75 | EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time)); |
| 76 | EXPECT_EQ(k24HourClock, GetHourClockType()); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 77 | // k{Keep,Drop}AmPm should not affect for 24h clock. |
| 78 | EXPECT_EQ(clock24h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 79 | TimeFormatTimeOfDayWithHourClockType(time, |
| 80 | k24HourClock, |
| 81 | kKeepAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 82 | EXPECT_EQ(clock24h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 83 | TimeFormatTimeOfDayWithHourClockType(time, |
| 84 | k24HourClock, |
| 85 | kDropAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 86 | // k{Keep,Drop}AmPm affects for 12h clock. |
| 87 | EXPECT_EQ(clock12h_pm, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 88 | TimeFormatTimeOfDayWithHourClockType(time, |
| 89 | k12HourClock, |
| 90 | kKeepAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 91 | EXPECT_EQ(clock12h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 92 | TimeFormatTimeOfDayWithHourClockType(time, |
| 93 | k12HourClock, |
| 94 | kDropAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | TEST(TimeFormattingTest, TimeFormatTimeOfDayJP) { |
| 98 | // Test for a locale that uses different mark than "AM" and "PM". |
| 99 | // As an instance, we use third_party/icu/source/data/locales/ja.txt. |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 100 | i18n::SetICUDefaultLocale("ja_JP"); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 101 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 102 | Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 103 | string16 clock24h(ASCIIToUTF16("15:42")); |
[email protected] | fdbb5c7e | 2013-03-15 15:48:43 | [diff] [blame] | 104 | string16 clock12h_pm(WideToUTF16(L"\x5348\x5f8c" L"3:42")); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 105 | string16 clock12h(ASCIIToUTF16("3:42")); |
| 106 | |
| 107 | // The default is 24h clock. |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 108 | EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time)); |
| 109 | EXPECT_EQ(k24HourClock, GetHourClockType()); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 110 | // k{Keep,Drop}AmPm should not affect for 24h clock. |
| 111 | EXPECT_EQ(clock24h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 112 | TimeFormatTimeOfDayWithHourClockType(time, |
| 113 | k24HourClock, |
| 114 | kKeepAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 115 | EXPECT_EQ(clock24h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 116 | TimeFormatTimeOfDayWithHourClockType(time, |
| 117 | k24HourClock, |
| 118 | kDropAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 119 | // k{Keep,Drop}AmPm affects for 12h clock. |
| 120 | EXPECT_EQ(clock12h_pm, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 121 | TimeFormatTimeOfDayWithHourClockType(time, |
| 122 | k12HourClock, |
| 123 | kKeepAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 124 | EXPECT_EQ(clock12h, |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 125 | TimeFormatTimeOfDayWithHourClockType(time, |
| 126 | k12HourClock, |
| 127 | kDropAmPm)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TEST(TimeFormattingTest, TimeFormatDateUS) { |
| 131 | // See third_party/icu/source/data/locales/en.txt. |
| 132 | // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy". |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 133 | i18n::SetICUDefaultLocale("en_US"); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 134 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 135 | Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 136 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 137 | EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time)); |
| 138 | EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time)); |
[email protected] | 5ece804 | 2013-01-03 19:00:29 | [diff] [blame] | 139 | |
[email protected] | 5ece804 | 2013-01-03 19:00:29 | [diff] [blame] | 140 | EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"), |
| 141 | TimeFormatShortDateAndTime(time)); |
jww | 62c6b3e | 2015-02-08 19:35:22 | [diff] [blame] | 142 | EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(), |
| 143 | TimeFormatShortDateAndTimeWithTimeZone(time)); |
[email protected] | 5ece804 | 2013-01-03 19:00:29 | [diff] [blame] | 144 | |
[email protected] | 5ece804 | 2013-01-03 19:00:29 | [diff] [blame] | 145 | EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"), |
| 146 | TimeFormatFriendlyDateAndTime(time)); |
[email protected] | 5ece804 | 2013-01-03 19:00:29 | [diff] [blame] | 147 | |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 148 | EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"), |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 149 | TimeFormatFriendlyDate(time)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | TEST(TimeFormattingTest, TimeFormatDateGB) { |
| 153 | // 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". |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 155 | i18n::SetICUDefaultLocale("en_GB"); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 156 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 157 | Time time(Time::FromLocalExploded(kTestDateTimeExploded)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 158 | |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 159 | EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time)); |
| 160 | EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time)); |
jshin | 8fe2fe9a | 2015-02-20 01:37:58 | [diff] [blame^] | 161 | EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"), |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 162 | TimeFormatShortDateAndTime(time)); |
jshin | 8fe2fe9a | 2015-02-20 01:37:58 | [diff] [blame^] | 163 | EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(), |
jww | 62c6b3e | 2015-02-08 19:35:22 | [diff] [blame] | 164 | TimeFormatShortDateAndTimeWithTimeZone(time)); |
jshin | 8fe2fe9a | 2015-02-20 01:37:58 | [diff] [blame^] | 165 | EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"), |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 166 | TimeFormatFriendlyDateAndTime(time)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 167 | EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"), |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 168 | TimeFormatFriendlyDate(time)); |
[email protected] | 60e6c84 | 2011-05-30 11:45:43 | [diff] [blame] | 169 | } |
[email protected] | 3c8fe063 | 2011-10-07 20:16:31 | [diff] [blame] | 170 | |
| 171 | } // namespace |
| 172 | } // namespace base |