Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
3 : * This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef vm_DateTime_h
8 : #define vm_DateTime_h
9 :
10 : #include "mozilla/Assertions.h"
11 : #include "mozilla/Attributes.h"
12 : #include "mozilla/FloatingPoint.h"
13 : #include "mozilla/MathAlgorithms.h"
14 :
15 : #include <stdint.h>
16 :
17 : #include "js/Conversions.h"
18 : #include "js/Date.h"
19 : #include "js/Initialization.h"
20 : #include "js/Value.h"
21 : #include "threading/ExclusiveData.h"
22 :
23 : namespace js {
24 :
25 : /* Constants defined by ES5 15.9.1.10. */
26 : const double HoursPerDay = 24;
27 : const double MinutesPerHour = 60;
28 : const double SecondsPerMinute = 60;
29 : const double msPerSecond = 1000;
30 : const double msPerMinute = msPerSecond * SecondsPerMinute;
31 : const double msPerHour = msPerMinute * MinutesPerHour;
32 :
33 : /* ES5 15.9.1.2. */
34 : const double msPerDay = msPerHour * HoursPerDay;
35 :
36 : /*
37 : * Additional quantities not mentioned in the spec. Be careful using these!
38 : * They aren't doubles (and aren't defined in terms of all the other constants
39 : * so that they can be used in constexpr scenarios; if you need constants that
40 : * trigger floating point semantics, you'll have to manually cast to get it.
41 : */
42 : const unsigned SecondsPerHour = 60 * 60;
43 : const unsigned SecondsPerDay = SecondsPerHour * 24;
44 :
45 : const double StartOfTime = -8.64e15;
46 : const double EndOfTime = 8.64e15;
47 :
48 : extern bool
49 : InitDateTimeState();
50 :
51 : extern void
52 : FinishDateTimeState();
53 :
54 : /*
55 : * Stores date/time information, particularly concerning the current local
56 : * time zone, and implements a small cache for daylight saving time offset
57 : * computation.
58 : *
59 : * The basic idea is premised upon this fact: the DST offset never changes more
60 : * than once in any thirty-day period. If we know the offset at t_0 is o_0,
61 : * the offset at [t_1, t_2] is also o_0, where t_1 + 3_0 days == t_2,
62 : * t_1 <= t_0, and t0 <= t2. (In other words, t_0 is always somewhere within a
63 : * thirty-day range where the DST offset is constant: DST changes never occur
64 : * more than once in any thirty-day period.) Therefore, if we intelligently
65 : * retain knowledge of the offset for a range of dates (which may vary over
66 : * time), and if requests are usually for dates within that range, we can often
67 : * provide a response without repeated offset calculation.
68 : *
69 : * Our caching strategy is as follows: on the first request at date t_0 compute
70 : * the requested offset o_0. Save { start: t_0, end: t_0, offset: o_0 } as the
71 : * cache's state. Subsequent requests within that range are straightforwardly
72 : * handled. If a request for t_i is far outside the range (more than thirty
73 : * days), compute o_i = dstOffset(t_i) and save { start: t_i, end: t_i,
74 : * offset: t_i }. Otherwise attempt to *overextend* the range to either
75 : * [start - 30d, end] or [start, end + 30d] as appropriate to encompass
76 : * t_i. If the offset o_i30 is the same as the cached offset, extend the
77 : * range. Otherwise the over-guess crossed a DST change -- compute
78 : * o_i = dstOffset(t_i) and either extend the original range (if o_i == offset)
79 : * or start a new one beneath/above the current one with o_i30 as the offset.
80 : *
81 : * This cache strategy results in 0 to 2 DST offset computations. The naive
82 : * always-compute strategy is 1 computation, and since cache maintenance is a
83 : * handful of integer arithmetic instructions the speed difference between
84 : * always-1 and 1-with-cache is negligible. Caching loses if two computations
85 : * happen: when the date is within 30 days of the cached range and when that
86 : * 30-day range crosses a DST change. This is relatively uncommon. Further,
87 : * instances of such are often dominated by in-range hits, so caching is an
88 : * overall slight win.
89 : *
90 : * Why 30 days? For correctness the duration must be smaller than any possible
91 : * duration between DST changes. Past that, note that 1) a large duration
92 : * increases the likelihood of crossing a DST change while reducing the number
93 : * of cache misses, and 2) a small duration decreases the size of the cached
94 : * range while producing more misses. Using a month as the interval change is
95 : * a balance between these two that tries to optimize for the calendar month at
96 : * a time that a site might display. (One could imagine an adaptive duration
97 : * that accommodates near-DST-change dates better; we don't believe the
98 : * potential win from better caching offsets the loss from extra complexity.)
99 : */
100 : class DateTimeInfo
101 : {
102 : static ExclusiveData<DateTimeInfo>* instance;
103 : friend class ExclusiveData<DateTimeInfo>;
104 :
105 : friend bool InitDateTimeState();
106 : friend void FinishDateTimeState();
107 :
108 : DateTimeInfo();
109 :
110 : public:
111 : // The spec implicitly assumes DST and time zone adjustment information
112 : // never change in the course of a function -- sometimes even across
113 : // reentrancy. So make critical sections as narrow as possible.
114 :
115 : /*
116 : * Get the DST offset in milliseconds at a UTC time. This is usually
117 : * either 0 or |msPerSecond * SecondsPerHour|, but at least one exotic time
118 : * zone (Lord Howe Island, Australia) has a fractional-hour offset, just to
119 : * keep things interesting.
120 : */
121 3 : static int64_t getDSTOffsetMilliseconds(int64_t utcMilliseconds) {
122 6 : auto guard = instance->lock();
123 6 : return guard->internalGetDSTOffsetMilliseconds(utcMilliseconds);
124 : }
125 :
126 : /* ES5 15.9.1.7. */
127 5 : static double localTZA() {
128 10 : auto guard = instance->lock();
129 10 : return guard->localTZA_;
130 : }
131 :
132 : private:
133 : // We don't want anyone accidentally calling *only*
134 : // DateTimeInfo::updateTimeZoneAdjustment() to respond to a system time
135 : // zone change (missing the necessary poking of ICU as well), so ensure
136 : // only JS::ResetTimeZone() can call this via access restrictions.
137 : friend void JS::ResetTimeZone();
138 :
139 322 : static void updateTimeZoneAdjustment() {
140 644 : auto guard = instance->lock();
141 322 : guard->internalUpdateTimeZoneAdjustment();
142 322 : }
143 :
144 : /*
145 : * The current local time zone adjustment, cached because retrieving this
146 : * dynamically is Slow, and a certain venerable benchmark which shall not
147 : * be named depends on it being fast.
148 : *
149 : * SpiderMonkey occasionally and arbitrarily updates this value from the
150 : * system time zone to attempt to keep this reasonably up-to-date. If
151 : * temporary inaccuracy can't be tolerated, JSAPI clients may call
152 : * JS::ResetTimeZone to forcibly sync this with the system time zone.
153 : */
154 : double localTZA_;
155 :
156 : /*
157 : * Compute the DST offset at the given UTC time in seconds from the epoch.
158 : * (getDSTOffsetMilliseconds attempts to return a cached value, but in case
159 : * of a cache miss it calls this method. The cache is represented through
160 : * the offset* and *{Start,End}Seconds fields below.)
161 : */
162 : int64_t computeDSTOffsetMilliseconds(int64_t utcSeconds);
163 :
164 : int64_t offsetMilliseconds;
165 : int64_t rangeStartSeconds, rangeEndSeconds; // UTC-based
166 :
167 : int64_t oldOffsetMilliseconds;
168 : int64_t oldRangeStartSeconds, oldRangeEndSeconds; // UTC-based
169 :
170 : /*
171 : * Cached offset in seconds from the current UTC time to the current
172 : * local standard time (i.e. not including any offset due to DST).
173 : */
174 : int32_t utcToLocalStandardOffsetSeconds;
175 :
176 : static const int64_t MaxUnixTimeT = 2145859200; /* time_t 12/31/2037 */
177 :
178 : static const int64_t RangeExpansionAmount = 30 * SecondsPerDay;
179 :
180 : int64_t internalGetDSTOffsetMilliseconds(int64_t utcMilliseconds);
181 : void internalUpdateTimeZoneAdjustment();
182 :
183 : void sanityCheck();
184 : };
185 :
186 : enum class IcuTimeZoneStatus { Valid, NeedsUpdate };
187 :
188 : extern ExclusiveData<IcuTimeZoneStatus>*
189 : IcuTimeZoneState;
190 :
191 : /**
192 : * ICU's default time zone, used for various date/time formatting operations
193 : * that include the local time in the representation, is allowed to go stale
194 : * for unfortunate performance reasons. Call this function when an up-to-date
195 : * default time zone is required, to resync ICU's default time zone with
196 : * reality.
197 : */
198 : extern void
199 : ResyncICUDefaultTimeZone();
200 :
201 : } /* namespace js */
202 :
203 : #endif /* vm_DateTime_h */
|