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