YDSTime.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: YDSTime.cpp 1162 2008-03-27 21:18:13Z snelsen $"
00002 
00003 
00004 
00005 //============================================================================
00006 //
00007 //  This file is part of GPSTk, the GPS Toolkit.
00008 //
00009 //  The GPSTk is free software; you can redistribute it and/or modify
00010 //  it under the terms of the GNU Lesser General Public License as published
00011 //  by the Free Software Foundation; either version 2.1 of the License, or
00012 //  any later version.
00013 //
00014 //  The GPSTk is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 //  GNU Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with GPSTk; if not, write to the Free Software Foundation,
00021 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //  
00023 //  Copyright 2004, The University of Texas at Austin
00024 //
00025 //============================================================================
00026 
00027 #include "YDSTime.hpp"
00028 #include "TimeConverters.hpp"
00029 
00030 namespace gpstk
00031 {
00032    YDSTime& YDSTime::operator=( const YDSTime& right )
00033       throw()
00034    {
00035       year = right.year;
00036       doy = right.doy;
00037       sod = right.sod;
00038       return *this;
00039    }
00040    
00041    CommonTime YDSTime::convertToCommonTime() const
00042       throw(InvalidRequest)
00043    {
00044       try
00045       {
00046          long jday = convertCalendarToJD( year, 1, 1 ) + doy - 1;
00047          return CommonTime( jday, sod );
00048       }
00049       catch (InvalidParameter& ip)
00050       {
00051          InvalidRequest ir(ip);
00052          GPSTK_THROW(ir);
00053       }
00054    }
00055    
00056    void YDSTime::convertFromCommonTime( const CommonTime& ct )
00057       throw()
00058    {
00059       long jday, secDay;
00060       double fsecDay;
00061       ct.get( jday, secDay, fsecDay );
00062       sod = static_cast<double>( secDay ) + fsecDay;
00063 
00064       int month, day;
00065       convertJDtoCalendar( jday, year, month, day );
00066       doy = jday - convertCalendarToJD( year, 1, 1 ) + 1;
00067    }
00068    
00069    std::string YDSTime::printf( const std::string& fmt ) const
00070       throw( gpstk::StringUtils::StringException )
00071    {
00072       try
00073       {
00074          using gpstk::StringUtils::formattedPrint;
00075          std::string rv = fmt;
00076          
00077          rv = formattedPrint( rv, getFormatPrefixInt() + "Y",
00078                               "Yd", year );
00079          rv = formattedPrint(rv, getFormatPrefixInt() + "y",
00080                              "yd", static_cast<short>(year % 100) );
00081          rv = formattedPrint( rv, getFormatPrefixInt() + "j",
00082                               "ju", doy );
00083          rv = formattedPrint( rv, getFormatPrefixFloat() + "s",
00084                               "sf", sod );
00085          return rv;
00086       }
00087       catch( gpstk::StringUtils::StringException& exc)
00088       {
00089          GPSTK_RETHROW( exc );
00090       }
00091    }
00092 
00093    std::string YDSTime::printError( const std::string& fmt ) const
00094       throw( gpstk::StringUtils::StringException )
00095    {
00096       try
00097       {
00098          using gpstk::StringUtils::formattedPrint;
00099          std::string rv = fmt;
00100          
00101          rv = formattedPrint( rv, getFormatPrefixInt() + "Y",
00102                               "Ys", getError().c_str() );
00103          rv = formattedPrint(rv, getFormatPrefixInt() + "y",
00104                              "ys", getError().c_str() );
00105          rv = formattedPrint( rv, getFormatPrefixInt() + "j",
00106                               "js", getError().c_str() );
00107          rv = formattedPrint( rv, getFormatPrefixFloat() + "s",
00108                               "ss", getError().c_str() );
00109          return rv;
00110       }
00111       catch( gpstk::StringUtils::StringException& exc)
00112       {
00113          GPSTK_RETHROW( exc );
00114       }
00115    }
00116 
00117    bool YDSTime::setFromInfo( const IdToValue& info )
00118       throw()
00119    {
00120       using namespace gpstk::StringUtils;
00121 
00122       for( IdToValue::const_iterator i = info.begin();
00123            i != info.end(); i++ )
00124       {
00125          switch( i->first )
00126          {
00127             case 'Y':
00128                year = asInt( i->second );
00129                break;
00130             
00131             case 'y':
00132                switch( i->second.length() )
00133                {
00134                   case 2:
00135                      year = asInt( i->second ) + 1900;
00136                      if( year < 1980 )
00137                         year += 100;
00138                      break;
00139                   case 3:
00140                      year = asInt( i->second ) + 1000;
00141                      if( year < 1980 )
00142                         year += 100;
00143                      break;
00144                   default:
00145                      year = asInt( i->second );
00146                      break;
00147                };
00148                break;
00149 
00150             case 'j':
00151                doy = asInt( i->second );
00152                break;
00153 
00154             case 's':
00155                sod = asDouble( i->second );
00156                break;
00157             
00158             default:
00159                   // do nothing
00160                break;
00161          };
00162       }
00163       
00164       return true;
00165    }
00166 
00167    bool YDSTime::isValid() const
00168       throw()
00169    {
00170       YDSTime temp;
00171       temp.convertFromCommonTime( convertToCommonTime() );
00172       if( *this == temp )
00173       {
00174          return true;
00175       }
00176       return false;
00177    }
00178 
00179    void YDSTime::reset()
00180       throw()
00181    {
00182       year = doy = 0;
00183       sod = 0.0;
00184    }
00185 
00186    bool YDSTime::operator==( const YDSTime& right ) const
00187       throw()
00188    {
00189       if( year == right.year && 
00190           doy == right.doy &&
00191           sod == right.sod )
00192       {
00193          return true;
00194       }
00195       return false;          
00196    }
00197 
00198    bool YDSTime::operator!=( const YDSTime& right ) const
00199       throw()
00200    {
00201       return (! operator==( right ) );
00202    }
00203 
00204    bool YDSTime::operator<( const YDSTime& right ) const
00205       throw()
00206    {
00207       if( year < right.year )
00208       {
00209          return true;
00210       }
00211       if( year > right.year )
00212       {
00213          return false;
00214       }
00215       if( doy < right.doy )
00216       { 
00217          return true;
00218       }
00219       if( doy > right.doy )
00220       {
00221          return false;
00222       }
00223       if( sod < right.sod )
00224       {
00225          return true;
00226       }
00227       return false;
00228    }
00229 
00230    bool YDSTime::operator>( const YDSTime& right ) const
00231       throw()
00232    {
00233       return (! operator<=( right ) );
00234    }
00235 
00236    bool YDSTime::operator<=( const YDSTime& right ) const
00237       throw()
00238    {
00239       return ( operator<( right ) || operator==( right ) );
00240    }
00241 
00242    bool YDSTime::operator>=( const YDSTime& right ) const
00243       throw()
00244    {
00245       return (! operator<( right ) );
00246    }
00247    
00248 } // namespace

Generated on Tue May 22 03:31:02 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1