1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | *----------------------------------------------------------------------
|
---|
40 | *
|
---|
41 | * prtime.h --
|
---|
42 | *
|
---|
43 | * NSPR date and time functions
|
---|
44 | *
|
---|
45 | *-----------------------------------------------------------------------
|
---|
46 | */
|
---|
47 |
|
---|
48 | #ifndef prtime_h___
|
---|
49 | #define prtime_h___
|
---|
50 |
|
---|
51 | #include "prlong.h"
|
---|
52 |
|
---|
53 | PR_BEGIN_EXTERN_C
|
---|
54 |
|
---|
55 | /**********************************************************************/
|
---|
56 | /************************* TYPES AND CONSTANTS ************************/
|
---|
57 | /**********************************************************************/
|
---|
58 |
|
---|
59 | #define PR_MSEC_PER_SEC 1000UL
|
---|
60 | #define PR_USEC_PER_SEC 1000000UL
|
---|
61 | #define PR_NSEC_PER_SEC 1000000000UL
|
---|
62 | #define PR_USEC_PER_MSEC 1000UL
|
---|
63 | #define PR_NSEC_PER_MSEC 1000000UL
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * PRTime --
|
---|
67 | *
|
---|
68 | * NSPR represents basic time as 64-bit signed integers relative
|
---|
69 | * to midnight (00:00:00), January 1, 1970 Greenwich Mean Time (GMT).
|
---|
70 | * (GMT is also known as Coordinated Universal Time, UTC.)
|
---|
71 | * The units of time are in microseconds. Negative times are allowed
|
---|
72 | * to represent times prior to the January 1970 epoch. Such values are
|
---|
73 | * intended to be exported to other systems or converted to human
|
---|
74 | * readable form.
|
---|
75 | *
|
---|
76 | * Notes on porting: PRTime corresponds to time_t in ANSI C. NSPR 1.0
|
---|
77 | * simply uses PRInt64.
|
---|
78 | */
|
---|
79 |
|
---|
80 | typedef PRInt64 PRTime;
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Time zone and daylight saving time corrections applied to GMT to
|
---|
84 | * obtain the local time of some geographic location
|
---|
85 | */
|
---|
86 |
|
---|
87 | typedef struct PRTimeParameters {
|
---|
88 | PRInt32 tp_gmt_offset; /* the offset from GMT in seconds */
|
---|
89 | PRInt32 tp_dst_offset; /* contribution of DST in seconds */
|
---|
90 | } PRTimeParameters;
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * PRExplodedTime --
|
---|
94 | *
|
---|
95 | * Time broken down into human-readable components such as year, month,
|
---|
96 | * day, hour, minute, second, and microsecond. Time zone and daylight
|
---|
97 | * saving time corrections may be applied. If they are applied, the
|
---|
98 | * offsets from the GMT must be saved in the 'tm_params' field so that
|
---|
99 | * all the information is available to reconstruct GMT.
|
---|
100 | *
|
---|
101 | * Notes on porting: PRExplodedTime corrresponds to struct tm in
|
---|
102 | * ANSI C, with the following differences:
|
---|
103 | * - an additional field tm_usec;
|
---|
104 | * - replacing tm_isdst by tm_params;
|
---|
105 | * - the month field is spelled tm_month, not tm_mon;
|
---|
106 | * - we use absolute year, AD, not the year since 1900.
|
---|
107 | * The corresponding type in NSPR 1.0 is called PRTime. Below is
|
---|
108 | * a table of date/time type correspondence in the three APIs:
|
---|
109 | * API time since epoch time in components
|
---|
110 | * ANSI C time_t struct tm
|
---|
111 | * NSPR 1.0 PRInt64 PRTime
|
---|
112 | * NSPR 2.0 PRTime PRExplodedTime
|
---|
113 | */
|
---|
114 |
|
---|
115 | typedef struct PRExplodedTime {
|
---|
116 | PRInt32 tm_usec; /* microseconds past tm_sec (0-99999) */
|
---|
117 | PRInt32 tm_sec; /* seconds past tm_min (0-61, accomodating
|
---|
118 | up to two leap seconds) */
|
---|
119 | PRInt32 tm_min; /* minutes past tm_hour (0-59) */
|
---|
120 | PRInt32 tm_hour; /* hours past tm_day (0-23) */
|
---|
121 | PRInt32 tm_mday; /* days past tm_mon (1-31, note that it
|
---|
122 | starts from 1) */
|
---|
123 | PRInt32 tm_month; /* months past tm_year (0-11, Jan = 0) */
|
---|
124 | PRInt16 tm_year; /* absolute year, AD (note that we do not
|
---|
125 | count from 1900) */
|
---|
126 |
|
---|
127 | PRInt8 tm_wday; /* calculated day of the week
|
---|
128 | (0-6, Sun = 0) */
|
---|
129 | PRInt16 tm_yday; /* calculated day of the year
|
---|
130 | (0-365, Jan 1 = 0) */
|
---|
131 |
|
---|
132 | PRTimeParameters tm_params; /* time parameters used by conversion */
|
---|
133 | } PRExplodedTime;
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * PRTimeParamFn --
|
---|
137 | *
|
---|
138 | * A function of PRTimeParamFn type returns the time zone and
|
---|
139 | * daylight saving time corrections for some geographic location,
|
---|
140 | * given the current time in GMT. The input argument gmt should
|
---|
141 | * point to a PRExplodedTime that is in GMT, i.e., whose
|
---|
142 | * tm_params contains all 0's.
|
---|
143 | *
|
---|
144 | * For any time zone other than GMT, the computation is intended to
|
---|
145 | * consist of two steps:
|
---|
146 | * - Figure out the time zone correction, tp_gmt_offset. This number
|
---|
147 | * usually depends on the geographic location only. But it may
|
---|
148 | * also depend on the current time. For example, all of China
|
---|
149 | * is one time zone right now. But this situation may change
|
---|
150 | * in the future.
|
---|
151 | * - Figure out the daylight saving time correction, tp_dst_offset.
|
---|
152 | * This number depends on both the geographic location and the
|
---|
153 | * current time. Most of the DST rules are expressed in local
|
---|
154 | * current time. If so, one should apply the time zone correction
|
---|
155 | * to GMT before applying the DST rules.
|
---|
156 | */
|
---|
157 |
|
---|
158 | typedef PRTimeParameters (PR_CALLBACK *PRTimeParamFn)(const PRExplodedTime *gmt);
|
---|
159 |
|
---|
160 | /**********************************************************************/
|
---|
161 | /****************************** FUNCTIONS *****************************/
|
---|
162 | /**********************************************************************/
|
---|
163 |
|
---|
164 | /*
|
---|
165 | * The PR_Now routine returns the current time relative to the
|
---|
166 | * epoch, midnight, January 1, 1970 UTC. The units of the returned
|
---|
167 | * value are microseconds since the epoch.
|
---|
168 | *
|
---|
169 | * The values returned are not guaranteed to advance in a linear fashion
|
---|
170 | * due to the application of time correction protocols which synchronize
|
---|
171 | * computer clocks to some external time source. Consequently it should
|
---|
172 | * not be depended on for interval timing.
|
---|
173 | *
|
---|
174 | * The implementation is machine dependent.
|
---|
175 | * Cf. time_t time(time_t *tp) in ANSI C.
|
---|
176 | */
|
---|
177 | #if defined(HAVE_WATCOM_BUG_2)
|
---|
178 | PRTime __pascal __export __loadds
|
---|
179 | #else
|
---|
180 | NSPR_API(PRTime)
|
---|
181 | #endif
|
---|
182 | PR_Now(void);
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * Expand time binding it to time parameters provided by PRTimeParamFn.
|
---|
186 | * The calculation is envisoned to proceed in the following steps:
|
---|
187 | * - From given PRTime, calculate PRExplodedTime in GMT
|
---|
188 | * - Apply the given PRTimeParamFn to the GMT that we just calculated
|
---|
189 | * to obtain PRTimeParameters.
|
---|
190 | * - Add the PRTimeParameters offsets to GMT to get the local time
|
---|
191 | * as PRExplodedTime.
|
---|
192 | */
|
---|
193 |
|
---|
194 | NSPR_API(void) PR_ExplodeTime(
|
---|
195 | PRTime usecs, PRTimeParamFn params, PRExplodedTime *exploded);
|
---|
196 |
|
---|
197 | /* Reverse operation of PR_ExplodeTime */
|
---|
198 | #if defined(HAVE_WATCOM_BUG_2)
|
---|
199 | PRTime __pascal __export __loadds
|
---|
200 | #else
|
---|
201 | NSPR_API(PRTime)
|
---|
202 | #endif
|
---|
203 | PR_ImplodeTime(const PRExplodedTime *exploded);
|
---|
204 |
|
---|
205 | /*
|
---|
206 | * Adjust exploded time to normalize field overflows after manipulation.
|
---|
207 | * Note that the following fields of PRExplodedTime should not be
|
---|
208 | * manipulated:
|
---|
209 | * - tm_month and tm_year: because the number of days in a month and
|
---|
210 | * number of days in a year are not constant, it is ambiguous to
|
---|
211 | * manipulate the month and year fields, although one may be tempted
|
---|
212 | * to. For example, what does "a month from January 31st" mean?
|
---|
213 | * - tm_wday and tm_yday: these fields are calculated by NSPR. Users
|
---|
214 | * should treat them as "read-only".
|
---|
215 | */
|
---|
216 |
|
---|
217 | NSPR_API(void) PR_NormalizeTime(
|
---|
218 | PRExplodedTime *exploded, PRTimeParamFn params);
|
---|
219 |
|
---|
220 | /**********************************************************************/
|
---|
221 | /*********************** TIME PARAMETER FUNCTIONS *********************/
|
---|
222 | /**********************************************************************/
|
---|
223 |
|
---|
224 | /* Time parameters that suit current host machine */
|
---|
225 | NSPR_API(PRTimeParameters) PR_LocalTimeParameters(const PRExplodedTime *gmt);
|
---|
226 |
|
---|
227 | /* Time parameters that represent Greenwich Mean Time */
|
---|
228 | NSPR_API(PRTimeParameters) PR_GMTParameters(const PRExplodedTime *gmt);
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * Time parameters that represent the US Pacific Time Zone, with the
|
---|
232 | * current daylight saving time rules (for testing only)
|
---|
233 | */
|
---|
234 | NSPR_API(PRTimeParameters) PR_USPacificTimeParameters(const PRExplodedTime *gmt);
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * This parses a time/date string into a PRTime
|
---|
238 | * (microseconds after "1-Jan-1970 00:00:00 GMT").
|
---|
239 | * It returns PR_SUCCESS on success, and PR_FAILURE
|
---|
240 | * if the time/date string can't be parsed.
|
---|
241 | *
|
---|
242 | * Many formats are handled, including:
|
---|
243 | *
|
---|
244 | * 14 Apr 89 03:20:12
|
---|
245 | * 14 Apr 89 03:20 GMT
|
---|
246 | * Fri, 17 Mar 89 4:01:33
|
---|
247 | * Fri, 17 Mar 89 4:01 GMT
|
---|
248 | * Mon Jan 16 16:12 PDT 1989
|
---|
249 | * Mon Jan 16 16:12 +0130 1989
|
---|
250 | * 6 May 1992 16:41-JST (Wednesday)
|
---|
251 | * 22-AUG-1993 10:59:12.82
|
---|
252 | * 22-AUG-1993 10:59pm
|
---|
253 | * 22-AUG-1993 12:59am
|
---|
254 | * 22-AUG-1993 12:59 PM
|
---|
255 | * Friday, August 04, 1995 3:54 PM
|
---|
256 | * 06/21/95 04:24:34 PM
|
---|
257 | * 20/06/95 21:07
|
---|
258 | * 95-06-08 19:32:48 EDT
|
---|
259 | *
|
---|
260 | * If the input string doesn't contain a description of the timezone,
|
---|
261 | * we consult the `default_to_gmt' to decide whether the string should
|
---|
262 | * be interpreted relative to the local time zone (PR_FALSE) or GMT (PR_TRUE).
|
---|
263 | * The correct value for this argument depends on what standard specified
|
---|
264 | * the time string which you are parsing.
|
---|
265 | */
|
---|
266 |
|
---|
267 | NSPR_API(PRStatus) PR_ParseTimeString (
|
---|
268 | const char *string,
|
---|
269 | PRBool default_to_gmt,
|
---|
270 | PRTime *result);
|
---|
271 |
|
---|
272 | /*
|
---|
273 | * FIXME: should we also have a formatting function, such as asctime, ctime,
|
---|
274 | * and strftime in standard C library? But this would involve
|
---|
275 | * internationalization issues. Might want to provide a US English version.
|
---|
276 | */
|
---|
277 |
|
---|
278 | /**********************************************************************/
|
---|
279 | /*********************** OLD COMPATIBILITYFUNCTIONS *******************/
|
---|
280 | /**********************************************************************/
|
---|
281 | #ifndef NO_NSPR_10_SUPPORT
|
---|
282 |
|
---|
283 | /* Format a time value into a buffer. Same semantics as strftime() */
|
---|
284 | NSPR_API(PRUint32) PR_FormatTime(char *buf, int buflen, const char *fmt,
|
---|
285 | const PRExplodedTime *tm);
|
---|
286 |
|
---|
287 | /* Format a time value into a buffer. Time is always in US English format, regardless
|
---|
288 | * of locale setting.
|
---|
289 | */
|
---|
290 | NSPR_API(PRUint32)
|
---|
291 | PR_FormatTimeUSEnglish( char* buf, PRUint32 bufSize,
|
---|
292 | const char* format, const PRExplodedTime* tm );
|
---|
293 |
|
---|
294 | #endif /* NO_NSPR_10_SUPPORT */
|
---|
295 |
|
---|
296 | PR_END_EXTERN_C
|
---|
297 |
|
---|
298 | #endif /* prtime_h___ */
|
---|