00001 #pragma ident "$Id: JulianDate.cpp 3178 2012-06-29 16:32:18Z 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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <cmath>
00042 #include "JulianDate.hpp"
00043 #include "TimeConstants.hpp"
00044
00045 namespace gpstk
00046 {
00047 JulianDate& JulianDate::operator=( const JulianDate& right )
00048 throw()
00049 {
00050 jd = right.jd;
00051 timeSystem = right.timeSystem;
00052 return *this;
00053 }
00054
00055 CommonTime JulianDate::convertToCommonTime() const
00056 throw( gpstk::InvalidRequest )
00057 {
00058 try
00059 {
00060 long double temp_jd( jd + 0.5 );
00061 long jday( static_cast<long>( temp_jd ) );
00062 long double sod =
00063 ( temp_jd - static_cast<long double>( jday ) ) * SEC_PER_DAY;
00064
00065 CommonTime ct;
00066 return ct.set( jday,
00067 static_cast<long>( sod ),
00068 static_cast<double>( sod - static_cast<long>( sod ) ),
00069 timeSystem );
00070 }
00071 catch (InvalidParameter& ip)
00072 {
00073 InvalidRequest ir(ip);
00074 GPSTK_THROW(ir);
00075 }
00076 }
00077
00078 void JulianDate::convertFromCommonTime( const CommonTime& ct )
00079 throw()
00080 {
00081 long jday, sod;
00082 double fsod;
00083 ct.get( jday, sod, fsod, timeSystem );
00084
00085 jd = static_cast<long double>( jday ) +
00086 ( static_cast<long double>( sod )
00087 + static_cast<long double>( fsod ) ) * DAY_PER_SEC
00088 - 0.5;
00089 }
00090
00091 std::string JulianDate::printf( 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 "JLf", jd );
00101 rv = formattedPrint( rv, getFormatPrefixInt() + "P",
00102 "Ps", timeSystem.asString().c_str() );
00103 return rv;
00104 }
00105 catch( gpstk::StringUtils::StringException& se )
00106 {
00107 GPSTK_RETHROW( se );
00108 }
00109 }
00110
00111 std::string JulianDate::printError( const std::string& fmt ) const
00112 throw( gpstk::StringUtils::StringException )
00113 {
00114 try
00115 {
00116 using gpstk::StringUtils::formattedPrint;
00117 std::string rv( fmt );
00118
00119 rv = formattedPrint( rv, getFormatPrefixFloat() + "J",
00120 "Js", getError().c_str() );
00121 rv = formattedPrint( rv, getFormatPrefixInt() + "P",
00122 "Ps", getError().c_str() );
00123 return rv;
00124 }
00125 catch( gpstk::StringUtils::StringException& se )
00126 {
00127 GPSTK_RETHROW( se );
00128 }
00129 }
00130
00131 bool JulianDate::setFromInfo( const IdToValue& info )
00132 throw()
00133 {
00134 using namespace gpstk::StringUtils;
00135
00136 for( IdToValue::const_iterator i = info.begin(); i != info.end(); i++ )
00137 {
00138 switch( i->first )
00139 {
00140 case 'J':
00141 jd = asLongDouble( i->second );
00142 break;
00143
00144 case 'P':
00145 timeSystem = static_cast<TimeSystem>(asInt( i->second ));
00146 break;
00147
00148 default:
00149
00150 break;
00151 };
00152 }
00153
00154 return true;
00155 }
00156
00157 bool JulianDate::isValid() const
00158 throw()
00159 {
00160 JulianDate temp;
00161 temp.convertFromCommonTime( convertToCommonTime() );
00162 if( *this == temp )
00163 {
00164 return true;
00165 }
00166 return false;
00167 }
00168
00169 void JulianDate::reset()
00170 throw()
00171 {
00172 jd = 0.0;
00173 timeSystem = TimeSystem::Unknown;
00174 }
00175
00176 bool JulianDate::operator==( const JulianDate& right ) const
00177 throw()
00178 {
00180 if ((timeSystem != TimeSystem::Any &&
00181 right.timeSystem != TimeSystem::Any) &&
00182 timeSystem != right.timeSystem)
00183 return false;
00184
00185 if( fabs(jd - right.jd) < CommonTime::eps )
00186 {
00187 return true;
00188 }
00189 return false;
00190 }
00191
00192 bool JulianDate::operator!=( const JulianDate& right ) const
00193 throw()
00194 {
00195 return ( !operator==( right ) );
00196 }
00197
00198 bool JulianDate::operator<( const JulianDate& right ) const
00199 throw( gpstk::InvalidRequest )
00200 {
00202 if ((timeSystem != TimeSystem::Any &&
00203 right.timeSystem != TimeSystem::Any) &&
00204 timeSystem != right.timeSystem)
00205 {
00206 gpstk::InvalidRequest ir("CommonTime objects not in same time system, cannot be compared");
00207 GPSTK_THROW(ir);
00208 }
00209
00210 if( jd < right.jd )
00211 {
00212 return true;
00213 }
00214 return false;
00215 }
00216
00217 bool JulianDate::operator>( const JulianDate& right ) const
00218 throw( gpstk::InvalidRequest )
00219 {
00220 return ( !operator<=( right ) );
00221 }
00222
00223 bool JulianDate::operator<=( const JulianDate& right ) const
00224 throw( gpstk::InvalidRequest )
00225 {
00226 return ( operator<( right ) ||
00227 operator==( right ) );
00228 }
00229
00230 bool JulianDate::operator>=( const JulianDate& right ) const
00231 throw( gpstk::InvalidRequest )
00232 {
00233 return ( !operator<( right ) );
00234 }
00235
00236 }