00001 #pragma ident "$Id: CivilTime.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 #include <cmath>
00040 #include "CivilTime.hpp"
00041 #include "TimeConverters.hpp"
00042
00043 namespace gpstk
00044 {
00046 const char * CivilTime::MonthNames[] =
00047 {
00048 "Error",
00049 "January","February", "March", "April",
00050 "May", "June","July", "August",
00051 "September", "October", "November", "December"
00052 };
00053
00055 const char * CivilTime::MonthAbbrevNames[] =
00056 {
00057 "err", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
00058 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
00059 };
00060
00061 CivilTime& CivilTime::operator=( const CivilTime& right )
00062 throw()
00063 {
00064 year = right.year;
00065 month = right.month;
00066 day = right.day;
00067 hour = right.hour;
00068 minute = right.minute;
00069 second = right.second;
00070 timeSystem = right.timeSystem;
00071 return *this;
00072 }
00073
00074 CommonTime CivilTime::convertToCommonTime() const
00075 throw( gpstk::InvalidRequest )
00076 {
00077 try
00078 {
00079
00080 long jday = convertCalendarToJD( year, month, day );
00081
00082 double sod = convertTimeToSOD( hour, minute, second );
00083
00084
00085 CommonTime ct;
00086 return ct.set( jday, static_cast<long>(sod) ,
00087 (sod - static_cast<long>(sod)),
00088 timeSystem );
00089 }
00090 catch (InvalidParameter& ip)
00091 {
00092 InvalidRequest ir(ip);
00093 GPSTK_THROW(ir);
00094 }
00095 }
00096
00097 void CivilTime::convertFromCommonTime( const CommonTime& ct )
00098 throw()
00099 {
00100 long jday, sod;
00101 double fsod;
00102
00103 ct.get( jday, sod, fsod, timeSystem );
00104
00105 convertJDtoCalendar( jday, year, month, day );
00106
00107 convertSODtoTime( static_cast<double>( sod ), hour, minute, second );
00108
00109 second += fsod;
00110 }
00111
00112 std::string CivilTime::printf( const std::string& fmt ) const
00113 throw( gpstk::StringUtils::StringException )
00114 {
00115 try
00116 {
00117 using gpstk::StringUtils::formattedPrint;
00118 std::string rv = fmt;
00119
00120 rv = formattedPrint( rv, getFormatPrefixInt() + "Y",
00121 "Yd", year );
00122 rv = formattedPrint( rv, getFormatPrefixInt() + "y",
00123 "yd", static_cast<short>( year % 100 ) );
00124 rv = formattedPrint( rv, getFormatPrefixInt() + "m",
00125 "mu", month );
00126 rv = formattedPrint( rv, getFormatPrefixInt() + "b",
00127 "bs", MonthAbbrevNames[month] );
00128 rv = formattedPrint( rv, getFormatPrefixInt() + "B",
00129 "Bs", MonthNames[month] );
00130 rv = formattedPrint( rv, getFormatPrefixInt() + "d",
00131 "du", day );
00132 rv = formattedPrint( rv, getFormatPrefixInt() + "H",
00133 "Hu", hour );
00134 rv = formattedPrint( rv, getFormatPrefixInt() + "M",
00135 "Mu", minute );
00136 rv = formattedPrint( rv, getFormatPrefixInt() + "S",
00137 "Su", static_cast<short>( second ) );
00138 rv = formattedPrint( rv, getFormatPrefixFloat() + "f",
00139 "ff", second );
00140 rv = formattedPrint( rv, getFormatPrefixInt() + "P",
00141 "Ps", timeSystem.asString().c_str() );
00142 return rv;
00143 }
00144 catch( gpstk::StringUtils::StringException& exc )
00145 {
00146 GPSTK_RETHROW( exc );
00147 }
00148 }
00149
00150 std::string CivilTime::printError( const std::string& fmt) const
00151 throw( gpstk::StringUtils::StringException )
00152 {
00153 try
00154 {
00155 using gpstk::StringUtils::formattedPrint;
00156 std::string rv = fmt;
00157
00158 rv = formattedPrint( rv, getFormatPrefixInt() + "Y",
00159 "Ys", getError().c_str() );
00160 rv = formattedPrint( rv, getFormatPrefixInt() + "y",
00161 "ys", getError().c_str() );
00162 rv = formattedPrint( rv, getFormatPrefixInt() + "m",
00163 "ms", getError().c_str() );
00164 rv = formattedPrint( rv, getFormatPrefixInt() + "b",
00165 "bs", getError().c_str() );
00166 rv = formattedPrint( rv, getFormatPrefixInt() + "B",
00167 "Bs", getError().c_str() );
00168 rv = formattedPrint( rv, getFormatPrefixInt() + "d",
00169 "ds", getError().c_str() );
00170 rv = formattedPrint( rv, getFormatPrefixInt() + "H",
00171 "Hs", getError().c_str() );
00172 rv = formattedPrint( rv, getFormatPrefixInt() + "M",
00173 "Ms", getError().c_str() );
00174 rv = formattedPrint( rv, getFormatPrefixInt() + "S",
00175 "Ss", getError().c_str() );
00176 rv = formattedPrint( rv, getFormatPrefixFloat() + "f",
00177 "fs", getError().c_str() );
00178 rv = formattedPrint( rv, getFormatPrefixInt() + "P",
00179 "Ps", getError().c_str() );
00180 return rv;
00181 }
00182 catch( gpstk::StringUtils::StringException& exc )
00183 {
00184 GPSTK_RETHROW( exc );
00185 }
00186 }
00187
00188 bool CivilTime::setFromInfo( const IdToValue& info )
00189 throw()
00190 {
00191 using namespace gpstk::StringUtils;
00192
00193 for( IdToValue::const_iterator i = info.begin(); i != info.end(); i++ )
00194 {
00195 switch( i->first )
00196 {
00197 case 'Y':
00198 year = asInt( i->second );
00199 break;
00200
00201 case 'y':
00202 switch( i->second.length() )
00203 {
00204 case 2:
00205 year = asInt( i->second ) + 1900;
00206 if( year < 1980 )
00207 year += 100;
00208 break;
00209 case 3:
00210 year = asInt( i->second ) + 1000;
00211 if( year < 1980 )
00212 year += 100;
00213 break;
00214 default:
00215 year = asInt( i->second );
00216 break;
00217 };
00218 break;
00219
00220 case 'm':
00221 month = asInt( i->second );
00222 break;
00223
00224 case 'b':
00225 case 'B':
00226 {
00227 std::string thisMonth( i->second );
00228 lowerCase(thisMonth);
00229
00230 if (isLike(thisMonth, "jan.*")) month = 1;
00231 else if (isLike(thisMonth, "feb.*")) month = 2;
00232 else if (isLike(thisMonth, "mar.*")) month = 3;
00233 else if (isLike(thisMonth, "apr.*")) month = 4;
00234 else if (isLike(thisMonth, "may.*")) month = 5;
00235 else if (isLike(thisMonth, "jun.*")) month = 6;
00236 else if (isLike(thisMonth, "jul.*")) month = 7;
00237 else if (isLike(thisMonth, "aug.*")) month = 8;
00238 else if (isLike(thisMonth, "sep.*")) month = 9;
00239 else if (isLike(thisMonth, "oct.*")) month = 10;
00240 else if (isLike(thisMonth, "nov.*")) month = 11;
00241 else if (isLike(thisMonth, "dec.*")) month = 12;
00242 else
00243 {
00244 return false;
00245 }
00246 }
00247 break;
00248
00249 case 'd':
00250 day = asInt( i->second );
00251 break;
00252
00253 case 'H':
00254 hour = asInt( i->second );
00255 break;
00256
00257 case 'M':
00258 minute = asInt( i->second );
00259 break;
00260
00261 case 'S':
00262 case 'f':
00263 second = asDouble( i->second );
00264 if (i->first == 'S')
00265 second = floor(second);
00266 break;
00267
00268 case 'P':
00269 timeSystem = static_cast<TimeSystem>(asInt( i->second ));
00270 break;
00271
00272 default:
00273
00274 break;
00275 };
00276 }
00277
00278 return true;
00279 }
00280
00281 bool CivilTime::isValid() const
00282 throw()
00283 {
00284 CivilTime temp;
00285 temp.convertFromCommonTime( convertToCommonTime() );
00286 if( *this == temp )
00287 {
00288 return true;
00289 }
00290 return false;
00291 }
00292
00293 void CivilTime::reset()
00294 throw()
00295 {
00296 year = 0;
00297 month = day = 1;
00298 hour = minute = 0;
00299 second = 0.0;
00300 timeSystem = TimeSystem::Unknown;
00301 }
00302
00303 bool CivilTime::operator==( const CivilTime& right ) const
00304 throw()
00305 {
00307 if ((timeSystem != TimeSystem::Any &&
00308 right.timeSystem != TimeSystem::Any) &&
00309 timeSystem != right.timeSystem)
00310 return false;
00311
00312 if( year == right.year &&
00313 month == right.month &&
00314 day == right.day &&
00315 hour == right.hour &&
00316 minute == right.minute &&
00317 fabs(second - right.second) < CommonTime::eps )
00318 {
00319 return true;
00320 }
00321 return false;
00322 }
00323
00324 bool CivilTime::operator!=( const CivilTime& right ) const
00325 throw()
00326 {
00327 return ( !operator==( right ) );
00328 }
00329
00330 bool CivilTime::operator<( const CivilTime& right ) const
00331 throw( gpstk::InvalidRequest )
00332 {
00334 if ((timeSystem != TimeSystem::Any &&
00335 right.timeSystem != TimeSystem::Any) &&
00336 timeSystem != right.timeSystem)
00337 {
00338 gpstk::InvalidRequest ir("CommonTime objects not in same time system, cannot be compared");
00339 GPSTK_THROW(ir);
00340 }
00341
00342 if( year < right.year )
00343 {
00344 return true;
00345 }
00346 if( year > right.year )
00347 {
00348 return false;
00349 }
00350 if( month < right.month )
00351 {
00352 return true;
00353 }
00354 if( month > right.month )
00355 {
00356 return false;
00357 }
00358 if( day < right.day )
00359 {
00360 return true;
00361 }
00362 if( day > right.day )
00363 {
00364 return false;
00365 }
00366 if( hour < right.hour )
00367 {
00368 return true;
00369 }
00370 if( hour > right.hour )
00371 {
00372 return false;
00373 }
00374 if( minute < right.minute )
00375 {
00376 return true;
00377 }
00378 if( minute > right.minute )
00379 {
00380 return false;
00381 }
00382 if( second < right.second )
00383 {
00384 return true;
00385 }
00386
00387 return false;
00388 }
00389
00390 bool CivilTime::operator>( const CivilTime& right ) const
00391 throw( gpstk::InvalidRequest )
00392 {
00393 return ( !operator<=( right ) );
00394 }
00395
00396 bool CivilTime::operator<=( const CivilTime& right ) const
00397 throw( gpstk::InvalidRequest )
00398 {
00399 return ( operator<( right ) || operator==( right ) );
00400 }
00401
00402 bool CivilTime::operator>=( const CivilTime& right ) const
00403 throw( gpstk::InvalidRequest )
00404 {
00405 return ( !operator<( right ) );
00406 }
00407
00408
00409
00410
00411
00412
00413
00414
00415 std::ostream& operator<<( std::ostream& s,
00416 const CivilTime& cit )
00417 {
00418 s << cit.printf("%02m/%02d/%04Y %02H:%02M:%02S %P");
00419 return s;
00420 }
00421
00422
00423 }