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