UnixTime.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: UnixTime.cpp 3178 2012-06-29 16:32:18Z 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
00022 //  
00023 //  Copyright 2004, The University of Texas at Austin
00024 //
00025 //============================================================================
00026 
00027 //============================================================================
00028 //
00029 //This software developed by Applied Research Laboratories at the University of
00030 //Texas at Austin, under contract to an agency or agencies within the U.S. 
00031 //Department of Defense. The U.S. Government retains all rights to use,
00032 //duplicate, distribute, disclose, or release this software. 
00033 //
00034 //Pursuant to DoD Directive 523024 
00035 //
00036 // DISTRIBUTION STATEMENT A: This software has been approved for public 
00037 //                           release, distribution is unlimited.
00038 //
00039 //=============================================================================
00040 
00041 #include "UnixTime.hpp"
00042 #include "TimeConstants.hpp"
00043 
00044 namespace gpstk
00045 {
00046    UnixTime& UnixTime::operator=( const UnixTime& right )
00047       throw()
00048    {
00049       tv.tv_sec  = right.tv.tv_sec ;
00050       tv.tv_usec = right.tv.tv_usec;
00051       timeSystem = right.timeSystem;
00052       return *this;
00053    }
00054    
00055    CommonTime UnixTime::convertToCommonTime() const
00056       throw( gpstk::InvalidRequest )
00057    {
00058       try
00059       {
00060          CommonTime ct;
00061          return ct.set( ( MJD_JDAY + UNIX_MJD + tv.tv_sec / SEC_PER_DAY ),
00062                         ( tv.tv_sec % SEC_PER_DAY ),
00063                         ( static_cast<double>( tv.tv_usec ) * 1e-6 ),
00064                         timeSystem );
00065       }
00066       catch (InvalidParameter& ip)
00067       {
00068          InvalidRequest ir(ip);
00069          GPSTK_THROW(ip);
00070       }
00071    }
00072    
00073    void UnixTime::convertFromCommonTime( const CommonTime& ct )
00074       throw( gpstk::InvalidRequest )
00075    {
00077       static const CommonTime MIN_CT = UnixTime(0, 0, TimeSystem::Any);
00080       static const CommonTime MAX_CT = UnixTime(2147483647, 999999, TimeSystem::Any);
00081 
00082       if ( ct < MIN_CT || ct > MAX_CT )
00083       {
00084          InvalidRequest ir("Unable to convert given CommonTime to UnixTime.");
00085          GPSTK_THROW(ir);
00086       }
00087                              
00088       long jday, sod;
00089       double fsod;
00090       ct.get( jday, sod, fsod, timeSystem );
00091       
00092       tv.tv_sec = (jday - MJD_JDAY - UNIX_MJD) * SEC_PER_DAY + sod;
00093       
00094          // round to the nearest microsecond
00095       tv.tv_usec = static_cast<time_t>( fsod * 1e6 + 0.5 ) ;
00096       
00097       if (tv.tv_usec >= 1000000) 
00098       {
00099          tv.tv_usec -= 1000000; 
00100          ++tv.tv_sec; 
00101       }
00102    }
00103    
00104    std::string UnixTime::printf( const std::string& fmt ) const
00105       throw( gpstk::StringUtils::StringException )
00106    {
00107       try
00108       {
00109          using gpstk::StringUtils::formattedPrint;
00110          std::string rv( fmt );
00111          
00112          rv = formattedPrint(rv, getFormatPrefixInt() + "U",
00113                              "Ulu", tv.tv_sec);
00114          rv = formattedPrint(rv, getFormatPrefixInt() + "u",
00115                              "ulu", tv.tv_usec);
00116          rv = formattedPrint(rv, getFormatPrefixInt() + "P",
00117                              "Ps", timeSystem.asString().c_str() );         
00118          return rv;         
00119       }
00120       catch( gpstk::StringUtils::StringException& se )
00121       {
00122          GPSTK_RETHROW( se );
00123       }
00124    }
00125 
00126    std::string UnixTime::printError( const std::string& fmt ) const
00127       throw( gpstk::StringUtils::StringException )
00128    {
00129       try
00130       {
00131          using gpstk::StringUtils::formattedPrint;
00132          std::string rv( fmt );
00133          
00134          rv = formattedPrint(rv, getFormatPrefixInt() + "U",
00135                              "Us", getError().c_str());
00136          rv = formattedPrint(rv, getFormatPrefixInt() + "u",
00137                              "us", getError().c_str());  
00138          rv = formattedPrint( rv, getFormatPrefixInt() + "P",
00139                              "Ps", getError().c_str());           
00140          return rv;         
00141       }
00142       catch( gpstk::StringUtils::StringException& se )
00143       {
00144          GPSTK_RETHROW( se );
00145       }
00146    }
00147 
00148    bool UnixTime::setFromInfo( const IdToValue& info )
00149       throw()
00150    {
00151       using namespace gpstk::StringUtils;
00152       
00153       for( IdToValue::const_iterator i = info.begin(); i != info.end(); i++ )
00154       {
00155          switch( i->first )
00156          {
00157             case 'U':
00158                tv.tv_sec = asInt( i->second );
00159                break;
00160                
00161             case 'u':
00162                tv.tv_usec = asInt( i->second );
00163                break;
00164 
00165             case 'P':
00166                timeSystem = static_cast<TimeSystem>(asInt( i->second ));
00167                break;
00168                
00169             default:
00170                   // do nothing
00171                break;
00172          };
00173       }
00174       
00175       return true;
00176    }
00177    
00178    bool UnixTime::isValid() const
00179       throw()
00180    {
00181       UnixTime temp;
00182       temp.convertFromCommonTime( convertToCommonTime() );
00183       if( *this == temp )
00184       {
00185          return true;
00186       }
00187       return false;
00188    }
00189 
00190    void UnixTime::reset()
00191       throw()
00192    {
00193       tv.tv_sec = tv.tv_usec = 0;
00194       timeSystem = TimeSystem::Unknown;
00195    }
00196    
00197    bool UnixTime::operator==( const UnixTime& right ) const
00198       throw()
00199    {
00201       if ((timeSystem != TimeSystem::Any &&
00202            right.timeSystem != TimeSystem::Any) &&
00203           timeSystem != right.timeSystem)
00204          return false;
00205 
00206       if( tv.tv_sec == right.tv.tv_sec  &&
00207           abs(tv.tv_usec - right.tv.tv_usec) < CommonTime::eps )
00208       {
00209          return true;
00210       }
00211       return false;
00212    }
00213 
00214    bool UnixTime::operator!=( const UnixTime& right ) const
00215       throw()
00216    {
00217       return ( !operator==( right ) );
00218    }
00219 
00220    bool UnixTime::operator<( const UnixTime& right ) const
00221       throw( gpstk::InvalidRequest )
00222    {
00224       if ((timeSystem != TimeSystem::Any &&
00225            right.timeSystem != TimeSystem::Any) &&
00226           timeSystem != right.timeSystem)
00227       {
00228          gpstk::InvalidRequest ir("CommonTime objects not in same time system, cannot be compared");
00229          GPSTK_THROW(ir)
00230       }
00231 
00232       if( tv.tv_sec  <  right.tv.tv_sec )
00233       {
00234          return true;
00235       }
00236       if( tv.tv_sec  == right.tv.tv_sec  &&
00237           tv.tv_usec <  right.tv.tv_usec   )
00238       {
00239          return true;
00240       }
00241       return false;
00242    }
00243 
00244    bool UnixTime::operator>( const UnixTime& right ) const
00245       throw( gpstk::InvalidRequest )
00246    {
00247       return ( !operator<=( right ) );
00248    }
00249 
00250    bool UnixTime::operator<=( const UnixTime& right ) const
00251       throw( gpstk::InvalidRequest )
00252    {
00253       return ( operator<( right ) ||
00254                operator==( right ) );
00255    }
00256 
00257    bool UnixTime::operator>=( const UnixTime& right ) const
00258       throw( gpstk::InvalidRequest )
00259    {
00260       return ( !operator<( right ) );
00261    }
00262 
00263 } // namespace

Generated on Fri May 24 03:31:14 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1