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