⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 y2k.c

📁 Netscape NSPR库源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- *//*  * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ *  * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. *  * The Original Code is the Netscape Portable Runtime (NSPR). *  * The Initial Developer of the Original Code is Netscape * Communications Corporation.  Portions created by Netscape are  * Copyright (C) 1998-2000 Netscape Communications Corporation.  All * Rights Reserved. *  * Contributor(s): *  * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable  * instead of those above.  If you wish to allow use of your  * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL.  If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. *//* * file: y2k.c * description: Test for y2k compliance for NSPR. * * Sep 1999. lth. Added "Sun" specified dates to the test data. *//************************************************************************* Includes***********************************************************************//* Used to get the command line option */#include "plgetopt.h"#include "prinit.h"#include "prtime.h"#include "prprf.h"#include "prlog.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef XP_MAC#include "prlog.h"#include "macstdlibextras.h"extern void SetupMacPrintfLog(char *logFile);#endif#define PRINT_DETAILSint failed_already=0;PRBool debug_mode = PR_FALSE;static char *dayOfWeek[] =	{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???" };static char *month[] =	{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",	  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???" };PRLogModuleInfo *lm;static void PrintExplodedTime(const PRExplodedTime *et) {    PRInt32 totalOffset;    PRInt32 hourOffset, minOffset;    const char *sign;    /* Print day of the week, month, day, hour, minute, and second */    printf("%s %s %2ld %02ld:%02ld:%02ld ",	    dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday,	    et->tm_hour, et->tm_min, et->tm_sec);    /* Print year */    printf("%hd ", et->tm_year);    /* Print time zone */    totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset;    if (totalOffset == 0) {	printf("UTC ");    } else {        sign = "+";        if (totalOffset < 0) {	    totalOffset = -totalOffset;	    sign = "-";        }        hourOffset = totalOffset / 3600;        minOffset = (totalOffset % 3600) / 60;        printf("%s%02ld%02ld ", sign, hourOffset, minOffset);    }#ifdef PRINT_DETAILS	printf("{%d, %d, %d, %d, %d, %d, %d, %d, %d, { %d, %d}}\n",et->tm_usec,											et->tm_sec,											et->tm_min,											et->tm_hour,											et->tm_mday,											et->tm_month,											et->tm_year,											et->tm_wday,											et->tm_yday,											et->tm_params.tp_gmt_offset,											et->tm_params.tp_dst_offset);#endif}static int ExplodedTimeIsEqual(const PRExplodedTime *et1,	const PRExplodedTime *et2){    if (et1->tm_usec == et2->tm_usec &&	    et1->tm_sec == et2->tm_sec &&	    et1->tm_min == et2->tm_min &&	    et1->tm_hour == et2->tm_hour &&	    et1->tm_mday == et2->tm_mday &&	    et1->tm_month == et2->tm_month &&	    et1->tm_year == et2->tm_year &&	    et1->tm_wday == et2->tm_wday &&	    et1->tm_yday == et2->tm_yday &&	    et1->tm_params.tp_gmt_offset == et2->tm_params.tp_gmt_offset &&	    et1->tm_params.tp_dst_offset == et2->tm_params.tp_dst_offset) {        return 1;    } else {	return 0;    }}/* * TEST 1: TestExplodeImplodeTime * Description: * For each given timestamp T (a PRTime value), call PR_ExplodeTime * with GMT, US Pacific, and local time parameters.  Compare the * resulting calendar (exploded) time values with the expected * values. * * Note: the expected local time values depend on the local time * zone.  The local time values stored in this test are for the US * Pacific Time Zone.  If you are running this test in a different * time zone, you need to modify the values in the localt array. * An example is provided below. * * Call PR_ImplodeTime for each of the exploded values and compare * the resulting PRTime values with the original input. *  * This test is run for the values of time T corresponding to the * following dates: * - 12/31/99 - before 2000  * - 01/01/00 - after 2000  * - Leap year - Feb 29, 2000  * - March 1st, 2001 (after 1 year)  * - March 1st, 2005 (after second leap year)  * - 09/09/99 (used by some programs as an end of file marker) * * Call PR_Now, convert to calendar time using PR_ExplodeTime and * manually check the result for correctness. The time should match * the system clock. * * Tested functions: PR_Now, PR_ExplodeTime, PR_ImplodeTime, * PR_LocalTimeParameters, PR_GMTParameters.  */static PRTime prt[] = {    LL_INIT(220405, 2133125120),  /* 946634400000000 */    LL_INIT(220425, 2633779200),  /* 946720800000000 */    LL_INIT(221612, 2107598848),  /* 951818400000000 */    LL_INIT(228975, 663398400),  /* 983440800000000 */    LL_INIT(258365, 1974568960),  /* 1109671200000000 */    LL_INIT(218132, 1393788928),  /* 936871200000000 */    /* Sun's dates follow */    LL_INIT( 213062, 4077979648 ), /* Dec 31 1998 10:00:00 */    LL_INIT( 218152, 1894443008 ), /* Sep 10 1999 10:00:00 */    LL_INIT( 221592, 1606944768 ), /* Feb 28 2000 10:00:00 */    LL_INIT( 227768, 688924672 ), /* Dec 31 2000 10:00:00 */    LL_INIT( 227788, 1189578752 ), /* Jan 1  2001 10:00:00 */};static PRExplodedTime gmt[] = {    { 0, 0, 0, 10, 31, 11, 1999, 5, 364, {0, 0}}, /* 1999/12/31 10:00:00 GMT */    { 0, 0, 0, 10, 1, 0, 2000, 6, 0, {0, 0}}, /* 2000/01/01 10:00:00 GMT */    { 0, 0, 0, 10, 29, 1, 2000, 2, 59, {0, 0}}, /* 2000/02/29 10:00:00 GMT */    { 0, 0, 0, 10, 1, 2, 2001, 4, 59, {0, 0}}, /* 2001/3/1 10:00:00 GMT */    { 0, 0, 0, 10, 1, 2, 2005, 2, 59, {0, 0}}, /* 2005/3/1 10:00:00 GMT */    { 0, 0, 0, 10, 9, 8, 1999, 4, 251, {0, 0}},  /* 1999/9/9 10:00:00 GMT */    /* Sun's dates follow */    { 0, 0, 0, 10, 31, 11, 1998, 4, 364, {0, 0}},  /* 12/31/1998 10:00:00 GMT */    { 0, 0, 0, 10, 10, 8, 1999, 5, 252, {0, 0}},  /* 9/10/1999 10:00:00 GMT */    { 0, 0, 0, 10, 28, 1, 2000, 1, 58, {0, 0}},  /* 2/28/2000 10:00:00 GMT */    { 0, 0, 0, 10, 31, 11, 2000, 0, 365, {0, 0}},  /* 12/31/2000 10:00:00 GMT */    { 0, 0, 0, 10, 1, 0, 2001, 1, 0, {0, 0}}  /* 1/1/2001 10:00:00 GMT */};static PRExplodedTime uspt[] = {{ 0, 0, 0, 2, 31, 11, 1999, 5, 364, {-28800, 0}}, /* 1999/12/31 2:00:00 PST */{ 0, 0, 0, 2, 1, 0, 2000, 6, 0, {-28800, 0}}, /* 2000/01/01 2:00:00 PST */{ 0, 0, 0, 2, 29, 1, 2000, 2, 59, {-28800, 0}}, /* 2000/02/29 2:00:00 PST */{ 0, 0, 0, 2, 1, 2, 2001, 4, 59, {-28800, 0}}, /* 2001/3/1 2:00:00 PST */{ 0, 0, 0, 2, 1, 2, 2005, 2, 59, {-28800, 0}}, /* 2005/3/1 2:00:00 PST */{ 0, 0, 0, 3, 9, 8, 1999, 4, 251, {-28800, 3600}},  /* 1999/9/9 3:00:00 PDT */    /* Sun's dates follow */    { 0, 0, 0, 2, 31, 11, 1998, 4, 364, {-28800, 0}},  /* 12/31/1998 00:00:00 GMT */    { 0, 0, 0, 3, 10, 8, 1999, 5, 252, {-28800, 3600}},  /* 9/10/1999 00:00:00 GMT */    { 0, 0, 0, 2, 28, 1, 2000, 1, 58, {-28800, 0}},  /* 2/28/2000 00:00:00 GMT */    { 0, 0, 0, 2, 31, 11, 2000, 0, 365, {-28800, 0}},  /* 12/31/2000 00:00:00 GMT */    { 0, 0, 0, 2, 1, 0, 2001, 1, 0, {-28800, 0}}  /* 1/1/2001 00:00:00 GMT */};/* * This test assumes that we are in US Pacific Time Zone. * If you are running this test in a different time zone, * you need to modify the localt array and fill in the * expected results.  The localt array for US Eastern Time * Zone is provided as an example. */static PRExplodedTime localt[] = {{ 0, 0, 0, 2, 31, 11, 1999, 5, 364, {-28800, 0}}, /* 1999/12/31 2:00:00 PST */{ 0, 0, 0, 2, 1, 0, 2000, 6, 0, {-28800, 0}}, /* 2000/01/01 2:00:00 PST */{ 0, 0, 0, 2, 29, 1, 2000, 2, 59, {-28800, 0}}, /* 2000/02/29 2:00:00 PST */{ 0, 0, 0, 2, 1, 2, 2001, 4, 59, {-28800, 0}}, /* 2001/3/1 2:00:00 PST */{ 0, 0, 0, 2, 1, 2, 2005, 2, 59, {-28800, 0}}, /* 2005/3/1 2:00:00 PST */{ 0, 0, 0, 3, 9, 8, 1999, 4, 251, {-28800, 3600}},  /* 1999/9/9 3:00:00 PDT */    /* Sun's dates follow */    { 0, 0, 0, 2, 31, 11, 1998, 4, 364, {-28800, 0}},  /* 12/31/1998 00:00:00 GMT */    { 0, 0, 0, 3, 10, 8, 1999, 5, 252, {-28800, 3600}},  /* 9/10/1999 00:00:00 GMT */    { 0, 0, 0, 2, 28, 1, 2000, 1, 58, {-28800, 0}},  /* 2/28/2000 00:00:00 GMT */    { 0, 0, 0, 2, 31, 11, 2000, 0, 365, {-28800, 0}},  /* 12/31/2000 00:00:00 GMT */    { 0, 0, 0, 2, 1, 0, 2001, 1, 0, {-28800, 0}}  /* 1/1/2001 00:00:00 GMT */};#ifdef US_EASTERN_TIMEstatic PRExplodedTime localt[] = {{ 0, 0, 0, 5, 31, 11, 1999, 5, 364, {-18000, 0}}, /* 1999/12/31 2:00:00 EST */{ 0, 0, 0, 5, 1, 0, 2000, 6, 0, {-18000, 0}}, /* 2000/01/01 2:00:00 EST */{ 0, 0, 0, 5, 29, 1, 2000, 2, 59, {-18000, 0}}, /* 2000/02/29 2:00:00 EST */{ 0, 0, 0, 5, 1, 2, 2001, 4, 59, {-18000, 0}}, /* 2001/3/1 2:00:00 EST */{ 0, 0, 0, 5, 1, 2, 2005, 2, 59, {-18000, 0}}, /* 2005/3/1 2:00:00 EST */{ 0, 0, 0, 6, 9, 8, 1999, 4, 251, {-18000, 3600}},  /* 1999/9/9 3:00:00 EDT */    /* Sun's dates follow */    { 0, 0, 0, 5, 31, 11, 1998, 4, 364, {-18000 0}},  /* 12/31/1998 00:00:00 GMT */    { 0, 0, 0, 6, 10, 8, 1999, 5, 252, {-18000 3600}},  /* 9/10/1999 00:00:00 GMT */    { 0, 0, 0, 5, 28, 1, 2000, 1, 58, {-18000 0}},  /* 2/28/2000 00:00:00 GMT */    { 0, 0, 0, 5, 31, 11, 2000, 0, 365, {-18000 0}},  /* 12/31/2000 00:00:00 GMT */    { 0, 0, 0, 5, 1, 0, 2001, 1, 0, {-18000 0}}  /* 1/1/2001 00:00:00 GMT */};#endifstatic PRStatus TestExplodeImplodeTime(void){    PRTime prt_tmp;    PRTime now;    int idx;    int array_size = sizeof(prt) / sizeof(PRTime);    PRExplodedTime et_tmp;    char buf[1024];    for (idx = 0; idx < array_size; idx++) {        PR_snprintf(buf, sizeof(buf), "%lld", prt[idx]);        if (debug_mode) printf("Time stamp %s\n", buf);         PR_ExplodeTime(prt[idx], PR_GMTParameters, &et_tmp);        if (!ExplodedTimeIsEqual(&et_tmp, &gmt[idx])) {            fprintf(stderr, "GMT not equal\n");            PrintExplodedTime(&et_tmp);            PrintExplodedTime(&gmt[idx]);            exit(1);        }        prt_tmp = PR_ImplodeTime(&et_tmp);        if (LL_NE(prt_tmp, prt[idx])) {            fprintf(stderr, "PRTime not equal\n");            exit(1);        }        if (debug_mode) {            printf("GMT: ");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -