prtime.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Portions are Copyright (C) 2011 Google Inc */
  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. * CVS revision 3.10
  44. * This file contains definitions of NSPR's basic types required by
  45. * prtime.cc. These types have been copied over from the following NSPR
  46. * files prtime.h, prtypes.h(CVS revision 3.35), prlong.h(CVS revision 3.13)
  47. *
  48. *---------------------------------------------------------------------------
  49. */
  50. #ifndef BASE_PRTIME_H__
  51. #define BASE_PRTIME_H__
  52. #include <stdint.h>
  53. #include "base/base_export.h"
  54. typedef int8_t PRInt8;
  55. typedef int16_t PRInt16;
  56. typedef int32_t PRInt32;
  57. typedef int64_t PRInt64;
  58. typedef int PRIntn;
  59. typedef PRIntn PRBool;
  60. #define PR_TRUE 1
  61. #define PR_FALSE 0
  62. typedef enum { PR_FAILURE = -1, PR_SUCCESS = 0 } PRStatus;
  63. #define PR_ASSERT DCHECK
  64. #define PR_CALLBACK
  65. #define PR_INT16_MAX 32767
  66. #define NSPR_API(__type) extern __type
  67. /*
  68. * Long-long (64-bit signed integer type) support macros used by
  69. * PR_ImplodeTime().
  70. * See http://lxr.mozilla.org/nspr/source/pr/include/prlong.h
  71. */
  72. #define LL_I2L(l, i) ((l) = (PRInt64)(i))
  73. #define LL_MUL(r, a, b) ((r) = (a) * (b))
  74. #define LL_ADD(r, a, b) ((r) = (a) + (b))
  75. #define LL_SUB(r, a, b) ((r) = (a) - (b))
  76. /**********************************************************************/
  77. /************************* TYPES AND CONSTANTS ************************/
  78. /**********************************************************************/
  79. #define PR_MSEC_PER_SEC 1000UL
  80. #define PR_USEC_PER_SEC 1000000UL
  81. #define PR_NSEC_PER_SEC 1000000000UL
  82. #define PR_USEC_PER_MSEC 1000UL
  83. #define PR_NSEC_PER_MSEC 1000000UL
  84. /*
  85. * PRTime --
  86. *
  87. * NSPR represents basic time as 64-bit signed integers relative
  88. * to midnight (00:00:00), January 1, 1970 Greenwich Mean Time (GMT).
  89. * (GMT is also known as Coordinated Universal Time, UTC.)
  90. * The units of time are in microseconds. Negative times are allowed
  91. * to represent times prior to the January 1970 epoch. Such values are
  92. * intended to be exported to other systems or converted to human
  93. * readable form.
  94. *
  95. * Notes on porting: PRTime corresponds to time_t in ANSI C. NSPR 1.0
  96. * simply uses PRInt64.
  97. */
  98. typedef PRInt64 PRTime;
  99. /*
  100. * Time zone and daylight saving time corrections applied to GMT to
  101. * obtain the local time of some geographic location
  102. */
  103. typedef struct PRTimeParameters {
  104. PRInt32 tp_gmt_offset; /* the offset from GMT in seconds */
  105. PRInt32 tp_dst_offset; /* contribution of DST in seconds */
  106. } PRTimeParameters;
  107. /*
  108. * PRExplodedTime --
  109. *
  110. * Time broken down into human-readable components such as year, month,
  111. * day, hour, minute, second, and microsecond. Time zone and daylight
  112. * saving time corrections may be applied. If they are applied, the
  113. * offsets from the GMT must be saved in the 'tm_params' field so that
  114. * all the information is available to reconstruct GMT.
  115. *
  116. * Notes on porting: PRExplodedTime corrresponds to struct tm in
  117. * ANSI C, with the following differences:
  118. * - an additional field tm_usec;
  119. * - replacing tm_isdst by tm_params;
  120. * - the month field is spelled tm_month, not tm_mon;
  121. * - we use absolute year, AD, not the year since 1900.
  122. * The corresponding type in NSPR 1.0 is called PRTime. Below is
  123. * a table of date/time type correspondence in the three APIs:
  124. * API time since epoch time in components
  125. * ANSI C time_t struct tm
  126. * NSPR 1.0 PRInt64 PRTime
  127. * NSPR 2.0 PRTime PRExplodedTime
  128. */
  129. typedef struct PRExplodedTime {
  130. PRInt32 tm_usec; /* microseconds past tm_sec (0-99999) */
  131. PRInt32 tm_sec; /* seconds past tm_min (0-61, accomodating
  132. up to two leap seconds) */
  133. PRInt32 tm_min; /* minutes past tm_hour (0-59) */
  134. PRInt32 tm_hour; /* hours past tm_day (0-23) */
  135. PRInt32 tm_mday; /* days past tm_mon (1-31, note that it
  136. starts from 1) */
  137. PRInt32 tm_month; /* months past tm_year (0-11, Jan = 0) */
  138. PRInt16 tm_year; /* absolute year, AD (note that we do not
  139. count from 1900) */
  140. PRInt8 tm_wday; /* calculated day of the week
  141. (0-6, Sun = 0) */
  142. PRInt16 tm_yday; /* calculated day of the year
  143. (0-365, Jan 1 = 0) */
  144. PRTimeParameters tm_params; /* time parameters used by conversion */
  145. } PRExplodedTime;
  146. /*
  147. * PRTimeParamFn --
  148. *
  149. * A function of PRTimeParamFn type returns the time zone and
  150. * daylight saving time corrections for some geographic location,
  151. * given the current time in GMT. The input argument gmt should
  152. * point to a PRExplodedTime that is in GMT, i.e., whose
  153. * tm_params contains all 0's.
  154. *
  155. * For any time zone other than GMT, the computation is intended to
  156. * consist of two steps:
  157. * - Figure out the time zone correction, tp_gmt_offset. This number
  158. * usually depends on the geographic location only. But it may
  159. * also depend on the current time. For example, all of China
  160. * is one time zone right now. But this situation may change
  161. * in the future.
  162. * - Figure out the daylight saving time correction, tp_dst_offset.
  163. * This number depends on both the geographic location and the
  164. * current time. Most of the DST rules are expressed in local
  165. * current time. If so, one should apply the time zone correction
  166. * to GMT before applying the DST rules.
  167. */
  168. typedef PRTimeParameters (PR_CALLBACK *PRTimeParamFn)(const PRExplodedTime *gmt);
  169. /**********************************************************************/
  170. /****************************** FUNCTIONS *****************************/
  171. /**********************************************************************/
  172. NSPR_API(PRTime)
  173. PR_ImplodeTime(const PRExplodedTime *exploded);
  174. /*
  175. * Adjust exploded time to normalize field overflows after manipulation.
  176. * Note that the following fields of PRExplodedTime should not be
  177. * manipulated:
  178. * - tm_month and tm_year: because the number of days in a month and
  179. * number of days in a year are not constant, it is ambiguous to
  180. * manipulate the month and year fields, although one may be tempted
  181. * to. For example, what does "a month from January 31st" mean?
  182. * - tm_wday and tm_yday: these fields are calculated by NSPR. Users
  183. * should treat them as "read-only".
  184. */
  185. NSPR_API(void) PR_NormalizeTime(
  186. PRExplodedTime *exploded, PRTimeParamFn params);
  187. /**********************************************************************/
  188. /*********************** TIME PARAMETER FUNCTIONS *********************/
  189. /**********************************************************************/
  190. /* Time parameters that represent Greenwich Mean Time */
  191. NSPR_API(PRTimeParameters) PR_GMTParameters(const PRExplodedTime *gmt);
  192. /*
  193. * This parses a time/date string into a PRTime
  194. * (microseconds after "1-Jan-1970 00:00:00 GMT").
  195. * It returns PR_SUCCESS on success, and PR_FAILURE
  196. * if the time/date string can't be parsed.
  197. *
  198. * Many formats are handled, including:
  199. *
  200. * 14 Apr 89 03:20:12
  201. * 14 Apr 89 03:20 GMT
  202. * Fri, 17 Mar 89 4:01:33
  203. * Fri, 17 Mar 89 4:01 GMT
  204. * Mon Jan 16 16:12 PDT 1989
  205. * Mon Jan 16 16:12 +0130 1989
  206. * 6 May 1992 16:41-JST (Wednesday)
  207. * 22-AUG-1993 10:59:12.82
  208. * 22-AUG-1993 10:59pm
  209. * 22-AUG-1993 12:59am
  210. * 22-AUG-1993 12:59 PM
  211. * Friday, August 04, 1995 3:54 PM
  212. * 06/21/95 04:24:34 PM
  213. * 20/06/95 21:07
  214. * 95-06-08 19:32:48 EDT
  215. * 1995-06-17T23:11:25.342156Z
  216. *
  217. * If the input string doesn't contain a description of the timezone,
  218. * we consult the `default_to_gmt' to decide whether the string should
  219. * be interpreted relative to the local time zone (PR_FALSE) or GMT (PR_TRUE).
  220. * The correct value for this argument depends on what standard specified
  221. * the time string which you are parsing.
  222. */
  223. /*
  224. * This is the only funtion that should be called from outside base, and only
  225. * from the unit test.
  226. */
  227. BASE_EXPORT PRStatus PR_ParseTimeString (
  228. const char *string,
  229. PRBool default_to_gmt,
  230. PRTime *result);
  231. #endif // BASE_PRTIME_H__