CivilTime.cpp

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

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