nsprpub/pr/tests/timetest.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/timetest.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,739 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * file: timetest.c
    1.11 + * description: test time and date routines
    1.12 + */
    1.13 +/***********************************************************************
    1.14 +** Includes
    1.15 +***********************************************************************/
    1.16 +/* Used to get the command line option */
    1.17 +#include "plgetopt.h"
    1.18 +
    1.19 +#include "prinit.h"
    1.20 +#include "prtime.h"
    1.21 +#include "prprf.h"
    1.22 +
    1.23 +#include <stdio.h>
    1.24 +#include <stdlib.h>
    1.25 +#include <string.h>
    1.26 +
    1.27 +int failed_already=0;
    1.28 +PRBool debug_mode = PR_FALSE;
    1.29 +
    1.30 +static char *dayOfWeek[] =
    1.31 +	{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???" };
    1.32 +static char *month[] =
    1.33 +	{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    1.34 +	  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???" };
    1.35 +
    1.36 +static void PrintExplodedTime(const PRExplodedTime *et) {
    1.37 +    PRInt32 totalOffset;
    1.38 +    PRInt32 hourOffset, minOffset;
    1.39 +    const char *sign;
    1.40 +
    1.41 +    /* Print day of the week, month, day, hour, minute, and second */
    1.42 +    if (debug_mode) printf("%s %s %ld %02ld:%02ld:%02ld ",
    1.43 +	    dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday,
    1.44 +	    et->tm_hour, et->tm_min, et->tm_sec);
    1.45 +
    1.46 +    /* Print time zone */
    1.47 +    totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset;
    1.48 +    if (totalOffset == 0) {
    1.49 +	if (debug_mode) printf("UTC ");
    1.50 +    } else {
    1.51 +        sign = "+";
    1.52 +        if (totalOffset < 0) {
    1.53 +	    totalOffset = -totalOffset;
    1.54 +	    sign = "-";
    1.55 +        }
    1.56 +        hourOffset = totalOffset / 3600;
    1.57 +        minOffset = (totalOffset % 3600) / 60;
    1.58 +        if (debug_mode) 
    1.59 +            printf("%s%02ld%02ld ", sign, hourOffset, minOffset);
    1.60 +    }
    1.61 +
    1.62 +    /* Print year */
    1.63 +    if (debug_mode) printf("%hd", et->tm_year);
    1.64 +}
    1.65 +
    1.66 +static int ExplodedTimeIsEqual(const PRExplodedTime *et1,
    1.67 +	const PRExplodedTime *et2)
    1.68 +{
    1.69 +    if (et1->tm_usec == et2->tm_usec &&
    1.70 +	    et1->tm_sec == et2->tm_sec &&
    1.71 +	    et1->tm_min == et2->tm_min &&
    1.72 +	    et1->tm_hour == et2->tm_hour &&
    1.73 +	    et1->tm_mday == et2->tm_mday &&
    1.74 +	    et1->tm_month == et2->tm_month &&
    1.75 +	    et1->tm_year == et2->tm_year &&
    1.76 +	    et1->tm_wday == et2->tm_wday &&
    1.77 +	    et1->tm_yday == et2->tm_yday &&
    1.78 +	    et1->tm_params.tp_gmt_offset == et2->tm_params.tp_gmt_offset &&
    1.79 +	    et1->tm_params.tp_dst_offset == et2->tm_params.tp_dst_offset) {
    1.80 +        return 1;
    1.81 +    } else {
    1.82 +	return 0;
    1.83 +    }
    1.84 +}
    1.85 +
    1.86 +static void
    1.87 +testParseTimeString(PRTime t)
    1.88 +{
    1.89 +    PRExplodedTime et;
    1.90 +    PRTime t2;
    1.91 +    char timeString[128];
    1.92 +    char buf[128];
    1.93 +    PRInt32 totalOffset;
    1.94 +    PRInt32 hourOffset, minOffset;
    1.95 +    const char *sign;
    1.96 +    PRInt64 usec_per_sec;
    1.97 +
    1.98 +    /* Truncate the microsecond part of PRTime */
    1.99 +    LL_I2L(usec_per_sec, PR_USEC_PER_SEC);
   1.100 +    LL_DIV(t, t, usec_per_sec);
   1.101 +    LL_MUL(t, t, usec_per_sec);
   1.102 +
   1.103 +    PR_ExplodeTime(t, PR_LocalTimeParameters, &et);
   1.104 +
   1.105 +    /* Print day of the week, month, day, hour, minute, and second */
   1.106 +    PR_snprintf(timeString, 128, "%s %s %ld %02ld:%02ld:%02ld ",
   1.107 +	    dayOfWeek[et.tm_wday], month[et.tm_month], et.tm_mday,
   1.108 +	    et.tm_hour, et.tm_min, et.tm_sec);
   1.109 +    /* Print time zone */
   1.110 +    totalOffset = et.tm_params.tp_gmt_offset + et.tm_params.tp_dst_offset;
   1.111 +    if (totalOffset == 0) {
   1.112 +	strcat(timeString, "GMT ");  /* I wanted to use "UTC" here, but
   1.113 +                                      * PR_ParseTimeString doesn't 
   1.114 +                                      * understand "UTC".  */
   1.115 +    } else {
   1.116 +        sign = "+";
   1.117 +        if (totalOffset < 0) {
   1.118 +	    totalOffset = -totalOffset;
   1.119 +	    sign = "-";
   1.120 +        }
   1.121 +        hourOffset = totalOffset / 3600;
   1.122 +        minOffset = (totalOffset % 3600) / 60;
   1.123 +        PR_snprintf(buf, 128, "%s%02ld%02ld ", sign, hourOffset, minOffset);
   1.124 +	strcat(timeString, buf);
   1.125 +    }
   1.126 +    /* Print year */
   1.127 +    PR_snprintf(buf, 128, "%hd", et.tm_year);
   1.128 +    strcat(timeString, buf);
   1.129 +
   1.130 +    if (PR_ParseTimeString(timeString, PR_FALSE, &t2) == PR_FAILURE) {
   1.131 +	fprintf(stderr, "PR_ParseTimeString() failed\n");
   1.132 +	exit(1);
   1.133 +    }
   1.134 +    if (LL_NE(t, t2)) {
   1.135 +	fprintf(stderr, "PR_ParseTimeString() incorrect\n");
   1.136 +	PR_snprintf(buf, 128, "t is %lld, t2 is %lld, time string is %s\n",
   1.137 +                t, t2, timeString);
   1.138 +	fprintf(stderr, "%s\n", buf);
   1.139 +	exit(1);
   1.140 +    }
   1.141 +}
   1.142 +
   1.143 +int main(int argc, char** argv)
   1.144 +{
   1.145 +	/* The command line argument: -d is used to determine if the test is being run
   1.146 +	in debug mode. The regress tool requires only one line output:PASS or FAIL.
   1.147 +	All of the printfs associated with this test has been handled with a if (debug_mode)
   1.148 +	test.
   1.149 +	Usage: test_name -d
   1.150 +	*/
   1.151 +	PLOptStatus os;
   1.152 +	PLOptState *opt;
   1.153 +    
   1.154 +    PR_STDIO_INIT();
   1.155 +	opt = PL_CreateOptState(argc, argv, "d");
   1.156 +	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
   1.157 +    {
   1.158 +		if (PL_OPT_BAD == os) continue;
   1.159 +        switch (opt->option)
   1.160 +        {
   1.161 +        case 'd':  /* debug mode */
   1.162 +			debug_mode = PR_TRUE;
   1.163 +            break;
   1.164 +         default:
   1.165 +            break;
   1.166 +        }
   1.167 +    }
   1.168 +	PL_DestroyOptState(opt);
   1.169 +
   1.170 + /* main test */
   1.171 +	
   1.172 +    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
   1.173 +
   1.174 +    /* Testing zero PRTime (the epoch) */
   1.175 +    {
   1.176 +	PRTime t;
   1.177 +	PRExplodedTime et;
   1.178 +
   1.179 +	LL_I2L(t, 0);
   1.180 +	if (debug_mode) printf("The NSPR epoch is:\n");
   1.181 +        PR_ExplodeTime(t, PR_LocalTimeParameters, &et);
   1.182 +	PrintExplodedTime(&et);
   1.183 +	if (debug_mode) printf("\n");
   1.184 +	PR_ExplodeTime(t, PR_GMTParameters, &et);
   1.185 +	PrintExplodedTime(&et);
   1.186 +	if (debug_mode) printf("\n\n");
   1.187 +	testParseTimeString(t);
   1.188 +    }
   1.189 +
   1.190 +    /*
   1.191 +     *************************************************************
   1.192 +     **
   1.193 +     **  Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime
   1.194 +     **  on the current time
   1.195 +     **
   1.196 +     *************************************************************
   1.197 +     */
   1.198 +
   1.199 +    {
   1.200 +	PRTime t1, t2;
   1.201 +	PRExplodedTime et;
   1.202 +
   1.203 +	if (debug_mode) {
   1.204 +	printf("*********************************************\n");
   1.205 +	printf("**                                         **\n");
   1.206 +    printf("** Testing PR_Now(), PR_ExplodeTime, and   **\n");
   1.207 +	printf("** PR_ImplodeTime on the current time      **\n");
   1.208 +	printf("**                                         **\n");
   1.209 +	printf("*********************************************\n\n");
   1.210 +	}
   1.211 +	t1 = PR_Now();
   1.212 +
   1.213 +        /* First try converting to UTC */
   1.214 +
   1.215 +        PR_ExplodeTime(t1, PR_GMTParameters, &et);
   1.216 +        if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) {
   1.217 +	    if (debug_mode) printf("ERROR: UTC has nonzero gmt or dst offset.\n");
   1.218 +		else failed_already=1;
   1.219 +	    return 1;
   1.220 +        }
   1.221 +        if (debug_mode) printf("Current UTC is ");
   1.222 +	PrintExplodedTime(&et);
   1.223 +	if (debug_mode) printf("\n");
   1.224 +
   1.225 +        t2 = PR_ImplodeTime(&et);
   1.226 +        if (LL_NE(t1, t2)) {
   1.227 +	    if (debug_mode) printf("ERROR: Explode and implode are NOT inverse.\n");
   1.228 +		else printf("FAIL\n");
   1.229 +	    return 1;
   1.230 +        }
   1.231 +
   1.232 +        /* Next, try converting to local (US Pacific) time */
   1.233 +
   1.234 +        PR_ExplodeTime(t1, PR_LocalTimeParameters, &et);
   1.235 +        if (debug_mode) printf("Current local time is ");
   1.236 +	PrintExplodedTime(&et);
   1.237 +	if (debug_mode) printf("\n");
   1.238 +	if (debug_mode) printf("GMT offset is %ld, DST offset is %ld\n",
   1.239 +		et.tm_params.tp_gmt_offset, et.tm_params.tp_dst_offset);
   1.240 +        t2 = PR_ImplodeTime(&et);
   1.241 +        if (LL_NE(t1, t2)) {
   1.242 +	    if (debug_mode) printf("ERROR: Explode and implode are NOT inverse.\n");
   1.243 +	    return 1;
   1.244 +	}
   1.245 +
   1.246 +	if (debug_mode) printf("Please examine the results\n");
   1.247 +	testParseTimeString(t1);
   1.248 +    }
   1.249 +
   1.250 +
   1.251 +    /*
   1.252 +     *******************************************
   1.253 +     **
   1.254 +     ** Testing PR_NormalizeTime()
   1.255 +     **
   1.256 +     *******************************************
   1.257 +     */
   1.258 +
   1.259 +    /* July 4, 2001 is Wednesday */
   1.260 +    {
   1.261 +	PRExplodedTime et;
   1.262 +
   1.263 +	if (debug_mode)  {
   1.264 +	printf("\n");
   1.265 +	printf("**********************************\n");
   1.266 +	printf("**                              **\n");
   1.267 +	printf("** Testing PR_NormalizeTime()   **\n");
   1.268 +	printf("**                              **\n");
   1.269 +	printf("**********************************\n\n");
   1.270 +	}
   1.271 +        et.tm_year    = 2001;
   1.272 +        et.tm_month   = 7 - 1;
   1.273 +        et.tm_mday    = 4;
   1.274 +        et.tm_hour    = 0;
   1.275 +        et.tm_min     = 0;
   1.276 +        et.tm_sec     = 0;
   1.277 +	et.tm_usec    = 0;
   1.278 +        et.tm_params  = PR_GMTParameters(&et);
   1.279 +
   1.280 +	PR_NormalizeTime(&et, PR_GMTParameters);
   1.281 +
   1.282 +	if (debug_mode) printf("July 4, 2001 is %s.\n", dayOfWeek[et.tm_wday]);
   1.283 +	if (et.tm_wday == 3) {
   1.284 +	    if (debug_mode) printf("PASS\n");
   1.285 +        } else {
   1.286 +            if (debug_mode) printf("ERROR: It should be Wednesday\n");
   1.287 +			else failed_already=1;
   1.288 +	    return 1;
   1.289 +	}
   1.290 +	testParseTimeString(PR_ImplodeTime(&et));
   1.291 +
   1.292 +        /* June 12, 1997 23:00 PST == June 13, 1997 00:00 PDT */
   1.293 +        et.tm_year    = 1997;
   1.294 +        et.tm_month   = 6 - 1;
   1.295 +        et.tm_mday    = 12;
   1.296 +        et.tm_hour    = 23;
   1.297 +        et.tm_min     = 0;
   1.298 +        et.tm_sec     = 0;
   1.299 +	et.tm_usec    = 0;
   1.300 +        et.tm_params.tp_gmt_offset = -8 * 3600;
   1.301 +	et.tm_params.tp_dst_offset = 0;
   1.302 +
   1.303 +	PR_NormalizeTime(&et, PR_USPacificTimeParameters);
   1.304 +
   1.305 +	if (debug_mode) {
   1.306 +	    printf("Thu Jun 12, 1997 23:00:00 PST is ");
   1.307 +	}
   1.308 +	PrintExplodedTime(&et);
   1.309 +	if (debug_mode) printf(".\n");
   1.310 +	if (et.tm_wday == 5) {
   1.311 +	    if (debug_mode) printf("PASS\n");
   1.312 +        } else {
   1.313 +            if (debug_mode) printf("ERROR: It should be Friday\n");
   1.314 +			else failed_already=1;
   1.315 +	    return 1;
   1.316 +	}
   1.317 +	testParseTimeString(PR_ImplodeTime(&et));
   1.318 +
   1.319 +        /* Feb 14, 1997 00:00:00 PDT == Feb 13, 1997 23:00:00 PST */
   1.320 +        et.tm_year    = 1997;
   1.321 +        et.tm_month   = 2 - 1;
   1.322 +        et.tm_mday    = 14;
   1.323 +        et.tm_hour    = 0;
   1.324 +        et.tm_min     = 0;
   1.325 +        et.tm_sec     = 0;
   1.326 +	et.tm_usec    = 0;
   1.327 +        et.tm_params.tp_gmt_offset = -8 * 3600;
   1.328 +	et.tm_params.tp_dst_offset = 3600;
   1.329 +
   1.330 +	PR_NormalizeTime(&et, PR_USPacificTimeParameters);
   1.331 +
   1.332 +	if (debug_mode) {
   1.333 +	    printf("Fri Feb 14, 1997 00:00:00 PDT is ");
   1.334 +	}
   1.335 +	PrintExplodedTime(&et);
   1.336 +	if (debug_mode) printf(".\n");
   1.337 +	if (et.tm_wday == 4) {
   1.338 +	    if (debug_mode) printf("PASS\n");
   1.339 +        } else {
   1.340 +            if (debug_mode) printf("ERROR: It should be Thursday\n");
   1.341 +			else failed_already=1;
   1.342 +	    return 1;
   1.343 +	}
   1.344 +	testParseTimeString(PR_ImplodeTime(&et));
   1.345 +
   1.346 +        /* What time is Nov. 7, 1996, 18:29:23 PDT? */
   1.347 +        et.tm_year    = 1996;
   1.348 +        et.tm_month   = 11 - 1;
   1.349 +        et.tm_mday    = 7;
   1.350 +        et.tm_hour    = 18;
   1.351 +        et.tm_min     = 29;
   1.352 +        et.tm_sec     = 23;
   1.353 +	et.tm_usec    = 0;
   1.354 +        et.tm_params.tp_gmt_offset = -8 * 3600;  /* PDT */
   1.355 +	et.tm_params.tp_dst_offset = 3600; 
   1.356 +
   1.357 +	PR_NormalizeTime(&et, PR_LocalTimeParameters);
   1.358 +        if (debug_mode) printf("Nov 7 18:29:23 PDT 1996 is ");
   1.359 +	PrintExplodedTime(&et);
   1.360 +	if (debug_mode) printf(".\n");
   1.361 +	testParseTimeString(PR_ImplodeTime(&et));
   1.362 +
   1.363 +        /* What time is Oct. 7, 1995, 18:29:23 PST? */
   1.364 +        et.tm_year    = 1995;
   1.365 +        et.tm_month   = 10 - 1;
   1.366 +        et.tm_mday    = 7;
   1.367 +        et.tm_hour    = 18;
   1.368 +        et.tm_min     = 29;
   1.369 +        et.tm_sec     = 23;
   1.370 +        et.tm_params.tp_gmt_offset = -8 * 3600;  /* PST */
   1.371 +	et.tm_params.tp_dst_offset = 0;
   1.372 +
   1.373 +	PR_NormalizeTime(&et, PR_LocalTimeParameters);
   1.374 +        if (debug_mode) printf("Oct 7 18:29:23 PST 1995 is ");
   1.375 +	PrintExplodedTime(&et);
   1.376 +	if (debug_mode) printf(".\n");
   1.377 +	testParseTimeString(PR_ImplodeTime(&et));
   1.378 +
   1.379 +	if (debug_mode) printf("Please examine the results\n");
   1.380 +    }
   1.381 +
   1.382 +    /*
   1.383 +     **************************************************************
   1.384 +     **
   1.385 +     ** Testing range of years
   1.386 +     **
   1.387 +     **************************************************************
   1.388 +     */
   1.389 +
   1.390 +    {
   1.391 +	PRExplodedTime et1, et2;
   1.392 +    PRTime  ttt;
   1.393 +	PRTime secs;
   1.394 +
   1.395 +	if (debug_mode) {
   1.396 +	printf("\n");
   1.397 +	printf("***************************************\n");
   1.398 +	printf("**                                   **\n");
   1.399 +	printf("**  Testing range of years           **\n");
   1.400 +	printf("**                                   **\n");
   1.401 +	printf("***************************************\n\n");
   1.402 +	}
   1.403 +	/* April 4, 1917 GMT */
   1.404 +	et1.tm_usec = 0;
   1.405 +	et1.tm_sec = 0;
   1.406 +	et1.tm_min = 0;
   1.407 +	et1.tm_hour = 0;
   1.408 +	et1.tm_mday = 4;
   1.409 +	et1.tm_month = 4 - 1;
   1.410 +	et1.tm_year = 1917;
   1.411 +	et1.tm_params = PR_GMTParameters(&et1);
   1.412 +	PR_NormalizeTime(&et1, PR_LocalTimeParameters);
   1.413 +	secs = PR_ImplodeTime(&et1);
   1.414 +	if (LL_GE_ZERO(secs)) {
   1.415 +	    if (debug_mode)
   1.416 +		printf("ERROR: April 4, 1917 GMT returns a nonnegative second count\n");
   1.417 +		failed_already = 1;
   1.418 +	    return 1;
   1.419 +        }
   1.420 +	PR_ExplodeTime(secs, PR_LocalTimeParameters, &et2);
   1.421 +	if (!ExplodedTimeIsEqual(&et1, &et2)) {
   1.422 +		if (debug_mode)
   1.423 +		printf("ERROR: PR_ImplodeTime and PR_ExplodeTime are not inverse for April 4, 1917 GMT\n");
   1.424 +		failed_already=1;
   1.425 +	    return 1;
   1.426 +        }
   1.427 +    ttt = PR_ImplodeTime(&et1);
   1.428 +	testParseTimeString( ttt );
   1.429 +
   1.430 +	if (debug_mode) printf("Test passed for April 4, 1917\n");
   1.431 +
   1.432 +	/* July 4, 2050 */
   1.433 +	et1.tm_usec = 0;
   1.434 +	et1.tm_sec = 0;
   1.435 +	et1.tm_min = 0;
   1.436 +	et1.tm_hour = 0;
   1.437 +	et1.tm_mday = 4;
   1.438 +	et1.tm_month = 7 - 1;
   1.439 +	et1.tm_year = 2050;
   1.440 +	et1.tm_params = PR_GMTParameters(&et1);
   1.441 +	PR_NormalizeTime(&et1, PR_LocalTimeParameters);
   1.442 +	secs = PR_ImplodeTime(&et1);
   1.443 +	if (!LL_GE_ZERO(secs)) {
   1.444 +	    if (debug_mode)
   1.445 +			printf("ERROR: July 4, 2050 GMT returns a negative second count\n");
   1.446 +		failed_already = 1;
   1.447 +	    return 1;
   1.448 +        }
   1.449 +	PR_ExplodeTime(secs, PR_LocalTimeParameters, &et2);
   1.450 +	if (!ExplodedTimeIsEqual(&et1, &et2)) {
   1.451 +	    if (debug_mode)
   1.452 +		printf("ERROR: PR_ImplodeTime and PR_ExplodeTime are not inverse for July 4, 2050 GMT\n");
   1.453 +		failed_already=1;
   1.454 +	    return 1;
   1.455 +        }
   1.456 +	testParseTimeString(PR_ImplodeTime(&et1));
   1.457 +
   1.458 +	if (debug_mode) printf("Test passed for July 4, 2050\n");
   1.459 +
   1.460 +    }
   1.461 +
   1.462 +    /*
   1.463 +     **************************************************************
   1.464 +     **
   1.465 +     **  Stress test
   1.466 +     *
   1.467 +     **      Go through four years, starting from
   1.468 +     **      00:00:00 PST Jan. 1, 2005, incrementing
   1.469 +     **      every 10 minutes.
   1.470 +     **
   1.471 +     **************************************************************
   1.472 +     */
   1.473 +
   1.474 +    {
   1.475 +	PRExplodedTime et, et1, et2;
   1.476 +	PRInt64 usecPer10Min;
   1.477 +	int day, hour, min;
   1.478 +	PRTime usecs;
   1.479 +	int dstInEffect = 0;
   1.480 +
   1.481 +	if (debug_mode) {
   1.482 +	printf("\n");
   1.483 +	printf("*******************************************************\n");
   1.484 +	printf("**                                                   **\n");
   1.485 +	printf("**        Stress test  Pacific Time                  **\n");
   1.486 +	printf("**  Starting from midnight Jan. 1, 2005 PST,         **\n");
   1.487 +	printf("**  going through four years in 10-minute increment  **\n");
   1.488 +	printf("**                                                   **\n");
   1.489 +	printf("*******************************************************\n\n");
   1.490 +	}
   1.491 +	LL_I2L(usecPer10Min, 600000000L);
   1.492 +
   1.493 +	/* 00:00:00 PST Jan. 1, 2005 */
   1.494 +	et.tm_usec = 0;
   1.495 +	et.tm_sec = 0;
   1.496 +	et.tm_min = 0;
   1.497 +	et.tm_hour = 0;
   1.498 +	et.tm_mday = 1;
   1.499 +	et.tm_month = 0;
   1.500 +	et.tm_year = 2005;
   1.501 +	et.tm_params.tp_gmt_offset = -8 * 3600;
   1.502 +	et.tm_params.tp_dst_offset = 0;
   1.503 +	usecs = PR_ImplodeTime(&et);
   1.504 +
   1.505 +        for (day = 0; day < 4 * 365 + 1; day++) {
   1.506 +	    for (hour = 0; hour < 24; hour++) {
   1.507 +		for (min = 0; min < 60; min += 10) {
   1.508 +	            LL_ADD(usecs, usecs, usecPer10Min);
   1.509 +		    PR_ExplodeTime(usecs, PR_USPacificTimeParameters, &et1);
   1.510 +
   1.511 +		    et2 = et;
   1.512 +		    et2.tm_usec += 600000000L;
   1.513 +		    PR_NormalizeTime(&et2, PR_USPacificTimeParameters);
   1.514 +
   1.515 +		    if (!ExplodedTimeIsEqual(&et1, &et2)) {
   1.516 +			printf("ERROR: componentwise comparison failed\n");
   1.517 +			PrintExplodedTime(&et1);
   1.518 +			printf("\n");
   1.519 +			PrintExplodedTime(&et2);
   1.520 +			printf("\n");
   1.521 +			failed_already=1;
   1.522 +		        return 1;
   1.523 +		    }
   1.524 +
   1.525 +		    if (LL_NE(usecs, PR_ImplodeTime(&et1))) { 
   1.526 +			printf("ERROR: PR_ExplodeTime and PR_ImplodeTime are not inverse\n");
   1.527 +			PrintExplodedTime(&et1);
   1.528 +			printf("\n");
   1.529 +			failed_already=1;
   1.530 +		        return 1;
   1.531 +		    }
   1.532 +		    testParseTimeString(usecs);
   1.533 +
   1.534 +		    if (!dstInEffect && et1.tm_params.tp_dst_offset) {
   1.535 +		        dstInEffect = 1;
   1.536 +		        if (debug_mode) {
   1.537 +			    printf("DST changeover from ");
   1.538 +			    PrintExplodedTime(&et);
   1.539 +			    printf(" to ");
   1.540 +			    PrintExplodedTime(&et1);
   1.541 +			    printf(".\n");
   1.542 +		    	}
   1.543 +                    } else if (dstInEffect && !et1.tm_params.tp_dst_offset) {
   1.544 +		        dstInEffect = 0;
   1.545 +			if (debug_mode) {
   1.546 +			    printf("DST changeover from ");
   1.547 +			    PrintExplodedTime(&et);
   1.548 +			    printf(" to ");
   1.549 +			    PrintExplodedTime(&et1);
   1.550 +			    printf(".\n");
   1.551 +			}
   1.552 +                    }
   1.553 +
   1.554 +		    et = et1;
   1.555 +		}
   1.556 +	    }
   1.557 +        }
   1.558 +	if (debug_mode) printf("Test passed\n");
   1.559 +    }
   1.560 +
   1.561 +
   1.562 +    /* Same stress test, but with PR_LocalTimeParameters */
   1.563 +
   1.564 +    {
   1.565 +	PRExplodedTime et, et1, et2;
   1.566 +	PRInt64 usecPer10Min;
   1.567 +	int day, hour, min;
   1.568 +	PRTime usecs;
   1.569 +	int dstInEffect = 0;
   1.570 +
   1.571 +	if (debug_mode) {
   1.572 +	printf("\n");
   1.573 +	printf("*******************************************************\n");
   1.574 +	printf("**                                                   **\n");
   1.575 +	printf("**         Stress test    Local Time                 **\n");
   1.576 +	printf("**  Starting from midnight Jan. 1, 2005 PST,         **\n");
   1.577 +	printf("**  going through four years in 10-minute increment  **\n");
   1.578 +	printf("**                                                   **\n");
   1.579 +	printf("*******************************************************\n\n");
   1.580 +	}
   1.581 +	
   1.582 +	LL_I2L(usecPer10Min, 600000000L);
   1.583 +
   1.584 +	/* 00:00:00 PST Jan. 1, 2005 */
   1.585 +	et.tm_usec = 0;
   1.586 +	et.tm_sec = 0;
   1.587 +	et.tm_min = 0;
   1.588 +	et.tm_hour = 0;
   1.589 +	et.tm_mday = 1;
   1.590 +	et.tm_month = 0;
   1.591 +	et.tm_year = 2005;
   1.592 +	et.tm_params.tp_gmt_offset = -8 * 3600;
   1.593 +	et.tm_params.tp_dst_offset = 0;
   1.594 +	usecs = PR_ImplodeTime(&et);
   1.595 +
   1.596 +        for (day = 0; day < 4 * 365 + 1; day++) {
   1.597 +	    for (hour = 0; hour < 24; hour++) {
   1.598 +		for (min = 0; min < 60; min += 10) {
   1.599 +	            LL_ADD(usecs, usecs, usecPer10Min);
   1.600 +		    PR_ExplodeTime(usecs, PR_LocalTimeParameters, &et1);
   1.601 +
   1.602 +		    et2 = et;
   1.603 +		    et2.tm_usec += 600000000L;
   1.604 +		    PR_NormalizeTime(&et2, PR_LocalTimeParameters);
   1.605 +
   1.606 +		    if (!ExplodedTimeIsEqual(&et1, &et2)) {
   1.607 +			printf("ERROR: componentwise comparison failed\n");
   1.608 +			PrintExplodedTime(&et1);
   1.609 +			printf("\n");
   1.610 +			PrintExplodedTime(&et2);
   1.611 +			printf("\n");
   1.612 +		        return 1;
   1.613 +		    }
   1.614 +
   1.615 +		    if (LL_NE(usecs, PR_ImplodeTime(&et1))) {
   1.616 +                        printf("ERROR: PR_ExplodeTime and PR_ImplodeTime are not inverse\n");
   1.617 +			PrintExplodedTime(&et1);
   1.618 +			printf("\n");
   1.619 +			failed_already=1;
   1.620 +		        return 1;
   1.621 +		    }
   1.622 +		    testParseTimeString(usecs);
   1.623 +
   1.624 +		    if (!dstInEffect && et1.tm_params.tp_dst_offset) {
   1.625 +		        dstInEffect = 1;
   1.626 +		        if (debug_mode) {
   1.627 +			    printf("DST changeover from ");
   1.628 +			    PrintExplodedTime(&et);
   1.629 +			    printf(" to ");
   1.630 +			    PrintExplodedTime(&et1);
   1.631 +			    printf(".\n");
   1.632 +			}
   1.633 +                    } else if (dstInEffect && !et1.tm_params.tp_dst_offset) {
   1.634 +		        dstInEffect = 0;
   1.635 +			if (debug_mode) {
   1.636 +			    printf("DST changeover from ");
   1.637 +			    PrintExplodedTime(&et);
   1.638 +			    printf(" to ");
   1.639 +			    PrintExplodedTime(&et1);
   1.640 +			    printf(".\n");
   1.641 +			}
   1.642 +                    }
   1.643 +
   1.644 +		    et = et1;
   1.645 +		}
   1.646 +	    }
   1.647 +        }
   1.648 +	if (debug_mode) printf("Test passed\n");
   1.649 +    }
   1.650 +
   1.651 +    /* Same stress test, but with PR_LocalTimeParameters and going backward */
   1.652 +
   1.653 +    {
   1.654 +	PRExplodedTime et, et1, et2;
   1.655 +	PRInt64 usecPer10Min;
   1.656 +	int day, hour, min;
   1.657 +	PRTime usecs;
   1.658 +	int dstInEffect = 0;
   1.659 +
   1.660 +	if (debug_mode) {
   1.661 +	printf("\n");
   1.662 +	printf("*******************************************************\n");
   1.663 +	printf("**                                                   **\n");
   1.664 +	printf("**           Stress test    Local Time               **\n");
   1.665 +	printf("**  Starting from midnight Jan. 1, 2009 PST,         **\n");
   1.666 +	printf("**  going back four years in 10-minute increment     **\n");
   1.667 +	printf("**                                                   **\n");
   1.668 +	printf("*******************************************************\n\n");
   1.669 +	}
   1.670 +
   1.671 +	LL_I2L(usecPer10Min, 600000000L);
   1.672 +
   1.673 +	/* 00:00:00 PST Jan. 1, 2009 */
   1.674 +	et.tm_usec = 0;
   1.675 +	et.tm_sec = 0;
   1.676 +	et.tm_min = 0;
   1.677 +	et.tm_hour = 0;
   1.678 +	et.tm_mday = 1;
   1.679 +	et.tm_month = 0;
   1.680 +	et.tm_year = 2009;
   1.681 +	et.tm_params.tp_gmt_offset = -8 * 3600;
   1.682 +	et.tm_params.tp_dst_offset = 0;
   1.683 +	usecs = PR_ImplodeTime(&et);
   1.684 +
   1.685 +        for (day = 0; day < 4 * 365 + 1; day++) {
   1.686 +	    for (hour = 0; hour < 24; hour++) {
   1.687 +		for (min = 0; min < 60; min += 10) {
   1.688 +	            LL_SUB(usecs, usecs, usecPer10Min);
   1.689 +		    PR_ExplodeTime(usecs, PR_LocalTimeParameters, &et1);
   1.690 +
   1.691 +		    et2 = et;
   1.692 +		    et2.tm_usec -= 600000000L;
   1.693 +		    PR_NormalizeTime(&et2, PR_LocalTimeParameters);
   1.694 +
   1.695 +		    if (!ExplodedTimeIsEqual(&et1, &et2)) {
   1.696 +		        printf("ERROR: componentwise comparison failed\n");
   1.697 +			PrintExplodedTime(&et1);
   1.698 +			printf("\n");
   1.699 +			PrintExplodedTime(&et2);
   1.700 +			printf("\n");
   1.701 +		        return 1;
   1.702 +		    }
   1.703 +
   1.704 +		    if (LL_NE(usecs, PR_ImplodeTime(&et1))) {
   1.705 +			printf("ERROR: PR_ExplodeTime and PR_ImplodeTime are not inverse\n");
   1.706 +			PrintExplodedTime(&et1);
   1.707 +			printf("\n");
   1.708 +			failed_already=1;
   1.709 +		        return 1;
   1.710 +		    }
   1.711 +		    testParseTimeString(usecs);
   1.712 +
   1.713 +		    if (!dstInEffect && et1.tm_params.tp_dst_offset) {
   1.714 +		        dstInEffect = 1;
   1.715 +		        if (debug_mode) {
   1.716 +			    printf("DST changeover from ");
   1.717 +			    PrintExplodedTime(&et);
   1.718 +			    printf(" to ");
   1.719 +			    PrintExplodedTime(&et1);
   1.720 +			    printf(".\n");
   1.721 +			}
   1.722 +                    } else if (dstInEffect && !et1.tm_params.tp_dst_offset) {
   1.723 +		        dstInEffect = 0;
   1.724 +			if (debug_mode) {
   1.725 +			    printf("DST changeover from ");
   1.726 +			    PrintExplodedTime(&et);
   1.727 +			    printf(" to ");
   1.728 +			    PrintExplodedTime(&et1);
   1.729 +			    printf(".\n");
   1.730 +			}
   1.731 +                    }
   1.732 +
   1.733 +		    et = et1;
   1.734 +		}
   1.735 +	    }
   1.736 +        }
   1.737 +    }
   1.738 +
   1.739 +	if (failed_already) return 1;
   1.740 +	else return 0;
   1.741 +
   1.742 +}

mercurial