00001 #pragma ident "$Id: MJD.cpp 1863 2009-04-17 20:13:21Z snelsen $"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "MJD.hpp"
00028 #include "TimeConstants.hpp"
00029
00030 namespace gpstk
00031 {
00032 MJD& MJD::operator=( const MJD& right )
00033 throw()
00034 {
00035 mjd = right.mjd;
00036 return *this;
00037 }
00038
00039 CommonTime MJD::convertToCommonTime() const
00040 throw(InvalidRequest)
00041 {
00042 try
00043 {
00044
00045 long double tmp( mjd + MJD_JDAY );
00046
00047 long jday( static_cast<long>( tmp ) );
00048
00049 tmp -= static_cast<long>( tmp );
00050
00051 tmp *= SEC_PER_DAY;
00052
00053
00054 double dTmp( static_cast<double>( tmp ) );
00055
00056 return CommonTime( jday,
00057 static_cast<long>( dTmp ),
00058 dTmp - static_cast<long>( dTmp ) );
00059 }
00060 catch (InvalidParameter& ip)
00061 {
00062 InvalidRequest ir(ip);
00063 GPSTK_THROW(ip);
00064 }
00065
00066 }
00067
00068 void MJD::convertFromCommonTime( const CommonTime& ct )
00069 throw()
00070 {
00071 long jday, sod;
00072 double fsod;
00073 ct.get( jday, sod, fsod );
00074
00075 mjd = static_cast<long double>( jday - MJD_JDAY ) +
00076 ( static_cast<long double>( sod )
00077 + static_cast<long double>( fsod ) ) * DAY_PER_SEC;
00078 }
00079
00080 std::string MJD::printf( const std::string& fmt ) const
00081 throw( gpstk::StringUtils::StringException )
00082 {
00083 try
00084 {
00085 using gpstk::StringUtils::formattedPrint;
00086 std::string rv( fmt );
00087
00088 rv = formattedPrint( rv, getFormatPrefixFloat() + "Q",
00089 "QLf", mjd );
00090 return rv;
00091 }
00092 catch( gpstk::StringUtils::StringException& se )
00093 {
00094 GPSTK_RETHROW( se );
00095 }
00096 }
00097
00098 std::string MJD::printError( const std::string& fmt ) const
00099 throw( gpstk::StringUtils::StringException )
00100 {
00101 try
00102 {
00103 using gpstk::StringUtils::formattedPrint;
00104 std::string rv( fmt );
00105
00106 rv = formattedPrint( rv, getFormatPrefixFloat() + "Q",
00107 "Qs", getError().c_str() );
00108 return rv;
00109 }
00110 catch( gpstk::StringUtils::StringException& se )
00111 {
00112 GPSTK_RETHROW( se );
00113 }
00114 }
00115
00116 bool MJD::setFromInfo( const IdToValue& info )
00117 throw()
00118 {
00119 using namespace gpstk::StringUtils;
00120
00121 IdToValue::const_iterator itr = info.find('Q');
00122 if( itr != info.end() )
00123 {
00124 mjd = gpstk::StringUtils::asLongDouble( itr->second );
00125 }
00126
00127 return true;
00128 }
00129
00130 bool MJD::isValid() const
00131 throw()
00132 {
00133 MJD temp;
00134 temp.convertFromCommonTime( convertToCommonTime() );
00135 if( *this == temp )
00136 {
00137 return true;
00138 }
00139 return false;
00140 }
00141
00142 void MJD::reset()
00143 throw()
00144 {
00145 mjd = 0.0;
00146 }
00147
00148 bool MJD::operator==( const MJD& right ) const
00149 throw()
00150 {
00151 if( mjd == right.mjd )
00152 {
00153 return true;
00154 }
00155 return false;
00156 }
00157
00158 bool MJD::operator!=( const MJD& right ) const
00159 throw()
00160 {
00161 return ( !operator==( right ) );
00162 }
00163
00164 bool MJD::operator<( const MJD& right ) const
00165 throw()
00166 {
00167 if( mjd < right.mjd )
00168 {
00169 return true;
00170 }
00171 return false;
00172 }
00173
00174 bool MJD::operator>( const MJD& right ) const
00175 throw()
00176 {
00177 return ( !operator<=( right ) );
00178 }
00179
00180 bool MJD::operator<=( const MJD& right ) const
00181 throw()
00182 {
00183 return ( operator<( right ) ||
00184 operator==( right ) );
00185 }
00186
00187 bool MJD::operator>=( const MJD& right ) const
00188 throw()
00189 {
00190 return ( !operator<( right ) );
00191 }
00192
00193 }