v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-date.cc
Go to the documentation of this file.
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
4 // met:
5 //
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.
15 //
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.
27 
28 #include "v8.h"
29 
30 #include "global-handles.h"
31 #include "snapshot.h"
32 #include "cctest.h"
33 
34 using namespace v8::internal;
35 
36 class DateCacheMock: public DateCache {
37  public:
38  struct Rule {
39  int year, start_month, start_day, end_month, end_day, offset_sec;
40  };
41 
42  DateCacheMock(int local_offset, Rule* rules, int rules_count)
43  : local_offset_(local_offset), rules_(rules), rules_count_(rules_count) {}
44 
45  protected:
46  virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec) {
47  int days = DaysFromTime(time_sec * 1000);
48  int time_in_day_sec = TimeInDay(time_sec * 1000, days) / 1000;
49  int year, month, day;
50  YearMonthDayFromDays(days, &year, &month, &day);
51  Rule* rule = FindRuleFor(year, month, day, time_in_day_sec);
52  return rule == NULL ? 0 : rule->offset_sec * 1000;
53  }
54 
55 
56  virtual int GetLocalOffsetFromOS() {
57  return local_offset_;
58  }
59 
60  private:
61  Rule* FindRuleFor(int year, int month, int day, int time_in_day_sec) {
62  Rule* result = NULL;
63  for (int i = 0; i < rules_count_; i++)
64  if (Match(&rules_[i], year, month, day, time_in_day_sec)) {
65  result = &rules_[i];
66  }
67  return result;
68  }
69 
70 
71  bool Match(Rule* rule, int year, int month, int day, int time_in_day_sec) {
72  if (rule->year != 0 && rule->year != year) return false;
73  if (rule->start_month > month) return false;
74  if (rule->end_month < month) return false;
75  int start_day = ComputeRuleDay(year, rule->start_month, rule->start_day);
76  if (rule->start_month == month && start_day > day) return false;
77  if (rule->start_month == month && start_day == day &&
78  2 * 3600 > time_in_day_sec)
79  return false;
80  int end_day = ComputeRuleDay(year, rule->end_month, rule->end_day);
81  if (rule->end_month == month && end_day < day) return false;
82  if (rule->end_month == month && end_day == day &&
83  2 * 3600 <= time_in_day_sec)
84  return false;
85  return true;
86  }
87 
88 
89  int ComputeRuleDay(int year, int month, int day) {
90  if (day != 0) return day;
91  int days = DaysFromYearMonth(year, month);
92  // Find the first Sunday of the month.
93  while (Weekday(days + day) != 6) day++;
94  return day + 1;
95  }
96 
97  int local_offset_;
98  Rule* rules_;
99  int rules_count_;
100 };
101 
102 static int64_t TimeFromYearMonthDay(DateCache* date_cache,
103  int year,
104  int month,
105  int day) {
106  int64_t result = date_cache->DaysFromYearMonth(year, month);
107  return (result + day - 1) * DateCache::kMsPerDay;
108 }
109 
110 
111 static void CheckDST(int64_t time) {
112  Isolate* isolate = CcTest::i_isolate();
113  DateCache* date_cache = isolate->date_cache();
114  int64_t actual = date_cache->ToLocal(time);
115  int64_t expected = time + date_cache->GetLocalOffsetFromOS() +
116  date_cache->GetDaylightSavingsOffsetFromOS(time / 1000);
117  CHECK_EQ(actual, expected);
118 }
119 
120 
121 TEST(DaylightSavingsTime) {
122  LocalContext context;
123  v8::Isolate* isolate = context->GetIsolate();
124  v8::HandleScope scope(isolate);
125  DateCacheMock::Rule rules[] = {
126  {0, 2, 0, 10, 0, 3600}, // DST from March to November in any year.
127  {2010, 2, 0, 7, 20, 3600}, // DST from March to August 20 in 2010.
128  {2010, 7, 20, 8, 10, 0}, // No DST from August 20 to September 10 in 2010.
129  {2010, 8, 10, 10, 0, 3600}, // DST from September 10 to November in 2010.
130  };
131 
132  int local_offset_ms = -36000000; // -10 hours.
133 
134  DateCacheMock* date_cache =
135  new DateCacheMock(local_offset_ms, rules, ARRAY_SIZE(rules));
136 
137  reinterpret_cast<Isolate*>(isolate)->set_date_cache(date_cache);
138 
139  int64_t start_of_2010 = TimeFromYearMonthDay(date_cache, 2010, 0, 1);
140  int64_t start_of_2011 = TimeFromYearMonthDay(date_cache, 2011, 0, 1);
141  int64_t august_20 = TimeFromYearMonthDay(date_cache, 2010, 7, 20);
142  int64_t september_10 = TimeFromYearMonthDay(date_cache, 2010, 8, 10);
143  CheckDST((august_20 + september_10) / 2);
144  CheckDST(september_10);
145  CheckDST(september_10 + 2 * 3600);
146  CheckDST(september_10 + 2 * 3600 - 1000);
147  CheckDST(august_20 + 2 * 3600);
148  CheckDST(august_20 + 2 * 3600 - 1000);
149  CheckDST(august_20);
150  // Check each day of 2010.
151  for (int64_t time = start_of_2011 + 2 * 3600;
152  time >= start_of_2010;
153  time -= DateCache::kMsPerDay) {
154  CheckDST(time);
155  CheckDST(time - 1000);
156  CheckDST(time + 1000);
157  }
158  // Check one day from 2010 to 2100.
159  for (int year = 2100; year >= 2010; year--) {
160  CheckDST(TimeFromYearMonthDay(date_cache, year, 5, 5));
161  }
162  CheckDST((august_20 + september_10) / 2);
163  CheckDST(september_10);
164  CheckDST(september_10 + 2 * 3600);
165  CheckDST(september_10 + 2 * 3600 - 1000);
166  CheckDST(august_20 + 2 * 3600);
167  CheckDST(august_20 + 2 * 3600 - 1000);
168  CheckDST(august_20);
169 }
170 
171 
172 TEST(DateCacheVersion) {
173  FLAG_allow_natives_syntax = true;
174  v8::Isolate* isolate = CcTest::isolate();
175  v8::Isolate::Scope isolate_scope(isolate);
176  v8::HandleScope scope(isolate);
177  v8::Handle<v8::Context> context = v8::Context::New(isolate);
178  v8::Context::Scope context_scope(context);
179  v8::Handle<v8::Array> date_cache_version =
180  v8::Handle<v8::Array>::Cast(CompileRun("%DateCacheVersion()"));
181 
182  CHECK_EQ(1, static_cast<int32_t>(date_cache_version->Length()));
183  CHECK(date_cache_version->Get(0)->IsNumber());
184  CHECK_EQ(0.0, date_cache_version->Get(0)->NumberValue());
185 
187 
188  CHECK_EQ(1, static_cast<int32_t>(date_cache_version->Length()));
189  CHECK(date_cache_version->Get(0)->IsNumber());
190  CHECK_EQ(1.0, date_cache_version->Get(0)->NumberValue());
191 }
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
DateCache * date_cache()
Definition: isolate.h:1025
#define CHECK_EQ(expected, value)
Definition: checks.h:252
Local< Value > Get(Handle< Value > key)
Definition: api.cc:3139
DateCacheMock(int local_offset, Rule *rules, int rules_count)
Definition: test-date.cc:42
virtual int GetLocalOffsetFromOS()
Definition: test-date.cc:56
virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec)
Definition: test-date.cc:46
#define CHECK(condition)
Definition: checks.h:75
v8::Isolate * GetIsolate()
Definition: api.cc:5233
virtual int GetLocalOffsetFromOS()
Definition: date.h:191
static V8_INLINE Handle< T > Cast(Handle< S > that)
Definition: v8.h:297
virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec)
Definition: date.h:186
static i::Isolate * i_isolate()
Definition: cctest.h:102
int DaysFromYearMonth(int year, int month)
Definition: date.cc:158
static const int64_t kMsPerDay
Definition: date.h:43
static Local< Context > New(Isolate *isolate, ExtensionConfiguration *extensions=NULL, Handle< ObjectTemplate > global_template=Handle< ObjectTemplate >(), Handle< Value > global_object=Handle< Value >())
Definition: api.cc:5188
uint32_t Length() const
Definition: api.cc:5800
static void DateTimeConfigurationChangeNotification(Isolate *isolate)
Definition: api.cc:5711
#define ARRAY_SIZE(a)
Definition: globals.h:333
int64_t ToLocal(int64_t time_ms)
Definition: date.h:129
static v8::Isolate * isolate()
Definition: cctest.h:96