v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
date.h
Go to the documentation of this file.
1// Copyright 2012 the V8 project 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#ifndef V8_DATE_DATE_H_
6#define V8_DATE_DATE_H_
7
8#include <cmath>
9
12#include "src/common/globals.h"
13#include "src/objects/smi.h"
14
15namespace v8 {
16namespace internal {
17
19 public:
20 static const int kMsPerMin = 60 * 1000;
21 static const int kSecPerDay = 24 * 60 * 60;
22 static const int64_t kMsPerDay = kSecPerDay * 1000;
23 static const int64_t kMsPerMonth = kMsPerDay * 30;
24
25 // The largest time that can be passed to OS date-time library functions.
26 static const int kMaxEpochTimeInSec = kMaxInt;
27 static const int64_t kMaxEpochTimeInMs = static_cast<int64_t>(kMaxInt) * 1000;
28
29 // The largest time that can be stored in JSDate.
30 static const int64_t kMaxTimeInMs =
31 static_cast<int64_t>(864000000) * 10000000;
32
33 // Conservative upper bound on time that can be stored in JSDate
34 // before UTC conversion.
35 static const int64_t kMaxTimeBeforeUTCInMs = kMaxTimeInMs + kMsPerMonth;
36
37 // Sentinel that denotes an invalid local offset.
38 static const int kInvalidLocalOffsetInMs = kMaxInt;
39 // Sentinel that denotes an invalid cache stamp.
40 // It is an invariant of DateCache that cache stamp is non-negative.
41 static const int kInvalidStamp = -1;
42
43 DateCache();
44
45 virtual ~DateCache() {
46 delete tz_cache_;
47 tz_cache_ = nullptr;
48 }
49
50 // Clears cached timezone information and increments the cache stamp.
51 void ResetDateCache(
52 base::TimezoneCache::TimeZoneDetection time_zone_detection);
53
54 // Computes floor(time_ms / kMsPerDay).
55 static int DaysFromTime(int64_t time_ms) {
56 if (time_ms < 0) time_ms -= (kMsPerDay - 1);
57 return static_cast<int>(time_ms / kMsPerDay);
58 }
59
60 // Computes modulo(time_ms, kMsPerDay) given that
61 // days = floor(time_ms / kMsPerDay).
62 static int TimeInDay(int64_t time_ms, int days) {
63 return static_cast<int>(time_ms - days * kMsPerDay);
64 }
65
66 // Performs the success path of the ECMA 262 TimeClip operation (when the
67 // value is within the range, truncates it to an integer). Returns false if
68 // the value is outside the range, and should be clipped to NaN.
69 // ECMA 262 - ES#sec-timeclip TimeClip (time)
70 static bool TryTimeClip(double* time) {
71 if (-kMaxTimeInMs <= *time && *time <= kMaxTimeInMs) {
72 // Inline the finite part of DoubleToInteger here, since the range check
73 // already covers the non-finite checks.
74 *time = ((*time > 0) ? std::floor(*time) : std::ceil(*time)) + 0.0;
75 return true;
76 }
77 return false;
78 }
79
80 // Given the number of days since the epoch, computes the weekday.
81 // ECMA 262 - 15.9.1.6.
82 int Weekday(int days) {
83 int result = (days + 4) % 7;
84 return result >= 0 ? result : result + 7;
85 }
86
87 bool IsLeap(int year) {
88 return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
89 }
90
91 // ECMA 262 - ES#sec-local-time-zone-adjustment
92 int LocalOffsetInMs(int64_t time, bool is_utc);
93
94 const char* LocalTimezone(int64_t time_ms) {
95 if (time_ms < 0 || time_ms > kMaxEpochTimeInMs) {
96 time_ms = EquivalentTime(time_ms);
97 }
98 bool is_dst = DaylightSavingsOffsetInMs(time_ms) != 0;
99 const char** name = is_dst ? &dst_tz_name_ : &tz_name_;
100 if (*name == nullptr) {
101 *name = tz_cache_->LocalTimezone(static_cast<double>(time_ms));
102 }
103 return *name;
104 }
105
106 // ECMA 262 - 15.9.5.26
107 int TimezoneOffset(int64_t time_ms) {
108 int64_t local_ms = ToLocal(time_ms);
109 return static_cast<int>((time_ms - local_ms) / kMsPerMin);
110 }
111
112 // ECMA 262 - ES#sec-localtime-t
113 // LocalTime(t) = t + LocalTZA(t, true)
114 int64_t ToLocal(int64_t time_ms) {
115 return time_ms + LocalOffsetInMs(time_ms, true);
116 }
117
118 // ECMA 262 - ES#sec-utc-t
119 // UTC(t) = t - LocalTZA(t, false)
120 int64_t ToUTC(int64_t time_ms) {
121 return time_ms - LocalOffsetInMs(time_ms, false);
122 }
123
124 // Computes a time equivalent to the given time according
125 // to ECMA 262 - 15.9.1.9.
126 // The issue here is that some library calls don't work right for dates
127 // that cannot be represented using a non-negative signed 32 bit integer
128 // (measured in whole seconds based on the 1970 epoch).
129 // We solve this by mapping the time to a year with same leap-year-ness
130 // and same starting day for the year. The ECMAscript specification says
131 // we must do this, but for compatibility with other browsers, we use
132 // the actual year if it is in the range 1970..2037
133 int64_t EquivalentTime(int64_t time_ms) {
134 int days = DaysFromTime(time_ms);
135 int time_within_day_ms = static_cast<int>(time_ms - days * kMsPerDay);
136 int year, month, day;
137 YearMonthDayFromDays(days, &year, &month, &day);
138 int new_days = DaysFromYearMonth(EquivalentYear(year), month) + day - 1;
139 return static_cast<int64_t>(new_days) * kMsPerDay + time_within_day_ms;
140 }
141
142 // Returns an equivalent year in the range [2008-2035] matching
143 // - leap year,
144 // - week day of first day.
145 // ECMA 262 - 15.9.1.9.
146 int EquivalentYear(int year) {
147 int week_day = Weekday(DaysFromYearMonth(year, 0));
148 int recent_year = (IsLeap(year) ? 1956 : 1967) + (week_day * 12) % 28;
149 // Find the year in the range 2008..2037 that is equivalent mod 28.
150 // Add 3*28 to give a positive argument to the modulus operator.
151 return 2008 + (recent_year + 3 * 28 - 2008) % 28;
152 }
153
154 // Given the number of days since the epoch, computes
155 // the corresponding year, month, and day.
156 void YearMonthDayFromDays(int days, int* year, int* month, int* day);
157
158 // Computes the number of days since the epoch for
159 // the first day of the given month in the given year.
160 int DaysFromYearMonth(int year, int month);
161
162 // Breaks down the time value.
163 void BreakDownTime(int64_t time_ms, int* year, int* month, int* day,
164 int* weekday, int* hour, int* min, int* sec, int* ms);
165
166 // Cache stamp is used for invalidating caches in JSDate.
167 // We increment the stamp each time when the timezone information changes.
168 // JSDate objects perform stamp check and invalidate their caches if
169 // their saved stamp is not equal to the current stamp.
170 Tagged<Smi> stamp() { return stamp_; }
171 void* stamp_address() { return &stamp_; }
172
173 // These functions are virtual so that we can override them when testing.
174 virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec) {
175 double time_ms = static_cast<double>(time_sec * 1000);
176 return static_cast<int>(tz_cache_->DaylightSavingsOffset(time_ms));
177 }
178
179 virtual int GetLocalOffsetFromOS(int64_t time_ms, bool is_utc);
180
181 private:
182 // The implementation relies on the fact that no time zones have more than one
183 // time zone offset change (including DST offset changes) per 19 days. In
184 // Egypt in 2010 they decided to suspend DST during Ramadan. This led to a
185 // short interval where DST is in effect from September 10 to September 30.
186 static const int kDefaultTimeZoneOffsetDeltaInMs = 19 * kSecPerDay * 1000;
187
188 static const int kCacheSize = 32;
189
190 // Stores a segment of time where time zone offset does not change.
191 struct CacheItem {
192 int64_t start_ms;
193 int64_t end_ms;
196 };
197
198 // Computes the daylight savings offset for the given time.
199 // ECMA 262 - 15.9.1.8
200 int DaylightSavingsOffsetInMs(int64_t time_ms) {
201 int time_sec = (time_ms >= 0 && time_ms <= kMaxEpochTimeInMs)
202 ? static_cast<int>(time_ms / 1000)
203 : static_cast<int>(EquivalentTime(time_ms) / 1000);
204 return GetDaylightSavingsOffsetFromOS(time_sec);
205 }
206
207 // Sets the before_ and the after_ segments from the timezone offset cache
208 // such that the before_ segment starts earlier than the given time and the
209 // after_ segment start later than the given time. Both segments might be
210 // invalid. The last_used counters of the before_ and after_ are updated.
211 void ProbeCache(int64_t time_ms);
212
213 // Finds the least recently used segment from the timezone offset cache that
214 // is not equal to the given 'skip' segment.
215 CacheItem* LeastRecentlyUsedCacheItem(CacheItem* skip);
216
217 // Extends the after_ segment with the given point or resets it
218 // if it starts later than the given time + kDefaultDSTDeltaInMs.
219 inline void ExtendTheAfterSegment(int64_t time_sec, int offset_ms);
220
221 // Makes the given segment invalid.
222 inline void ClearSegment(CacheItem* segment);
223
224 bool InvalidSegment(CacheItem* segment) {
225 return segment->start_ms > segment->end_ms;
226 }
227
229
230 // Daylight Saving Time cache.
231 CacheItem cache_[kCacheSize];
235
237
238 // Year/Month/Day cache.
244
245 // Timezone name cache
246 const char* tz_name_;
247 const char* dst_tz_name_;
248
250};
251
252// Routines shared between Date and Temporal
253
254// ES6 section 20.3.1.14 MakeDate (day, time)
255double MakeDate(double day, double time);
256
257// ES6 section 20.3.1.13 MakeDay (year, month, date)
258double MakeDay(double year, double month, double date);
259
260// ES6 section 20.3.1.12 MakeTime (hour, min, sec, ms)
261double MakeTime(double hour, double min, double sec, double ms);
262
264
272
273// ES6 section 20.3.4.41.1 ToDateString(tv)
274DateBuffer ToDateString(double time_val, DateCache* date_cache,
275 ToDateStringMode mode);
276
277double ParseDateTimeString(Isolate* isolate, DirectHandle<String> str);
278
279} // namespace internal
280} // namespace v8
281
282#endif // V8_DATE_DATE_H_
const char * name
Definition builtins.cc:39
virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec)
Definition date.h:174
int EquivalentYear(int year)
Definition date.h:146
bool IsLeap(int year)
Definition date.h:87
static int TimeInDay(int64_t time_ms, int days)
Definition date.h:62
bool InvalidSegment(CacheItem *segment)
Definition date.h:224
int DaylightSavingsOffsetInMs(int64_t time_ms)
Definition date.h:200
Tagged< Smi > stamp_
Definition date.h:228
const char * LocalTimezone(int64_t time_ms)
Definition date.h:94
base::TimezoneCache * tz_cache_
Definition date.h:249
int64_t ToLocal(int64_t time_ms)
Definition date.h:114
static bool TryTimeClip(double *time)
Definition date.h:70
int Weekday(int days)
Definition date.h:82
int64_t ToUTC(int64_t time_ms)
Definition date.h:120
static int DaysFromTime(int64_t time_ms)
Definition date.h:55
Tagged< Smi > stamp()
Definition date.h:170
const char * tz_name_
Definition date.h:246
const char * dst_tz_name_
Definition date.h:247
CacheItem * after_
Definition date.h:234
void * stamp_address()
Definition date.h:171
int TimezoneOffset(int64_t time_ms)
Definition date.h:107
CacheItem * before_
Definition date.h:233
virtual ~DateCache()
Definition date.h:45
int64_t EquivalentTime(int64_t time_ms)
Definition date.h:133
double hour
double days
DateRecord date
ZoneVector< RpoNumber > & result
double MakeDate(double day, double time)
Definition date.cc:491
ToDateStringMode
Definition date.h:265
DateBuffer ToDateString(double time_val, DateCache *date_cache, ToDateStringMode mode)
Definition date.cc:572
base::SmallVector< char, 128 > DateBuffer
Definition date.h:263
double MakeDay(double year, double month, double date)
Definition date.cc:498
double MakeTime(double hour, double min, double sec, double ms)
Definition date.cc:541
constexpr int kMaxInt
Definition globals.h:374
double ParseDateTimeString(Isolate *isolate, DirectHandle< String > str)
Definition date.cc:626
bool ToLocal(v8::internal::MaybeDirectHandle< v8::internal::Object > maybe, Local< T > *local)
Definition api.h:303
#define V8_EXPORT_PRIVATE
Definition macros.h:460