📄 timetest.c
字号:
et.tm_params.tp_gmt_offset = -8 * 3600; /* PDT */ et.tm_params.tp_dst_offset = 3600; PR_NormalizeTime(&et, PR_LocalTimeParameters); if (debug_mode) printf("Nov 7 18:29:23 PDT 1996 is "); PrintExplodedTime(&et); if (debug_mode) printf(".\n"); testParseTimeString(PR_ImplodeTime(&et)); /* What time is Oct. 7, 1995, 18:29:23 PST? */ et.tm_year = 1995; et.tm_month = 10 - 1; et.tm_mday = 7; et.tm_hour = 18; et.tm_min = 29; et.tm_sec = 23; et.tm_params.tp_gmt_offset = -8 * 3600; /* PST */ et.tm_params.tp_dst_offset = 0; PR_NormalizeTime(&et, PR_LocalTimeParameters); if (debug_mode) printf("Oct 7 18:29:23 PST 1995 is "); PrintExplodedTime(&et); if (debug_mode) printf(".\n"); testParseTimeString(PR_ImplodeTime(&et)); if (debug_mode) printf("Please examine the results\n"); } /* ************************************************************** ** ** Testing range of years ** ************************************************************** */ { PRExplodedTime et1, et2; PRTime ttt; PRTime secs; if (debug_mode) { printf("\n"); printf("***************************************\n"); printf("** **\n"); printf("** Testing range of years **\n"); printf("** **\n"); printf("***************************************\n\n"); } /* April 4, 1917 GMT */ et1.tm_usec = 0; et1.tm_sec = 0; et1.tm_min = 0; et1.tm_hour = 0; et1.tm_mday = 4; et1.tm_month = 4 - 1; et1.tm_year = 1917; et1.tm_params = PR_GMTParameters(&et1); PR_NormalizeTime(&et1, PR_LocalTimeParameters); secs = PR_ImplodeTime(&et1); if (LL_GE_ZERO(secs)) { if (debug_mode) printf("ERROR: April 4, 1917 GMT returns a nonnegative second count\n"); failed_already = 1; return 1; } PR_ExplodeTime(secs, PR_LocalTimeParameters, &et2); if (!ExplodedTimeIsEqual(&et1, &et2)) { if (debug_mode) printf("ERROR: PR_ImplodeTime and PR_ExplodeTime are not inverse for April 4, 1917 GMT\n"); failed_already=1; return 1; } ttt = PR_ImplodeTime(&et1); testParseTimeString( ttt ); if (debug_mode) printf("Test passed for April 4, 1917\n"); /* July 4, 2050 */ et1.tm_usec = 0; et1.tm_sec = 0; et1.tm_min = 0; et1.tm_hour = 0; et1.tm_mday = 4; et1.tm_month = 7 - 1; et1.tm_year = 2050; et1.tm_params = PR_GMTParameters(&et1); PR_NormalizeTime(&et1, PR_LocalTimeParameters); secs = PR_ImplodeTime(&et1); if (!LL_GE_ZERO(secs)) { if (debug_mode) printf("ERROR: July 4, 2050 GMT returns a negative second count\n"); failed_already = 1; return 1; } PR_ExplodeTime(secs, PR_LocalTimeParameters, &et2); if (!ExplodedTimeIsEqual(&et1, &et2)) { if (debug_mode) printf("ERROR: PR_ImplodeTime and PR_ExplodeTime are not inverse for July 4, 2050 GMT\n"); failed_already=1; return 1; } testParseTimeString(PR_ImplodeTime(&et1)); if (debug_mode) printf("Test passed for July 4, 2050\n"); } /* ************************************************************** ** ** Stress test * ** Go through four years, starting from ** 00:00:00 PST Jan. 1, 1993, incrementing ** every 10 minutes. ** ************************************************************** */ { PRExplodedTime et, et1, et2; PRInt64 usecPer10Min; int day, hour, min; PRTime usecs; int dstInEffect = 0; if (debug_mode) { printf("\n"); printf("*******************************************************\n"); printf("** **\n"); printf("** Stress test **\n"); printf("** Starting from midnight Jan. 1, 1993 PST, **\n"); printf("** going through four years in 10-minute increment **\n"); printf("** **\n"); printf("*******************************************************\n\n"); } LL_I2L(usecPer10Min, 600000000L); /* 00:00:00 PST Jan. 1, 1993 */ et.tm_usec = 0; et.tm_sec = 0; et.tm_min = 0; et.tm_hour = 0; et.tm_mday = 1; et.tm_month = 0; et.tm_year = 1993; et.tm_params.tp_gmt_offset = -8 * 3600; et.tm_params.tp_dst_offset = 0; usecs = PR_ImplodeTime(&et); for (day = 0; day < 4 * 365 + 1; day++) { for (hour = 0; hour < 24; hour++) { for (min = 0; min < 60; min += 10) { LL_ADD(usecs, usecs, usecPer10Min); PR_ExplodeTime(usecs, PR_USPacificTimeParameters, &et1); et2 = et; et2.tm_usec += 600000000L; PR_NormalizeTime(&et2, PR_USPacificTimeParameters); if (!ExplodedTimeIsEqual(&et1, &et2)) { if (debug_mode) printf("ERROR: componentwise comparison failed\n"); PrintExplodedTime(&et1); if (debug_mode) printf("\n"); PrintExplodedTime(&et2); if (debug_mode) printf("\n"); failed_already=1; return 1; } if (LL_NE(usecs, PR_ImplodeTime(&et1))) { if (debug_mode) printf("ERROR: PR_ExplodeTime and PR_ImplodeTime are not inverse\n"); PrintExplodedTime(&et1); if (debug_mode) printf("\n"); failed_already=1; return 1; } testParseTimeString(usecs); if (!dstInEffect && et1.tm_params.tp_dst_offset) { dstInEffect = 1; if (debug_mode) printf("DST changeover from "); PrintExplodedTime(&et); if (debug_mode) printf(" to "); PrintExplodedTime(&et1); if (debug_mode) printf(".\n"); } else if (dstInEffect && !et1.tm_params.tp_dst_offset) { dstInEffect = 0; if (debug_mode) printf("DST changeover from "); PrintExplodedTime(&et); if (debug_mode) printf(" to "); PrintExplodedTime(&et1); if (debug_mode) printf(".\n"); } et = et1; } } } if (debug_mode) printf("Test passed\n"); } /* Same stress test, but with PR_LocalTimeParameters */ { PRExplodedTime et, et1, et2; PRInt64 usecPer10Min; int day, hour, min; PRTime usecs; int dstInEffect = 0; if (debug_mode) { printf("\n"); printf("*******************************************************\n"); printf("** **\n"); printf("** Stress test **\n"); printf("** Starting from midnight Jan. 1, 1993 PST, **\n"); printf("** going through four years in 10-minute increment **\n"); printf("** **\n"); printf("*******************************************************\n\n"); } LL_I2L(usecPer10Min, 600000000L); /* 00:00:00 PST Jan. 1, 1993 */ et.tm_usec = 0; et.tm_sec = 0; et.tm_min = 0; et.tm_hour = 0; et.tm_mday = 1; et.tm_month = 0; et.tm_year = 1993; et.tm_params.tp_gmt_offset = -8 * 3600; et.tm_params.tp_dst_offset = 0; usecs = PR_ImplodeTime(&et); for (day = 0; day < 4 * 365 + 1; day++) { for (hour = 0; hour < 24; hour++) { for (min = 0; min < 60; min += 10) { LL_ADD(usecs, usecs, usecPer10Min); PR_ExplodeTime(usecs, PR_LocalTimeParameters, &et1); et2 = et; et2.tm_usec += 600000000L; PR_NormalizeTime(&et2, PR_LocalTimeParameters); if (!ExplodedTimeIsEqual(&et1, &et2)) { if (debug_mode) printf("ERROR: componentwise comparison failed\n"); PrintExplodedTime(&et1); if (debug_mode) printf("\n"); PrintExplodedTime(&et2); if (debug_mode) printf("\n"); return 1; } if (LL_NE(usecs, PR_ImplodeTime(&et1))) { printf("ERROR: PR_ExplodeTime and PR_ImplodeTime are not inverse\n"); PrintExplodedTime(&et1); if (debug_mode) printf("\n"); failed_already=1; return 1; } testParseTimeString(usecs); if (!dstInEffect && et1.tm_params.tp_dst_offset) { dstInEffect = 1; if (debug_mode) printf("DST changeover from "); PrintExplodedTime(&et); if (debug_mode) printf(" to "); PrintExplodedTime(&et1); if (debug_mode) printf(".\n"); } else if (dstInEffect && !et1.tm_params.tp_dst_offset) { dstInEffect = 0; if (debug_mode) printf("DST changeover from "); PrintExplodedTime(&et); if (debug_mode) printf(" to "); PrintExplodedTime(&et1); if (debug_mode) printf(".\n"); } et = et1; } } } if (debug_mode) printf("Test passed\n"); } /* Same stress test, but with PR_LocalTimeParameters and going backward */ { PRExplodedTime et, et1, et2; PRInt64 usecPer10Min; int day, hour, min; PRTime usecs; int dstInEffect = 0; if (debug_mode) { printf("\n"); printf("*******************************************************\n"); printf("** **\n"); printf("** Stress test **\n"); printf("** Starting from midnight Jan. 1, 1997 PST, **\n"); printf("** going back four years in 10-minute increment **\n"); printf("** **\n"); printf("*******************************************************\n\n"); } LL_I2L(usecPer10Min, 600000000L); /* 00:00:00 PST Jan. 1, 1997 */ et.tm_usec = 0; et.tm_sec = 0; et.tm_min = 0; et.tm_hour = 0; et.tm_mday = 1; et.tm_month = 0; et.tm_year = 1997; et.tm_params.tp_gmt_offset = -8 * 3600; et.tm_params.tp_dst_offset = 0; usecs = PR_ImplodeTime(&et); for (day = 0; day < 4 * 365 + 1; day++) { for (hour = 0; hour < 24; hour++) { for (min = 0; min < 60; min += 10) { LL_SUB(usecs, usecs, usecPer10Min); PR_ExplodeTime(usecs, PR_LocalTimeParameters, &et1); et2 = et; et2.tm_usec -= 600000000L; PR_NormalizeTime(&et2, PR_LocalTimeParameters); if (!ExplodedTimeIsEqual(&et1, &et2)) { if (debug_mode) printf("ERROR: componentwise comparison failed\n"); PrintExplodedTime(&et1); if (debug_mode) printf("\n"); PrintExplodedTime(&et2); if (debug_mode) printf("\n"); return 1; } if (LL_NE(usecs, PR_ImplodeTime(&et1))) { if (debug_mode) printf("ERROR: PR_ExplodeTime and PR_ImplodeTime are not inverse\n"); PrintExplodedTime(&et1); if (debug_mode) printf("\n"); failed_already=1; return 1; } testParseTimeString(usecs); if (!dstInEffect && et1.tm_params.tp_dst_offset) { dstInEffect = 1; if (debug_mode) printf("DST changeover from "); PrintExplodedTime(&et); if (debug_mode) printf(" to "); PrintExplodedTime(&et1); if (debug_mode) printf(".\n"); } else if (dstInEffect && !et1.tm_params.tp_dst_offset) { dstInEffect = 0; if (debug_mode) printf("DST changeover from "); PrintExplodedTime(&et); if (debug_mode) printf(" to "); PrintExplodedTime(&et1); if (debug_mode) printf(".\n"); } et = et1; } } } }#ifdef XP_MAC if (1) { char dummyChar; printf("Press return to exit\n\n"); scanf("%c", &dummyChar); }#endif if (failed_already) return 1; else return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -