| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
/* |
| 7 |
* file: timetest.c |
| 8 |
* description: test time and date routines |
| 9 |
*/ |
| 10 |
/*********************************************************************** |
| 11 |
** Includes |
| 12 |
***********************************************************************/ |
| 13 |
/* Used to get the command line option */ |
| 14 |
#include "plgetopt.h" |
| 15 |
|
| 16 |
#include "prinit.h" |
| 17 |
#include "prtime.h" |
| 18 |
#include "prprf.h" |
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
int failed_already = 0; |
| 25 |
PRBool debug_mode = PR_FALSE; |
| 26 |
|
| 27 |
static char* dayOfWeek[] = {"Sun", "Mon", "Tue", "Wed", |
| 28 |
"Thu", "Fri", "Sat", "???"}; |
| 29 |
static char* month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", |
| 30 |
"Aug", "Sep", "Oct", "Nov", "Dec", "???"}; |
| 31 |
|
| 32 |
static void PrintExplodedTime(const PRExplodedTime* et) { |
| 33 |
PRInt32 totalOffset; |
| 34 |
PRInt32 hourOffset, minOffset; |
| 35 |
const char* sign; |
| 36 |
|
| 37 |
/* Print day of the week, month, day, hour, minute, and second */ |
| 38 |
if (debug_mode) |
| 39 |
printf("%s %s %ld %02ld:%02ld:%02ld ", dayOfWeek[et->tm_wday], |
| 40 |
month[et->tm_month], et->tm_mday, et->tm_hour, et->tm_min, |
| 41 |
et->tm_sec); |
| 42 |
|
| 43 |
/* Print time zone */ |
| 44 |
totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset; |
| 45 |
if (totalOffset == 0) { |
| 46 |
if (debug_mode) { |
| 47 |
printf("UTC "); |
| 48 |
} |
| 49 |
} else { |
| 50 |
sign = "+"; |
| 51 |
if (totalOffset < 0) { |
| 52 |
totalOffset = -totalOffset; |
| 53 |
sign = "-"; |
| 54 |
} |
| 55 |
hourOffset = totalOffset / 3600; |
| 56 |
minOffset = (totalOffset % 3600) / 60; |
| 57 |
if (debug_mode) { |
| 58 |
printf("%s%02ld%02ld ", sign, hourOffset, minOffset); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/* Print year */ |
| 63 |
if (debug_mode) { |
| 64 |
printf("%hd", et->tm_year); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
static int ExplodedTimeIsEqual(const PRExplodedTime* et1, |
| 69 |
const PRExplodedTime* et2) { |
| 70 |
if (et1->tm_usec == et2->tm_usec && et1->tm_sec == et2->tm_sec && |
| 71 |
et1->tm_min == et2->tm_min && et1->tm_hour == et2->tm_hour && |
| 72 |
et1->tm_mday == et2->tm_mday && et1->tm_month == et2->tm_month && |
| 73 |
et1->tm_year == et2->tm_year && et1->tm_wday == et2->tm_wday && |
| 74 |
et1->tm_yday == et2->tm_yday && |
| 75 |
et1->tm_params.tp_gmt_offset == et2->tm_params.tp_gmt_offset && |
| 76 |
et1->tm_params.tp_dst_offset == et2->tm_params.tp_dst_offset) { |
| 77 |
return 1; |
| 78 |
} else { |
| 79 |
return 0; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
static void testParseTimeString(PRTime t) { |
| 84 |
PRExplodedTime et; |
| 85 |
PRTime t2; |
| 86 |
char timeString[128]; |
| 87 |
char buf[128]; |
| 88 |
PRInt32 totalOffset; |
| 89 |
PRInt32 hourOffset, minOffset; |
| 90 |
const char* sign; |
| 91 |
PRInt64 usec_per_sec; |
| 92 |
|
| 93 |
/* Truncate the microsecond part of PRTime */ |
| 94 |
LL_I2L(usec_per_sec, PR_USEC_PER_SEC); |
| 95 |
LL_DIV(t, t, usec_per_sec); |
| 96 |
LL_MUL(t, t, usec_per_sec); |
| 97 |
|
| 98 |
PR_ExplodeTime(t, PR_LocalTimeParameters, &et); |
| 99 |
|
| 100 |
/* Print day of the week, month, day, hour, minute, and second */ |
| 101 |
PR_snprintf(timeString, 128, "%s %s %ld %02ld:%02ld:%02ld ", |
| 102 |
dayOfWeek[et.tm_wday], month[et.tm_month], et.tm_mday, et.tm_hour, |
| 103 |
et.tm_min, et.tm_sec); |
| 104 |
/* Print time zone */ |
| 105 |
totalOffset = et.tm_params.tp_gmt_offset + et.tm_params.tp_dst_offset; |
| 106 |
if (totalOffset == 0) { |
| 107 |
strcat(timeString, "GMT "); /* I wanted to use "UTC" here, but |
| 108 |
* PR_ParseTimeString doesn't |
| 109 |
* understand "UTC". */ |
| 110 |
} else { |
| 111 |
sign = "+"; |
| 112 |
if (totalOffset < 0) { |
| 113 |
totalOffset = -totalOffset; |
| 114 |
sign = "-"; |
| 115 |
} |
| 116 |
hourOffset = totalOffset / 3600; |
| 117 |
minOffset = (totalOffset % 3600) / 60; |
| 118 |
PR_snprintf(buf, 128, "%s%02ld%02ld ", sign, hourOffset, minOffset); |
| 119 |
strcat(timeString, buf); |
| 120 |
} |
| 121 |
/* Print year */ |
| 122 |
PR_snprintf(buf, 128, "%hd", et.tm_year); |
| 123 |
strcat(timeString, buf); |
| 124 |
|
| 125 |
if (PR_ParseTimeString(timeString, PR_FALSE, &t2) == PR_FAILURE) { |
| 126 |
fprintf(stderr, "PR_ParseTimeString() failed\n"); |
| 127 |
exit(1); |
| 128 |
} |
| 129 |
if (LL_NE(t, t2)) { |
| 130 |
fprintf(stderr, "PR_ParseTimeString() incorrect\n"); |
| 131 |
PR_snprintf(buf, 128, "t is %lld, t2 is %lld, time string is %s\n", t, t2, |
| 132 |
timeString); |
| 133 |
fprintf(stderr, "%s\n", buf); |
| 134 |
exit(1); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
int main(int argc, char** argv) { |
| 139 |
/* The command line argument: -d is used to determine if the test is being run |
| 140 |
in debug mode. The regress tool requires only one line output:PASS or FAIL. |
| 141 |
All of the printfs associated with this test has been handled with a if |
| 142 |
(debug_mode) test. Usage: test_name -d |
| 143 |
*/ |
| 144 |
PLOptStatus os; |
| 145 |
PLOptState* opt; |
| 146 |
|
| 147 |
PR_STDIO_INIT(); |
| 148 |
opt = PL_CreateOptState(argc, argv, "d"); |
| 149 |
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
| 150 |
if (PL_OPT_BAD == os) { |
| 151 |
continue; |
| 152 |
} |
| 153 |
switch (opt->option) { |
| 154 |
case 'd': /* debug mode */ |
| 155 |
debug_mode = PR_TRUE; |
| 156 |
break; |
| 157 |
default: |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
PL_DestroyOptState(opt); |
| 162 |
|
| 163 |
/* main test */ |
| 164 |
|
| 165 |
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
| 166 |
|
| 167 |
/* Testing zero PRTime (the epoch) */ |
| 168 |
{ |
| 169 |
PRTime t; |
| 170 |
PRExplodedTime et; |
| 171 |
|
| 172 |
LL_I2L(t, 0); |
| 173 |
if (debug_mode) { |
| 174 |
printf("The NSPR epoch is:\n"); |
| 175 |
} |
| 176 |
PR_ExplodeTime(t, PR_LocalTimeParameters, &et); |
| 177 |
PrintExplodedTime(&et); |
| 178 |
if (debug_mode) { |
| 179 |
printf("\n"); |
| 180 |
} |
| 181 |
PR_ExplodeTime(t, PR_GMTParameters, &et); |
| 182 |
PrintExplodedTime(&et); |
| 183 |
if (debug_mode) { |
| 184 |
printf("\n\n"); |
| 185 |
} |
| 186 |
testParseTimeString(t); |
| 187 |
} |
| 188 |
|
| 189 |
/* |
| 190 |
************************************************************* |
| 191 |
** |
| 192 |
** Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime |
| 193 |
** on the current time |
| 194 |
** |
| 195 |
************************************************************* |
| 196 |
*/ |
| 197 |
|
| 198 |
{ |
| 199 |
PRTime t1, t2; |
| 200 |
PRExplodedTime et; |
| 201 |
|
| 202 |
if (debug_mode) { |
| 203 |
printf("*********************************************\n"); |
| 204 |
printf("** **\n"); |
| 205 |
printf("** Testing PR_Now(), PR_ExplodeTime, and **\n"); |
| 206 |
printf("** PR_ImplodeTime on the current time **\n"); |
| 207 |
printf("** **\n"); |
| 208 |
printf("*********************************************\n\n"); |
| 209 |
} |
| 210 |
t1 = PR_Now(); |
| 211 |
|
| 212 |
/* First try converting to UTC */ |
| 213 |
|
| 214 |
PR_ExplodeTime(t1, PR_GMTParameters, &et); |
| 215 |
if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) { |
| 216 |
if (debug_mode) { |
| 217 |
printf("ERROR: UTC has nonzero gmt or dst offset.\n"); |
| 218 |
} else { |
| 219 |
failed_already = 1; |
| 220 |
} |
| 221 |
return 1; |
| 222 |
} |
| 223 |
if (debug_mode) { |
| 224 |
printf("Current UTC is "); |
| 225 |
} |
| 226 |
PrintExplodedTime(&et); |
| 227 |
if (debug_mode) { |
| 228 |
printf("\n"); |
| 229 |
} |
| 230 |
|
| 231 |
t2 = PR_ImplodeTime(&et); |
| 232 |
if (LL_NE(t1, t2)) { |
| 233 |
if (debug_mode) { |
| 234 |
printf("ERROR: Explode and implode are NOT inverse.\n"); |
| 235 |
} else { |
| 236 |
printf("FAIL\n"); |
| 237 |
} |
| 238 |
return 1; |
| 239 |
} |
| 240 |
|
| 241 |
/* Next, try converting to local (US Pacific) time */ |
| 242 |
|
| 243 |
PR_ExplodeTime(t1, PR_LocalTimeParameters, &et); |
| 244 |
if (debug_mode) { |
| 245 |
printf("Current local time is "); |
| 246 |
} |
| 247 |
PrintExplodedTime(&et); |
| 248 |
if (debug_mode) { |
| 249 |
printf("\n"); |
| 250 |
} |
| 251 |
if (debug_mode) |
| 252 |
printf("GMT offset is %ld, DST offset is %ld\n", |
| 253 |
et.tm_params.tp_gmt_offset, et.tm_params.tp_dst_offset); |
| 254 |
t2 = PR_ImplodeTime(&et); |
| 255 |
if (LL_NE(t1, t2)) { |
| 256 |
if (debug_mode) { |
| 257 |
printf("ERROR: Explode and implode are NOT inverse.\n"); |
| 258 |
} |
| 259 |
return 1; |
| 260 |
} |
| 261 |
|
| 262 |
if (debug_mode) { |
| 263 |
printf("Please examine the results\n"); |
| 264 |
} |
| 265 |
testParseTimeString(t1); |
| 266 |
} |
| 267 |
|
| 268 |
/* |
| 269 |
******************************************* |
| 270 |
** |
| 271 |
** Testing PR_NormalizeTime() |
| 272 |
** |
| 273 |
******************************************* |
| 274 |
*/ |
| 275 |
|
| 276 |
/* July 4, 2001 is Wednesday */ |
| 277 |
{ |
| 278 |
PRExplodedTime et; |
| 279 |
|
| 280 |
if (debug_mode) { |
| 281 |
printf("\n"); |
| 282 |
printf("**********************************\n"); |
| 283 |
printf("** **\n"); |
| 284 |
printf("** Testing PR_NormalizeTime() **\n"); |
| 285 |
printf("** **\n"); |
| 286 |
printf("**********************************\n\n"); |
| 287 |
} |
| 288 |
et.tm_year = 2001; |
| 289 |
et.tm_month = 7 - 1; |
| 290 |
et.tm_mday = 4; |
| 291 |
et.tm_hour = 0; |
| 292 |
et.tm_min = 0; |
| 293 |
et.tm_sec = 0; |
| 294 |
et.tm_usec = 0; |
| 295 |
et.tm_params = PR_GMTParameters(&et); |
| 296 |
|
| 297 |
PR_NormalizeTime(&et, PR_GMTParameters); |
| 298 |
|
| 299 |
if (debug_mode) { |
| 300 |
printf("July 4, 2001 is %s.\n", dayOfWeek[et.tm_wday]); |
| 301 |
} |
| 302 |
if (et.tm_wday == 3) { |
| 303 |
if (debug_mode) { |
| 304 |
printf("PASS\n"); |
| 305 |
} |
| 306 |
} else { |
| 307 |
if (debug_mode) { |
| 308 |
printf("ERROR: It should be Wednesday\n"); |
| 309 |
} else { |
| 310 |
failed_already = 1; |
| 311 |
} |
| 312 |
return 1; |
| 313 |
} |
| 314 |
testParseTimeString(PR_ImplodeTime(&et)); |
| 315 |
|
| 316 |
/* June 12, 1997 23:00 PST == June 13, 1997 00:00 PDT */ |
| 317 |
et.tm_year = 1997; |
| 318 |
et.tm_month = 6 - 1; |
| 319 |
et.tm_mday = 12; |
| 320 |
et.tm_hour = 23; |
| 321 |
et.tm_min = 0; |
| 322 |
et.tm_sec = 0; |
| 323 |
et.tm_usec = 0; |
| 324 |
et.tm_params.tp_gmt_offset = -8 * 3600; |
| 325 |
et.tm_params.tp_dst_offset = 0; |
| 326 |
|
| 327 |
PR_NormalizeTime(&et, PR_USPacificTimeParameters); |
| 328 |
|
| 329 |
if (debug_mode) { |
| 330 |
printf("Thu Jun 12, 1997 23:00:00 PST is "); |
| 331 |
} |
| 332 |
PrintExplodedTime(&et); |
| 333 |
if (debug_mode) { |
| 334 |
printf(".\n"); |
| 335 |
} |
| 336 |
if (et.tm_wday == 5) { |
| 337 |
if (debug_mode) { |
| 338 |
printf("PASS\n"); |
| 339 |
} |
| 340 |
} else { |
| 341 |
if (debug_mode) { |
| 342 |
printf("ERROR: It should be Friday\n"); |
| 343 |
} else { |
| 344 |
failed_already = 1; |
| 345 |
} |
| 346 |
return 1; |
| 347 |
} |
| 348 |
testParseTimeString(PR_ImplodeTime(&et)); |
| 349 |
|
| 350 |
/* Feb 14, 1997 00:00:00 PDT == Feb 13, 1997 23:00:00 PST */ |
| 351 |
et.tm_year = 1997; |
| 352 |
et.tm_month = 2 - 1; |
| 353 |
et.tm_mday = 14; |
| 354 |
et.tm_hour = 0; |
| 355 |
et.tm_min = 0; |
| 356 |
et.tm_sec = 0; |
| 357 |
et.tm_usec = 0; |
| 358 |
et.tm_params.tp_gmt_offset = -8 * 3600; |
| 359 |
et.tm_params.tp_dst_offset = 3600; |
| 360 |
|
| 361 |
PR_NormalizeTime(&et, PR_USPacificTimeParameters); |
| 362 |
|
| 363 |
if (debug_mode) { |
| 364 |
printf("Fri Feb 14, 1997 00:00:00 PDT is "); |
| 365 |
} |
| 366 |
PrintExplodedTime(&et); |
| 367 |
if (debug_mode) { |
| 368 |
printf(".\n"); |
| 369 |
} |
| 370 |
if (et.tm_wday == 4) { |
| 371 |
if (debug_mode) { |
| 372 |
printf("PASS\n"); |
| 373 |
} |
| 374 |
} else { |
| 375 |
if (debug_mode) { |
| 376 |
printf("ERROR: It should be Thursday\n"); |
| 377 |
} else { |
| 378 |
failed_already = 1; |
| 379 |
} |
| 380 |
return 1; |
| 381 |
} |
| 382 |
testParseTimeString(PR_ImplodeTime(&et)); |
| 383 |
|
| 384 |
/* What time is Nov. 7, 1996, 18:29:23 PDT? */ |
| 385 |
et.tm_year = 1996; |
| 386 |
et.tm_month = 11 - 1; |
| 387 |
et.tm_mday = 7; |
| 388 |
et.tm_hour = 18; |
| 389 |
et.tm_min = 29; |
| 390 |
et.tm_sec = 23; |
| 391 |
et.tm_usec = 0; |
| 392 |
et.tm_params.tp_gmt_offset = -8 * 3600; /* PDT */ |
| 393 |
et.tm_params.tp_dst_offset = 3600; |
| 394 |
|
| 395 |
PR_NormalizeTime(&et, PR_LocalTimeParameters); |
| 396 |
if (debug_mode) { |
| 397 |
printf("Nov 7 18:29:23 PDT 1996 is "); |
| 398 |
} |
| 399 |
PrintExplodedTime(&et); |
| 400 |
if (debug_mode) { |
| 401 |
printf(".\n"); |
| 402 |
} |
| 403 |
testParseTimeString(PR_ImplodeTime(&et)); |
| 404 |
|
| 405 |
/* What time is Oct. 7, 1995, 18:29:23 PST? */ |
| 406 |
et.tm_year = 1995; |
| 407 |
et.tm_month = 10 - 1; |
| 408 |
et.tm_mday = 7; |
| 409 |
et.tm_hour = 18; |
| 410 |
et.tm_min = 29; |
| 411 |
et.tm_sec = 23; |
| 412 |
et.tm_params.tp_gmt_offset = -8 * 3600; /* PST */ |
| 413 |
et.tm_params.tp_dst_offset = 0; |
| 414 |
|
| 415 |
PR_NormalizeTime(&et, PR_LocalTimeParameters); |
| 416 |
if (debug_mode) { |
| 417 |
printf("Oct 7 18:29:23 PST 1995 is "); |
| 418 |
} |
| 419 |
PrintExplodedTime(&et); |
| 420 |
if (debug_mode) { |
| 421 |
printf(".\n"); |
| 422 |
} |
| 423 |
testParseTimeString(PR_ImplodeTime(&et)); |
| 424 |
|
| 425 |
if (debug_mode) { |
| 426 |
printf("Please examine the results\n"); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
/* |
| 431 |
************************************************************** |
| 432 |
** |
| 433 |
** Testing range of years |
| 434 |
** |
| 435 |
************************************************************** |
| 436 |
*/ |
| 437 |
|
| 438 |
{ |
| 439 |
PRExplodedTime et1, et2; |
| 440 |
PRTime ttt; |
| 441 |
PRTime secs; |
| 442 |
|
| 443 |
if (debug_mode) { |
| 444 |
printf("\n"); |
| 445 |
printf("***************************************\n"); |
| 446 |
printf("** **\n"); |
| 447 |
printf("** Testing range of years **\n"); |
| 448 |
printf("** **\n"); |
| 449 |
printf("***************************************\n\n"); |
| 450 |
} |
| 451 |
/* April 4, 1917 GMT */ |
| 452 |
et1.tm_usec = 0; |
| 453 |
et1.tm_sec = 0; |
| 454 |
et1.tm_min = 0; |
| 455 |
et1.tm_hour = 0; |
| 456 |
et1.tm_mday = 4; |
| 457 |
et1.tm_month = 4 - 1; |
| 458 |
et1.tm_year = 1917; |
| 459 |
et1.tm_params = PR_GMTParameters(&et1); |
| 460 |
PR_NormalizeTime(&et1, PR_LocalTimeParameters); |
| 461 |
secs = PR_ImplodeTime(&et1); |
| 462 |
if (LL_GE_ZERO(secs)) { |
| 463 |
if (debug_mode) { |
| 464 |
printf("ERROR: April 4, 1917 GMT returns a nonnegative second count\n"); |
| 465 |
} |
| 466 |
failed_already = 1; |
| 467 |
return 1; |
| 468 |
} |
| 469 |
PR_ExplodeTime(secs, PR_LocalTimeParameters, &et2); |
| 470 |
if (!ExplodedTimeIsEqual(&et1, &et2)) { |
| 471 |
if (debug_mode) { |
| 472 |
printf( |
| 473 |
"ERROR: PR_ImplodeTime and PR_ExplodeTime are not inverse for " |
| 474 |
"April 4, 1917 GMT\n"); |
| 475 |
} |
| 476 |
failed_already = 1; |
| 477 |
return 1; |
| 478 |
} |
| 479 |
ttt = PR_ImplodeTime(&et1); |
| 480 |
testParseTimeString(ttt); |
| 481 |
|
| 482 |
if (debug_mode) { |
| 483 |
printf("Test passed for April 4, 1917\n"); |
| 484 |
} |
| 485 |
|
| 486 |
/* July 4, 2050 */ |
| 487 |
et1.tm_usec = 0; |
| 488 |
et1.tm_sec = 0; |
| 489 |
et1.tm_min = 0; |
| 490 |
et1.tm_hour = 0; |
| 491 |
et1.tm_mday = 4; |
| 492 |
et1.tm_month = 7 - 1; |
| 493 |
et1.tm_year = 2050; |
| 494 |
et1.tm_params = PR_GMTParameters(&et1); |
| 495 |
PR_NormalizeTime(&et1, PR_LocalTimeParameters); |
| 496 |
secs = PR_ImplodeTime(&et1); |
| 497 |
if (!LL_GE_ZERO(secs)) { |
| 498 |
if (debug_mode) { |
| 499 |
printf("ERROR: July 4, 2050 GMT returns a negative second count\n"); |
| 500 |
} |
| 501 |
failed_already = 1; |
| 502 |
return 1; |
| 503 |
} |
| 504 |
PR_ExplodeTime(secs, PR_LocalTimeParameters, &et2); |
| 505 |
if (!ExplodedTimeIsEqual(&et1, &et2)) { |
| 506 |
if (debug_mode) { |
| 507 |
printf( |
| 508 |
"ERROR: PR_ImplodeTime and PR_ExplodeTime are not inverse for July " |
| 509 |
"4, 2050 GMT\n"); |
| 510 |
} |
| 511 |
failed_already = 1; |
| 512 |
return 1; |
| 513 |
} |
| 514 |
testParseTimeString(PR_ImplodeTime(&et1)); |
| 515 |
|
| 516 |
if (debug_mode) { |
| 517 |
printf("Test passed for July 4, 2050\n"); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
/* |
| 522 |
************************************************************** |
| 523 |
** |
| 524 |
** Stress test |
| 525 |
* |
| 526 |
** Go through four years, starting from |
| 527 |
** 00:00:00 PST Jan. 1, 2005, incrementing |
| 528 |
** every 10 minutes. |
| 529 |
** |
| 530 |
************************************************************** |
| 531 |
*/ |
| 532 |
|
| 533 |
{ |
| 534 |
PRExplodedTime et, et1, et2; |
| 535 |
PRInt64 usecPer10Min; |
| 536 |
int day, hour, min; |
| 537 |
PRTime usecs; |
| 538 |
int dstInEffect = 0; |
| 539 |
|
| 540 |
if (debug_mode) { |
| 541 |
printf("\n"); |
| 542 |
printf("*******************************************************\n"); |
| 543 |
printf("** **\n"); |
| 544 |
printf("** Stress test Pacific Time **\n"); |
| 545 |
printf("** Starting from midnight Jan. 1, 2005 PST, **\n"); |
| 546 |
printf("** going through four years in 10-minute increment **\n"); |
| 547 |
printf("** **\n"); |
| 548 |
printf("*******************************************************\n\n"); |
| 549 |
} |
| 550 |
LL_I2L(usecPer10Min, 600000000L); |
| 551 |
|
| 552 |
/* 00:00:00 PST Jan. 1, 2005 */ |
| 553 |
et.tm_usec = 0; |
| 554 |
et.tm_sec = 0; |
| 555 |
et.tm_min = 0; |
| 556 |
et.tm_hour = 0; |
| 557 |
et.tm_mday = 1; |
| 558 |
et.tm_month = 0; |
| 559 |
et.tm_year = 2005; |
| 560 |
et.tm_params.tp_gmt_offset = -8 * 3600; |
| 561 |
et.tm_params.tp_dst_offset = 0; |
| 562 |
usecs = PR_ImplodeTime(&et); |
| 563 |
|
| 564 |
for (day = 0; day < 4 * 365 + 1; day++) { |
| 565 |
for (hour = 0; hour < 24; hour++) { |
| 566 |
for (min = 0; min < 60; min += 10) { |
| 567 |
LL_ADD(usecs, usecs, usecPer10Min); |
| 568 |
PR_ExplodeTime(usecs, PR_USPacificTimeParameters, &et1); |
| 569 |
|
| 570 |
et2 = et; |
| 571 |
et2.tm_usec += 600000000L; |
| 572 |
PR_NormalizeTime(&et2, PR_USPacificTimeParameters); |
| 573 |
|
| 574 |
if (!ExplodedTimeIsEqual(&et1, &et2)) { |
| 575 |
printf("ERROR: componentwise comparison failed\n"); |
| 576 |
PrintExplodedTime(&et1); |
| 577 |
printf("\n"); |
| 578 |
PrintExplodedTime(&et2); |
| 579 |
printf("\n"); |
| 580 |
failed_already = 1; |
| 581 |
return 1; |
| 582 |
} |
| 583 |
|
| 584 |
if (LL_NE(usecs, PR_ImplodeTime(&et1))) { |
| 585 |
printf( |
| 586 |
"ERROR: PR_ExplodeTime and PR_ImplodeTime are not inverse\n"); |
| 587 |
PrintExplodedTime(&et1); |
| 588 |
printf("\n"); |
| 589 |
failed_already = 1; |
| 590 |
return 1; |
| 591 |
} |
| 592 |
testParseTimeString(usecs); |
| 593 |
|
| 594 |
if (!dstInEffect && et1.tm_params.tp_dst_offset) { |
| 595 |
dstInEffect = 1; |
| 596 |
if (debug_mode) { |
| 597 |
printf("DST changeover from "); |
| 598 |
PrintExplodedTime(&et); |
| 599 |
printf(" to "); |
| 600 |
PrintExplodedTime(&et1); |
| 601 |
printf(".\n"); |
| 602 |
} |
| 603 |
} else if (dstInEffect && !et1.tm_params.tp_dst_offset) { |
| 604 |
dstInEffect = 0; |
| 605 |
if (debug_mode) { |
| 606 |
printf("DST changeover from "); |
| 607 |
PrintExplodedTime(&et); |
| 608 |
printf(" to "); |
| 609 |
PrintExplodedTime(&et1); |
| 610 |
printf(".\n"); |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
et = et1; |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
if (debug_mode) { |
| 619 |
printf("Test passed\n"); |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
/* Same stress test, but with PR_LocalTimeParameters */ |
| 624 |
|
| 625 |
{ |
| 626 |
PRExplodedTime et, et1, et2; |
| 627 |
PRInt64 usecPer10Min; |
| 628 |
int day, hour, min; |
| 629 |
PRTime usecs; |
| 630 |
int dstInEffect = 0; |
| 631 |
|
| 632 |
if (debug_mode) { |
| 633 |
printf("\n"); |
| 634 |
printf("*******************************************************\n"); |
| 635 |
printf("** **\n"); |
| 636 |
printf("** Stress test Local Time **\n"); |
| 637 |
printf("** Starting from midnight Jan. 1, 2005 PST, **\n"); |
| 638 |
printf("** going through four years in 10-minute increment **\n"); |
| 639 |
printf("** **\n"); |
| 640 |
printf("*******************************************************\n\n"); |
| 641 |
} |
| 642 |
|
| 643 |
LL_I2L(usecPer10Min, 600000000L); |
| 644 |
|
| 645 |
/* 00:00:00 PST Jan. 1, 2005 */ |
| 646 |
et.tm_usec = 0; |
| 647 |
et.tm_sec = 0; |
| 648 |
et.tm_min = 0; |
| 649 |
et.tm_hour = 0; |
| 650 |
et.tm_mday = 1; |
| 651 |
et.tm_month = 0; |
| 652 |
et.tm_year = 2005; |
| 653 |
et.tm_params.tp_gmt_offset = -8 * 3600; |
| 654 |
et.tm_params.tp_dst_offset = 0; |
| 655 |
usecs = PR_ImplodeTime(&et); |
| 656 |
|
| 657 |
for (day = 0; day < 4 * 365 + 1; day++) { |
| 658 |
for (hour = 0; hour < 24; hour++) { |
| 659 |
for (min = 0; min < 60; min += 10) { |
| 660 |
LL_ADD(usecs, usecs, usecPer10Min); |
| 661 |
PR_ExplodeTime(usecs, PR_LocalTimeParameters, &et1); |
| 662 |
|
| 663 |
et2 = et; |
| 664 |
et2.tm_usec += 600000000L; |
| 665 |
PR_NormalizeTime(&et2, PR_LocalTimeParameters); |
| 666 |
|
| 667 |
if (!ExplodedTimeIsEqual(&et1, &et2)) { |
| 668 |
printf("ERROR: componentwise comparison failed\n"); |
| 669 |
PrintExplodedTime(&et1); |
| 670 |
printf("\n"); |
| 671 |
PrintExplodedTime(&et2); |
| 672 |
printf("\n"); |
| 673 |
return 1; |
| 674 |
} |
| 675 |
|
| 676 |
if (LL_NE(usecs, PR_ImplodeTime(&et1))) { |
| 677 |
printf( |
| 678 |
"ERROR: PR_ExplodeTime and PR_ImplodeTime are not inverse\n"); |
| 679 |
PrintExplodedTime(&et1); |
| 680 |
printf("\n"); |
| 681 |
failed_already = 1; |
| 682 |
return 1; |
| 683 |
} |
| 684 |
testParseTimeString(usecs); |
| 685 |
|
| 686 |
if (!dstInEffect && et1.tm_params.tp_dst_offset) { |
| 687 |
dstInEffect = 1; |
| 688 |
if (debug_mode) { |
| 689 |
printf("DST changeover from "); |
| 690 |
PrintExplodedTime(&et); |
| 691 |
printf(" to "); |
| 692 |
PrintExplodedTime(&et1); |
| 693 |
printf(".\n"); |
| 694 |
} |
| 695 |
} else if (dstInEffect && !et1.tm_params.tp_dst_offset) { |
| 696 |
dstInEffect = 0; |
| 697 |
if (debug_mode) { |
| 698 |
printf("DST changeover from "); |
| 699 |
PrintExplodedTime(&et); |
| 700 |
printf(" to "); |
| 701 |
PrintExplodedTime(&et1); |
| 702 |
printf(".\n"); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
et = et1; |
| 707 |
} |
| 708 |
} |
| 709 |
} |
| 710 |
if (debug_mode) { |
| 711 |
printf("Test passed\n"); |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
/* Same stress test, but with PR_LocalTimeParameters and going backward */ |
| 716 |
|
| 717 |
{ |
| 718 |
PRExplodedTime et, et1, et2; |
| 719 |
PRInt64 usecPer10Min; |
| 720 |
int day, hour, min; |
| 721 |
PRTime usecs; |
| 722 |
int dstInEffect = 0; |
| 723 |
|
| 724 |
if (debug_mode) { |
| 725 |
printf("\n"); |
| 726 |
printf("*******************************************************\n"); |
| 727 |
printf("** **\n"); |
| 728 |
printf("** Stress test Local Time **\n"); |
| 729 |
printf("** Starting from midnight Jan. 1, 2009 PST, **\n"); |
| 730 |
printf("** going back four years in 10-minute increment **\n"); |
| 731 |
printf("** **\n"); |
| 732 |
printf("*******************************************************\n\n"); |
| 733 |
} |
| 734 |
|
| 735 |
LL_I2L(usecPer10Min, 600000000L); |
| 736 |
|
| 737 |
/* 00:00:00 PST Jan. 1, 2009 */ |
| 738 |
et.tm_usec = 0; |
| 739 |
et.tm_sec = 0; |
| 740 |
et.tm_min = 0; |
| 741 |
et.tm_hour = 0; |
| 742 |
et.tm_mday = 1; |
| 743 |
et.tm_month = 0; |
| 744 |
et.tm_year = 2009; |
| 745 |
et.tm_params.tp_gmt_offset = -8 * 3600; |
| 746 |
et.tm_params.tp_dst_offset = 0; |
| 747 |
usecs = PR_ImplodeTime(&et); |
| 748 |
|
| 749 |
for (day = 0; day < 4 * 365 + 1; day++) { |
| 750 |
for (hour = 0; hour < 24; hour++) { |
| 751 |
for (min = 0; min < 60; min += 10) { |
| 752 |
LL_SUB(usecs, usecs, usecPer10Min); |
| 753 |
PR_ExplodeTime(usecs, PR_LocalTimeParameters, &et1); |
| 754 |
|
| 755 |
et2 = et; |
| 756 |
et2.tm_usec -= 600000000L; |
| 757 |
PR_NormalizeTime(&et2, PR_LocalTimeParameters); |
| 758 |
|
| 759 |
if (!ExplodedTimeIsEqual(&et1, &et2)) { |
| 760 |
printf("ERROR: componentwise comparison failed\n"); |
| 761 |
PrintExplodedTime(&et1); |
| 762 |
printf("\n"); |
| 763 |
PrintExplodedTime(&et2); |
| 764 |
printf("\n"); |
| 765 |
return 1; |
| 766 |
} |
| 767 |
|
| 768 |
if (LL_NE(usecs, PR_ImplodeTime(&et1))) { |
| 769 |
printf( |
| 770 |
"ERROR: PR_ExplodeTime and PR_ImplodeTime are not inverse\n"); |
| 771 |
PrintExplodedTime(&et1); |
| 772 |
printf("\n"); |
| 773 |
failed_already = 1; |
| 774 |
return 1; |
| 775 |
} |
| 776 |
testParseTimeString(usecs); |
| 777 |
|
| 778 |
if (!dstInEffect && et1.tm_params.tp_dst_offset) { |
| 779 |
dstInEffect = 1; |
| 780 |
if (debug_mode) { |
| 781 |
printf("DST changeover from "); |
| 782 |
PrintExplodedTime(&et); |
| 783 |
printf(" to "); |
| 784 |
PrintExplodedTime(&et1); |
| 785 |
printf(".\n"); |
| 786 |
} |
| 787 |
} else if (dstInEffect && !et1.tm_params.tp_dst_offset) { |
| 788 |
dstInEffect = 0; |
| 789 |
if (debug_mode) { |
| 790 |
printf("DST changeover from "); |
| 791 |
PrintExplodedTime(&et); |
| 792 |
printf(" to "); |
| 793 |
PrintExplodedTime(&et1); |
| 794 |
printf(".\n"); |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
et = et1; |
| 799 |
} |
| 800 |
} |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
if (failed_already) { |
| 805 |
return 1; |
| 806 |
} else { |
| 807 |
return 0; |
| 808 |
} |
| 809 |
} |