prtime.h 9.9 KB

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