xref: /aosp_15_r20/external/libchrome/base/third_party/nspr/prtime.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker /* Portions are Copyright (C) 2011 Google Inc */
2*635a8641SAndroid Build Coastguard Worker /* ***** BEGIN LICENSE BLOCK *****
3*635a8641SAndroid Build Coastguard Worker  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4*635a8641SAndroid Build Coastguard Worker  *
5*635a8641SAndroid Build Coastguard Worker  * The contents of this file are subject to the Mozilla Public License Version
6*635a8641SAndroid Build Coastguard Worker  * 1.1 (the "License"); you may not use this file except in compliance with
7*635a8641SAndroid Build Coastguard Worker  * the License. You may obtain a copy of the License at
8*635a8641SAndroid Build Coastguard Worker  * http://www.mozilla.org/MPL/
9*635a8641SAndroid Build Coastguard Worker  *
10*635a8641SAndroid Build Coastguard Worker  * Software distributed under the License is distributed on an "AS IS" basis,
11*635a8641SAndroid Build Coastguard Worker  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12*635a8641SAndroid Build Coastguard Worker  * for the specific language governing rights and limitations under the
13*635a8641SAndroid Build Coastguard Worker  * License.
14*635a8641SAndroid Build Coastguard Worker  *
15*635a8641SAndroid Build Coastguard Worker  * The Original Code is the Netscape Portable Runtime (NSPR).
16*635a8641SAndroid Build Coastguard Worker  *
17*635a8641SAndroid Build Coastguard Worker  * The Initial Developer of the Original Code is
18*635a8641SAndroid Build Coastguard Worker  * Netscape Communications Corporation.
19*635a8641SAndroid Build Coastguard Worker  * Portions created by the Initial Developer are Copyright (C) 1998-2000
20*635a8641SAndroid Build Coastguard Worker  * the Initial Developer. All Rights Reserved.
21*635a8641SAndroid Build Coastguard Worker  *
22*635a8641SAndroid Build Coastguard Worker  * Contributor(s):
23*635a8641SAndroid Build Coastguard Worker  *
24*635a8641SAndroid Build Coastguard Worker  * Alternatively, the contents of this file may be used under the terms of
25*635a8641SAndroid Build Coastguard Worker  * either the GNU General Public License Version 2 or later (the "GPL"), or
26*635a8641SAndroid Build Coastguard Worker  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27*635a8641SAndroid Build Coastguard Worker  * in which case the provisions of the GPL or the LGPL are applicable instead
28*635a8641SAndroid Build Coastguard Worker  * of those above. If you wish to allow use of your version of this file only
29*635a8641SAndroid Build Coastguard Worker  * under the terms of either the GPL or the LGPL, and not to allow others to
30*635a8641SAndroid Build Coastguard Worker  * use your version of this file under the terms of the MPL, indicate your
31*635a8641SAndroid Build Coastguard Worker  * decision by deleting the provisions above and replace them with the notice
32*635a8641SAndroid Build Coastguard Worker  * and other provisions required by the GPL or the LGPL. If you do not delete
33*635a8641SAndroid Build Coastguard Worker  * the provisions above, a recipient may use your version of this file under
34*635a8641SAndroid Build Coastguard Worker  * the terms of any one of the MPL, the GPL or the LGPL.
35*635a8641SAndroid Build Coastguard Worker  *
36*635a8641SAndroid Build Coastguard Worker  * ***** END LICENSE BLOCK ***** */
37*635a8641SAndroid Build Coastguard Worker 
38*635a8641SAndroid Build Coastguard Worker /*
39*635a8641SAndroid Build Coastguard Worker  *---------------------------------------------------------------------------
40*635a8641SAndroid Build Coastguard Worker  *
41*635a8641SAndroid Build Coastguard Worker  * prtime.h --
42*635a8641SAndroid Build Coastguard Worker  *
43*635a8641SAndroid Build Coastguard Worker  *     NSPR date and time functions
44*635a8641SAndroid Build Coastguard Worker  * CVS revision 3.10
45*635a8641SAndroid Build Coastguard Worker  * This file contains definitions of NSPR's basic types required by
46*635a8641SAndroid Build Coastguard Worker  * prtime.cc. These types have been copied over from the following NSPR
47*635a8641SAndroid Build Coastguard Worker  * files prtime.h, prtypes.h(CVS revision 3.35), prlong.h(CVS revision 3.13)
48*635a8641SAndroid Build Coastguard Worker  *
49*635a8641SAndroid Build Coastguard Worker  *---------------------------------------------------------------------------
50*635a8641SAndroid Build Coastguard Worker  */
51*635a8641SAndroid Build Coastguard Worker 
52*635a8641SAndroid Build Coastguard Worker #ifndef BASE_PRTIME_H__
53*635a8641SAndroid Build Coastguard Worker #define BASE_PRTIME_H__
54*635a8641SAndroid Build Coastguard Worker 
55*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
56*635a8641SAndroid Build Coastguard Worker 
57*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
58*635a8641SAndroid Build Coastguard Worker 
59*635a8641SAndroid Build Coastguard Worker typedef int8_t PRInt8;
60*635a8641SAndroid Build Coastguard Worker typedef int16_t PRInt16;
61*635a8641SAndroid Build Coastguard Worker typedef int32_t PRInt32;
62*635a8641SAndroid Build Coastguard Worker typedef int64_t PRInt64;
63*635a8641SAndroid Build Coastguard Worker typedef int PRIntn;
64*635a8641SAndroid Build Coastguard Worker 
65*635a8641SAndroid Build Coastguard Worker typedef PRIntn PRBool;
66*635a8641SAndroid Build Coastguard Worker #define PR_TRUE 1
67*635a8641SAndroid Build Coastguard Worker #define PR_FALSE 0
68*635a8641SAndroid Build Coastguard Worker 
69*635a8641SAndroid Build Coastguard Worker typedef enum { PR_FAILURE = -1, PR_SUCCESS = 0 } PRStatus;
70*635a8641SAndroid Build Coastguard Worker 
71*635a8641SAndroid Build Coastguard Worker #define PR_ASSERT DCHECK
72*635a8641SAndroid Build Coastguard Worker #define PR_CALLBACK
73*635a8641SAndroid Build Coastguard Worker #define PR_INT16_MAX 32767
74*635a8641SAndroid Build Coastguard Worker #define NSPR_API(__type) extern __type
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker /*
77*635a8641SAndroid Build Coastguard Worker  * Long-long (64-bit signed integer type) support macros used by
78*635a8641SAndroid Build Coastguard Worker  * PR_ImplodeTime().
79*635a8641SAndroid Build Coastguard Worker  * See http://lxr.mozilla.org/nspr/source/pr/include/prlong.h
80*635a8641SAndroid Build Coastguard Worker  */
81*635a8641SAndroid Build Coastguard Worker 
82*635a8641SAndroid Build Coastguard Worker #define LL_I2L(l, i) ((l) = (PRInt64)(i))
83*635a8641SAndroid Build Coastguard Worker #define LL_MUL(r, a, b) ((r) = (a) * (b))
84*635a8641SAndroid Build Coastguard Worker #define LL_ADD(r, a, b) ((r) = (a) + (b))
85*635a8641SAndroid Build Coastguard Worker #define LL_SUB(r, a, b) ((r) = (a) - (b))
86*635a8641SAndroid Build Coastguard Worker 
87*635a8641SAndroid Build Coastguard Worker /**********************************************************************/
88*635a8641SAndroid Build Coastguard Worker /************************* TYPES AND CONSTANTS ************************/
89*635a8641SAndroid Build Coastguard Worker /**********************************************************************/
90*635a8641SAndroid Build Coastguard Worker 
91*635a8641SAndroid Build Coastguard Worker #define PR_MSEC_PER_SEC		1000UL
92*635a8641SAndroid Build Coastguard Worker #define PR_USEC_PER_SEC		1000000UL
93*635a8641SAndroid Build Coastguard Worker #define PR_NSEC_PER_SEC		1000000000UL
94*635a8641SAndroid Build Coastguard Worker #define PR_USEC_PER_MSEC	1000UL
95*635a8641SAndroid Build Coastguard Worker #define PR_NSEC_PER_MSEC	1000000UL
96*635a8641SAndroid Build Coastguard Worker 
97*635a8641SAndroid Build Coastguard Worker /*
98*635a8641SAndroid Build Coastguard Worker  * PRTime --
99*635a8641SAndroid Build Coastguard Worker  *
100*635a8641SAndroid Build Coastguard Worker  *     NSPR represents basic time as 64-bit signed integers relative
101*635a8641SAndroid Build Coastguard Worker  *     to midnight (00:00:00), January 1, 1970 Greenwich Mean Time (GMT).
102*635a8641SAndroid Build Coastguard Worker  *     (GMT is also known as Coordinated Universal Time, UTC.)
103*635a8641SAndroid Build Coastguard Worker  *     The units of time are in microseconds. Negative times are allowed
104*635a8641SAndroid Build Coastguard Worker  *     to represent times prior to the January 1970 epoch. Such values are
105*635a8641SAndroid Build Coastguard Worker  *     intended to be exported to other systems or converted to human
106*635a8641SAndroid Build Coastguard Worker  *     readable form.
107*635a8641SAndroid Build Coastguard Worker  *
108*635a8641SAndroid Build Coastguard Worker  *     Notes on porting: PRTime corresponds to time_t in ANSI C.  NSPR 1.0
109*635a8641SAndroid Build Coastguard Worker  *     simply uses PRInt64.
110*635a8641SAndroid Build Coastguard Worker  */
111*635a8641SAndroid Build Coastguard Worker 
112*635a8641SAndroid Build Coastguard Worker typedef PRInt64 PRTime;
113*635a8641SAndroid Build Coastguard Worker 
114*635a8641SAndroid Build Coastguard Worker /*
115*635a8641SAndroid Build Coastguard Worker  * Time zone and daylight saving time corrections applied to GMT to
116*635a8641SAndroid Build Coastguard Worker  * obtain the local time of some geographic location
117*635a8641SAndroid Build Coastguard Worker  */
118*635a8641SAndroid Build Coastguard Worker 
119*635a8641SAndroid Build Coastguard Worker typedef struct PRTimeParameters {
120*635a8641SAndroid Build Coastguard Worker     PRInt32 tp_gmt_offset;     /* the offset from GMT in seconds */
121*635a8641SAndroid Build Coastguard Worker     PRInt32 tp_dst_offset;     /* contribution of DST in seconds */
122*635a8641SAndroid Build Coastguard Worker } PRTimeParameters;
123*635a8641SAndroid Build Coastguard Worker 
124*635a8641SAndroid Build Coastguard Worker /*
125*635a8641SAndroid Build Coastguard Worker  * PRExplodedTime --
126*635a8641SAndroid Build Coastguard Worker  *
127*635a8641SAndroid Build Coastguard Worker  *     Time broken down into human-readable components such as year, month,
128*635a8641SAndroid Build Coastguard Worker  *     day, hour, minute, second, and microsecond.  Time zone and daylight
129*635a8641SAndroid Build Coastguard Worker  *     saving time corrections may be applied.  If they are applied, the
130*635a8641SAndroid Build Coastguard Worker  *     offsets from the GMT must be saved in the 'tm_params' field so that
131*635a8641SAndroid Build Coastguard Worker  *     all the information is available to reconstruct GMT.
132*635a8641SAndroid Build Coastguard Worker  *
133*635a8641SAndroid Build Coastguard Worker  *     Notes on porting: PRExplodedTime corrresponds to struct tm in
134*635a8641SAndroid Build Coastguard Worker  *     ANSI C, with the following differences:
135*635a8641SAndroid Build Coastguard Worker  *       - an additional field tm_usec;
136*635a8641SAndroid Build Coastguard Worker  *       - replacing tm_isdst by tm_params;
137*635a8641SAndroid Build Coastguard Worker  *       - the month field is spelled tm_month, not tm_mon;
138*635a8641SAndroid Build Coastguard Worker  *       - we use absolute year, AD, not the year since 1900.
139*635a8641SAndroid Build Coastguard Worker  *     The corresponding type in NSPR 1.0 is called PRTime.  Below is
140*635a8641SAndroid Build Coastguard Worker  *     a table of date/time type correspondence in the three APIs:
141*635a8641SAndroid Build Coastguard Worker  *         API          time since epoch          time in components
142*635a8641SAndroid Build Coastguard Worker  *       ANSI C             time_t                  struct tm
143*635a8641SAndroid Build Coastguard Worker  *       NSPR 1.0           PRInt64                   PRTime
144*635a8641SAndroid Build Coastguard Worker  *       NSPR 2.0           PRTime                  PRExplodedTime
145*635a8641SAndroid Build Coastguard Worker  */
146*635a8641SAndroid Build Coastguard Worker 
147*635a8641SAndroid Build Coastguard Worker typedef struct PRExplodedTime {
148*635a8641SAndroid Build Coastguard Worker     PRInt32 tm_usec;		    /* microseconds past tm_sec (0-99999)  */
149*635a8641SAndroid Build Coastguard Worker     PRInt32 tm_sec;             /* seconds past tm_min (0-61, accomodating
150*635a8641SAndroid Build Coastguard Worker                                    up to two leap seconds) */
151*635a8641SAndroid Build Coastguard Worker     PRInt32 tm_min;             /* minutes past tm_hour (0-59) */
152*635a8641SAndroid Build Coastguard Worker     PRInt32 tm_hour;            /* hours past tm_day (0-23) */
153*635a8641SAndroid Build Coastguard Worker     PRInt32 tm_mday;            /* days past tm_mon (1-31, note that it
154*635a8641SAndroid Build Coastguard Worker 				                starts from 1) */
155*635a8641SAndroid Build Coastguard Worker     PRInt32 tm_month;           /* months past tm_year (0-11, Jan = 0) */
156*635a8641SAndroid Build Coastguard Worker     PRInt16 tm_year;            /* absolute year, AD (note that we do not
157*635a8641SAndroid Build Coastguard Worker 				                count from 1900) */
158*635a8641SAndroid Build Coastguard Worker 
159*635a8641SAndroid Build Coastguard Worker     PRInt8 tm_wday;		        /* calculated day of the week
160*635a8641SAndroid Build Coastguard Worker 				                (0-6, Sun = 0) */
161*635a8641SAndroid Build Coastguard Worker     PRInt16 tm_yday;            /* calculated day of the year
162*635a8641SAndroid Build Coastguard Worker 				                (0-365, Jan 1 = 0) */
163*635a8641SAndroid Build Coastguard Worker 
164*635a8641SAndroid Build Coastguard Worker     PRTimeParameters tm_params;  /* time parameters used by conversion */
165*635a8641SAndroid Build Coastguard Worker } PRExplodedTime;
166*635a8641SAndroid Build Coastguard Worker 
167*635a8641SAndroid Build Coastguard Worker /*
168*635a8641SAndroid Build Coastguard Worker  * PRTimeParamFn --
169*635a8641SAndroid Build Coastguard Worker  *
170*635a8641SAndroid Build Coastguard Worker  *     A function of PRTimeParamFn type returns the time zone and
171*635a8641SAndroid Build Coastguard Worker  *     daylight saving time corrections for some geographic location,
172*635a8641SAndroid Build Coastguard Worker  *     given the current time in GMT.  The input argument gmt should
173*635a8641SAndroid Build Coastguard Worker  *     point to a PRExplodedTime that is in GMT, i.e., whose
174*635a8641SAndroid Build Coastguard Worker  *     tm_params contains all 0's.
175*635a8641SAndroid Build Coastguard Worker  *
176*635a8641SAndroid Build Coastguard Worker  *     For any time zone other than GMT, the computation is intended to
177*635a8641SAndroid Build Coastguard Worker  *     consist of two steps:
178*635a8641SAndroid Build Coastguard Worker  *       - Figure out the time zone correction, tp_gmt_offset.  This number
179*635a8641SAndroid Build Coastguard Worker  *         usually depends on the geographic location only.  But it may
180*635a8641SAndroid Build Coastguard Worker  *         also depend on the current time.  For example, all of China
181*635a8641SAndroid Build Coastguard Worker  *         is one time zone right now.  But this situation may change
182*635a8641SAndroid Build Coastguard Worker  *         in the future.
183*635a8641SAndroid Build Coastguard Worker  *       - Figure out the daylight saving time correction, tp_dst_offset.
184*635a8641SAndroid Build Coastguard Worker  *         This number depends on both the geographic location and the
185*635a8641SAndroid Build Coastguard Worker  *         current time.  Most of the DST rules are expressed in local
186*635a8641SAndroid Build Coastguard Worker  *         current time.  If so, one should apply the time zone correction
187*635a8641SAndroid Build Coastguard Worker  *         to GMT before applying the DST rules.
188*635a8641SAndroid Build Coastguard Worker  */
189*635a8641SAndroid Build Coastguard Worker 
190*635a8641SAndroid Build Coastguard Worker typedef PRTimeParameters (PR_CALLBACK *PRTimeParamFn)(const PRExplodedTime *gmt);
191*635a8641SAndroid Build Coastguard Worker 
192*635a8641SAndroid Build Coastguard Worker /**********************************************************************/
193*635a8641SAndroid Build Coastguard Worker /****************************** FUNCTIONS *****************************/
194*635a8641SAndroid Build Coastguard Worker /**********************************************************************/
195*635a8641SAndroid Build Coastguard Worker 
196*635a8641SAndroid Build Coastguard Worker NSPR_API(PRTime)
197*635a8641SAndroid Build Coastguard Worker PR_ImplodeTime(const PRExplodedTime *exploded);
198*635a8641SAndroid Build Coastguard Worker 
199*635a8641SAndroid Build Coastguard Worker /*
200*635a8641SAndroid Build Coastguard Worker  * Adjust exploded time to normalize field overflows after manipulation.
201*635a8641SAndroid Build Coastguard Worker  * Note that the following fields of PRExplodedTime should not be
202*635a8641SAndroid Build Coastguard Worker  * manipulated:
203*635a8641SAndroid Build Coastguard Worker  *   - tm_month and tm_year: because the number of days in a month and
204*635a8641SAndroid Build Coastguard Worker  *     number of days in a year are not constant, it is ambiguous to
205*635a8641SAndroid Build Coastguard Worker  *     manipulate the month and year fields, although one may be tempted
206*635a8641SAndroid Build Coastguard Worker  *     to.  For example, what does "a month from January 31st" mean?
207*635a8641SAndroid Build Coastguard Worker  *   - tm_wday and tm_yday: these fields are calculated by NSPR.  Users
208*635a8641SAndroid Build Coastguard Worker  *     should treat them as "read-only".
209*635a8641SAndroid Build Coastguard Worker  */
210*635a8641SAndroid Build Coastguard Worker 
211*635a8641SAndroid Build Coastguard Worker NSPR_API(void) PR_NormalizeTime(
212*635a8641SAndroid Build Coastguard Worker     PRExplodedTime *exploded, PRTimeParamFn params);
213*635a8641SAndroid Build Coastguard Worker 
214*635a8641SAndroid Build Coastguard Worker /**********************************************************************/
215*635a8641SAndroid Build Coastguard Worker /*********************** TIME PARAMETER FUNCTIONS *********************/
216*635a8641SAndroid Build Coastguard Worker /**********************************************************************/
217*635a8641SAndroid Build Coastguard Worker 
218*635a8641SAndroid Build Coastguard Worker /* Time parameters that represent Greenwich Mean Time */
219*635a8641SAndroid Build Coastguard Worker NSPR_API(PRTimeParameters) PR_GMTParameters(const PRExplodedTime *gmt);
220*635a8641SAndroid Build Coastguard Worker 
221*635a8641SAndroid Build Coastguard Worker /*
222*635a8641SAndroid Build Coastguard Worker  * This parses a time/date string into a PRTime
223*635a8641SAndroid Build Coastguard Worker  * (microseconds after "1-Jan-1970 00:00:00 GMT").
224*635a8641SAndroid Build Coastguard Worker  * It returns PR_SUCCESS on success, and PR_FAILURE
225*635a8641SAndroid Build Coastguard Worker  * if the time/date string can't be parsed.
226*635a8641SAndroid Build Coastguard Worker  *
227*635a8641SAndroid Build Coastguard Worker  * Many formats are handled, including:
228*635a8641SAndroid Build Coastguard Worker  *
229*635a8641SAndroid Build Coastguard Worker  *   14 Apr 89 03:20:12
230*635a8641SAndroid Build Coastguard Worker  *   14 Apr 89 03:20 GMT
231*635a8641SAndroid Build Coastguard Worker  *   Fri, 17 Mar 89 4:01:33
232*635a8641SAndroid Build Coastguard Worker  *   Fri, 17 Mar 89 4:01 GMT
233*635a8641SAndroid Build Coastguard Worker  *   Mon Jan 16 16:12 PDT 1989
234*635a8641SAndroid Build Coastguard Worker  *   Mon Jan 16 16:12 +0130 1989
235*635a8641SAndroid Build Coastguard Worker  *   6 May 1992 16:41-JST (Wednesday)
236*635a8641SAndroid Build Coastguard Worker  *   22-AUG-1993 10:59:12.82
237*635a8641SAndroid Build Coastguard Worker  *   22-AUG-1993 10:59pm
238*635a8641SAndroid Build Coastguard Worker  *   22-AUG-1993 12:59am
239*635a8641SAndroid Build Coastguard Worker  *   22-AUG-1993 12:59 PM
240*635a8641SAndroid Build Coastguard Worker  *   Friday, August 04, 1995 3:54 PM
241*635a8641SAndroid Build Coastguard Worker  *   06/21/95 04:24:34 PM
242*635a8641SAndroid Build Coastguard Worker  *   20/06/95 21:07
243*635a8641SAndroid Build Coastguard Worker  *   95-06-08 19:32:48 EDT
244*635a8641SAndroid Build Coastguard Worker  *   1995-06-17T23:11:25.342156Z
245*635a8641SAndroid Build Coastguard Worker  *
246*635a8641SAndroid Build Coastguard Worker  * If the input string doesn't contain a description of the timezone,
247*635a8641SAndroid Build Coastguard Worker  * we consult the `default_to_gmt' to decide whether the string should
248*635a8641SAndroid Build Coastguard Worker  * be interpreted relative to the local time zone (PR_FALSE) or GMT (PR_TRUE).
249*635a8641SAndroid Build Coastguard Worker  * The correct value for this argument depends on what standard specified
250*635a8641SAndroid Build Coastguard Worker  * the time string which you are parsing.
251*635a8641SAndroid Build Coastguard Worker  */
252*635a8641SAndroid Build Coastguard Worker 
253*635a8641SAndroid Build Coastguard Worker /*
254*635a8641SAndroid Build Coastguard Worker  * This is the only funtion that should be called from outside base, and only
255*635a8641SAndroid Build Coastguard Worker  * from the unit test.
256*635a8641SAndroid Build Coastguard Worker  */
257*635a8641SAndroid Build Coastguard Worker 
258*635a8641SAndroid Build Coastguard Worker BASE_EXPORT PRStatus PR_ParseTimeString (
259*635a8641SAndroid Build Coastguard Worker 	const char *string,
260*635a8641SAndroid Build Coastguard Worker 	PRBool default_to_gmt,
261*635a8641SAndroid Build Coastguard Worker 	PRTime *result);
262*635a8641SAndroid Build Coastguard Worker 
263*635a8641SAndroid Build Coastguard Worker #endif  // BASE_PRTIME_H__
264