UnixTime.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: UnixTime.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 "UnixTime.hpp"
00028 #include "TimeConstants.hpp"
00029 
00030 namespace gpstk
00031 {
00032    UnixTime& UnixTime::operator=( const UnixTime& right )
00033       throw()
00034    {
00035       tv.tv_sec = right.tv.tv_sec;
00036       tv.tv_usec = right.tv.tv_usec;
00037       return *this;
00038    }
00039    
00040    CommonTime UnixTime::convertToCommonTime() const
00041       throw( InvalidRequest )
00042    {
00043       try
00044       {
00045          return CommonTime( ( MJD_JDAY + UNIX_MJD + tv.tv_sec / SEC_PER_DAY ),
00046                             ( tv.tv_sec % SEC_PER_DAY ),
00047                             ( static_cast<double>( tv.tv_usec ) * 1e-6 ) );
00048       }
00049       catch (InvalidParameter& ip)
00050       {
00051          InvalidRequest ir(ip);
00052          GPSTK_THROW(ip);
00053       }
00054    }
00055    
00056    void UnixTime::convertFromCommonTime( const CommonTime& ct )
00057       throw( InvalidRequest )
00058    {
00060       static const CommonTime MIN_CT = UnixTime();
00063       static const CommonTime MAX_CT = UnixTime(2147483647, 999999);
00064 
00065       if ( ct < MIN_CT || ct > MAX_CT )
00066       {
00067          InvalidRequest ir("Unable to convert given CommonTime to UnixTime.");
00068          GPSTK_THROW(ir);
00069       }
00070                              
00071       long jday, sod;
00072       double fsod;
00073       ct.get( jday, sod, fsod );
00074       
00075       tv.tv_sec = (jday - MJD_JDAY - UNIX_MJD) * SEC_PER_DAY + sod;
00076       
00077          // round to the nearest microsecond
00078       tv.tv_usec = static_cast<time_t>( fsod * 1e6 + 0.5 ) ;
00079       
00080       if (tv.tv_usec >= 1000000) 
00081       {
00082          tv.tv_usec -= 1000000; 
00083          ++tv.tv_sec; 
00084       }
00085    }
00086    
00087    std::string UnixTime::printf( const std::string& fmt ) const
00088       throw( gpstk::StringUtils::StringException )
00089    {
00090       try
00091       {
00092          using gpstk::StringUtils::formattedPrint;
00093          std::string rv( fmt );
00094          
00095          rv = formattedPrint(rv, getFormatPrefixInt() + "U",
00096                              "Ulu", tv.tv_sec);
00097          rv = formattedPrint(rv, getFormatPrefixInt() + "u",
00098                              "ulu", tv.tv_usec);         
00099          return rv;         
00100       }
00101       catch( gpstk::StringUtils::StringException& se )
00102       {
00103          GPSTK_RETHROW( se );
00104       }
00105    }
00106 
00107    std::string UnixTime::printError( const std::string& fmt ) const
00108       throw( gpstk::StringUtils::StringException )
00109    {
00110       try
00111       {
00112          using gpstk::StringUtils::formattedPrint;
00113          std::string rv( fmt );
00114          
00115          rv = formattedPrint(rv, getFormatPrefixInt() + "U",
00116                              "Us", getError().c_str());
00117          rv = formattedPrint(rv, getFormatPrefixInt() + "u",
00118                              "us", getError().c_str());         
00119          return rv;         
00120       }
00121       catch( gpstk::StringUtils::StringException& se )
00122       {
00123          GPSTK_RETHROW( se );
00124       }
00125    }
00126 
00127    bool UnixTime::setFromInfo( const IdToValue& info )
00128       throw()
00129    {
00130       using namespace gpstk::StringUtils;
00131       
00132       for( IdToValue::const_iterator i = info.begin(); i != info.end(); i++ )
00133       {
00134          switch( i->first )
00135          {
00136             case 'U':
00137                tv.tv_sec = asInt( i->second );
00138                break;
00139                
00140             case 'u':
00141                tv.tv_usec = asInt( i->second );
00142                break;
00143                
00144             default:
00145                   // do nothing
00146                break;
00147          };
00148       }
00149       
00150       return true;
00151    }
00152    
00153    bool UnixTime::isValid() const
00154       throw()
00155    {
00156       UnixTime temp;
00157       temp.convertFromCommonTime( convertToCommonTime() );
00158       if( *this == temp )
00159       {
00160          return true;
00161       }
00162       return false;
00163    }
00164 
00165    void UnixTime::reset()
00166       throw()
00167    {
00168       tv.tv_sec  = tv.tv_usec = 0;
00169    }
00170    
00171    bool UnixTime::operator==( const UnixTime& right ) const
00172       throw()
00173    {
00174       if( tv.tv_sec == right.tv.tv_sec &&
00175           tv.tv_usec == right.tv.tv_usec )
00176       {
00177          return true;
00178       }
00179       return false;
00180    }
00181 
00182    bool UnixTime::operator!=( const UnixTime& right ) const
00183       throw()
00184    {
00185       return ( !operator==( right ) );
00186    }
00187 
00188    bool UnixTime::operator<( const UnixTime& right ) const
00189       throw()
00190    {
00191       if( tv.tv_sec < right.tv.tv_sec )
00192       {
00193          return true;
00194       }
00195       if( tv.tv_sec == right.tv.tv_sec &&
00196           tv.tv_usec < right.tv.tv_usec )
00197       {
00198          return true;
00199       }
00200       return false;
00201    }
00202 
00203    bool UnixTime::operator>( const UnixTime& right ) const
00204       throw()
00205    {
00206       return ( !operator<=( right ) );
00207    }
00208 
00209    bool UnixTime::operator<=( const UnixTime& right ) const
00210       throw()
00211    {
00212       return ( operator<( right ) ||
00213                operator==( right ) );
00214    }
00215 
00216    bool UnixTime::operator>=( const UnixTime& right ) const
00217       throw()
00218    {
00219       return ( !operator<( right ) );
00220    }
00221 
00222 } // namespace

Generated on Wed Feb 8 03:31:03 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1