TimeString.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: TimeString.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 "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                   break;
00221 
00222                case 'U':
00223                   hunixsec = true;
00224                   break;
00225 
00226                case 'u':
00227                   hunixusec = true;
00228                   break;
00229                   
00230                case 'c':
00231                   hzcount29 = true;
00232                   break;
00233 
00234                case 'C':
00235                   hzcount32 = true;
00236                   break;
00237 
00238                case 'J':
00239                   hjulian = true;
00240                   break;
00241                   
00242                case 'K':
00243                   hansi = true;
00244                   break;
00245                   
00246                case 'E':
00247                   hepoch = true;
00248                   break;
00249 
00250                default:
00251                {
00252                      // do nothing
00253                }
00254                break;
00255 
00256             };
00257          }
00258 
00259          if( hyear )
00260          {
00261             if( hmonth && hday )
00262             {
00263                CivilTime tt;
00264                tt.setFromInfo( info );
00265                if( hsod )
00266                {
00267                   convertSODtoTime( asDouble( info['s'] ), 
00268                                     tt.hour, tt.minute, tt.second );
00269                }
00270                t = tt.convertToCommonTime();
00271                return;
00272             }
00273             else  // use YDSTime as default
00274             {
00275                YDSTime tt;
00276                tt.setFromInfo( info );
00277                if( hhour && hmin && hsec )
00278                {
00279                   tt.sod = convertTimeToSOD( asInt( info['H'] ), 
00280                                              asInt( info['M'] ), 
00281                                              asDouble( info['S'] ) );
00282                }
00283                t = tt.convertToCommonTime();
00284                return;
00285             }
00286 
00287          } // end of if( hyear )
00288 
00289          if( hzcount32 ||
00290              (hfullweek && hzcount) ||
00291              (hepoch && (hzcount29 || 
00292                          (hweek && hzcount))) )
00293          {
00294             GPSWeekZcount tt;
00295             tt.setFromInfo( info );
00296             t = tt.convertToCommonTime();
00297             return;
00298          }
00299 
00300          if ( ((hepoch && hweek) || hfullweek) )
00301          {
00302             GPSWeekSecond tt;
00303             tt.setFromInfo( info );
00304             if( hdow && !hsow )
00305             {
00306                tt.sow = asInt( info['w'] ) * SEC_PER_DAY;
00307                if( hsod )
00308                {
00309                   tt.sow += asDouble( info['s'] );
00310                }
00311                else if( hhour && hmin && hsec )
00312                {
00313                   tt.sow += convertTimeToSOD( asInt( info['H'] ), 
00314                                               asInt( info['M'] ), 
00315                                               asDouble( info['S'] ) );
00316                }
00317             }
00318             t = tt.convertToCommonTime();
00319             return;
00320          }
00321 
00322          if( hmjd )
00323          {
00324             MJD tt;
00325             tt.setFromInfo( info );
00326             t = tt.convertToCommonTime();
00327             return;
00328          }
00329 
00330          if( hjulian )
00331          {
00332             JulianDate tt;
00333             tt.setFromInfo( info );
00334             t = tt.convertToCommonTime();
00335             return;
00336          }
00337 
00338          if( hansi )
00339          {
00340             ANSITime tt;
00341             tt.setFromInfo( info );
00342             t = tt.convertToCommonTime();
00343             return;
00344          } 
00345          
00346          if( hunixsec || hunixusec )
00347          {
00348             UnixTime tt;
00349             tt.setFromInfo( info );
00350             t = tt.convertToCommonTime();
00351             return;
00352          }
00353 
00354          InvalidRequest ir("Incomplete time specification for readTime");
00355          GPSTK_THROW( ir );
00356       }
00357       catch( gpstk::StringUtils::StringException& se )
00358       {
00359          GPSTK_RETHROW( se );
00360       }
00361    }   
00362 
00363    void mixedScanTime( CommonTime& t,
00364                        const std::string& str,
00365                        const std::string& fmt )
00366       throw( gpstk::InvalidRequest,
00367              gpstk::StringUtils::StringException )
00368    {
00369       try
00370       {
00371          using namespace gpstk::StringUtils;
00372 
00373             // Get the mapping of character (from fmt) to value (from str).
00374          TimeTag::IdToValue info;
00375          TimeTag::getInfo( str, fmt, info );
00376          
00377             // These indicate which information has been found.
00378          bool hsow( false ), hweek( false ), hfullweek( false ),
00379             hdow( false ), hyear( false ), hmonth( false ), hday( false ),
00380             hzcount( false ), hdoy( false ), hzcount29( false ), 
00381             hhour( false ), hmin( false ), hsec( false ),
00382             hsod( false ), hepoch( false ), hunixsec( false ),
00383             hunixusec( false );
00384 
00385             // MJD, Julian Date, ANSI time, Unix time, and 32-bit Zcounts
00386             // are treated as stand-alone types and are not mixed with others
00387             // if detected.
00388          
00389             // These variables will hold the values for use later.
00390          double isow, isod, isec;
00391          int iweek, ifullweek, idow, iyear, imonth, iday, izcount, idoy,
00392             izcount29, ihour, imin, iepoch, iunixsec, iunixusec;
00393          
00394          for( TimeTag::IdToValue::iterator itr = info.begin();
00395               itr != info.end(); itr++ )
00396          {
00397             switch( itr->first )
00398             {
00399                case 'Q':
00400                   t = MJD( asLongDouble(itr->second) );
00401                   return;
00402 
00403                case 'J':
00404                   t = JulianDate( asLongDouble(itr->second) );
00405                   return;
00406                   
00407                case 'C':
00408                   t = GPSWeekZcount().setZcount32( asInt(itr->second) );
00409                   return;
00410 
00411                case 'K':
00412                   t = ANSITime( asInt(itr->second) );
00413                   return;
00414                   
00415                case 'U':
00416                case 'u':
00417                {
00418                   UnixTime tt;
00419                   tt.setFromInfo( info );
00420                   t = tt.convertToCommonTime();
00421                   return;
00422                }
00423                break;
00424 
00425                case 'Z':
00426                   hzcount = true;
00427                   izcount = asInt(itr->second);
00428                   break;
00429 
00430                case 's':
00431                   hsod = true;
00432                   isod = asDouble(itr->second);
00433                   break;
00434 
00435                case 'g':
00436                   hsow = true;
00437                   isow = asDouble(itr->second);
00438                   break;
00439 
00440                case 'w':
00441                   idow = asInt(itr->second);
00442                   hdow = true;
00443                   break;
00444 
00445                case 'G':
00446                   hweek = true;
00447                   iweek = asInt(itr->second);
00448                   break;
00449 
00450                case 'F':
00451                   hfullweek = true;
00452                   ifullweek = asInt(itr->second);
00453                   break;
00454 
00455                case 'j':
00456                   hdoy = true;
00457                   idoy = asInt(itr->second);
00458                   break;
00459 
00460                case 'b':
00461                case 'B':
00462                   hmonth = true;
00463                   imonth = asInt(itr->second);
00464                   break;
00465 
00466                case 'Y':
00467                case 'y':
00468                   hyear = true;
00469                   iyear = asInt(itr->second);
00470                   break;
00471 
00472                case 'a':
00473                case 'A':
00474                {
00475                   hdow = true;
00476                   std::string thisDay = firstWord( itr->second );
00477                   lowerCase(thisDay);
00478                   if (isLike(thisDay, "sun.*")) idow = 0;
00479                   else if (isLike(thisDay, "mon.*")) idow = 1;
00480                   else if (isLike(thisDay, "tue.*")) idow = 2;
00481                   else if (isLike(thisDay, "wed.*")) idow = 3;
00482                   else if (isLike(thisDay, "thu.*")) idow = 4;
00483                   else if (isLike(thisDay, "fri.*")) idow = 5;
00484                   else if (isLike(thisDay, "sat.*")) idow = 6;
00485                }
00486                break;
00487                   
00488                case 'm':
00489                   hmonth = true;
00490                   imonth = asInt(itr->second);
00491                   break;
00492 
00493                case 'd':
00494                   hday = true;
00495                   iday = asInt(itr->second);
00496                   break;
00497 
00498                case 'H':
00499                   hhour = true;
00500                   ihour = asInt(itr->second);
00501                   break;
00502 
00503                case 'M':
00504                   hmin = true;
00505                   imin = asInt(itr->second);
00506                   break;
00507 
00508                case 'S':
00509                   hsec = true;
00510                   isec = asDouble(itr->second);
00511                   break;
00512 
00513                case 'f':
00514                   hsec = true;
00515                   isec = asDouble(itr->second);
00516                   break;
00517 
00518                case 'c':
00519                   hzcount29 = true;
00520                   izcount29 = asInt(itr->second);
00521                   break;
00522 
00523                case 'E':
00524                   hepoch = true;
00525                   iepoch = asInt(itr->second);
00526                   break;
00527 
00528                default:
00529                      // do nothing
00530                   break;
00531 
00532             };
00533          }
00534 
00535 
00536             // We'll copy this time to 't' after all of the processing.
00537          CommonTime ct;
00538          
00539             // Go through all of the types in order of least precise to most
00540             // precise.
00541          if( hepoch ) 
00542          {
00543             GPSWeekSecond tt(ct);
00544             tt.setEpoch( iepoch );
00545             ct = tt.convertToCommonTime();
00546          }
00547          
00548          if( hyear )
00549          {
00550             YDSTime tt(ct);
00551             tt.year = iyear;
00552             ct = tt.convertToCommonTime();
00553          }
00554  
00555          if( hmonth )
00556          {
00557             CivilTime tt(ct);
00558             tt.month = imonth;
00559             ct = tt.convertToCommonTime();
00560          }
00561 
00562          if( hfullweek )
00563          {
00564             GPSWeekSecond tt(ct);
00565             tt.week = ifullweek;
00566             ct = tt.convertToCommonTime();
00567          }
00568          
00569          if( hweek )
00570          {
00571             GPSWeekSecond tt(ct);
00572             tt.setWeek10( iweek );
00573             ct = tt.convertToCommonTime();
00574          }
00575          
00576          if( hdow )
00577          {
00578             GPSWeekSecond tt(ct);
00579             tt.sow = static_cast<double>(idow) * SEC_PER_DAY;
00580             ct = tt.convertToCommonTime();
00581          }
00582          
00583          if( hday )
00584          {
00585             CivilTime tt(ct);
00586             tt.day = iday;
00587             ct = tt.convertToCommonTime();
00588          }
00589          
00590          if( hdoy )
00591          {
00592             YDSTime tt(ct);
00593             tt.doy = idoy;
00594             ct = tt.convertToCommonTime();
00595          }
00596          
00597          if( hzcount29 )
00598          {
00599             GPSWeekZcount tt(ct);
00600             tt.setZcount29( izcount29 );
00601             ct = tt.convertToCommonTime(); 
00602          }
00603 
00604          if( hzcount )
00605          {
00606             GPSWeekZcount tt(ct);
00607             tt.zcount = izcount;
00608             ct = tt.convertToCommonTime();
00609          }
00610 
00611          if( hhour )
00612          {
00613             CivilTime tt(ct);
00614             tt.hour = ihour;
00615             ct = tt.convertToCommonTime();
00616          }
00617 
00618          if( hmin )
00619          {
00620             CivilTime tt(ct);
00621             tt.minute = imin;
00622             ct = tt.convertToCommonTime();
00623          }
00624          
00625          if( hsow )
00626          {
00627             GPSWeekSecond tt(ct);
00628             tt.sow = isow;
00629             ct = tt.convertToCommonTime();
00630          }
00631          
00632          if( hsod )
00633          {
00634             YDSTime tt(ct);
00635             tt.sod = isod;
00636             ct = tt.convertToCommonTime();
00637          }
00638 
00639          if( hsec )
00640          {
00641             CivilTime tt(ct);
00642             tt.second = isec;
00643             ct = tt.convertToCommonTime();
00644          }
00645          
00646          t = ct;
00647       }
00648       catch( gpstk::StringUtils::StringException& se )
00649       {
00650          GPSTK_RETHROW( se );
00651       }
00652    }   
00653 
00654 } // namespace gpstk

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