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