Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / i18n / time_formatting_unittest.cc
blob9f152c0b2d90e88796be753c0a7a05008a08118c
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.
5 #include "base/i18n/time_formatting.h"
7 #include "base/i18n/rtl.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/icu/source/common/unicode/uversion.h"
13 #include "third_party/icu/source/i18n/unicode/calendar.h"
14 #include "third_party/icu/source/i18n/unicode/timezone.h"
15 #include "third_party/icu/source/i18n/unicode/tzfmt.h"
17 namespace base {
18 namespace {
20 const Time::Exploded kTestDateTimeExploded = {
21 2011, 4, 6, 30, // Sat, Apr 30, 2011
22 15, 42, 7, 0 // 15:42:07.000
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;
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));
34 icu::UnicodeString name;
35 zone_formatter->format(UTZFMT_STYLE_SPECIFIC_SHORT, *zone,
36 static_cast<UDate>(time.ToDoubleT() * 1000),
37 name, nullptr);
38 return base::string16(name.getBuffer(), name.length());
41 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) {
42 // Test for a locale defaulted to 12h clock.
43 // As an instance, we use third_party/icu/source/data/locales/en.txt.
44 i18n::SetICUDefaultLocale("en_US");
46 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
47 string16 clock24h(ASCIIToUTF16("15:42"));
48 string16 clock12h_pm(ASCIIToUTF16("3:42 PM"));
49 string16 clock12h(ASCIIToUTF16("3:42"));
50 string16 clock24h_millis(ASCIIToUTF16("15:42:07.000"));
52 // The default is 12h clock.
53 EXPECT_EQ(clock12h_pm, TimeFormatTimeOfDay(time));
54 EXPECT_EQ(clock24h_millis, TimeFormatTimeOfDayWithMilliseconds(time));
55 EXPECT_EQ(k12HourClock, GetHourClockType());
56 // k{Keep,Drop}AmPm should not affect for 24h clock.
57 EXPECT_EQ(clock24h,
58 TimeFormatTimeOfDayWithHourClockType(time,
59 k24HourClock,
60 kKeepAmPm));
61 EXPECT_EQ(clock24h,
62 TimeFormatTimeOfDayWithHourClockType(time,
63 k24HourClock,
64 kDropAmPm));
65 // k{Keep,Drop}AmPm affects for 12h clock.
66 EXPECT_EQ(clock12h_pm,
67 TimeFormatTimeOfDayWithHourClockType(time,
68 k12HourClock,
69 kKeepAmPm));
70 EXPECT_EQ(clock12h,
71 TimeFormatTimeOfDayWithHourClockType(time,
72 k12HourClock,
73 kDropAmPm));
76 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) {
77 // Test for a locale defaulted to 24h clock.
78 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt.
79 i18n::SetICUDefaultLocale("en_GB");
81 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
82 string16 clock24h(ASCIIToUTF16("15:42"));
83 string16 clock12h_pm(ASCIIToUTF16("3:42 pm"));
84 string16 clock12h(ASCIIToUTF16("3:42"));
85 string16 clock24h_millis(ASCIIToUTF16("15:42:07.000"));
87 // The default is 24h clock.
88 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time));
89 EXPECT_EQ(clock24h_millis, TimeFormatTimeOfDayWithMilliseconds(time));
90 EXPECT_EQ(k24HourClock, GetHourClockType());
91 // k{Keep,Drop}AmPm should not affect for 24h clock.
92 EXPECT_EQ(clock24h,
93 TimeFormatTimeOfDayWithHourClockType(time,
94 k24HourClock,
95 kKeepAmPm));
96 EXPECT_EQ(clock24h,
97 TimeFormatTimeOfDayWithHourClockType(time,
98 k24HourClock,
99 kDropAmPm));
100 // k{Keep,Drop}AmPm affects for 12h clock.
101 EXPECT_EQ(clock12h_pm,
102 TimeFormatTimeOfDayWithHourClockType(time,
103 k12HourClock,
104 kKeepAmPm));
105 EXPECT_EQ(clock12h,
106 TimeFormatTimeOfDayWithHourClockType(time,
107 k12HourClock,
108 kDropAmPm));
111 TEST(TimeFormattingTest, TimeFormatTimeOfDayJP) {
112 // 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.
114 i18n::SetICUDefaultLocale("ja_JP");
116 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
117 string16 clock24h(ASCIIToUTF16("15:42"));
118 string16 clock12h_pm(WideToUTF16(L"\x5348\x5f8c" L"3:42"));
119 string16 clock12h(ASCIIToUTF16("3:42"));
121 // The default is 24h clock.
122 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time));
123 EXPECT_EQ(k24HourClock, GetHourClockType());
124 // k{Keep,Drop}AmPm should not affect for 24h clock.
125 EXPECT_EQ(clock24h,
126 TimeFormatTimeOfDayWithHourClockType(time,
127 k24HourClock,
128 kKeepAmPm));
129 EXPECT_EQ(clock24h,
130 TimeFormatTimeOfDayWithHourClockType(time,
131 k24HourClock,
132 kDropAmPm));
133 // k{Keep,Drop}AmPm affects for 12h clock.
134 EXPECT_EQ(clock12h_pm,
135 TimeFormatTimeOfDayWithHourClockType(time,
136 k12HourClock,
137 kKeepAmPm));
138 EXPECT_EQ(clock12h,
139 TimeFormatTimeOfDayWithHourClockType(time,
140 k12HourClock,
141 kDropAmPm));
144 TEST(TimeFormattingTest, TimeFormatDateUS) {
145 // 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".
147 i18n::SetICUDefaultLocale("en_US");
149 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
151 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time));
152 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time));
154 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"),
155 TimeFormatShortDateAndTime(time));
156 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(time),
157 TimeFormatShortDateAndTimeWithTimeZone(time));
159 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"),
160 TimeFormatFriendlyDateAndTime(time));
162 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"),
163 TimeFormatFriendlyDate(time));
166 TEST(TimeFormattingTest, TimeFormatDateGB) {
167 // 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".
169 i18n::SetICUDefaultLocale("en_GB");
171 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
173 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time));
174 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time));
175 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"),
176 TimeFormatShortDateAndTime(time));
177 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(time),
178 TimeFormatShortDateAndTimeWithTimeZone(time));
179 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"),
180 TimeFormatFriendlyDateAndTime(time));
181 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"),
182 TimeFormatFriendlyDate(time));
185 } // namespace
186 } // namespace base