apr_time.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef APR_TIME_H
  17. #define APR_TIME_H
  18. /**
  19. * @file apr_time.h
  20. * @brief APR Time Library
  21. */
  22. #include "apr.h"
  23. #include "apr_pools.h"
  24. #include "apr_errno.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif /* __cplusplus */
  28. /**
  29. * @defgroup apr_time Time Routines
  30. * @ingroup APR
  31. * @{
  32. */
  33. /** month names */
  34. APR_DECLARE_DATA extern const char apr_month_snames[12][4];
  35. /** day names */
  36. APR_DECLARE_DATA extern const char apr_day_snames[7][4];
  37. /** number of microseconds since 00:00:00 January 1, 1970 UTC */
  38. typedef apr_int64_t apr_time_t;
  39. /** mechanism to properly type apr_time_t literals */
  40. #define APR_TIME_C(val) APR_INT64_C(val)
  41. /** mechanism to properly print apr_time_t values */
  42. #define APR_TIME_T_FMT APR_INT64_T_FMT
  43. /** intervals for I/O timeouts, in microseconds */
  44. typedef apr_int64_t apr_interval_time_t;
  45. /** short interval for I/O timeouts, in microseconds */
  46. typedef apr_int32_t apr_short_interval_time_t;
  47. /** number of microseconds per second */
  48. #define APR_USEC_PER_SEC APR_TIME_C(1000000)
  49. /** @return apr_time_t as a second */
  50. #define apr_time_sec(time) ((time) / APR_USEC_PER_SEC)
  51. /** @return apr_time_t as a usec */
  52. #define apr_time_usec(time) ((time) % APR_USEC_PER_SEC)
  53. /** @return apr_time_t as a msec */
  54. #define apr_time_msec(time) (((time) / 1000) % 1000)
  55. /** @return apr_time_t as a msec */
  56. #define apr_time_as_msec(time) ((time) / 1000)
  57. /** @return milliseconds as an apr_time_t */
  58. #define apr_time_from_msec(msec) ((apr_time_t)(msec) * 1000)
  59. /** @return seconds as an apr_time_t */
  60. #define apr_time_from_sec(sec) ((apr_time_t)(sec) * APR_USEC_PER_SEC)
  61. /** @return a second and usec combination as an apr_time_t */
  62. #define apr_time_make(sec, usec) ((apr_time_t)(sec) * APR_USEC_PER_SEC \
  63. + (apr_time_t)(usec))
  64. /**
  65. * @return the current time
  66. */
  67. APR_DECLARE(apr_time_t) apr_time_now(void);
  68. /** @see apr_time_exp_t */
  69. typedef struct apr_time_exp_t apr_time_exp_t;
  70. /**
  71. * a structure similar to ANSI struct tm with the following differences:
  72. * - tm_usec isn't an ANSI field
  73. * - tm_gmtoff isn't an ANSI field (it's a BSDism)
  74. */
  75. struct apr_time_exp_t {
  76. /** microseconds past tm_sec */
  77. apr_int32_t tm_usec;
  78. /** (0-61) seconds past tm_min */
  79. apr_int32_t tm_sec;
  80. /** (0-59) minutes past tm_hour */
  81. apr_int32_t tm_min;
  82. /** (0-23) hours past midnight */
  83. apr_int32_t tm_hour;
  84. /** (1-31) day of the month */
  85. apr_int32_t tm_mday;
  86. /** (0-11) month of the year */
  87. apr_int32_t tm_mon;
  88. /** year since 1900 */
  89. apr_int32_t tm_year;
  90. /** (0-6) days since Sunday */
  91. apr_int32_t tm_wday;
  92. /** (0-365) days since January 1 */
  93. apr_int32_t tm_yday;
  94. /** daylight saving time */
  95. apr_int32_t tm_isdst;
  96. /** seconds east of UTC */
  97. apr_int32_t tm_gmtoff;
  98. };
  99. /**
  100. * Convert an ansi time_t to an apr_time_t
  101. * @param result the resulting apr_time_t
  102. * @param input the time_t to convert
  103. */
  104. APR_DECLARE(apr_status_t) apr_time_ansi_put(apr_time_t *result,
  105. time_t input);
  106. /**
  107. * Convert a time to its human readable components using an offset
  108. * from GMT.
  109. * @param result the exploded time
  110. * @param input the time to explode
  111. * @param offs the number of seconds offset to apply
  112. */
  113. APR_DECLARE(apr_status_t) apr_time_exp_tz(apr_time_exp_t *result,
  114. apr_time_t input,
  115. apr_int32_t offs);
  116. /**
  117. * Convert a time to its human readable components (GMT).
  118. * @param result the exploded time
  119. * @param input the time to explode
  120. */
  121. APR_DECLARE(apr_status_t) apr_time_exp_gmt(apr_time_exp_t *result,
  122. apr_time_t input);
  123. /**
  124. * Convert a time to its human readable components in the local timezone.
  125. * @param result the exploded time
  126. * @param input the time to explode
  127. */
  128. APR_DECLARE(apr_status_t) apr_time_exp_lt(apr_time_exp_t *result,
  129. apr_time_t input);
  130. /**
  131. * Convert time value from human readable format to a numeric apr_time_t
  132. * (elapsed microseconds since the epoch).
  133. * @param result the resulting imploded time
  134. * @param input the input exploded time
  135. */
  136. APR_DECLARE(apr_status_t) apr_time_exp_get(apr_time_t *result,
  137. apr_time_exp_t *input);
  138. /**
  139. * Convert time value from human readable format to a numeric apr_time_t that
  140. * always represents GMT.
  141. * @param result the resulting imploded time
  142. * @param input the input exploded time
  143. */
  144. APR_DECLARE(apr_status_t) apr_time_exp_gmt_get(apr_time_t *result,
  145. apr_time_exp_t *input);
  146. /**
  147. * Sleep for the specified number of micro-seconds.
  148. * @param t desired amount of time to sleep.
  149. * @warning May sleep for longer than the specified time.
  150. */
  151. APR_DECLARE(void) apr_sleep(apr_interval_time_t t);
  152. /** length of a RFC822 Date */
  153. #define APR_RFC822_DATE_LEN (30)
  154. /**
  155. * apr_rfc822_date formats dates in the RFC822
  156. * format in an efficient manner. It is a fixed length
  157. * format which requires APR_RFC822_DATA_LEN bytes of storage,
  158. * including the trailing NUL terminator.
  159. * @param date_str String to write to.
  160. * @param t the time to convert
  161. */
  162. APR_DECLARE(apr_status_t) apr_rfc822_date(char *date_str, apr_time_t t);
  163. /** length of a CTIME date */
  164. #define APR_CTIME_LEN (25)
  165. /**
  166. * apr_ctime formats dates in the ctime() format
  167. * in an efficient manner. It is a fixed length format
  168. * and requires APR_CTIME_LEN bytes of storage including
  169. * the trailing NUL terminator.
  170. * Unlike ANSI/ISO C ctime(), apr_ctime() does not include
  171. * a \\n at the end of the string.
  172. * @param date_str String to write to.
  173. * @param t the time to convert
  174. */
  175. APR_DECLARE(apr_status_t) apr_ctime(char *date_str, apr_time_t t);
  176. /**
  177. * Formats the exploded time according to the format specified
  178. * @param s string to write to
  179. * @param retsize The length of the returned string
  180. * @param max The maximum length of the string
  181. * @param format The format for the time string
  182. * @param tm The time to convert
  183. */
  184. APR_DECLARE(apr_status_t) apr_strftime(char *s, apr_size_t *retsize,
  185. apr_size_t max, const char *format,
  186. apr_time_exp_t *tm);
  187. /**
  188. * Improve the clock resolution for the lifetime of the given pool.
  189. * Generally this is only desirable on benchmarking and other very
  190. * time-sensitive applications, and has no impact on most platforms.
  191. * @param p The pool to associate the finer clock resolution
  192. */
  193. APR_DECLARE(void) apr_time_clock_hires(apr_pool_t *p);
  194. /** @} */
  195. #ifdef __cplusplus
  196. }
  197. #endif
  198. #endif /* ! APR_TIME_H */