michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: *---------------------------------------------------------------------- michael@0: * michael@0: * prtime.h -- michael@0: * michael@0: * NSPR date and time functions michael@0: * michael@0: *----------------------------------------------------------------------- michael@0: */ michael@0: michael@0: #ifndef prtime_h___ michael@0: #define prtime_h___ michael@0: michael@0: #include "prlong.h" michael@0: michael@0: PR_BEGIN_EXTERN_C michael@0: michael@0: /**********************************************************************/ michael@0: /************************* TYPES AND CONSTANTS ************************/ michael@0: /**********************************************************************/ michael@0: michael@0: #define PR_MSEC_PER_SEC 1000L michael@0: #define PR_USEC_PER_SEC 1000000L michael@0: #define PR_NSEC_PER_SEC 1000000000L michael@0: #define PR_USEC_PER_MSEC 1000L michael@0: #define PR_NSEC_PER_MSEC 1000000L michael@0: michael@0: /* michael@0: * PRTime -- michael@0: * michael@0: * NSPR represents basic time as 64-bit signed integers relative michael@0: * to midnight (00:00:00), January 1, 1970 Greenwich Mean Time (GMT). michael@0: * (GMT is also known as Coordinated Universal Time, UTC.) michael@0: * The units of time are in microseconds. Negative times are allowed michael@0: * to represent times prior to the January 1970 epoch. Such values are michael@0: * intended to be exported to other systems or converted to human michael@0: * readable form. michael@0: * michael@0: * Notes on porting: PRTime corresponds to time_t in ANSI C. NSPR 1.0 michael@0: * simply uses PRInt64. michael@0: */ michael@0: michael@0: typedef PRInt64 PRTime; michael@0: michael@0: /* michael@0: * Time zone and daylight saving time corrections applied to GMT to michael@0: * obtain the local time of some geographic location michael@0: */ michael@0: michael@0: typedef struct PRTimeParameters { michael@0: PRInt32 tp_gmt_offset; /* the offset from GMT in seconds */ michael@0: PRInt32 tp_dst_offset; /* contribution of DST in seconds */ michael@0: } PRTimeParameters; michael@0: michael@0: /* michael@0: * PRExplodedTime -- michael@0: * michael@0: * Time broken down into human-readable components such as year, month, michael@0: * day, hour, minute, second, and microsecond. Time zone and daylight michael@0: * saving time corrections may be applied. If they are applied, the michael@0: * offsets from the GMT must be saved in the 'tm_params' field so that michael@0: * all the information is available to reconstruct GMT. michael@0: * michael@0: * Notes on porting: PRExplodedTime corrresponds to struct tm in michael@0: * ANSI C, with the following differences: michael@0: * - an additional field tm_usec; michael@0: * - replacing tm_isdst by tm_params; michael@0: * - the month field is spelled tm_month, not tm_mon; michael@0: * - we use absolute year, AD, not the year since 1900. michael@0: * The corresponding type in NSPR 1.0 is called PRTime. Below is michael@0: * a table of date/time type correspondence in the three APIs: michael@0: * API time since epoch time in components michael@0: * ANSI C time_t struct tm michael@0: * NSPR 1.0 PRInt64 PRTime michael@0: * NSPR 2.0 PRTime PRExplodedTime michael@0: */ michael@0: michael@0: typedef struct PRExplodedTime { michael@0: PRInt32 tm_usec; /* microseconds past tm_sec (0-99999) */ michael@0: PRInt32 tm_sec; /* seconds past tm_min (0-61, accomodating michael@0: up to two leap seconds) */ michael@0: PRInt32 tm_min; /* minutes past tm_hour (0-59) */ michael@0: PRInt32 tm_hour; /* hours past tm_day (0-23) */ michael@0: PRInt32 tm_mday; /* days past tm_mon (1-31, note that it michael@0: starts from 1) */ michael@0: PRInt32 tm_month; /* months past tm_year (0-11, Jan = 0) */ michael@0: PRInt16 tm_year; /* absolute year, AD (note that we do not michael@0: count from 1900) */ michael@0: michael@0: PRInt8 tm_wday; /* calculated day of the week michael@0: (0-6, Sun = 0) */ michael@0: PRInt16 tm_yday; /* calculated day of the year michael@0: (0-365, Jan 1 = 0) */ michael@0: michael@0: PRTimeParameters tm_params; /* time parameters used by conversion */ michael@0: } PRExplodedTime; michael@0: michael@0: /* michael@0: * PRTimeParamFn -- michael@0: * michael@0: * A function of PRTimeParamFn type returns the time zone and michael@0: * daylight saving time corrections for some geographic location, michael@0: * given the current time in GMT. The input argument gmt should michael@0: * point to a PRExplodedTime that is in GMT, i.e., whose michael@0: * tm_params contains all 0's. michael@0: * michael@0: * For any time zone other than GMT, the computation is intended to michael@0: * consist of two steps: michael@0: * - Figure out the time zone correction, tp_gmt_offset. This number michael@0: * usually depends on the geographic location only. But it may michael@0: * also depend on the current time. For example, all of China michael@0: * is one time zone right now. But this situation may change michael@0: * in the future. michael@0: * - Figure out the daylight saving time correction, tp_dst_offset. michael@0: * This number depends on both the geographic location and the michael@0: * current time. Most of the DST rules are expressed in local michael@0: * current time. If so, one should apply the time zone correction michael@0: * to GMT before applying the DST rules. michael@0: */ michael@0: michael@0: typedef PRTimeParameters (PR_CALLBACK *PRTimeParamFn)(const PRExplodedTime *gmt); michael@0: michael@0: /**********************************************************************/ michael@0: /****************************** FUNCTIONS *****************************/ michael@0: /**********************************************************************/ michael@0: michael@0: /* michael@0: * The PR_Now routine returns the current time relative to the michael@0: * epoch, midnight, January 1, 1970 UTC. The units of the returned michael@0: * value are microseconds since the epoch. michael@0: * michael@0: * The values returned are not guaranteed to advance in a linear fashion michael@0: * due to the application of time correction protocols which synchronize michael@0: * computer clocks to some external time source. Consequently it should michael@0: * not be depended on for interval timing. michael@0: * michael@0: * The implementation is machine dependent. michael@0: * Cf. time_t time(time_t *tp) in ANSI C. michael@0: */ michael@0: NSPR_API(PRTime) michael@0: PR_Now(void); michael@0: michael@0: /* michael@0: * Expand time binding it to time parameters provided by PRTimeParamFn. michael@0: * The calculation is envisoned to proceed in the following steps: michael@0: * - From given PRTime, calculate PRExplodedTime in GMT michael@0: * - Apply the given PRTimeParamFn to the GMT that we just calculated michael@0: * to obtain PRTimeParameters. michael@0: * - Add the PRTimeParameters offsets to GMT to get the local time michael@0: * as PRExplodedTime. michael@0: */ michael@0: michael@0: NSPR_API(void) PR_ExplodeTime( michael@0: PRTime usecs, PRTimeParamFn params, PRExplodedTime *exploded); michael@0: michael@0: /* Reverse operation of PR_ExplodeTime */ michael@0: NSPR_API(PRTime) michael@0: PR_ImplodeTime(const PRExplodedTime *exploded); michael@0: michael@0: /* michael@0: * Adjust exploded time to normalize field overflows after manipulation. michael@0: * Note that the following fields of PRExplodedTime should not be michael@0: * manipulated: michael@0: * - tm_month and tm_year: because the number of days in a month and michael@0: * number of days in a year are not constant, it is ambiguous to michael@0: * manipulate the month and year fields, although one may be tempted michael@0: * to. For example, what does "a month from January 31st" mean? michael@0: * - tm_wday and tm_yday: these fields are calculated by NSPR. Users michael@0: * should treat them as "read-only". michael@0: */ michael@0: michael@0: NSPR_API(void) PR_NormalizeTime( michael@0: PRExplodedTime *exploded, PRTimeParamFn params); michael@0: michael@0: /**********************************************************************/ michael@0: /*********************** TIME PARAMETER FUNCTIONS *********************/ michael@0: /**********************************************************************/ michael@0: michael@0: /* Time parameters that suit current host machine */ michael@0: NSPR_API(PRTimeParameters) PR_LocalTimeParameters(const PRExplodedTime *gmt); michael@0: michael@0: /* Time parameters that represent Greenwich Mean Time */ michael@0: NSPR_API(PRTimeParameters) PR_GMTParameters(const PRExplodedTime *gmt); michael@0: michael@0: /* michael@0: * Time parameters that represent the US Pacific Time Zone, with the michael@0: * current daylight saving time rules (for testing only) michael@0: */ michael@0: NSPR_API(PRTimeParameters) PR_USPacificTimeParameters(const PRExplodedTime *gmt); michael@0: michael@0: /* michael@0: * This parses a time/date string into a PRExplodedTime michael@0: * struct. It populates all fields but it can't split michael@0: * the offset from UTC into tp_gmt_offset and tp_dst_offset in michael@0: * most cases (exceptions: PST/PDT, MST/MDT, CST/CDT, EST/EDT, GMT/BST). michael@0: * In those cases tp_gmt_offset will be the sum of these two and michael@0: * tp_dst_offset will be 0. michael@0: * It returns PR_SUCCESS on success, and PR_FAILURE michael@0: * if the time/date string can't be parsed. michael@0: * michael@0: * Many formats are handled, including: michael@0: * michael@0: * 14 Apr 89 03:20:12 michael@0: * 14 Apr 89 03:20 GMT michael@0: * Fri, 17 Mar 89 4:01:33 michael@0: * Fri, 17 Mar 89 4:01 GMT michael@0: * Mon Jan 16 16:12 PDT 1989 michael@0: * Mon Jan 16 16:12 +0130 1989 michael@0: * 6 May 1992 16:41-JST (Wednesday) michael@0: * 22-AUG-1993 10:59:12.82 michael@0: * 22-AUG-1993 10:59pm michael@0: * 22-AUG-1993 12:59am michael@0: * 22-AUG-1993 12:59 PM michael@0: * Friday, August 04, 1995 3:54 PM michael@0: * 06/21/95 04:24:34 PM michael@0: * 20/06/95 21:07 michael@0: * 95-06-08 19:32:48 EDT michael@0: * michael@0: * If the input string doesn't contain a description of the timezone, michael@0: * we consult the `default_to_gmt' to decide whether the string should michael@0: * be interpreted relative to the local time zone (PR_FALSE) or GMT (PR_TRUE). michael@0: * The correct value for this argument depends on what standard specified michael@0: * the time string which you are parsing. michael@0: */ michael@0: michael@0: NSPR_API(PRStatus) PR_ParseTimeStringToExplodedTime ( michael@0: const char *string, michael@0: PRBool default_to_gmt, michael@0: PRExplodedTime *result); michael@0: michael@0: /* michael@0: * This uses PR_ParseTimeStringToExplodedTime to parse michael@0: * a time/date string and PR_ImplodeTime to transform it into michael@0: * a PRTime (microseconds after "1-Jan-1970 00:00:00 GMT"). michael@0: * It returns PR_SUCCESS on success, and PR_FAILURE michael@0: * if the time/date string can't be parsed. michael@0: */ michael@0: michael@0: NSPR_API(PRStatus) PR_ParseTimeString ( michael@0: const char *string, michael@0: PRBool default_to_gmt, michael@0: PRTime *result); michael@0: michael@0: /* Format a time value into a buffer. Same semantics as strftime() */ michael@0: NSPR_API(PRUint32) PR_FormatTime(char *buf, int buflen, const char *fmt, michael@0: const PRExplodedTime *tm); michael@0: michael@0: /* Format a time value into a buffer. Time is always in US English format, regardless michael@0: * of locale setting. michael@0: */ michael@0: NSPR_API(PRUint32) michael@0: PR_FormatTimeUSEnglish( char* buf, PRUint32 bufSize, michael@0: const char* format, const PRExplodedTime* tm ); michael@0: michael@0: PR_END_EXTERN_C michael@0: michael@0: #endif /* prtime_h___ */