1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
#include "apr_errno.h"
19
#include "apr_general.h"
22
#include "apr_strings.h"
27
/* The time value is used throughout the tests, so just make this a global.
28
* Also, we need a single value that we can test for the positive tests, so
29
* I chose the number below, it corresponds to:
30
* 2002-08-14 12:05:36.186711 -25200 [257 Sat].
31
* Which happens to be when I wrote the new tests.
33
static apr_time_t now = APR_INT64_C(1032030336186711);
35
static char* print_time (apr_pool_t *pool, const apr_time_exp_t *xt)
37
return apr_psprintf (pool,
38
"%04d-%02d-%02d %02d:%02d:%02d.%06d %+05d [%d %s]%s",
48
apr_day_snames[xt->tm_wday],
49
(xt->tm_isdst ? " DST" : ""));
53
static void test_now(abts_case *tc, void *data)
59
current = apr_time_now();
62
timediff = os_now - (current / APR_USEC_PER_SEC);
63
/* Even though these are called so close together, there is the chance
64
* that the time will be slightly off, so accept anything between -1 and
67
ABTS_ASSERT(tc, "apr_time and OS time do not agree",
68
(timediff > -2) && (timediff < 2));
71
static void test_gmtstr(abts_case *tc, void *data)
76
rv = apr_time_exp_gmt(&xt, now);
77
if (rv == APR_ENOTIMPL) {
78
ABTS_NOT_IMPL(tc, "apr_time_exp_gmt");
80
ABTS_TRUE(tc, rv == APR_SUCCESS);
81
ABTS_STR_EQUAL(tc, "2002-08-14 19:05:36.186711 +0000 [257 Sat]",
85
static void test_exp_lt(abts_case *tc, void *data)
89
time_t posix_secs = (time_t)apr_time_sec(now);
90
struct tm *posix_exp = localtime(&posix_secs);
92
rv = apr_time_exp_lt(&xt, now);
93
if (rv == APR_ENOTIMPL) {
94
ABTS_NOT_IMPL(tc, "apr_time_exp_lt");
96
ABTS_TRUE(tc, rv == APR_SUCCESS);
98
#define CHK_FIELD(f) \
99
ABTS_ASSERT(tc, "Mismatch in " #f, posix_exp->f == xt.f)
113
static void test_exp_get_gmt(abts_case *tc, void *data)
118
apr_int64_t hr_off_64;
120
rv = apr_time_exp_gmt(&xt, now);
121
ABTS_TRUE(tc, rv == APR_SUCCESS);
122
rv = apr_time_exp_get(&imp, &xt);
123
if (rv == APR_ENOTIMPL) {
124
ABTS_NOT_IMPL(tc, "apr_time_exp_get");
126
ABTS_TRUE(tc, rv == APR_SUCCESS);
127
hr_off_64 = (apr_int64_t) xt.tm_gmtoff * APR_USEC_PER_SEC;
128
ABTS_TRUE(tc, now + hr_off_64 == imp);
131
static void test_exp_get_lt(abts_case *tc, void *data)
136
apr_int64_t hr_off_64;
138
rv = apr_time_exp_lt(&xt, now);
139
ABTS_TRUE(tc, rv == APR_SUCCESS);
140
rv = apr_time_exp_get(&imp, &xt);
141
if (rv == APR_ENOTIMPL) {
142
ABTS_NOT_IMPL(tc, "apr_time_exp_get");
144
ABTS_TRUE(tc, rv == APR_SUCCESS);
145
hr_off_64 = (apr_int64_t) xt.tm_gmtoff * APR_USEC_PER_SEC;
146
ABTS_TRUE(tc, now + hr_off_64 == imp);
149
static void test_imp_gmt(abts_case *tc, void *data)
155
rv = apr_time_exp_gmt(&xt, now);
156
ABTS_TRUE(tc, rv == APR_SUCCESS);
157
rv = apr_time_exp_gmt_get(&imp, &xt);
158
if (rv == APR_ENOTIMPL) {
159
ABTS_NOT_IMPL(tc, "apr_time_exp_gmt_get");
161
ABTS_TRUE(tc, rv == APR_SUCCESS);
162
ABTS_TRUE(tc, now == imp);
165
static void test_rfcstr(abts_case *tc, void *data)
170
rv = apr_rfc822_date(str, now);
171
if (rv == APR_ENOTIMPL) {
172
ABTS_NOT_IMPL(tc, "apr_rfc822_date");
174
ABTS_TRUE(tc, rv == APR_SUCCESS);
175
ABTS_STR_EQUAL(tc, "Sat, 14 Sep 2002 19:05:36 GMT", str);
178
static void test_ctime(abts_case *tc, void *data)
181
char apr_str[STR_SIZE];
182
char libc_str[STR_SIZE];
183
apr_time_t now_sec = apr_time_sec(now);
184
time_t posix_sec = (time_t) now_sec;
186
rv = apr_ctime(apr_str, now);
187
if (rv == APR_ENOTIMPL) {
188
ABTS_NOT_IMPL(tc, "apr_ctime");
190
ABTS_TRUE(tc, rv == APR_SUCCESS);
191
strcpy(libc_str, ctime(&posix_sec));
192
*strchr(libc_str, '\n') = '\0';
194
ABTS_STR_EQUAL(tc, libc_str, apr_str);
197
static void test_strftime(abts_case *tc, void *data)
204
rv = apr_time_exp_gmt(&xt, now);
205
str = apr_palloc(p, STR_SIZE + 1);
206
rv = apr_strftime(str, &sz, STR_SIZE, "%R %A %d %B %Y", &xt);
207
if (rv == APR_ENOTIMPL) {
208
ABTS_NOT_IMPL(tc, "apr_strftime");
210
ABTS_TRUE(tc, rv == APR_SUCCESS);
211
ABTS_STR_EQUAL(tc, "19:05 Saturday 14 September 2002", str);
214
static void test_strftimesmall(abts_case *tc, void *data)
221
rv = apr_time_exp_gmt(&xt, now);
222
rv = apr_strftime(str, &sz, STR_SIZE, "%T", &xt);
223
if (rv == APR_ENOTIMPL) {
224
ABTS_NOT_IMPL(tc, "apr_strftime");
226
ABTS_TRUE(tc, rv == APR_SUCCESS);
227
ABTS_STR_EQUAL(tc, "19:05:36", str);
230
static void test_exp_tz(abts_case *tc, void *data)
234
apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
236
rv = apr_time_exp_tz(&xt, now, hr_off);
237
if (rv == APR_ENOTIMPL) {
238
ABTS_NOT_IMPL(tc, "apr_time_exp_tz");
240
ABTS_TRUE(tc, rv == APR_SUCCESS);
241
ABTS_TRUE(tc, (xt.tm_usec == 186711) &&
244
(xt.tm_hour == 14) &&
245
(xt.tm_mday == 14) &&
247
(xt.tm_year == 102) &&
249
(xt.tm_yday == 256));
252
static void test_strftimeoffset(abts_case *tc, void *data)
258
apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
260
apr_time_exp_tz(&xt, now, hr_off);
261
rv = apr_strftime(str, &sz, STR_SIZE, "%T", &xt);
262
if (rv == APR_ENOTIMPL) {
263
ABTS_NOT_IMPL(tc, "apr_strftime");
265
ABTS_TRUE(tc, rv == APR_SUCCESS);
268
/* 0.9.4 and earlier rejected valid dates in 2038 */
269
static void test_2038(abts_case *tc, void *data)
274
/* 2038-01-19T03:14:07.000000Z */
282
APR_ASSERT_SUCCESS(tc, "explode January 19th, 2038",
283
apr_time_exp_get(&t, &xt));
286
abts_suite *testtime(abts_suite *suite)
288
suite = ADD_SUITE(suite)
290
abts_run_test(suite, test_now, NULL);
291
abts_run_test(suite, test_gmtstr, NULL);
292
abts_run_test(suite, test_exp_lt, NULL);
293
abts_run_test(suite, test_exp_get_gmt, NULL);
294
abts_run_test(suite, test_exp_get_lt, NULL);
295
abts_run_test(suite, test_imp_gmt, NULL);
296
abts_run_test(suite, test_rfcstr, NULL);
297
abts_run_test(suite, test_ctime, NULL);
298
abts_run_test(suite, test_strftime, NULL);
299
abts_run_test(suite, test_strftimesmall, NULL);
300
abts_run_test(suite, test_exp_tz, NULL);
301
abts_run_test(suite, test_strftimeoffset, NULL);
302
abts_run_test(suite, test_2038, NULL);