diff options
author | dreid <dreid@13f79535-47bb-0310-9956-ffa450edef68> | 2001-06-29 08:23:15 +0000 |
---|---|---|
committer | dreid <dreid@13f79535-47bb-0310-9956-ffa450edef68> | 2001-06-29 08:23:15 +0000 |
commit | b6c4b2b9a44f4c6ee66146ceacbe4c45a2023af9 (patch) | |
tree | 93be3814534870ad0a91932981ef9db199e0eb78 | |
parent | 87cccad7a70141c0d872a419e1a7b45ce227d897 (diff) | |
download | libapr-b6c4b2b9a44f4c6ee66146ceacbe4c45a2023af9.tar.gz |
Update the time code to make it clearer what's going on and to
correctly set the gmtoffset when we have it. Add a comment to try
and it more obvious what's going on following discussion on
the list.
Update the time test to be more obvious and use the new test
functions.
git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@61824 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | test/testtime.c | 148 | ||||
-rw-r--r-- | time/unix/time.c | 64 |
2 files changed, 79 insertions, 133 deletions
diff --git a/test/testtime.c b/test/testtime.c index 3730d547d..2bac3f3b2 100644 --- a/test/testtime.c +++ b/test/testtime.c @@ -58,9 +58,7 @@ #include "apr_lib.h" #include <errno.h> #include <stdio.h> -#ifdef BEOS -#include <unistd.h> -#endif +#include "test_apr.h" #define STR_SIZE 45 @@ -75,39 +73,21 @@ int main(void) apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */ apr_int64_t hr_off_64; - fprintf(stdout, "Testing Time functions.\n"); + printf("APR Time Functions\n==================\n\n"); - if (apr_pool_create(&p, NULL) != APR_SUCCESS){ - printf("Failed to create a context!\n"); - exit (-1); - } + STD_TEST_NEQ("Creating a pool to use", apr_pool_create(&p, NULL)) - printf("\tapr_time_now...................................."); + printf("%-60s", " apr_time_now()"); now = apr_time_now(); printf("OK\n"); - printf("\tapr_explode_gmt................................."); - if (apr_explode_gmt(&xt, now) != APR_SUCCESS) { - printf("Couldn't explode the time\n"); - exit(-1); - } - printf("OK\n"); + STD_TEST_NEQ(" apr_explode_gmt", apr_explode_gmt(&xt, now)) + + STD_TEST_NEQ(" apr_explode_localtime", apr_explode_localtime(&xt2, now)) - printf("\tapr_explode_localtime..........................."); - if (apr_explode_localtime(&xt2, now) != APR_SUCCESS) { - printf("Couldn't explode the time\n"); - exit(-1); - } - printf("OK\n"); + STD_TEST_NEQ(" apr_implode_time (GMT)", apr_implode_time(&imp, &xt)) - printf("\tapr_implode_time................................"); - if (apr_implode_time(&imp, &xt) != APR_SUCCESS) { - printf("Couldn't implode the time\n"); - exit(-1); - } - printf("OK\n"); - - printf("\tchecking the explode/implode (GMT).............."); + printf("%-60s", " checking GMT explode == implode"); if (imp != now) { printf("mismatch\n" "\t\tapr_now() %" APR_INT64_T_FMT "\n" @@ -119,96 +99,56 @@ int main(void) } printf("OK\n"); - printf("\tchecking the explode/implode (local time)......."); - if (apr_implode_time(&imp, &xt2) != APR_SUCCESS) { - printf("Couldn't implode the time\n"); - exit(-1); - } - hr_off_64 = (apr_int64_t)xt2.tm_gmtoff * APR_USEC_PER_SEC; /* microseconds */ - if (imp != now + hr_off_64) { - printf("mismatch\n" - "\t\tapr_now() %" APR_INT64_T_FMT "\n" - "\t\tapr_implode() returned %" APR_INT64_T_FMT "\n" - "\t\terror delta was %" APR_TIME_T_FMT "\n" - "\t\tshould have been %" APR_INT64_T_FMT "\n", - now, imp, imp-now, hr_off_64); - exit(-1); - } - printf("OK\n"); - str = apr_pcalloc(p, sizeof(char) * STR_SIZE); str2 = apr_pcalloc(p, sizeof(char) * STR_SIZE); imp = 0; - printf("\tapr_rfc822_date................."); - if (apr_rfc822_date(str, now) != APR_SUCCESS){ - printf("Failed!\n"); - exit (-1); - } - printf("%s\n", str); - - printf("\tapr_ctime.......(local)........."); - if (apr_ctime(str, now) != APR_SUCCESS){ - printf("Failed!\n"); + if (!str || !str2) { + printf("Failure!\n"); + fprintf(stderr,"Failed to allocate memory!\n"); exit(-1); } - printf("%s\n", str); - - printf("\tapr_strftime...................."); - if (str == NULL){ - printf("Couldn't allocate memory.\n"); - exit (-1); - } - if (apr_strftime(str, &sz, STR_SIZE, "%R %A %d %B %Y", &xt) != - APR_SUCCESS){ - printf("Failed!"); - exit (-1); - } - printf("%s\n", str); - printf("\tCurrent time (GMT).............................."); - if (apr_strftime(str, &sz, STR_SIZE, "%T ", &xt) != APR_SUCCESS){ - printf("Failed!\n"); - exit (-1); - } - printf ("%s\n", str); + STD_TEST_NEQ(" apr_rfc822_date", apr_rfc822_date(str, now)) + printf(" ( %s )\n", str); - printf("\tTrying to explode time with 5 hour offset......."); - if (apr_explode_time(&xt2, now, hr_off) != APR_SUCCESS){ - printf("Failed.\n"); - exit(-1); - } - printf("OK\n"); + STD_TEST_NEQ(" apr_ctime (local time)", apr_ctime(str, now)) + printf(" ( %s )\n", str); - printf("\tOffset Time Zone -5 hours......................."); - if (apr_strftime(str2, &sz, STR_SIZE, "%T (%z)", &xt2) != APR_SUCCESS){ - printf("Failed!\n"); - exit(-1); - } - printf("%s\n", str2); + STD_TEST_NEQ(" apr_strftime (GMT) (24H day date month year)", + apr_strftime(str, &sz, STR_SIZE, "%R %A %d %B %Y", &xt)) + printf(" ( %s )\n", str); - printf("\tComparing the created times....................."); - if (! strcmp(str,str2)){ - printf("Failed!\n"); - exit(-1); - } else { - printf("OK\n"); - } + STD_TEST_NEQ(" apr_strftime (GMT) (HH:MM:SS)", + apr_strftime(str, &sz, STR_SIZE, "%T", &xt)) + printf (" ( %s )\n", str); + + STD_TEST_NEQ(" apr_explode_time (GMT -5 hours)", + apr_explode_time(&xt2, now, hr_off)) + + STD_TEST_NEQ(" apr_strftime (offset) (HH:MM:SS)", + apr_strftime(str2, &sz, STR_SIZE, "%T", &xt2)) + printf(" ( %s )\n", str2); + + TEST_EQ(" Comparing the GMT and offset time strings", + strcmp(str, str2), 0, "OK", "Failed") + printf(" ( %s != %s )\n", str, str2); - printf("\tChecking imploded time after offset............."); - apr_implode_time(&imp, &xt2); - hr_off_64 = (apr_int64_t)hr_off * APR_USEC_PER_SEC; /* microseconds */ + STD_TEST_NEQ(" apr_implode_time (offset)", + apr_implode_time(&imp, &xt2)) + + hr_off_64 = (apr_int64_t) hr_off * APR_USEC_PER_SEC; /* microseconds */ + printf("%-60s"," Checking offset is correct"); if (imp != now + hr_off_64){ - printf("mismatch\n" - "\t\tapr_now() %" APR_INT64_T_FMT "\n" - "\t\tapr_implode() returned %" APR_INT64_T_FMT "\n" - "\t\terror delta was %" APR_TIME_T_FMT "\n" - "\t\tshould have been %" APR_INT64_T_FMT "\n", - now, imp, imp-now, hr_off_64); + printf("Failed! :(\n"); + printf("Difference is %" APR_INT64_T_FMT " (should be %" + APR_INT64_T_FMT")\n", imp - now, hr_off_64); exit(-1); } printf("OK\n"); - + printf(" ( %lld - %lld = %lld )\n", imp, now, imp - now); + + printf("\nTest Complete.\n"); return 1; } diff --git a/time/unix/time.c b/time/unix/time.c index 94a394eba..4763665d4 100644 --- a/time/unix/time.c +++ b/time/unix/time.c @@ -57,6 +57,7 @@ #include "apr_lib.h" #include "apr_private.h" #include "apr_strings.h" + /* System Headers required for time library */ #if APR_HAVE_SYS_TIME_H #include <sys/time.h> @@ -69,6 +70,28 @@ #endif /* End System Headers */ +static apr_int32_t get_offset(struct tm *tm) +{ +#ifdef HAVE_GMTOFF + return tm->tm_gmtoff; +#elif defined(HAVE___OFFSET) + return tm->__tm_gmtoff; +#else + /* we don't have an offset field to use, so calculate it */ + { + time_t t1 = time(0), t2 = 0; + struct tm t; + +# if APR_HAS_THREADS && defined (_POSIX_THREAD_SAFE_FUNCTIONS) + gmtime_r(tt, &t); +# else + t = *gmtime(tt); +# endif + t2 = mktime(&t); + return (apr_int32_t) difftime(t1,t2); + } +#endif +} apr_status_t apr_ansi_time_to_apr_time(apr_time_t *result, time_t input) { @@ -76,7 +99,7 @@ apr_status_t apr_ansi_time_to_apr_time(apr_time_t *result, time_t input) return APR_SUCCESS; } - +/* NB NB NB NB This returns GMT!!!!!!!!!! */ apr_time_t apr_time_now(void) { struct timeval tv; @@ -84,31 +107,6 @@ apr_time_t apr_time_now(void) return tv.tv_sec * APR_USEC_PER_SEC + tv.tv_usec; } -static void set_xt_gmtoff_from_tm(apr_exploded_time_t *xt, struct tm *tm, - time_t *tt) -{ -#ifdef HAVE_GMTOFF - xt->tm_gmtoff = tm->tm_gmtoff; -#elif defined(HAVE___OFFSET) - xt->tm_gmtoff = tm->__tm_gmtoff; -#else - { - struct tm t; - int days = 0, hours = 0, minutes = 0; -#if APR_HAS_THREADS && defined (_POSIX_THREAD_SAFE_FUNCTIONS) - gmtime_r(tt, &t); -#else - t = *gmtime(tt); -#endif - days = xt->tm_yday - t.tm_yday; - hours = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + - xt->tm_hour - t.tm_hour); - minutes = hours * 60 + xt->tm_min - t.tm_min; - xt->tm_gmtoff = minutes * 60; - } -#endif -} - static void explode_time(apr_exploded_time_t *xt, apr_time_t t, apr_int32_t offset, int use_localtime) { @@ -137,7 +135,7 @@ static void explode_time(apr_exploded_time_t *xt, apr_time_t t, xt->tm_wday = tm.tm_wday; xt->tm_yday = tm.tm_yday; xt->tm_isdst = tm.tm_isdst; - set_xt_gmtoff_from_tm(xt,&tm,&tt); + xt->tm_gmtoff = get_offset(&tm); } apr_status_t apr_explode_time(apr_exploded_time_t *result, apr_time_t input, @@ -214,7 +212,11 @@ apr_status_t apr_os_exp_time_get(apr_os_exp_time_t **ostime, (*ostime)->tm_wday = aprtime->tm_wday; (*ostime)->tm_yday = aprtime->tm_yday; (*ostime)->tm_isdst = aprtime->tm_isdst; - /* XXX - Need to handle gmt_offset's here ! */ +#if HAVE_GMTOFF + (*ostime)->tm_gmtoff = aprtime->tm_gmtoff; +#else if defined(HAVE__OFFSET) + (*ostime)->__tm_gmtoff = aprtime->tm_gmtoff; +#endif return APR_SUCCESS; } @@ -237,7 +239,11 @@ apr_status_t apr_os_exp_time_put(apr_exploded_time_t *aprtime, aprtime->tm_wday = (*ostime)->tm_wday; aprtime->tm_yday = (*ostime)->tm_yday; aprtime->tm_isdst = (*ostime)->tm_isdst; - /* XXX - Need to handle gmt_offsets here */ +#if HAVE_GMTOFF + aprtime->tm_gmtoff = (*ostime)->tm_gmtoff; +#else if defined(HAVE__OFFSET) + aprtime->tm_gmtoff = (*ostime)->__tm_gmtoff; +#endif return APR_SUCCESS; } |