prtime.h 11 KB

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