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