2
0

fspr_time.h 7.3 KB

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