apr_time.h 7.6 KB

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