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