apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dr...@apache.org
Subject cvs commit: apr/test testtime.c
Date Fri, 29 Jun 2001 08:23:16 GMT
dreid       01/06/29 01:23:16

  Modified:    time/unix time.c
               test     testtime.c
  Log:
  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.
  
  Revision  Changes    Path
  1.46      +35 -29    apr/time/unix/time.c
  
  Index: time.c
  ===================================================================
  RCS file: /home/cvs/apr/time/unix/time.c,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- time.c	2001/06/28 02:17:36	1.45
  +++ time.c	2001/06/29 08:23:14	1.46
  @@ -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,14 +70,36 @@
   #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)
   {
       *result = (apr_time_t)input * APR_USEC_PER_SEC;
       return APR_SUCCESS;
   }
  -
   
  +/* NB NB NB NB This returns GMT!!!!!!!!!! */
   apr_time_t apr_time_now(void)
   {
       struct timeval tv;
  @@ -84,31 +107,6 @@
       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 @@
       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 @@
       (*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 @@
       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;
   }
   
  
  
  
  1.22      +43 -103   apr/test/testtime.c
  
  Index: testtime.c
  ===================================================================
  RCS file: /home/cvs/apr/test/testtime.c,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- testtime.c	2001/06/28 01:36:22	1.21
  +++ testtime.c	2001/06/29 08:23:15	1.22
  @@ -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 @@
       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");
   
  -    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_explode_gmt", apr_explode_gmt(&xt, now))
  +    
  +    STD_TEST_NEQ("    apr_explode_localtime", apr_explode_localtime(&xt2, now))
   
  -    printf("\tapr_implode_time................................");
  -    if (apr_implode_time(&imp, &xt) != APR_SUCCESS) {
  -        printf("Couldn't implode the time\n");
  -        exit(-1);
  -    }
  -    printf("OK\n");
  +    STD_TEST_NEQ("    apr_implode_time (GMT)", apr_implode_time(&imp, &xt))
   
  -    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 @@
       }
       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);
  +    STD_TEST_NEQ("    apr_rfc822_date", apr_rfc822_date(str, now))
  +    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_ctime (local time)", apr_ctime(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_strftime (GMT) (24H day date month year)", 
  +                 apr_strftime(str, &sz, STR_SIZE, "%R %A %d %B %Y", &xt))
  +    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) (HH:MM:SS)",
  +                 apr_strftime(str, &sz, STR_SIZE, "%T", &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_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;
   }    
   
  
  
  

Mime
View raw message