void UnixTime::convertFromCommonTime( const CommonTime& ct ) throw( InvalidRequest ) { /// This is the earliest CommonTime for which UnixTimes are valid. static const CommonTime MIN_CT = UnixTime(); /// This is the latest CommonTime for which UnixTimes are valid. /// (2^31 - 1) s and 999999 us static const CommonTime MAX_CT = UnixTime(2147483647, 999999); if ( ct < MIN_CT || ct > MAX_CT ) { InvalidRequest ir("Unable to convert given CommonTime to UnixTime."); GPSTK_THROW(ir); } long jday, sod; double fsod; ct.get( jday, sod, fsod ); tv.tv_sec = (jday - MJD_JDAY - UNIX_MJD) * SEC_PER_DAY + sod; // round to the nearest microsecond tv.tv_usec = static_cast<time_t>( fsod * 1e6 + 0.5 ) ; if (tv.tv_usec >= 1000000) { tv.tv_usec -= 1000000; ++tv.tv_sec; } }
// Test will check converting to/from CommonTime. unsigned toFromCommonTimeTest() { TUDEF("IRNWeekSecond", "isValid"); IRNWeekSecond compare(0,10.0,TimeSystem::IRN); //Initialize an object CommonTime truth; long truthDay, truthSOD; double truthFSOD; truth.set(2451412, 43210,0.0,TimeSystem::IRN); //-------------------------------------------------------------------- //Is the time after the BEGINNING_OF_TIME? //-------------------------------------------------------------------- TUASSERT(compare.convertToCommonTime() > CommonTime::BEGINNING_OF_TIME); //-------------------------------------------------------------------- //Is the set object valid? //-------------------------------------------------------------------- TUASSERT(compare.isValid()); CommonTime test = compare.convertToCommonTime(); //Convert to CommonTime long testDay, testSOD; double testFSOD; test.get(testDay, testSOD, testFSOD); truth.get(truthDay, truthSOD, truthFSOD); // Currently, IRNWeekSecond does not convert to proper CommonTime // These tests will be valid and will be uncommented once issue_248 has been // resolved and merged into master. // TUASSERTE(long, truthDay, testDay); // TUASSERTE(long, truthSOD, testSOD); // TUASSERTFE(truthFSOD, testFSOD); IRNWeekSecond test2; test2.convertFromCommonTime(test); //Convert From testFramework.changeSourceMethod("CommonTimeConversion"); //-------------------------------------------------------------------- //Is the result of conversion the same? //-------------------------------------------------------------------- TUASSERTE(TimeSystem, compare.getTimeSystem(), test2.getTimeSystem()); TUASSERTE(int, compare.week, test2.week); TUASSERTFE(compare.sow, test2.sow); //TUASSERT(test2 == test); TURETURN(); }
void JulianDate::convertFromCommonTime( const CommonTime& ct ) { long jday, sod; double fsod; ct.get( jday, sod, fsod, timeSystem ); jd = static_cast<long double>( jday ) + ( static_cast<long double>( sod ) + static_cast<long double>( fsod ) ) * DAY_PER_SEC - 0.5; }
//============================================================ // Test Suite: resetTest() //============================================================ // // Test will check the reset method. // //============================================================ int resetTest( void ) { TestUtil testFramework( "CommonTime", "reset" , __FILE__, __LINE__ ); CommonTime Compare; Compare.set(1000,200,0.2); // Initialize with value long day, sod; double fsod; Compare.reset(); // Reset it Compare.get(day,sod,fsod); testFramework.assert( TimeSystem(0) == Compare.getTimeSystem(), "Was the time system reset to expectation?", __LINE__ ); testFramework.assert( 0 == day, "Was the day value reset to expectation?", __LINE__ ); testFramework.assert( 0 == sod, "Was the sod value reset to expectation?", __LINE__ ); testFramework.assert( 0 == fsod, "Was the fsod value reset to expectation?", __LINE__ ); return testFramework.countFails(); }
void WeekSecond::convertFromCommonTime( const CommonTime& ct ) { if(static_cast<MJD>(ct).mjd < MJDEpoch()) { InvalidRequest ir("Unable to convert to Week/Second - before Epoch."); GPSTK_THROW(ir); } long jday, sod; double fsod; ct.get( jday, sod, fsod, timeSystem ); // find the number of days since the beginning of the Epoch jday -= MJD_JDAY + MJDEpoch(); // find out how many weeks that is week = static_cast<int>( jday / 7 ); // find out what the day of week is jday %= 7; sow = static_cast<double>( jday * SEC_PER_DAY + sod ) + fsod; }
void ANSITime::convertFromCommonTime( const CommonTime& ct ) throw(InvalidRequest) { /// This is the earliest CommonTime for which ANSITimes are valid. static const CommonTime MIN_CT = ANSITime(0); /// This is the latest CommonTime for which ANSITimes are valid. /// 2^31 - 1 seconds static const CommonTime MAX_CT = ANSITime(2147483647); if ( ct < MIN_CT || ct > MAX_CT ) { InvalidRequest ir("Unable to convert given CommonTime to ANSITime."); GPSTK_THROW(ir); } long jday, sod; double fsod; ct.get( jday, sod, fsod ); time = static_cast<time_t>((jday - MJD_JDAY - UNIX_MJD) * SEC_PER_DAY + sod); }
//============================================================ // Test Suite: rolloverTest() //============================================================ // // Test to check arithmetic operations function properly when // rolling over or under the three time variables // //============================================================ int rolloverTest( void ) { TestUtil testFramework( "CommonTime", "addSeconds", __FILE__, __LINE__ ); CommonTime fsodRollover; fsodRollover.set(10 , 6789 , 0.000999); CommonTime msodRollover; msodRollover.set(10 , 86399, 0.0001 ); CommonTime dayRollunder; dayRollunder.set(10 , 2 , 0.0001 ); CommonTime msodRollunder; msodRollunder.set(10 , 10 , 0.000001); CommonTime expectedfsodROver; expectedfsodROver.set(10, 6789 , 0.001000); CommonTime expectedmsodROver; expectedmsodROver.set(11, 0 , 0.0001); CommonTime expectedDayRUnder; expectedDayRUnder.set( 9, 86399, 0.0001); CommonTime expectedmsodRUnder; expectedmsodRUnder.set(10, 9 , 0.999999); long obtainedDay, expectedDay; long obtainedMsod, expectedMsod; double obtainedFsod, expectedFsod; double diff; long incrementSecLong = 1L , decrementSecLong = -3L; double incrementSecDouble = 0.000001, decrementSecDouble = -0.000002; //-------------------------------- //Rollover Tests //-------------------------------- //fsod Rollover test fsodRollover.addSeconds(incrementSecDouble); fsodRollover.get(obtainedDay,obtainedMsod,obtainedFsod); expectedfsodROver.get(expectedDay,expectedMsod,expectedFsod); testFramework.assert(obtainedDay == expectedDay , "Rollover of fsod affected day value" , __LINE__); testFramework.assert(obtainedMsod == expectedMsod, "Rollover of fsod did not change msod", __LINE__); diff = fabs(obtainedFsod - expectedFsod); testFramework.assert(diff < eps, "fsod did not rollover properly" , __LINE__); //msod Rollover test msodRollover.addSeconds(incrementSecLong); msodRollover.get(obtainedDay,obtainedMsod,obtainedFsod); expectedmsodROver.get(expectedDay,expectedMsod,expectedFsod); testFramework.assert(obtainedDay == expectedDay , "Rollover of msod did not change day" , __LINE__); testFramework.assert(obtainedMsod == expectedMsod, "msod did not rollover properly" , __LINE__); diff = fabs(obtainedFsod - expectedFsod); testFramework.assert(diff < eps, "Rollover of msod affected fsod oddly", __LINE__); //-------------------------------- //Rollunder Tests //-------------------------------- //fsod Rollover test dayRollunder.addSeconds(decrementSecLong); dayRollunder.get(obtainedDay,obtainedMsod,obtainedFsod); expectedDayRUnder.get(expectedDay,expectedMsod,expectedFsod); testFramework.assert(obtainedDay == expectedDay , "Rollunder of msod did not change day" , __LINE__); testFramework.assert(obtainedMsod == expectedMsod, "msod did not rollunder properly" , __LINE__); diff = fabs(obtainedFsod - expectedFsod); testFramework.assert(diff < eps, "Rollunder of msod affected fsod oddly", __LINE__); //msod Rollover test msodRollunder.addSeconds(decrementSecDouble); msodRollunder.get(obtainedDay,obtainedMsod,obtainedFsod); expectedmsodRUnder.get(expectedDay,expectedMsod,expectedFsod); testFramework.assert(obtainedDay == expectedDay , "Rollunder of fsod affected day value" , __LINE__); testFramework.assert(obtainedMsod == expectedMsod, "Rollunder of fsod did not change msod", __LINE__); diff = fabs(obtainedFsod - expectedFsod); testFramework.assert(diff < eps, "fsod did not rollunder properly" , __LINE__); return testFramework.countFails(); }
//============================================================ // Test Suite: arithmeticTest() //============================================================ // // Test to check arithmetic operations function properly // //============================================================ int arithmeticTest( void ) { TestUtil testFramework( "CommonTime", "Operators", __FILE__, __LINE__ ); CommonTime Arith1; Arith1.set(700000,1,0.1); CommonTime Arith2(Arith1); //Set second time equal to the first CommonTime Result; long day, day2, sod; double fsod, sod2; testFramework.assert( fabs((Arith1-Arith2) - 0) < eps, "Does it subtract between two CommonTime objects?", __LINE__ ); //---------------------------------------- // Add seconds with + //---------------------------------------- Result = Arith2 + 1; Result.get(day,sod,fsod); testFramework.assert( day == 700000, "Does it not add to the day value?", __LINE__ ); testFramework.assert( sod == 2, "Does it add to the sod value?", __LINE__ ); testFramework.assert( fabs(fsod - 0.1) < eps, "Does it not add to the fsod value?", __LINE__ ); //---------------------------------------- // Subtract seconds with - //---------------------------------------- Result = Arith2 - 1; Result.get(day,sod,fsod); testFramework.assert( day == 700000, "Does it not subtract from the day value?", __LINE__ ); testFramework.assert( sod == 0, "Does it subtract from the sod value?", __LINE__ ); testFramework.assert( fabs(fsod - 0.1) < eps, "Does it not subtract from the fsod value?", __LINE__ ); //---------------------------------------- // Add seconds with += //---------------------------------------- Arith2 += 1; testFramework.assert( fabs((Arith2-Arith1) - 1) < eps, "Does it add to a CommonTime object?", __LINE__ ); testFramework.assert( 1 == Arith2 - Arith1, "Check that values can be compared with integer seconds", __LINE__ ); //---------------------------------------- // Subtract seconds with -= //---------------------------------------- Arith2 -= 1; testFramework.assert(fabs((Arith2-Arith1) - 0) < eps, "Does it subtract from a CommonTime object?", __LINE__ ); //---------------------------------------- // Add days with addDays //---------------------------------------- Arith2.addDays((long)1); day = Arith2.getDays(); testFramework.assert( 700001. == day, "Does the addDays method function correctly with +?", __LINE__ ); // Subtract days with addDays Arith2.addDays((long)-1); day = Arith2.getDays(); testFramework.assert( 700000. == day, "Does the addDays method function correctly with -?", __LINE__ ); // Add seconds with addSeconds(double) Arith2.addSeconds(86400000.+1000.); testFramework.assert(fabs(86401000. - (Arith2-Arith1)) < eps, "Does the addSeconds method function correctly with +?", __LINE__ ); // Subtract seconds with addSeconds(long) Arith2.addSeconds((long)-86401000); testFramework.assert( fabs(0. - (Arith2-Arith1)) < eps, "Does the addSeconds method function correctly with -?", __LINE__ ); // Check that the two parameter get method returns day2 as the proper double Arith2.get(day2, sod2); testFramework.assert( (long)700000 == day2, "Does the 2 parameter get method reuturn days as a double?", __LINE__ ); testFramework.assert( ((double)0. - sod2) < eps, "Does the 2 parameter get method reuturn days as a double?", __LINE__ ); // Check seconds using getSecondOfDay() testFramework.assert( fabs(sod2 - Arith2.getSecondOfDay()) < eps, "Check seconds using getSecondOfDay()", __LINE__ ); // Add milliseconds with addMilliseconds(long) Arith2.addMilliseconds( (long)1 ); testFramework.assert( fabs(sod2+0.001 - Arith2.getSecondOfDay()) < eps, "Does the addMilliseconds method function correctly with +?", __LINE__ ); Arith2.addMilliseconds( (long)-1 ); testFramework.assert(fabs(sod2 - Arith2.getSecondOfDay()) < eps, "Does the addMilliseconds method function correctly with -?", __LINE__ ); //---------------------------------------- // The End! //---------------------------------------- return testFramework.countFails(); }