diff options
author | dgaudet <dgaudet@13f79535-47bb-0310-9956-ffa450edef68> | 2000-01-09 05:18:21 +0000 |
---|---|---|
committer | dgaudet <dgaudet@13f79535-47bb-0310-9956-ffa450edef68> | 2000-01-09 05:18:21 +0000 |
commit | ed4448046a3d6d3d6a4c2ef19b78e2f2343416ff (patch) | |
tree | d06ab8bf9f21fec0fa6102e91888a2be18c37f80 /time | |
parent | c7f96f85ebd7b02b28b2d04b91ff9679ca106e59 (diff) | |
download | libapr-ed4448046a3d6d3d6a4c2ef19b78e2f2343416ff.tar.gz |
time overhaul:
- ap_time_t is a 64-bit scalar, microseconds since epoch
- ap_exploded_time_t corresponds to struct tm with a few extras
probably broken on anything except linux.
git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@59578 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'time')
-rw-r--r-- | time/unix/Makefile.in | 5 | ||||
-rw-r--r-- | time/unix/access.c | 422 | ||||
-rw-r--r-- | time/unix/time.c | 277 | ||||
-rw-r--r-- | time/unix/timestr.c | 149 |
4 files changed, 203 insertions, 650 deletions
diff --git a/time/unix/Makefile.in b/time/unix/Makefile.in index 523334376..8e2a2cff2 100644 --- a/time/unix/Makefile.in +++ b/time/unix/Makefile.in @@ -15,7 +15,6 @@ INCLUDES=-I$(INCDIR) -I. #LIB=libtime.a OBJS=time.o \ - access.o \ timestr.o .c.o: @@ -51,10 +50,6 @@ depend: && rm Makefile.new # DO NOT REMOVE -access.o: access.c atime.h ../../include/apr_config.h \ - ../../include/apr_time.h ../../include/apr_general.h \ - ../../include/apr.h ../../include/apr_errno.h ../../include/apr_lib.h \ - ../../include/apr_file_io.h ../../include/apr_thread_proc.h time.o: time.c atime.h ../../include/apr_config.h \ ../../include/apr_time.h ../../include/apr_general.h \ ../../include/apr.h ../../include/apr_errno.h ../../include/apr_lib.h \ diff --git a/time/unix/access.c b/time/unix/access.c deleted file mode 100644 index 2892a225f..000000000 --- a/time/unix/access.c +++ /dev/null @@ -1,422 +0,0 @@ -/* ==================================================================== - * Copyright (c) 1999 The Apache Group. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. All advertising materials mentioning features or use of this - * software must display the following acknowledgment: - * "This product includes software developed by the Apache Group - * for use in the Apache HTTP server project (http://www.apache.org/)." - * - * 4. The names "Apache Server" and "Apache Group" must not be used to - * endorse or promote products derived from this software without - * prior written permission. For written permission, please contact - * apache@apache.org. - * - * 5. Products derived from this software may not be called "Apache" - * nor may "Apache" appear in their names without prior written - * permission of the Apache Group. - * - * 6. Redistributions of any form whatsoever must retain the following - * acknowledgment: - * "This product includes software developed by the Apache Group - * for use in the Apache HTTP server project (http://www.apache.org/)." - * - * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY - * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Group. - * For more information on the Apache Group and the Apache HTTP server - * project, please see <http://www.apache.org/>. - * - */ - -#include "atime.h" - -/* ***APRDOC******************************************************** - * ap_status_t ap_get_ansitime(ap_time_t *, ap_int64_t *) - * Get the current time in seconds since Jan 1, 1970. - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_get_ansitime(struct atime_t *atime, ap_ansi_time_t *rv) -{ - if (atime) { - (*rv) = atime->currtime->tv_sec; - return APR_SUCCESS; - } - return APR_ENOTIME; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_get_sec(ap_time_t *, ap_int64_t *) - * Get the number of seconds since the top of the minute - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_get_sec(struct atime_t *atime, ap_int32_t *rv) -{ - if (atime) { - (*rv) = atime->explodedtime->tm_sec; - return APR_SUCCESS; - } - return APR_ENOTIME; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_get_min(ap_time_t *, ap_int64_t *) - * Get the number of minutes since the top of the hour - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_get_min(struct atime_t *atime, ap_int32_t *rv) -{ - if (atime) { - (*rv) = atime->explodedtime->tm_min; - return APR_SUCCESS; - } - return APR_ENOTIME; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_get_min(ap_time_t *, ap_int64_t *) - * Get the number of minutes since the top of the hour - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_get_hour(struct atime_t *atime, ap_int32_t *rv) -{ - if (atime) { - (*rv) = atime->explodedtime->tm_hour; - return APR_SUCCESS; - } - return APR_ENOTIME; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_get_mday(ap_time_t *, ap_int64_t *) - * Get the number of days since the beginning of the month - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_get_mday(struct atime_t *atime, ap_int32_t *rv) -{ - if (atime) { - (*rv) = atime->explodedtime->tm_mday; - return APR_SUCCESS; - } - return APR_ENOTIME; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_get_mon(ap_time_t *, ap_int64_t *) - * Get the number of months since the beginning of the year - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_get_mon(struct atime_t *atime, ap_int32_t *rv) -{ - if (atime) { - (*rv) = atime->explodedtime->tm_mon; - return APR_SUCCESS; - } - return APR_ENOTIME; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_get_year(ap_time_t *, ap_int64_t *) - * Get the number of years since 1900 - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_get_year(struct atime_t *atime, ap_int32_t *rv) -{ - if (atime) { - (*rv) = atime->explodedtime->tm_year; - return APR_SUCCESS; - } - return APR_ENOTIME; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_get_wday(ap_time_t *, ap_int64_t *) - * Get the number of days since the beginning of the week. 0 == Sunday - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_get_wday(struct atime_t *atime, ap_int32_t *rv) -{ - if (atime) { - (*rv) = atime->explodedtime->tm_wday; - return APR_SUCCESS; - } - return APR_ENOTIME; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_set_ansitime(ap_time_t *, ap_int64_t) - * Set the current time in seconds since Jan 1, 1970. - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_set_ansitime(struct atime_t *atime, ap_ansi_time_t sec) -{ - if (atime) { - if (!atime->currtime) { - atime->currtime = ap_pcalloc(atime->cntxt, sizeof(struct timeval)); - } - atime->currtime->tv_sec = sec; - return APR_SUCCESS; - } - return APR_ENOTIME; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_set_sec(ap_time_t *, ap_int64_t) - * Set the number of sec since the top of the minute - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_set_sec(struct atime_t *atime, ap_int32_t value) -{ - if (!atime) { - return APR_ENOTIME; - } - if (atime->explodedtime == NULL) { - atime->explodedtime = (struct tm *)ap_palloc(atime->cntxt, - sizeof(struct tm)); - } - if (atime->explodedtime == NULL) { - return APR_ENOMEM; - } - atime->explodedtime->tm_sec = value; - return APR_SUCCESS; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_set_min(ap_time_t *, ap_int64_t) - * Set the number of minutes since the top of the hour - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_set_min(struct atime_t *atime, ap_int32_t value) -{ - if (!atime) { - return APR_ENOTIME; - } - if (atime->explodedtime == NULL) { - atime->explodedtime = (struct tm *)ap_palloc(atime->cntxt, - sizeof(struct tm)); - } - if (atime->explodedtime == NULL) { - return APR_ENOMEM; - } - atime->explodedtime->tm_min = value; - return APR_SUCCESS; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_set_min(ap_time_t *, ap_int64_t) - * Set the number of hours since the beginning of the day - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_set_hour(struct atime_t *atime, ap_int32_t value) -{ - if (!atime) { - return APR_ENOTIME; - } - if (atime->explodedtime == NULL) { - atime->explodedtime = (struct tm *)ap_palloc(atime->cntxt, - sizeof(struct tm)); - } - if (atime->explodedtime == NULL) { - return APR_ENOMEM; - } - atime->explodedtime->tm_hour = value; - return APR_SUCCESS; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_set_mday(ap_time_t *, ap_int64_t) - * Set the number of days since the beginning of the month - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_set_mday(struct atime_t *atime, ap_int32_t value) -{ - if (!atime) { - return APR_ENOTIME; - } - if (atime->explodedtime == NULL) { - atime->explodedtime = (struct tm *)ap_palloc(atime->cntxt, - sizeof(struct tm)); - } - if (atime->explodedtime == NULL) { - return APR_ENOMEM; - } - atime->explodedtime->tm_mday = value; - return APR_SUCCESS; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_set_mon(ap_time_t *, ap_int64_t) - * Set the number of months since the beginning of the year - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_set_mon(struct atime_t *atime, ap_int32_t value) -{ - if (!atime) { - return APR_ENOTIME; - } - if (atime->explodedtime == NULL) { - atime->explodedtime = (struct tm *)ap_palloc(atime->cntxt, - sizeof(struct tm)); - } - if (atime->explodedtime == NULL) { - return APR_ENOMEM; - } - atime->explodedtime->tm_mon = value; - return APR_SUCCESS; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_set_min(ap_time_t *, ap_int64_t) - * Set the number of years since the 1900 - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_set_year(struct atime_t *atime, ap_int32_t value) -{ - if (!atime) { - return APR_ENOTIME; - } - if (atime->explodedtime == NULL) { - atime->explodedtime = (struct tm *)ap_palloc(atime->cntxt, - sizeof(struct tm)); - } - if (atime->explodedtime == NULL) { - return APR_ENOMEM; - } - atime->explodedtime->tm_year = value; - return APR_SUCCESS; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_set_wday(ap_time_t *, ap_int64_t) - * Get the number of days since the beginning of the week. 0 == Sunday - * arg 1) The time value we care about. - * arg 2) Integer to store time value in - */ -ap_status_t ap_set_wday(struct atime_t *atime, ap_int32_t value) -{ - if (!atime) { - return APR_ENOTIME; - } - if (atime->explodedtime == NULL) { - atime->explodedtime = (struct tm *)ap_palloc(atime->cntxt, - sizeof(struct tm)); - } - if (atime->explodedtime == NULL) { - return APR_ENOMEM; - } - atime->explodedtime->tm_wday = value; - return APR_SUCCESS; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_get_timedata(ap_time_t *, char *, void *) - * Return the context associated with the current atime. - * arg 1) The currently open atime. - * arg 2) The user data associated with the atime. - */ -ap_status_t ap_get_timedata(struct atime_t *atime, char *key, void *data) -{ - if (atime != NULL) { - return ap_get_userdata(data, key, atime->cntxt); - } - else { - data = NULL; - return APR_ENOTIME; - } -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_set_timedata(ap_time_t *, void *, char *, - ap_status_t (*cleanup) (void *)) - * Set the context associated with the current atime. - * arg 1) The currently open atime. - * arg 2) The user data to associate with the atime. - */ -ap_status_t ap_set_timedata(struct atime_t *atime, void *data, char *key, - ap_status_t (*cleanup) (void *)) -{ - if (atime != NULL) { - return ap_set_userdata(data, key, cleanup, atime->cntxt); - } - else { - data = NULL; - return APR_ENOTIME; - } -} - -#if defined(HAVE_GMTOFF) -ap_status_t ap_get_gmtoff(int *tz, ap_time_t *tt, ap_context_t *cont) -{ - if (tt->currtime == NULL) { - tt->currtime = ap_pcalloc(cont, sizeof(struct timeval)); - } - tt->currtime->tv_sec = time(NULL); - tt->explodedtime = localtime(&tt->currtime->tv_sec); - *tz = (int) (tt->explodedtime->tm_gmtoff / 60); - return APR_SUCCESS; -} -#else -ap_status_t ap_get_gmtoff(int *tz, ap_time_t *tt, ap_context_t *cont) -{ - struct tm gmt; - int days, hours, minutes; - - if (tt->currtime == NULL) { - tt->currtime = ap_pcalloc(cont, sizeof(struct timeval)); - } - tt->currtime->tv_sec = time(NULL); - - /* Assume we are never more than 24 hours away. */ - /* remember gmtime/localtime return ptr to static */ - /* buffer... so be careful */ - gmt = *gmtime(&tt->currtime->tv_sec); - tt->explodedtime = localtime(&tt->currtime->tv_sec); - days = tt->explodedtime->tm_yday - gmt.tm_yday; - hours = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) - + tt->explodedtime->tm_hour - gmt.tm_hour); - minutes = hours * 60 + tt->explodedtime->tm_min - gmt.tm_min; - *tz = minutes; - return APR_SUCCESS; -} -#endif - - diff --git a/time/unix/time.c b/time/unix/time.c index dfefb4a93..2607b45fc 100644 --- a/time/unix/time.c +++ b/time/unix/time.c @@ -57,107 +57,147 @@ #include "apr_portable.h" /* ***APRDOC******************************************************** - * ap_status_t ap_make_time(ap_context_t *, ap_time_t *) - * Create an empty time entity. - * arg 1) The context to operate on. - * arg 2) The new time entity to create. + * ap_status_t ap_ansi_time_to_ap_time(ap_time_t *result, time_t input) + * convert an ansi time_t to an ap_time_t + * arg 1) the resulting ap_time_t + * arg 2) the time_t to convert */ -ap_status_t ap_make_time(struct atime_t **new, ap_context_t *cont) +ap_status_t ap_ansi_time_to_ap_time(ap_time_t *result, time_t input) { - (*new) = (struct atime_t *)ap_palloc(cont, sizeof(struct atime_t)); - - if ((*new) == NULL) { - return APR_ENOMEM; - } - - (*new)->cntxt = cont; - (*new)->explodedtime = ap_palloc(cont, sizeof(struct tm)); - (*new)->time_ex = 0; - (*new)->currtime = NULL; + *result = (ap_time_t)input * AP_USEC_PER_SEC; return APR_SUCCESS; } + /* ***APRDOC******************************************************** - * ap_status_t ap_make_init_time(ap_context_t *, ap_time_t *) - * Create a time entity and fill it out with the current time. - * arg 1) The context to operate on. - * arg 2) The new time entity to create. + * ap_time_t ap_now(void) + * return the current time */ -ap_status_t ap_make_init_time(struct atime_t **new, ap_context_t *cont) +ap_time_t ap_now(void) { - (*new) = (struct atime_t *)ap_palloc(cont, sizeof(struct atime_t)); + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec * AP_USEC_PER_SEC + tv.tv_usec; +} - if ((*new) == NULL) { - return APR_ENOMEM; - } - (*new)->cntxt = cont; - (*new)->explodedtime = ap_palloc(cont, sizeof(struct tm)); - (*new)->time_ex = 0; - gettimeofday((*new)->currtime, NULL); - return APR_SUCCESS; +static void tm_to_exp(ap_exploded_time_t *xt, struct tm *tm) +{ + xt->tm_sec = tm->tm_sec; + xt->tm_min = tm->tm_min; + xt->tm_hour = tm->tm_hour; + xt->tm_mday = tm->tm_mday; + xt->tm_mon = tm->tm_mon; + xt->tm_year = tm->tm_year; + xt->tm_wday = tm->tm_wday; + xt->tm_yday = tm->tm_yday; + xt->tm_isdst = tm->tm_isdst; } + /* ***APRDOC******************************************************** - * ap_status_t ap_current_time(ap_time_t *) - * Return the number of seconds since January 1, 1970. - * arg 1) The time entity to reference. + * ap_status_t ap_explode_gmt(ap_exploded_time_t *, ap_time_t) + * convert a time to its human readable components in GMT timezone + * arg 1) the exploded time + * arg 2) the time to explode */ -ap_status_t ap_current_time(struct atime_t *new) +ap_status_t ap_explode_gmt(ap_exploded_time_t *result, ap_time_t input) { - new->currtime = ap_palloc(new->cntxt, sizeof(struct timeval)); - gettimeofday(new->currtime, NULL); - new->time_ex = 0; - return APR_SUCCESS; -} + time_t t = input / AP_USEC_PER_SEC; +#if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) + struct tm banana; +#endif + + result->tm_usec = input % AP_USEC_PER_SEC; + +#if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) + gmtime_r(&t, &banana); + tm_to_exp(result, &banana); +#else + tm_to_exp(result, gmtime(&t)); +#endif + result->tm_gmtoff = 0; + return APR_SUCCESS; +} /* ***APRDOC******************************************************** - * ap_status_t ap_explode_time(ap_time_t *, ap_timetype_e) - * Convert time value from number of seconds since epoch to a set - * of integers representing the time in a human readable form. - * arg 1) The time entity to reference. - * arg 2) How to explode the time. One of: - * APR_LOCALTIME -- Use local time - * APR_UTCTIME -- Use UTC time + * ap_status_t ap_explode_localtime(ap_exploded_time_t *, ap_time_t) + * convert a time to its human readable components in local timezone + * arg 1) the exploded time + * arg 2) the time to explode */ -ap_status_t ap_explode_time(struct atime_t *atime, ap_timetype_e type) +ap_status_t ap_explode_localtime(ap_exploded_time_t *result, ap_time_t input) { - switch (type) { - case APR_LOCALTIME: { #if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) - localtime_r(&atime->currtime->tv_sec, atime->explodedtime); + time_t t = input / AP_USEC_PER_SEC; + struct tm apricot; + + result->tm_usec = input % AP_USEC_PER_SEC; + + localtime_r(&t, &apricot); + tm_to_exp(result, &apricot); +#if defined(HAVE_GMTOFF) + result->tm_gmtoff = apricot.tm_gmtoff; +#elif defined(HAVE___GMTOFF) + result->tm_gmtoff = apricot.__tm_gmtoff; #else - atime->explodedtime = localtime(&atime->currtime->tv_sec); -#endif - break; + /* solaris is backwards enough to have pthreads but no tm_gmtoff, feh */ + { + int days, hours, minutes; + + gmtime_r(&t, &apricot); + days = result->tm_yday - apricot.tm_yday; + hours = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + + result->tm_hour - apricot.tm_hour); + minutes = hours * 60 + result->tm_min - apricot.tm_min; + result->tm_gmtoff = minutes * 60; } - case APR_UTCTIME: { -#if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) - gmtime_r(&atime->currtime->tv_sec, atime->explodedtime); -#else - atime->explodedtime = gmtime(&atime->currtime->tv_sec); #endif - break; - } +#else + time_t t = input / AP_USEC_PER_SEC; + struct tm *tmx; + + result->tm_usec = input % AP_USEC_PER_SEC; + + tmx = localtime(&t); + tm_to_exp(result, tmx); +#if defined(HAVE_GMTOFF) + result->tm_gmtoff = tmx->tm_gmtoff; +#elif defined(HAVE___GMTOFF) + result->tm_gmtoff = tmx->__tm_gmtoff; +#else + /* need to create tm_gmtoff... assume we are never more than 24 hours away */ + { + int days, hours, minutes; + + tmx = gmtime(&t); + days = result->tm_yday - tmx->tm_yday; + hours = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + + result->tm_hour - tmx->tm_hour); + minutes = hours * 60 + result->tm_min - tmx->tm_min; + result->tm_gmtoff = minutes * 60; } - atime->time_ex = 1; +#endif +#endif return APR_SUCCESS; } + /* ***APRDOC******************************************************** - * ap_status_t ap_implode_time(ap_time_t *) + * ap_status_t ap_implode_time(ap_time_t *, ap_exploded_time_t *) * Convert time value from human readable format to number of seconds * since epoch - * arg 1) The time entity to reference. + * arg 1) the resulting imploded time + * arg 2) the input exploded time */ -ap_status_t ap_implode_time(struct atime_t *atime) +ap_status_t ap_implode_time(ap_time_t *t, ap_exploded_time_t *xt) { int year; time_t days; static const int dayoffset[12] = {306, 337, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275}; - year = atime->explodedtime->tm_year; + year = xt->tm_year; if (year < 70 || ((sizeof(time_t) <= 4) && (year >= 138))) { return APR_EBADDATE; @@ -165,120 +205,21 @@ ap_status_t ap_implode_time(struct atime_t *atime) /* shift new year to 1st March in order to make leap year calc easy */ - if (atime->explodedtime->tm_mon < 2) + if (xt->tm_mon < 2) year--; /* Find number of days since 1st March 1900 (in the Gregorian calendar). */ days = year * 365 + year / 4 - year / 100 + (year / 100 + 3) / 4; - days += dayoffset[atime->explodedtime->tm_mon] + - atime->explodedtime->tm_mday - 1; + days += dayoffset[xt->tm_mon] + xt->tm_mday - 1; days -= 25508; /* 1 jan 1970 is 25508 days since 1 mar 1900 */ - days = ((days * 24 + atime->explodedtime->tm_hour) * 60 + - atime->explodedtime->tm_min) * 60 + atime->explodedtime->tm_sec; + days = ((days * 24 + xt->tm_hour) * 60 + xt->tm_min) * 60 + xt->tm_sec; if (days < 0) { return APR_EBADDATE; } - atime->currtime = ap_palloc(atime->cntxt, sizeof(struct timeval)); - atime->currtime->tv_sec = days; /* must be a valid time */ - atime->currtime->tv_usec = 0; - atime->time_ex = 1; + days -= xt->tm_gmtoff; + *t = days * AP_USEC_PER_SEC + xt->tm_usec; return APR_SUCCESS; } - -/* ***APRDOC******************************************************** - * ap_status_t ap_get_os_time(ap_os_time_t **, ap_time_t *) - * Convert from apr time type to OS specific time value - * arg 1) The time value to convert. - * arg 2) The OS specific value to convert to. - */ -ap_status_t ap_get_os_time(ap_os_time_t **atime, struct atime_t *thetime) -{ - if (thetime == NULL) { - return APR_ENOTIME; - } - if (thetime->currtime == NULL) { - ap_implode_time(thetime); - } - atime = &(thetime->currtime); - return APR_SUCCESS; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_put_os_time(ap_time_t **, ap_os_time_t *, ap_context_t *) - * Convert to apr time type from OS specific time value - * arg 1) The context to use. - * arg 2) The time value to convert to. - * arg 3) The OS specific value to convert. - */ -ap_status_t ap_put_os_time(struct atime_t **thetime, ap_os_time_t *atime, - ap_context_t *cont) -{ - if (cont == NULL) { - return APR_ENOCONT; - } - if (thetime == NULL) { - (*thetime) = (struct atime_t *)ap_palloc(cont, sizeof(struct atime_t)); - (*thetime)->cntxt = cont; - } - (*thetime)->currtime = atime; - (*thetime)->time_ex = 0; - return APR_SUCCESS; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_timediff(ap_time_t *, ap_time_t *, ap_int32_t *) - * Retrieve the difference between two time structures in milliseconds. - * arg 1) The first time value - * arg 2) The second timevalue - * arg 3) The difference to return. - */ -ap_status_t ap_timediff(struct atime_t *a, struct atime_t *b, ap_int32_t *rv) -{ - register int us, s; - - us = a->currtime->tv_usec - b->currtime->tv_usec; - us /= 1000; - s = a->currtime->tv_sec - b->currtime->tv_sec; - s *= 1000; - *rv = s + us; - return APR_SUCCESS; -} - -/* ***APRDOC******************************************************** - * ap_status_t ap_timecmp(ap_time_t **, ap_time_t *, ap_time_t *) - * Compare two time values. - * arg 1) The first time value - * arg 2) The second time value. - * return) APR_LESS -- arg 1 < arg 2 - * APR_MORE -- arg 1 > arg 2 - * APR_EQUAL -- arg 1 = arg 2 - */ -ap_status_t ap_timecmp(struct atime_t *a, struct atime_t *b) -{ - if (a == NULL || a->currtime == NULL) { - return APR_LESS; - } - else if (b == NULL || b->currtime == NULL) { - return APR_MORE; - } - - if (a->currtime->tv_sec > b->currtime->tv_sec) { - return APR_MORE; - } - else if (a->currtime->tv_sec < b->currtime->tv_sec) { - return APR_LESS; - } - else { - if (a->currtime->tv_usec > b->currtime->tv_sec) { - return APR_MORE; - } - else { - return APR_LESS; - } - } - return APR_EQUAL; -} - diff --git a/time/unix/timestr.c b/time/unix/timestr.c index 8235fcfc7..a4b6aac4a 100644 --- a/time/unix/timestr.c +++ b/time/unix/timestr.c @@ -65,71 +65,110 @@ API_VAR_EXPORT const char ap_day_snames[7][4] = "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; -ap_status_t ap_timestr(char **date_str, struct atime_t *t, ap_timetype_e type, ap_context_t *p) +ap_status_t ap_rfc822_date(char *date_str, ap_time_t t) { - struct tm *tms; - char *date_str_ptr; + ap_exploded_time_t xt; + const char *s; int real_year; - (*date_str) = ap_palloc(p, 48 * sizeof(char)); - date_str_ptr = (*date_str); + ap_explode_gmt(&xt, t); - ap_explode_time(t, type); + /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */ + /* 12345678901234567890123456789 */ - /* Assumption: this is always 3 */ - /* i = strlen(ap_day_snames[tms->tm_wday]); */ - memcpy(date_str_ptr, ap_day_snames[t->explodedtime->tm_wday], 3); - date_str_ptr += 3; - *date_str_ptr++ = ','; - *date_str_ptr++ = ' '; - *date_str_ptr++ = t->explodedtime->tm_mday / 10 + '0'; - *date_str_ptr++ = t->explodedtime->tm_mday % 10 + '0'; - *date_str_ptr++ = ' '; - /* Assumption: this is also always 3 */ - /* i = strlen(ap_month_snames[tms->tm_mon]); */ - memcpy(date_str_ptr, ap_month_snames[t->explodedtime->tm_mon], 3); - date_str_ptr += 3; - *date_str_ptr++ = ' '; - real_year = 1900 + t->explodedtime->tm_year; + s = &ap_day_snames[xt.tm_wday][0]; + *date_str++ = *s++; + *date_str++ = *s++; + *date_str++ = *s++; + *date_str++ = ','; + *date_str++ = ' '; + *date_str++ = xt.tm_mday / 10 + '0'; + *date_str++ = xt.tm_mday % 10 + '0'; + *date_str++ = ' '; + s = &ap_month_snames[xt.tm_mon][0]; + *date_str++ = *s++; + *date_str++ = *s++; + *date_str++ = *s++; + *date_str++ = ' '; + real_year = 1900 + xt.tm_year; /* This routine isn't y10k ready. */ - *date_str_ptr++ = real_year / 1000 + '0'; - *date_str_ptr++ = real_year % 1000 / 100 + '0'; - *date_str_ptr++ = real_year % 100 / 10 + '0'; - *date_str_ptr++ = real_year % 10 + '0'; - *date_str_ptr++ = ' '; - *date_str_ptr++ = t->explodedtime->tm_hour / 10 + '0'; - *date_str_ptr++ = t->explodedtime->tm_hour % 10 + '0'; - *date_str_ptr++ = ':'; - *date_str_ptr++ = t->explodedtime->tm_min / 10 + '0'; - *date_str_ptr++ = t->explodedtime->tm_min % 10 + '0'; - *date_str_ptr++ = ':'; - *date_str_ptr++ = t->explodedtime->tm_sec / 10 + '0'; - *date_str_ptr++ = t->explodedtime->tm_sec % 10 + '0'; - if (type == APR_UTCTIME) { - *date_str_ptr++ = ' '; - *date_str_ptr++ = 'G'; - *date_str_ptr++ = 'M'; - *date_str_ptr++ = 'T'; - } - *date_str_ptr = '\0'; - return APR_SUCCESS; - /* RFC date format; as strftime '%a, %d %b %Y %T GMT' */ + *date_str++ = real_year / 1000 + '0'; + *date_str++ = real_year % 1000 / 100 + '0'; + *date_str++ = real_year % 100 / 10 + '0'; + *date_str++ = real_year % 10 + '0'; + *date_str++ = ' '; + *date_str++ = xt.tm_hour / 10 + '0'; + *date_str++ = xt.tm_hour % 10 + '0'; + *date_str++ = ':'; + *date_str++ = xt.tm_min / 10 + '0'; + *date_str++ = xt.tm_min % 10 + '0'; + *date_str++ = ':'; + *date_str++ = xt.tm_sec / 10 + '0'; + *date_str++ = xt.tm_sec % 10 + '0'; + *date_str++ = ' '; + *date_str++ = 'G'; + *date_str++ = 'M'; + *date_str++ = 'T'; + *date_str++ = 0; + return APR_SUCCESS; +} + +ap_status_t ap_ctime(char *date_str, ap_time_t t) +{ + ap_exploded_time_t xt; + const char *s; + int real_year; - /* The equivalent using sprintf. Use this for more legible but slower code - return ap_psprintf(p, - "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", - ap_day_snames[t->explodedtime->tm_wday], - t->explodedtime->tm_mday, - ap_month_snames[t->explodedtime->tm_mon], - t->explodedtime->tm_year + 1900, t->explodedtime->tm_hour, - t->explodedtime->tm_min, t->explodedtime->tm_sec); - */ + /* example: "Wed Jun 30 21:49:08 1993" */ + /* 123456789012345678901234 */ + + ap_explode_localtime(&xt, t); + s = &ap_day_snames[xt.tm_wday][0]; + *date_str++ = *s++; + *date_str++ = *s++; + *date_str++ = *s++; + *date_str++ = ' '; + s = &ap_month_snames[xt.tm_mon][0]; + *date_str++ = *s++; + *date_str++ = *s++; + *date_str++ = *s++; + *date_str++ = ' '; + *date_str++ = xt.tm_mday / 10 + '0'; + *date_str++ = xt.tm_mday % 10 + '0'; + *date_str++ = ' '; + *date_str++ = xt.tm_hour / 10 + '0'; + *date_str++ = xt.tm_hour % 10 + '0'; + *date_str++ = ':'; + *date_str++ = xt.tm_min / 10 + '0'; + *date_str++ = xt.tm_min % 10 + '0'; + *date_str++ = ':'; + *date_str++ = xt.tm_sec / 10 + '0'; + *date_str++ = xt.tm_sec % 10 + '0'; + *date_str++ = ' '; + real_year = 1900 + xt.tm_year; + *date_str++ = real_year / 1000 + '0'; + *date_str++ = real_year % 1000 / 100 + '0'; + *date_str++ = real_year % 100 / 10 + '0'; + *date_str++ = real_year % 10 + '0'; + *date_str++ = 0; + + return APR_SUCCESS; } ap_status_t ap_strftime(char *s, ap_size_t *retsize, ap_size_t max, - const char *format, struct atime_t *tm) + const char *format, ap_exploded_time_t *xt) { - (*retsize) = strftime(s, max, format, tm->explodedtime); + struct tm tm; + + tm.tm_sec = xt->tm_sec; + tm.tm_min = xt->tm_min; + tm.tm_hour = xt->tm_hour; + tm.tm_mday = xt->tm_mday; + tm.tm_mon = xt->tm_mon; + tm.tm_year = xt->tm_year; + tm.tm_wday = xt->tm_wday; + tm.tm_yday = xt->tm_yday; + tm.tm_isdst = xt->tm_isdst; + (*retsize) = strftime(s, max, format, &tm); return APR_SUCCESS; } - |