1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "src/global-handles.h"
31 #include "test/cctest/cctest.h"
33 using namespace v8::internal;
35 class DateCacheMock: public DateCache {
38 int year, start_month, start_day, end_month, end_day, offset_sec;
41 DateCacheMock(int local_offset, Rule* rules, int rules_count)
42 : local_offset_(local_offset), rules_(rules), rules_count_(rules_count) {}
45 virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec) {
46 int days = DaysFromTime(time_sec * 1000);
47 int time_in_day_sec = TimeInDay(time_sec * 1000, days) / 1000;
49 YearMonthDayFromDays(days, &year, &month, &day);
50 Rule* rule = FindRuleFor(year, month, day, time_in_day_sec);
51 return rule == NULL ? 0 : rule->offset_sec * 1000;
55 virtual int GetLocalOffsetFromOS() {
60 Rule* FindRuleFor(int year, int month, int day, int time_in_day_sec) {
62 for (int i = 0; i < rules_count_; i++)
63 if (Match(&rules_[i], year, month, day, time_in_day_sec)) {
70 bool Match(Rule* rule, int year, int month, int day, int time_in_day_sec) {
71 if (rule->year != 0 && rule->year != year) return false;
72 if (rule->start_month > month) return false;
73 if (rule->end_month < month) return false;
74 int start_day = ComputeRuleDay(year, rule->start_month, rule->start_day);
75 if (rule->start_month == month && start_day > day) return false;
76 if (rule->start_month == month && start_day == day &&
77 2 * 3600 > time_in_day_sec)
79 int end_day = ComputeRuleDay(year, rule->end_month, rule->end_day);
80 if (rule->end_month == month && end_day < day) return false;
81 if (rule->end_month == month && end_day == day &&
82 2 * 3600 <= time_in_day_sec)
88 int ComputeRuleDay(int year, int month, int day) {
89 if (day != 0) return day;
90 int days = DaysFromYearMonth(year, month);
91 // Find the first Sunday of the month.
92 while (Weekday(days + day) != 6) day++;
101 static int64_t TimeFromYearMonthDay(DateCache* date_cache,
105 int64_t result = date_cache->DaysFromYearMonth(year, month);
106 return (result + day - 1) * DateCache::kMsPerDay;
110 static void CheckDST(int64_t time) {
111 Isolate* isolate = CcTest::i_isolate();
112 DateCache* date_cache = isolate->date_cache();
113 int64_t actual = date_cache->ToLocal(time);
114 int64_t expected = time + date_cache->GetLocalOffsetFromOS() +
115 date_cache->GetDaylightSavingsOffsetFromOS(time / 1000);
116 CHECK_EQ(actual, expected);
120 TEST(DaylightSavingsTime) {
121 LocalContext context;
122 v8::Isolate* isolate = context->GetIsolate();
123 v8::HandleScope scope(isolate);
124 DateCacheMock::Rule rules[] = {
125 {0, 2, 0, 10, 0, 3600}, // DST from March to November in any year.
126 {2010, 2, 0, 7, 20, 3600}, // DST from March to August 20 in 2010.
127 {2010, 7, 20, 8, 10, 0}, // No DST from August 20 to September 10 in 2010.
128 {2010, 8, 10, 10, 0, 3600}, // DST from September 10 to November in 2010.
131 int local_offset_ms = -36000000; // -10 hours.
133 DateCacheMock* date_cache =
134 new DateCacheMock(local_offset_ms, rules, arraysize(rules));
136 reinterpret_cast<Isolate*>(isolate)->set_date_cache(date_cache);
138 int64_t start_of_2010 = TimeFromYearMonthDay(date_cache, 2010, 0, 1);
139 int64_t start_of_2011 = TimeFromYearMonthDay(date_cache, 2011, 0, 1);
140 int64_t august_20 = TimeFromYearMonthDay(date_cache, 2010, 7, 20);
141 int64_t september_10 = TimeFromYearMonthDay(date_cache, 2010, 8, 10);
142 CheckDST((august_20 + september_10) / 2);
143 CheckDST(september_10);
144 CheckDST(september_10 + 2 * 3600);
145 CheckDST(september_10 + 2 * 3600 - 1000);
146 CheckDST(august_20 + 2 * 3600);
147 CheckDST(august_20 + 2 * 3600 - 1000);
149 // Check each day of 2010.
150 for (int64_t time = start_of_2011 + 2 * 3600;
151 time >= start_of_2010;
152 time -= DateCache::kMsPerDay) {
154 CheckDST(time - 1000);
155 CheckDST(time + 1000);
157 // Check one day from 2010 to 2100.
158 for (int year = 2100; year >= 2010; year--) {
159 CheckDST(TimeFromYearMonthDay(date_cache, year, 5, 5));
161 CheckDST((august_20 + september_10) / 2);
162 CheckDST(september_10);
163 CheckDST(september_10 + 2 * 3600);
164 CheckDST(september_10 + 2 * 3600 - 1000);
165 CheckDST(august_20 + 2 * 3600);
166 CheckDST(august_20 + 2 * 3600 - 1000);
171 TEST(DateCacheVersion) {
172 FLAG_allow_natives_syntax = true;
173 v8::Isolate* isolate = CcTest::isolate();
174 v8::Isolate::Scope isolate_scope(isolate);
175 v8::HandleScope scope(isolate);
176 v8::Handle<v8::Context> context = v8::Context::New(isolate);
177 v8::Context::Scope context_scope(context);
178 v8::Handle<v8::Array> date_cache_version =
179 v8::Handle<v8::Array>::Cast(CompileRun("%DateCacheVersion()"));
181 CHECK_EQ(1, static_cast<int32_t>(date_cache_version->Length()));
182 CHECK(date_cache_version->Get(0)->IsNumber());
183 CHECK_EQ(0.0, date_cache_version->Get(0)->NumberValue());
185 v8::Date::DateTimeConfigurationChangeNotification(isolate);
187 CHECK_EQ(1, static_cast<int32_t>(date_cache_version->Length()));
188 CHECK(date_cache_version->Get(0)->IsNumber());
189 CHECK_EQ(1.0, date_cache_version->Get(0)->NumberValue());