PRTime calDateTime::IcaltimeToPRTime(icaltimetype const* icalt, icaltimezone const* tz) { icaltimetype tt; PRExplodedTime et; /* If the time is the special null time, return 0. */ if (icaltime_is_null_time(*icalt)) { return 0; } if (tz) { // use libical for timezone conversion, as it can handle all ics // timezones. having nspr do it is much harder. tt = icaltime_convert_to_zone(*icalt, const_cast<icaltimezone *>(tz)); } else { tt = *icalt; } /* Empty the destination */ memset(&et, 0, sizeof(struct PRExplodedTime)); /* Fill the fields */ if (icaltime_is_date(tt)) { et.tm_sec = et.tm_min = et.tm_hour = 0; } else { et.tm_sec = tt.second; et.tm_min = tt.minute; et.tm_hour = tt.hour; } et.tm_mday = static_cast<int16_t>(tt.day); et.tm_month = static_cast<int16_t>(tt.month-1); et.tm_year = static_cast<int16_t>(tt.year); return PR_ImplodeTime(&et); }
void _PR_FileTimeToPRTime(const FILETIME *filetime, PRTime *prtm) { PR_ASSERT(sizeof(FILETIME) == sizeof(PRTime)); CopyMemory(prtm, filetime, sizeof(PRTime)); #if defined(__MINGW32__) *prtm = (*prtm - _pr_filetime_offset) / 10LL; #else *prtm = (*prtm - _pr_filetime_offset) / 10i64; #endif #ifdef DEBUG /* Doublecheck our calculation. */ { SYSTEMTIME systime; PRExplodedTime etm; PRTime cmp; /* for comparison */ BOOL rv; rv = FileTimeToSystemTime(filetime, &systime); PR_ASSERT(0 != rv); /* * PR_ImplodeTime ignores wday and yday. */ etm.tm_usec = systime.wMilliseconds * PR_USEC_PER_MSEC; etm.tm_sec = systime.wSecond; etm.tm_min = systime.wMinute; etm.tm_hour = systime.wHour; etm.tm_mday = systime.wDay; etm.tm_month = systime.wMonth - 1; etm.tm_year = systime.wYear; /* * It is not well-documented what time zone the FILETIME's * are in. WIN32_FIND_DATA is documented to be in UTC (GMT). * But BY_HANDLE_FILE_INFORMATION is unclear about this. * By our best judgement, we assume that FILETIME is in UTC. */ etm.tm_params.tp_gmt_offset = 0; etm.tm_params.tp_dst_offset = 0; cmp = PR_ImplodeTime(&etm); /* * SYSTEMTIME is in milliseconds precision, so we convert PRTime's * microseconds to milliseconds before doing the comparison. */ PR_ASSERT((cmp / PR_USEC_PER_MSEC) == (*prtm / PR_USEC_PER_MSEC)); } #endif /* DEBUG */ }
/* readonly attribute PRTime lastModifiedTime; */ NS_IMETHODIMP nsZipHeader::GetLastModifiedTime(PRTime *aLastModifiedTime) { NS_ASSERTION(mInited, "Not initalised"); // Try to read timestamp from extra field PRUint16 blocksize; const PRUint8 *tsField = GetExtraField(ZIP_EXTENDED_TIMESTAMP_FIELD, PR_FALSE, &blocksize); if (tsField && blocksize >= 5) { PRUint32 pos = 4; PRUint8 flags; flags = READ8(tsField, &pos); if (flags & ZIP_EXTENDED_TIMESTAMP_MODTIME) { *aLastModifiedTime = (PRTime)(READ32(tsField, &pos)) * PR_USEC_PER_SEC; return NS_OK; } } // Use DOS date/time fields // Note that on DST shift we can't handle correctly the hour that is valid // in both DST zones PRExplodedTime time; time.tm_usec = 0; time.tm_hour = (mTime >> 11) & 0x1F; time.tm_min = (mTime >> 5) & 0x3F; time.tm_sec = (mTime & 0x1F) * 2; time.tm_year = (mDate >> 9) + 1980; time.tm_month = ((mDate >> 5) & 0x0F) - 1; time.tm_mday = mDate & 0x1F; time.tm_params.tp_gmt_offset = 0; time.tm_params.tp_dst_offset = 0; PR_NormalizeTime(&time, PR_GMTParameters); time.tm_params.tp_gmt_offset = PR_LocalTimeParameters(&time).tp_gmt_offset; PR_NormalizeTime(&time, PR_GMTParameters); time.tm_params.tp_dst_offset = PR_LocalTimeParameters(&time).tp_dst_offset; *aLastModifiedTime = PR_ImplodeTime(&time); return NS_OK; }
/****************************************************************** * * m a k e _ c e r t */ static CERTCertificate * make_cert(CERTCertificateRequest *req, unsigned long serial, CERTName *ca_subject) { CERTCertificate * cert; CERTValidity * validity = NULL; PRTime now, after; PRExplodedTime printableTime; now = PR_Now(); PR_ExplodeTime (now, PR_GMTParameters, &printableTime); printableTime.tm_month += 3; after = PR_ImplodeTime (&printableTime); validity = CERT_CreateValidity (now, after); if (validity == NULL) { PR_fprintf(errorFD, "%s: error creating certificate validity\n", PROGRAM_NAME); errorCount++; exit (ERRX); } cert = CERT_CreateCertificate (serial, ca_subject, validity, req); if (cert == NULL) { /* should probably be more precise here */ PR_fprintf(errorFD, "%s: error while generating certificate\n", PROGRAM_NAME); errorCount++; exit (ERRX); } return cert; }
nsresult nsMimeBaseEmitter::GenerateDateString(const char * dateString, nsACString &formattedDate, bool showDateForToday) { nsresult rv = NS_OK; if (!mDateFormatter) { mDateFormatter = do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); } /** * See if the user wants to have the date displayed in the senders * timezone (including the timezone offset). * We also evaluate the pref original_date which was introduced * as makeshift in bug 118899. */ bool displaySenderTimezone = false; bool displayOriginalDate = false; nsCOMPtr<nsIPrefService> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIPrefBranch> dateFormatPrefs; rv = prefs->GetBranch("mailnews.display.", getter_AddRefs(dateFormatPrefs)); NS_ENSURE_SUCCESS(rv, rv); dateFormatPrefs->GetBoolPref("date_senders_timezone", &displaySenderTimezone); dateFormatPrefs->GetBoolPref("original_date", &displayOriginalDate); // migrate old pref to date_senders_timezone if (displayOriginalDate && !displaySenderTimezone) dateFormatPrefs->SetBoolPref("date_senders_timezone", true); PRExplodedTime explodedMsgTime; // XXX Casting PRStatus to nsresult rv = static_cast<nsresult>( PR_ParseTimeStringToExplodedTime(dateString, false, &explodedMsgTime)); /** * To determine the date format to use, comparison of current and message * time has to be made. If displaying in local time, both timestamps have * to be in local time. If displaying in senders time zone, leave the compare * time in that time zone. * Otherwise in TZ+0100 on 2009-03-12 a message from 2009-03-11T20:49-0700 * would be displayed as "20:49 -0700" though it in fact is not from the * same day. */ PRExplodedTime explodedCompTime; if (displaySenderTimezone) explodedCompTime = explodedMsgTime; else PR_ExplodeTime(PR_ImplodeTime(&explodedMsgTime), PR_LocalTimeParameters, &explodedCompTime); PRExplodedTime explodedCurrentTime; PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &explodedCurrentTime); // If we want short dates, check if the message is from today, and if so // only show the time (e.g. 3:15 pm). nsDateFormatSelector dateFormat = kDateFormatShort; if (!showDateForToday && explodedCurrentTime.tm_year == explodedCompTime.tm_year && explodedCurrentTime.tm_month == explodedCompTime.tm_month && explodedCurrentTime.tm_mday == explodedCompTime.tm_mday) { // same day... dateFormat = kDateFormatNone; } nsAutoString formattedDateString; if (NS_SUCCEEDED(rv)) { rv = mDateFormatter->FormatPRExplodedTime(nullptr /* nsILocale* locale */, dateFormat, kTimeFormatNoSeconds, &explodedCompTime, formattedDateString); if (NS_SUCCEEDED(rv)) { if (displaySenderTimezone) { // offset of local time from UTC in minutes int32_t senderoffset = (explodedMsgTime.tm_params.tp_gmt_offset + explodedMsgTime.tm_params.tp_dst_offset) / 60; // append offset to date string char16_t *tzstring = nsTextFormatter::smprintf(MOZ_UTF16(" %+05d"), (senderoffset / 60 * 100) + (senderoffset % 60)); formattedDateString.Append(tzstring); nsTextFormatter::smprintf_free(tzstring); } CopyUTF16toUTF8(formattedDateString, formattedDate); } } return rv; }
static SECStatus der_TimeStringToTime(PRTime *dst, const char * string, int generalized) { PRExplodedTime genTime; long hourOff = 0, minOff = 0; uint16 century; char signum; if (string == NULL || dst == NULL) { PORT_SetError(SEC_ERROR_INVALID_ARGS); return SECFailure; } /* Verify time is formatted properly and capture information */ memset(&genTime, 0, sizeof genTime); if (generalized == UTC_STRING) { CAPTURE(genTime.tm_year, string, loser); century = (genTime.tm_year < 50) ? 20 : 19; } else { CAPTURE(century, string, loser); CAPTURE(genTime.tm_year, string, loser); } genTime.tm_year += century * 100; CAPTURE(genTime.tm_month, string, loser); if ((genTime.tm_month == 0) || (genTime.tm_month > 12)) goto loser; /* NSPR month base is 0 */ --genTime.tm_month; CAPTURE(genTime.tm_mday, string, loser); if ((genTime.tm_mday == 0) || (genTime.tm_mday > 31)) goto loser; CAPTURE(genTime.tm_hour, string, loser); if (genTime.tm_hour > 23) goto loser; CAPTURE(genTime.tm_min, string, loser); if (genTime.tm_min > 59) goto loser; if (ISDIGIT(string[0])) { CAPTURE(genTime.tm_sec, string, loser); if (genTime.tm_sec > 59) goto loser; } signum = *string++; if (signum == '+' || signum == '-') { CAPTURE(hourOff, string, loser); if (hourOff > 23) goto loser; CAPTURE(minOff, string, loser); if (minOff > 59) goto loser; if (signum == '-') { hourOff = -hourOff; minOff = -minOff; } } else if (signum != 'Z') { goto loser; } /* Convert the GMT offset to seconds and save it in genTime * for the implode time call. */ genTime.tm_params.tp_gmt_offset = (PRInt32)((hourOff * 60L + minOff) * 60L); *dst = PR_ImplodeTime(&genTime); return SECSuccess; loser: PORT_SetError(SEC_ERROR_INVALID_TIME); return SECFailure; }
time_t read_genTime(struct berval*from) { struct tm t; time_t retTime; time_t diffsec = 0; int i, gflag = 0, havesec = 0; memset (&t, 0, sizeof(t)); t.tm_isdst = -1; t.tm_year = strntoul (from->bv_val , 4, 10) - 1900L; t.tm_mon = strntoul (from->bv_val + 4, 2, 10) - 1; t.tm_mday = strntoul (from->bv_val + 6, 2, 10); t.tm_hour = strntoul (from->bv_val + 8, 2, 10); t.tm_min = strntoul (from->bv_val + 10, 2, 10); i =12; /* * the string can end with Z or -xxxx or +xxxx * or before to have the Z/+/- we may have two digits for the seconds. * If there's no Z/+/-, it means it's been expressed as local time * (not standard). */ while (from->bv_val[i]) { switch (from->bv_val[i]) { case 'Z': case 'z': gflag = 1; ++i; break; case '+': /* Offset from GMT is on 4 digits */ i++; diffsec -= strntoul (from->bv_val + i, 4, 10); gflag = 1; i += 4; break; case '-': /* Offset from GMT is on 4 digits */ i++; diffsec += strntoul (from->bv_val + i, 4, 10); gflag = 1; i += 4; break; default: if (havesec){ /* Ignore milliseconds */ i++; } else { t.tm_sec = strntoul (from->bv_val + i, 2, 10); havesec = 1; i += 2; } } /* end switch */ } if (gflag){ PRTime pt; PRExplodedTime expt = {0}; unsigned long year = strntoul (from->bv_val , 4, 10); expt.tm_year = (PRInt16)year; expt.tm_month = t.tm_mon; expt.tm_mday = t.tm_mday; expt.tm_hour = t.tm_hour; expt.tm_min = t.tm_min; expt.tm_sec = t.tm_sec; /* This is a GMT time */ expt.tm_params.tp_gmt_offset = 0; expt.tm_params.tp_dst_offset = 0; /* PRTime is expressed in microseconds */ pt = PR_ImplodeTime(&expt) / 1000000L; retTime = (time_t)pt; return (retTime + diffsec); } else { return mktime_r (&t); } }
int main(int argc, char** argv) { /* The command line argument: -d is used to determine if the test is being run in debug mode. The regress tool requires only one line output:PASS or FAIL. All of the printfs associated with this test has been handled with a if (debug_mode) test. Usage: test_name -d */ PLOptStatus os; PLOptState *opt; PR_STDIO_INIT(); opt = PL_CreateOptState(argc, argv, "d"); while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { if (PL_OPT_BAD == os) continue; switch (opt->option) { case 'd': /* debug mode */ debug_mode = PR_TRUE; break; default: break; } } PL_DestroyOptState(opt); /* main test */ PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); #ifdef XP_MAC SetupMacPrintfLog("timetest.log"); debug_mode = PR_TRUE; #endif /* Testing zero PRTime (the epoch) */ { PRTime t; PRExplodedTime et; LL_I2L(t, 0); if (debug_mode) printf("The NSPR epoch is:\n"); PR_ExplodeTime(t, PR_LocalTimeParameters, &et); printExplodedTime(&et); if (debug_mode) printf("\n"); PR_ExplodeTime(t, PR_GMTParameters, &et); printExplodedTime(&et); if (debug_mode) printf("\n\n"); testParseTimeString(t); } /* ************************************************************* ** ** Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime ** on the current time ** ************************************************************* */ { PRTime t1, t2; PRExplodedTime et; if (debug_mode) { printf("*********************************************\n"); printf("** **\n"); printf("** Testing PR_Now(), PR_ExplodeTime, and **\n"); printf("** PR_ImplodeTime on the current time **\n"); printf("** **\n"); printf("*********************************************\n\n"); } t1 = PR_Now(); /* First try converting to UTC */ PR_ExplodeTime(t1, PR_GMTParameters, &et); if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) { if (debug_mode) printf("ERROR: UTC has nonzero gmt or dst offset.\n"); else failed_already=1; return 1; } if (debug_mode) printf("Current UTC is "); printExplodedTime(&et); if (debug_mode) printf("\n"); t2 = PR_ImplodeTime(&et); if (LL_NE(t1, t2)) { if (debug_mode) printf("ERROR: Explode and implode are NOT inverse.\n"); else printf("FAIL\n"); return 1; } /* Next, try converting to local (US Pacific) time */ PR_ExplodeTime(t1, PR_LocalTimeParameters, &et); if (debug_mode) printf("Current local time is "); printExplodedTime(&et); if (debug_mode) printf("\n"); if (debug_mode) printf("GMT offset is %ld, DST offset is %ld\n", et.tm_params.tp_gmt_offset, et.tm_params.tp_dst_offset); t2 = PR_ImplodeTime(&et); if (LL_NE(t1, t2)) { if (debug_mode) printf("ERROR: Explode and implode are NOT inverse.\n"); return 1; } if (debug_mode) printf("Please examine the results\n"); testParseTimeString(t1); } /* ******************************************* ** ** Testing PR_NormalizeTime() ** ******************************************* */ /* July 4, 2001 is Wednesday */ { PRExplodedTime et; if (debug_mode) { printf("\n"); printf("**********************************\n"); printf("** **\n"); printf("** Testing PR_NormalizeTime() **\n"); printf("** **\n"); printf("**********************************\n\n"); } et.tm_year = 2001; et.tm_month = 7 - 1; et.tm_mday = 4; et.tm_hour = 0; et.tm_min = 0; et.tm_sec = 0; et.tm_usec = 0; et.tm_params = PR_GMTParameters(&et); PR_NormalizeTime(&et, PR_GMTParameters); if (debug_mode) printf("July 4, 2001 is %s.\n", dayOfWeek[et.tm_wday]); if (et.tm_wday == 3) { if (debug_mode) printf("PASS\n"); } else { if (debug_mode) printf("ERROR: It should be Wednesday\n"); else failed_already=1; return 1; } testParseTimeString(PR_ImplodeTime(&et)); /* June 12, 1997 23:00 PST == June 13, 1997 00:00 PDT */ et.tm_year = 1997; et.tm_month = 6 - 1; et.tm_mday = 12; et.tm_hour = 23; et.tm_min = 0; et.tm_sec = 0; et.tm_usec = 0; et.tm_params.tp_gmt_offset = -8 * 3600; et.tm_params.tp_dst_offset = 0; PR_NormalizeTime(&et, PR_USPacificTimeParameters); if (debug_mode) { printf("Thu Jun 12, 1997 23:00:00 PST is "); } printExplodedTime(&et); if (debug_mode) printf(".\n"); if (et.tm_wday == 5) { if (debug_mode) printf("PASS\n"); } else { if (debug_mode) printf("ERROR: It should be Friday\n"); else failed_already=1; return 1; } testParseTimeString(PR_ImplodeTime(&et)); /* Feb 14, 1997 00:00:00 PDT == Feb 13, 1997 23:00:00 PST */ et.tm_year = 1997; et.tm_month = 2 - 1; et.tm_mday = 14; et.tm_hour = 0; et.tm_min = 0; et.tm_sec = 0; et.tm_usec = 0; et.tm_params.tp_gmt_offset = -8 * 3600; et.tm_params.tp_dst_offset = 3600; PR_NormalizeTime(&et, PR_USPacificTimeParameters); if (debug_mode) { printf("Fri Feb 14, 1997 00:00:00 PDT is "); } printExplodedTime(&et); if (debug_mode) printf(".\n"); if (et.tm_wday == 4) { if (debug_mode) printf("PASS\n"); } else { if (debug_mode) printf("ERROR: It should be Thursday\n"); else failed_already=1; return 1; } testParseTimeString(PR_ImplodeTime(&et)); /* What time is Nov. 7, 1996, 18:29:23 PDT? */ et.tm_year = 1996; et.tm_month = 11 - 1; et.tm_mday = 7; et.tm_hour = 18; et.tm_min = 29; et.tm_sec = 23; et.tm_usec = 0; 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; } } } } if (failed_already) return 1; else return 0; }
int main(int argc, char** argv) #endif { PR_STDIO_INIT(); PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); /* ************************************************************* ** ** Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime ** on the current time ** ************************************************************* */ { PRTime t1, t2; PRExplodedTime et; printf("*********************************************\n"); printf("** **\n"); printf("** Testing PR_Now(), PR_ExplodeTime, and **\n"); printf("** PR_ImplodeTime on the current time **\n"); printf("** **\n"); printf("*********************************************\n\n"); t1 = PR_Now(); /* First try converting to UTC */ PR_ExplodeTime(t1, PR_GMTParameters, &et); if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) { printf("ERROR: UTC has nonzero gmt or dst offset.\n"); return 1; } printf("Current UTC is "); printExplodedTime(&et); printf("\n"); t2 = PR_ImplodeTime(&et); if (LL_NE(t1, t2)) { printf("ERROR: Explode and implode are NOT inverse.\n"); return 1; } /* Next, try converting to local (US Pacific) time */ PR_ExplodeTime(t1, PR_LocalTimeParameters, &et); printf("Current local time is "); printExplodedTime(&et); printf("\n"); printf("GMT offset is %ld, DST offset is %ld\n", et.tm_params.tp_gmt_offset, et.tm_params.tp_dst_offset); t2 = PR_ImplodeTime(&et); if (LL_NE(t1, t2)) { printf("ERROR: Explode and implode are NOT inverse.\n"); return 1; } } printf("Please examine the results\n"); return 0; }