PageRenderTime 40ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/gecko_api/include/prtime.h

http://firefox-mac-pdf.googlecode.com/
C Header | 315 lines | 63 code | 38 blank | 214 comment | 0 complexity | 3427594ae30c15d613b8bb960335bf4f MD5 | raw file
  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 PRExplodedTime
  210. * struct. It populates all fields but it can't split
  211. * the offset from UTC into tp_gmt_offset and tp_dst_offset in
  212. * most cases (exceptions: PST/PDT, MST/MDT, CST/CDT, EST/EDT, GMT/BST).
  213. * In those cases tp_gmt_offset will be the sum of these two and
  214. * tp_dst_offset will be 0.
  215. * It returns PR_SUCCESS on success, and PR_FAILURE
  216. * if the time/date string can't be parsed.
  217. *
  218. * Many formats are handled, including:
  219. *
  220. * 14 Apr 89 03:20:12
  221. * 14 Apr 89 03:20 GMT
  222. * Fri, 17 Mar 89 4:01:33
  223. * Fri, 17 Mar 89 4:01 GMT
  224. * Mon Jan 16 16:12 PDT 1989
  225. * Mon Jan 16 16:12 +0130 1989
  226. * 6 May 1992 16:41-JST (Wednesday)
  227. * 22-AUG-1993 10:59:12.82
  228. * 22-AUG-1993 10:59pm
  229. * 22-AUG-1993 12:59am
  230. * 22-AUG-1993 12:59 PM
  231. * Friday, August 04, 1995 3:54 PM
  232. * 06/21/95 04:24:34 PM
  233. * 20/06/95 21:07
  234. * 95-06-08 19:32:48 EDT
  235. *
  236. * If the input string doesn't contain a description of the timezone,
  237. * we consult the `default_to_gmt' to decide whether the string should
  238. * be interpreted relative to the local time zone (PR_FALSE) or GMT (PR_TRUE).
  239. * The correct value for this argument depends on what standard specified
  240. * the time string which you are parsing.
  241. */
  242. NSPR_API(PRStatus) PR_ParseTimeStringToExplodedTime (
  243. const char *string,
  244. PRBool default_to_gmt,
  245. PRExplodedTime *result);
  246. /*
  247. * This uses PR_ParseTimeStringToExplodedTime to parse
  248. * a time/date string and PR_ImplodeTime to transform it into
  249. * a PRTime (microseconds after "1-Jan-1970 00:00:00 GMT").
  250. * It returns PR_SUCCESS on success, and PR_FAILURE
  251. * if the time/date string can't be parsed.
  252. */
  253. NSPR_API(PRStatus) PR_ParseTimeString (
  254. const char *string,
  255. PRBool default_to_gmt,
  256. PRTime *result);
  257. /*
  258. * FIXME: should we also have a formatting function, such as asctime, ctime,
  259. * and strftime in standard C library? But this would involve
  260. * internationalization issues. Might want to provide a US English version.
  261. */
  262. /**********************************************************************/
  263. /*********************** OLD COMPATIBILITYFUNCTIONS *******************/
  264. /**********************************************************************/
  265. #ifndef NO_NSPR_10_SUPPORT
  266. /* Format a time value into a buffer. Same semantics as strftime() */
  267. NSPR_API(PRUint32) PR_FormatTime(char *buf, int buflen, const char *fmt,
  268. const PRExplodedTime *tm);
  269. /* Format a time value into a buffer. Time is always in US English format, regardless
  270. * of locale setting.
  271. */
  272. NSPR_API(PRUint32)
  273. PR_FormatTimeUSEnglish( char* buf, PRUint32 bufSize,
  274. const char* format, const PRExplodedTime* tm );
  275. #endif /* NO_NSPR_10_SUPPORT */
  276. PR_END_EXTERN_C
  277. #endif /* prtime_h___ */