TimeString.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: TimeString.cpp 3346 2013-03-04 16:48:35Z ocibu $"
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 #include "TimeString.hpp"
00028 
00029 #include "ANSITime.hpp"
00030 #include "CivilTime.hpp"
00031 #include "GPSWeekSecond.hpp"
00032 #include "GPSWeekZcount.hpp"
00033 #include "JulianDate.hpp"
00034 #include "MJD.hpp"
00035 #include "UnixTime.hpp"
00036 #include "YDSTime.hpp"
00037 
00038 #include "TimeConverters.hpp"
00039 #include "TimeConstants.hpp"
00040 
00041 namespace gpstk
00042 {
00043    std::string printTime( const CommonTime& t,
00044                           const std::string& fmt )
00045       throw( gpstk::StringUtils::StringException )
00046    {
00047       try
00048       {
00049          std::string rv( fmt );
00050 
00051             // Convert to each TimeTag type and run its printf using rv.
00052          rv = printAs<ANSITime>( t, rv );
00053          rv = printAs<CivilTime>( t, rv );
00054          rv = printAs<GPSWeekSecond>( t, rv );
00055          rv = printAs<GPSWeekZcount>( t, rv );
00056          rv = printAs<JulianDate>( t, rv );
00057          rv = printAs<MJD>( t, rv );
00058          rv = printAs<UnixTime>( t, rv );
00059          rv = printAs<YDSTime>( t, rv );
00060       
00061          return rv;
00062       }
00063       catch( gpstk::StringUtils::StringException& se )
00064       {
00065          GPSTK_RETHROW( se );
00066       }
00067    }
00068    
00071    void scanTime( TimeTag& btime,
00072                   const std::string& str,
00073                   const std::string& fmt )
00074       throw( gpstk::InvalidRequest,
00075              gpstk::StringUtils::StringException )
00076    {
00077       try
00078       {
00079             // Get the mapping of character (from fmt) to value (from str).
00080          TimeTag::IdToValue info;
00081          TimeTag::getInfo( str, fmt, info );
00082          
00083          if( btime.setFromInfo( info ) )
00084          {
00085             return;
00086          }
00087          
00088             // Convert to CommonTime, and try to set using all formats.
00089          CommonTime ct( btime.convertToCommonTime() );
00090          scanTime( ct, str, fmt );
00091 
00092             // Convert the CommonTime into the requested format.
00093          btime.convertFromCommonTime( ct );
00094       }
00095       catch( gpstk::InvalidRequest& ir )
00096       {
00097          GPSTK_RETHROW( ir );
00098       }
00099       catch( gpstk::StringUtils::StringException& se )
00100       {
00101          GPSTK_RETHROW( se );
00102       }
00103    }
00104    
00105    void scanTime( CommonTime& t,
00106                   const std::string& str,
00107                   const std::string& fmt )
00108       throw( gpstk::InvalidRequest,
00109              gpstk::StringUtils::StringException )
00110    {
00111       try
00112       {
00113          using namespace gpstk::StringUtils;
00114 
00115             // Get the mapping of character (from fmt) to value (from str).
00116          TimeTag::IdToValue info;
00117          TimeTag::getInfo( str, fmt, info );
00118          
00119             // These indicate which information has been found.
00120          bool hmjd( false ), hsow( false ), hweek( false ), hfullweek( false ),
00121             hdow( false ), hyear( false ), hmonth( false ), hday( false ),
00122             hzcount( false ), hdoy( false ), hzcount29( false ), 
00123             hzcount32( false ), hhour( false ), hmin( false ), hsec( false ),
00124             hsod( false ), hunixsec( false ), hunixusec( false ), 
00125             hepoch( false ), hansi( false ), hjulian( false );
00126 
00127             // These are to hold data that no one parses.
00128          int idow( 0 );
00129          
00130          for( TimeTag::IdToValue::iterator itr = info.begin();
00131               itr != info.end(); itr++ )
00132          {
00133             switch( itr->first )
00134             {
00135                case 'Q':
00136                   hmjd = true;
00137                   break;
00138 
00139                case 'Z':
00140                   hzcount = true;
00141                   break;
00142 
00143                case 's':
00144                   hsod = true;
00145                   break;
00146 
00147                case 'g':
00148                   hsow = true;
00149                   break;
00150 
00151                case 'w':
00152                   idow = asInt( itr->second );
00153                   hdow = true;
00154                   break;
00155 
00156                case 'G':
00157                   hweek = true;
00158                   break;
00159 
00160                case 'F':
00161                   hfullweek = true;
00162                   break;
00163 
00164                case 'j':
00165                   hdoy = true;
00166                   break;
00167 
00168                case 'b':
00169                case 'B':
00170                   hmonth = true;
00171                   break;
00172 
00173                case 'Y':
00174                case 'y':
00175                   hyear = true;
00176                   break;
00177 
00178                case 'a':
00179                case 'A':
00180                {
00181                   hdow = true;
00182                   std::string thisDay = firstWord( itr->second );
00183                   lowerCase(thisDay);
00184                   if (isLike(thisDay, "sun.*")) idow = 0;
00185                   else if (isLike(thisDay, "mon.*")) idow = 1;
00186                   else if (isLike(thisDay, "tue.*")) idow = 2;
00187                   else if (isLike(thisDay, "wed.*")) idow = 3;
00188                   else if (isLike(thisDay, "thu.*")) idow = 4;
00189                   else if (isLike(thisDay, "fri.*")) idow = 5;
00190                   else if (isLike(thisDay, "sat.*")) idow = 6;
00191                   else
00192                   {
00193                      hdow = false;
00194                   }
00195                }
00196                break;
00197                   
00198                case 'm':
00199                   hmonth = true;
00200                   break;
00201 
00202                case 'd':
00203                   hday = true;
00204                   break;
00205 
00206                case 'H':
00207                   hhour = true;
00208                   break;
00209 
00210                case 'M':
00211                   hmin = true;
00212                   break;
00213 
00214                case 'S':
00215                   hsec = true;
00216                   break;
00217 
00218                case 'f':
00219                   hsec = true;
00220                   info['S'] = info['f'];  // a small hack to make fractional seconds work
00221                   break;
00222 
00223                case 'U':
00224                   hunixsec = true;
00225                   break;
00226 
00227                case 'u':
00228                   hunixusec = true;
00229                   break;
00230                   
00231                case 'c':
00232                   hzcount29 = true;
00233                   break;
00234 
00235                case 'C':
00236                   hzcount32 = true;
00237                   break;
00238 
00239                case 'J':
00240                   hjulian = true;
00241                   break;
00242                   
00243                case 'K':
00244                   hansi = true;
00245                   break;
00246                   
00247                case 'E':
00248                   hepoch = true;
00249                   break;
00250 
00251                default:
00252                {
00253                      // do nothing
00254                }
00255                break;
00256 
00257             };
00258          }
00259 
00260          if( hyear )
00261          {
00262             if( hmonth && hday )
00263             {
00264                CivilTime tt;
00265                tt.setFromInfo( info );
00266                if( hsod )
00267                {
00268                   convertSODtoTime( asDouble( info['s'] ), 
00269                                     tt.hour, tt.minute, tt.second );
00270                }
00271                t = tt.convertToCommonTime();
00272                return;
00273             }
00274             else  // use YDSTime as default
00275             {
00276                YDSTime tt;
00277                tt.setFromInfo( info );
00278                if( hhour && hmin && hsec )
00279                {
00280                   tt.sod = convertTimeToSOD( asInt( info['H'] ), 
00281                                              asInt( info['M'] ), 
00282                                              asDouble( info['S'] ) );
00283                }
00284                t = tt.convertToCommonTime();
00285                return;
00286             }
00287 
00288          } // end of if( hyear )
00289 
00290          if( hzcount32 ||
00291              (hfullweek && hzcount) ||
00292              (hepoch && (hzcount29 || 
00293                          (hweek && hzcount))) )
00294          {
00295             GPSWeekZcount tt;
00296             tt.setFromInfo( info );
00297             t = tt.convertToCommonTime();
00298             return;
00299          }
00300 
00301          if ( ((hepoch && hweek) || hfullweek) )
00302          {
00303             GPSWeekSecond tt;
00304             tt.setFromInfo( info );
00305             if( hdow && !hsow )
00306             {
00307                tt.sow = asInt( info['w'] ) * SEC_PER_DAY;
00308                if( hsod )
00309                {
00310                   tt.sow += asDouble( info['s'] );
00311                }
00312                else if( hhour && hmin && hsec )
00313                {
00314                   tt.sow += convertTimeToSOD( asInt( info['H'] ), 
00315                                               asInt( info['M'] ), 
00316                                               asDouble( info['S'] ) );
00317                }
00318             }
00319             t = tt.convertToCommonTime();
00320             return;
00321          }
00322 
00323          if( hmjd )
00324          {
00325             MJD tt;
00326             tt.setFromInfo( info );
00327             t = tt.convertToCommonTime();
00328             return;
00329          }
00330 
00331          if( hjulian )
00332          {
00333             JulianDate tt;
00334             tt.setFromInfo( info );
00335             t = tt.convertToCommonTime();
00336             return;
00337          }
00338 
00339          if( hansi )
00340          {
00341             ANSITime tt;
00342             tt.setFromInfo( info );
00343             t = tt.convertToCommonTime();
00344             return;
00345          } 
00346          
00347          if( hunixsec || hunixusec )
00348          {
00349             UnixTime tt;
00350             tt.setFromInfo( info );
00351             t = tt.convertToCommonTime();
00352             return;
00353          }
00354 
00355          InvalidRequest ir("Incomplete time specification for readTime");
00356          GPSTK_THROW( ir );
00357       }
00358       catch( gpstk::StringUtils::StringException& se )
00359       {
00360          GPSTK_RETHROW( se );
00361       }
00362    }   
00363 
00364    void mixedScanTime( CommonTime& t,
00365                        const std::string& str,
00366                        const std::string& fmt )
00367       throw( gpstk::InvalidRequest,
00368              gpstk::StringUtils::StringException )
00369    {
00370       try
00371       {
00372          using namespace gpstk::StringUtils;
00373 
00374             // Get the mapping of character (from fmt) to value (from str).
00375          TimeTag::IdToValue info;
00376          TimeTag::getInfo( str, fmt, info );
00377          
00378             // These indicate which information has been found.
00379          bool hsow( false ), hweek( false ), hfullweek( false ),
00380             hdow( false ), hyear( false ), hmonth( false ), hday( false ),
00381             hzcount( false ), hdoy( false ), hzcount29( false ), 
00382             hhour( false ), hmin( false ), hsec( false ),
00383             hsod( false ), hepoch( false ), hunixsec( false ),
00384             hunixusec( false );
00385 
00386             // MJD, Julian Date, ANSI time, Unix time, and 32-bit Zcounts
00387             // are treated as stand-alone types and are not mixed with others
00388             // if detected.
00389          
00390             // These variables will hold the values for use later.
00391          double isow, isod, isec;
00392          int iweek, ifullweek, idow, iyear, imonth, iday, izcount, idoy,
00393             izcount29, ihour, imin, iepoch, iunixsec, iunixusec;
00394          
00395          for( TimeTag::IdToValue::iterator itr = info.begin();
00396               itr != info.end(); itr++ )
00397          {
00398             switch( itr->first )
00399             {
00400                case 'Q':
00401                   t = MJD( asLongDouble(itr->second) );
00402                   return;
00403 
00404                case 'J':
00405                   t = JulianDate( asLongDouble(itr->second) );
00406                   return;
00407                   
00408                case 'C':
00409                   t = GPSWeekZcount().setZcount32( asInt(itr->second) );
00410                   return;
00411 
00412                case 'K':
00413                   t = ANSITime( asInt(itr->second) );
00414                   return;
00415                   
00416                case 'U':
00417                case 'u':
00418                {
00419                   UnixTime tt;
00420                   tt.setFromInfo( info );
00421                   t = tt.convertToCommonTime();
00422                   return;
00423                }
00424                break;
00425 
00426                case 'Z':
00427                   hzcount = true;
00428                   izcount = asInt(itr->second);
00429                   break;
00430 
00431                case 's':
00432                   hsod = true;
00433                   isod = asDouble(itr->second);
00434                   break;
00435 
00436                case 'g':
00437                   hsow = true;
00438                   isow = asDouble(itr->second);
00439                   break;
00440 
00441                case 'w':
00442                   idow = asInt(itr->second);
00443                   hdow = true;
00444                   break;
00445 
00446                case 'G':
00447                   hweek = true;
00448                   iweek = asInt(itr->second);
00449                   break;
00450 
00451                case 'F':
00452                   hfullweek = true;
00453                   ifullweek = asInt(itr->second);
00454                   break;
00455 
00456                case 'j':
00457                   hdoy = true;
00458                   idoy = asInt(itr->second);
00459                   break;
00460 
00461                case 'b':
00462                case 'B':
00463                   hmonth = true;
00464                   imonth = asInt(itr->second);
00465                   break;
00466 
00467                case 'Y':
00468                case 'y':
00469                   hyear = true;
00470                   iyear = asInt(itr->second);
00471                   break;
00472 
00473                case 'a':
00474                case 'A':
00475                {
00476                   hdow = true;
00477                   std::string thisDay = firstWord( itr->second );
00478                   lowerCase(thisDay);
00479                   if (isLike(thisDay, "sun.*")) idow = 0;
00480                   else if (isLike(thisDay, "mon.*")) idow = 1;
00481                   else if (isLike(thisDay, "tue.*")) idow = 2;
00482                   else if (isLike(thisDay, "wed.*")) idow = 3;
00483                   else if (isLike(thisDay, "thu.*")) idow = 4;
00484                   else if (isLike(thisDay, "fri.*")) idow = 5;
00485                   else if (isLike(thisDay, "sat.*")) idow = 6;
00486                }
00487                break;
00488                   
00489                case 'm':
00490                   hmonth = true;
00491                   imonth = asInt(itr->second);
00492                   break;
00493 
00494                case 'd':
00495                   hday = true;
00496                   iday = asInt(itr->second);
00497                   break;
00498 
00499                case 'H':
00500                   hhour = true;
00501                   ihour = asInt(itr->second);
00502                   break;
00503 
00504                case 'M':
00505                   hmin = true;
00506                   imin = asInt(itr->second);
00507                   break;
00508 
00509                case 'S':
00510                   hsec = true;
00511                   isec = asDouble(itr->second);
00512                   break;
00513 
00514                case 'f':
00515                   hsec = true;
00516                   isec = asDouble(itr->second);
00517                   break;
00518 
00519                case 'c':
00520                   hzcount29 = true;
00521                   izcount29 = asInt(itr->second);
00522                   break;
00523 
00524                case 'E':
00525                   hepoch = true;
00526                   iepoch = asInt(itr->second);
00527                   break;
00528 
00529                default:
00530                      // do nothing
00531                   break;
00532 
00533             };
00534          }
00535 
00536 
00537             // We'll copy this time to 't' after all of the processing.
00538          CommonTime ct;
00539          
00540             // Go through all of the types in order of least precise to most
00541             // precise.
00542          if( hepoch ) 
00543          {
00544             GPSWeekSecond tt(ct);
00545             tt.setEpoch( iepoch );
00546             ct = tt.convertToCommonTime();
00547          }
00548          
00549          if( hyear )
00550          {
00551             YDSTime tt(ct);
00552             tt.year = iyear;
00553             ct = tt.convertToCommonTime();
00554          }
00555  
00556          if( hmonth )
00557          {
00558             CivilTime tt(ct);
00559             tt.month = imonth;
00560             ct = tt.convertToCommonTime();
00561          }
00562 
00563          if( hfullweek )
00564          {
00565             GPSWeekSecond tt(ct);
00566             tt.week = ifullweek;
00567             ct = tt.convertToCommonTime();
00568          }
00569          
00570          if( hweek )
00571          {
00572             GPSWeekSecond tt(ct);
00573             tt.setWeek10( iweek );
00574             ct = tt.convertToCommonTime();
00575          }
00576          
00577          if( hdow )
00578          {
00579             GPSWeekSecond tt(ct);
00580             tt.sow = static_cast<double>(idow) * SEC_PER_DAY;
00581             ct = tt.convertToCommonTime();
00582          }
00583          
00584          if( hday )
00585          {
00586             CivilTime tt(ct);
00587             tt.day = iday;
00588             ct = tt.convertToCommonTime();
00589          }
00590          
00591          if( hdoy )
00592          {
00593             YDSTime tt(ct);
00594             tt.doy = idoy;
00595             ct = tt.convertToCommonTime();
00596          }
00597          
00598          if( hzcount29 )
00599          {
00600             GPSWeekZcount tt(ct);
00601             tt.setZcount29( izcount29 );
00602             ct = tt.convertToCommonTime(); 
00603          }
00604 
00605          if( hzcount )
00606          {
00607             GPSWeekZcount tt(ct);
00608             tt.zcount = izcount;
00609             ct = tt.convertToCommonTime();
00610          }
00611 
00612          if( hhour )
00613          {
00614             CivilTime tt(ct);
00615             tt.hour = ihour;
00616             ct = tt.convertToCommonTime();
00617          }
00618 
00619          if( hmin )
00620          {
00621             CivilTime tt(ct);
00622             tt.minute = imin;
00623             ct = tt.convertToCommonTime();
00624          }
00625          
00626          if( hsow )
00627          {
00628             GPSWeekSecond tt(ct);
00629             tt.sow = isow;
00630             ct = tt.convertToCommonTime();
00631          }
00632          
00633          if( hsod )
00634          {
00635             YDSTime tt(ct);
00636             tt.sod = isod;
00637             ct = tt.convertToCommonTime();
00638          }
00639 
00640          if( hsec )
00641          {
00642             CivilTime tt(ct);
00643             tt.second = isec;
00644             ct = tt.convertToCommonTime();
00645          }
00646          
00647          t = ct;
00648       }
00649       catch( gpstk::StringUtils::StringException& se )
00650       {
00651          GPSTK_RETHROW( se );
00652       }
00653    }   
00654 
00655 } // namespace gpstk

Generated on Mon May 20 03:31:13 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1