Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / prmjtime.h
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is Mozilla Communicator client code, released
17  * March 31, 1998.
18  *
19  * The Initial Developer of the Original Code is
20  * Netscape Communications Corporation.
21  * Portions created by the Initial Developer are Copyright (C) 1998
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either of the GNU General Public License Version 2 or later (the "GPL"),
28  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39
40 #ifndef prmjtime_h___
41 #define prmjtime_h___
42 /*
43  * PR date stuff for mocha and java. Placed here temporarily not to break
44  * Navigator and localize changes to mocha.
45  */
46 #include <time.h>
47
48 #include "jslong.h"
49 #ifdef MOZILLA_CLIENT
50 #include "jscompat.h"
51 #endif
52
53 struct JSContext;
54
55 /*
56  * Implements a small cache for daylight saving time offset computation.
57  *
58  * The basic idea is premised upon this fact: the DST offset never changes more
59  * than once in any thirty-day period.  If we know the offset at t_0 is o_0,
60  * the offset at [t_1, t_2] is also o_0, where t_1 + 3_0 days == t_2,
61  * t_1 <= t_0, and t0 <= t2.  (In other words, t_0 is always somewhere within a
62  * thirty-day range where the DST offset is constant: DST changes never occur
63  * more than once in any thirty-day period.)  Therefore, if we intelligently
64  * retain knowledge of the offset for a range of dates (which may vary over
65  * time), and if requests are usually for dates within that range, we can often
66  * provide a response without repeated offset calculation.
67  *
68  * Our caching strategy is as follows: on the first request at date t_0 compute
69  * the requested offset o_0.  Save { start: t_0, end: t_0, offset: o_0 } as the
70  * cache's state.  Subsequent requests within that range are straightforwardly
71  * handled.  If a request for t_i is far outside the range (more than thirty
72  * days), compute o_i = dstOffset(t_i) and save { start: t_i, end: t_i,
73  * offset: t_i }.  Otherwise attempt to *overextend* the range to either
74  * [start - 30d, end] or [start, end + 30d] as appropriate to encompass
75  * t_i.  If the offset o_i30 is the same as the cached offset, extend the
76  * range.  Otherwise the over-guess crossed a DST change -- compute
77  * o_i = dstOffset(t_i) and either extend the original range (if o_i == offset)
78  * or start a new one beneath/above the current one with o_i30 as the offset.
79  *
80  * This cache strategy results in 0 to 2 DST offset computations.  The naive
81  * always-compute strategy is 1 computation, and since cache maintenance is a
82  * handful of integer arithmetic instructions the speed difference between
83  * always-1 and 1-with-cache is negligible.  Caching loses if two computations
84  * happen: when the date is within 30 days of the cached range and when that
85  * 30-day range crosses a DST change.  This is relatively uncommon.  Further,
86  * instances of such are often dominated by in-range hits, so caching is an
87  * overall slight win.
88  *
89  * Why 30 days?  For correctness the duration must be smaller than any possible
90  * duration between DST changes.  Past that, note that 1) a large duration
91  * increases the likelihood of crossing a DST change while reducing the number
92  * of cache misses, and 2) a small duration decreases the size of the cached
93  * range while producing more misses.  Using a month as the interval change is
94  * a balance between these two that tries to optimize for the calendar month at
95  * a time that a site might display.  (One could imagine an adaptive duration
96  * that accommodates near-DST-change dates better; we don't believe the
97  * potential win from better caching offsets the loss from extra complexity.)
98  */
99 class DSTOffsetCache {
100   public:
101     inline DSTOffsetCache();
102     JSInt64 getDSTOffsetMilliseconds(int64 localTimeMilliseconds, JSContext *cx);
103
104     inline void purge();
105 #ifdef JS_METER_DST_OFFSET_CACHING
106     void dumpStats();
107 #endif
108
109   private:
110     JSInt64 computeDSTOffsetMilliseconds(int64 localTimeSeconds);
111
112     JSInt64 offsetMilliseconds;
113     JSInt64 rangeStartSeconds, rangeEndSeconds;
114
115     JSInt64 oldOffsetMilliseconds;
116     JSInt64 oldRangeStartSeconds, oldRangeEndSeconds;
117
118 #ifdef JS_METER_DST_OFFSET_CACHING
119     size_t totalCalculations;
120     size_t hit;
121     size_t missIncreasing;
122     size_t missDecreasing;
123     size_t missIncreasingOffsetChangeUpper;
124     size_t missIncreasingOffsetChangeExpand;
125     size_t missLargeIncrease;
126     size_t missDecreasingOffsetChangeLower;
127     size_t missDecreasingOffsetChangeExpand;
128     size_t missLargeDecrease;
129 #endif
130
131     static const JSInt64 MAX_UNIX_TIMET = 2145859200; /* time_t 12/31/2037 */
132     static const JSInt64 MILLISECONDS_PER_SECOND = 1000;
133     static const JSInt64 SECONDS_PER_MINUTE = 60;
134     static const JSInt64 SECONDS_PER_HOUR = 60 * SECONDS_PER_MINUTE;
135     static const JSInt64 SECONDS_PER_DAY = 24 * SECONDS_PER_HOUR;
136
137     static const JSInt64 RANGE_EXPANSION_AMOUNT = 30 * SECONDS_PER_DAY;
138
139   private:
140     void sanityCheck();
141
142 #ifdef JS_METER_DST_OFFSET_CACHING
143 #define NOTE_GENERIC(member) this->member++
144 #else
145 #define NOTE_GENERIC(member) ((void)0)
146 #endif
147     void noteOffsetCalculation() {
148         NOTE_GENERIC(totalCalculations);
149     }
150     void noteCacheHit() {
151         NOTE_GENERIC(hit);
152     }
153     void noteCacheMissIncrease() {
154         NOTE_GENERIC(missIncreasing);
155     }
156     void noteCacheMissDecrease() {
157         NOTE_GENERIC(missDecreasing);
158     }
159     void noteCacheMissIncreasingOffsetChangeUpper() {
160         NOTE_GENERIC(missIncreasingOffsetChangeUpper);
161     }
162     void noteCacheMissIncreasingOffsetChangeExpand() {
163         NOTE_GENERIC(missIncreasingOffsetChangeExpand);
164     }
165     void noteCacheMissLargeIncrease() {
166         NOTE_GENERIC(missLargeIncrease);
167     }
168     void noteCacheMissDecreasingOffsetChangeLower() {
169         NOTE_GENERIC(missDecreasingOffsetChangeLower);
170     }
171     void noteCacheMissDecreasingOffsetChangeExpand() {
172         NOTE_GENERIC(missDecreasingOffsetChangeExpand);
173     }
174     void noteCacheMissLargeDecrease() {
175         NOTE_GENERIC(missLargeDecrease);
176     }
177 #undef NOTE_GENERIC
178 };
179
180 JS_BEGIN_EXTERN_C
181
182 typedef struct PRMJTime       PRMJTime;
183
184 /*
185  * Broken down form of 64 bit time value.
186  */
187 struct PRMJTime {
188     JSInt32 tm_usec;            /* microseconds of second (0-999999) */
189     JSInt8 tm_sec;              /* seconds of minute (0-59) */
190     JSInt8 tm_min;              /* minutes of hour (0-59) */
191     JSInt8 tm_hour;             /* hour of day (0-23) */
192     JSInt8 tm_mday;             /* day of month (1-31) */
193     JSInt8 tm_mon;              /* month of year (0-11) */
194     JSInt8 tm_wday;             /* 0=sunday, 1=monday, ... */
195     JSInt32 tm_year;            /* absolute year, AD */
196     JSInt16 tm_yday;            /* day of year (0 to 365) */
197     JSInt8 tm_isdst;            /* non-zero if DST in effect */
198 };
199
200 /* Some handy constants */
201 #define PRMJ_USEC_PER_SEC       1000000L
202 #define PRMJ_USEC_PER_MSEC      1000L
203
204 /* Return the current local time in micro-seconds */
205 extern JSInt64
206 PRMJ_Now(void);
207
208 /* Release the resources associated with PRMJ_Now; don't call PRMJ_Now again */
209 #if defined(JS_THREADSAFE) && (defined(HAVE_GETSYSTEMTIMEASFILETIME) || defined(HAVE_SYSTEMTIMETOFILETIME))
210 extern void
211 PRMJ_NowShutdown(void);
212 #else
213 #define PRMJ_NowShutdown()
214 #endif
215
216 /* get the difference between this time zone and  gmt timezone in seconds */
217 extern JSInt32
218 PRMJ_LocalGMTDifference(void);
219
220 /* Format a time value into a buffer. Same semantics as strftime() */
221 extern size_t
222 PRMJ_FormatTime(char *buf, int buflen, const char *fmt, PRMJTime *tm);
223
224 JS_END_EXTERN_C
225
226 #endif /* prmjtime_h___ */
227