home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / timetest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  22.4 KB  |  761 lines

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