DayTime.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: DayTime.hpp 2952 2011-10-28 14:53:20Z yanweignss $"
00002 
00003 
00004 
00010 #ifndef GPSTK_DAYTIME_HPP
00011 #define GPSTK_DAYTIME_HPP
00012 
00013 //============================================================================
00014 //
00015 //  This file is part of GPSTk, the GPS Toolkit.
00016 //
00017 //  The GPSTk is free software; you can redistribute it and/or modify
00018 //  it under the terms of the GNU Lesser General Public License as published
00019 //  by the Free Software Foundation; either version 2.1 of the License, or
00020 //  any later version.
00021 //
00022 //  The GPSTk is distributed in the hope that it will be useful,
00023 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 //  GNU Lesser General Public License for more details.
00026 //
00027 //  You should have received a copy of the GNU Lesser General Public
00028 //  License along with GPSTk; if not, write to the Free Software Foundation,
00029 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00030 //  
00031 //  Copyright 2004, The University of Texas at Austin
00032 //
00033 //============================================================================
00034 
00035 //============================================================================
00036 //
00037 //This software developed by Applied Research Laboratories at the University of
00038 //Texas at Austin, under contract to an agency or agencies within the U.S. 
00039 //Department of Defense. The U.S. Government retains all rights to use,
00040 //duplicate, distribute, disclose, or release this software. 
00041 //
00042 //Pursuant to DoD Directive 523024 
00043 //
00044 // DISTRIBUTION STATEMENT A: This software has been approved for public 
00045 //                           release, distribution is unlimited.
00046 //
00047 //=============================================================================
00048 
00049 
00050 
00051 
00052 
00053 
00054 #include <string>
00055 #if defined _MSC_VER
00056 #ifndef _WINSOCKAPI_
00057 // timeval is defined in winsock.h, which we don't want to include
00058 // because it breaks lots of this code
00059 #ifndef timeval
00060 struct timeval {
00061    long    tv_sec;         /* seconds */
00062    long    tv_usec;        /* and microseconds */
00063 };
00064 #endif   // #ifndef timeval
00065 #endif   // #ifndef _WINSOCKAPI_
00066 #else
00067 #include <sys/time.h>
00068 #endif
00069 
00070 #include "MathBase.hpp"
00071 #include "Exception.hpp"
00072 #include "StringUtils.hpp"
00073 #include "GPSZcount.hpp"
00074 #include "CommonTime.hpp"
00075 
00076 namespace gpstk
00077 {
00080 
00154    class DayTime
00155    {
00156    public:
00157          // ----------- Part  1: exceptions and constants --------------
00162       NEW_EXCEPTION_CLASS(DayTimeException, gpstk::Exception);
00163 
00168       NEW_EXCEPTION_CLASS(FormatException, gpstk::Exception);
00169 
00171       enum TimeFrame
00172       {
00173          Unknown,    
00174          UTC,        
00175          LocalSystem,
00176          GPS_Tx,     
00177          GPS_Rx,     
00178             // (e.g., rx data if clock bias is applied)
00179          GPS_SV,     
00180          GPS_Receiver
00181       };
00182 
00190 
00193       static const long FACTOR;
00194 
00196       static const double JD_TO_MJD;
00198       static const long MJD_JDAY;
00200       static const long GPS_EPOCH_JDAY;
00202       static const long GPS_EPOCH_MJD;
00204       static const long UNIX_MJD;
00206       static const long HALFWEEK;
00208       static const long FULLWEEK;
00210       static const long SEC_DAY;
00212       static const long MS_PER_DAY;
00213 
00215       static const double ONE_NSEC_TOLERANCE;
00217       static const double ONE_USEC_TOLERANCE;
00219       static const double ONE_MSEC_TOLERANCE;
00221       static const double ONE_SEC_TOLERANCE;
00223       static const double ONE_MIN_TOLERANCE;
00225       static const double ONE_HOUR_TOLERANCE;
00226       
00228       static double DAYTIME_TOLERANCE;
00229 
00232       static const long BEGIN_LIMIT_JDAY;
00235       static const long END_LIMIT_JDAY;
00237       static const DayTime BEGINNING_OF_TIME;
00239       static const DayTime END_OF_TIME;
00240 
00243       static bool DAYTIME_TEST_VALID;
00244 
00246       
00247          // ----------- Part  2: member functions: tolerance ------------
00248          //
00250       static double setDayTimeTolerance(const double tol)
00251          throw()
00252       { DAYTIME_TOLERANCE = tol; return DAYTIME_TOLERANCE; }
00253 
00255       static double getDayTimeTolerance() 
00256          throw()
00257       { return DAYTIME_TOLERANCE; }
00258    
00266       DayTime& setTolerance(const double tol) throw();
00267 
00272       double getTolerance() throw()
00273       { return tolerance; }
00274 
00275 
00276          // ----------- Part  3: member functions: constructors ------------
00277          //
00282       DayTime()
00283          throw(DayTimeException);
00284 
00291       DayTime(short GPSWeek,
00292               double GPSSecond,
00293               TimeFrame f = Unknown)
00294          throw(DayTimeException);
00295 
00305       DayTime(short GPSWeek,
00306               double GPSSecond,
00307               short year, 
00308               TimeFrame f = Unknown)
00309          throw(DayTimeException);
00310 
00320       DayTime(short GPSWeek, 
00321               long zcount,
00322               short year,
00323               TimeFrame f = Unknown)
00324          throw(DayTimeException);
00325 
00334       DayTime(unsigned long fullZcount, 
00335               TimeFrame f = Unknown)
00336          throw(DayTimeException);
00337 
00345       DayTime(const GPSZcount& z,
00346               TimeFrame f = Unknown)
00347          throw(DayTimeException);
00348 
00354       DayTime(const CommonTime& c,
00355               TimeFrame f = Unknown)
00356          throw(DayTimeException);
00357 
00372       DayTime(short year,
00373               short month,
00374               short day,
00375               short hour,
00376               short minute,
00377               double second,
00378               TimeFrame f = Unknown)
00379          throw(DayTimeException);
00380 
00387       DayTime(long double MJD, 
00388               TimeFrame f = Unknown)
00389          throw(DayTimeException);
00390 
00397       DayTime(double MJD, 
00398               TimeFrame f = Unknown)
00399          throw(DayTimeException);
00400 
00408       DayTime(short year,
00409               short doy,
00410               double sod, 
00411               TimeFrame f = Unknown)
00412          throw(DayTimeException);
00413 
00419       DayTime(const struct timeval& t, 
00420               TimeFrame f = Unknown)
00421          throw(DayTimeException);
00422 
00424       ~DayTime()
00425          throw()
00426       {}
00427 
00428          // --------- Part  4: member functions: assignment and copy ----------
00429          //
00431       DayTime(const DayTime &right)
00432          throw(DayTimeException);
00433 
00435       DayTime& operator=(const DayTime& right)
00436          throw();
00437 
00438          // ----------- Part  5: member functions: arithmetic ------------
00439          //
00445       double operator-(const DayTime& right) const
00446          throw();
00447 
00453       DayTime operator+(double sec) const
00454          throw();
00455 
00461       DayTime operator-(double sec) const
00462          throw();
00463 
00469       DayTime& operator+=(double sec)
00470          throw(DayTimeException);
00471 
00477       DayTime& operator-=(double sec)
00478          throw(DayTimeException);
00479 
00485       DayTime& addSeconds(double seconds)
00486          throw(DayTimeException);
00487 
00493       DayTime& addSeconds(long seconds)
00494          throw(DayTimeException);
00495 
00501       DayTime& addMilliSeconds(long msec)
00502          throw(DayTimeException);
00503 
00509       DayTime& addMicroSeconds(long usec)
00510          throw(DayTimeException);
00511 
00512          // ----------- Part  6: member functions: comparisons ------------
00513          //
00517       bool operator==(const DayTime &right) const
00518          throw();
00520       bool operator!=(const DayTime &right) const
00521          throw();
00523       bool operator<(const DayTime &right) const
00524          throw();
00526       bool operator>(const DayTime &right) const
00527          throw();
00529       bool operator<=(const DayTime &right) const
00530          throw();
00532       bool operator>=(const DayTime &right) const
00533          throw();
00534 
00535          // ----------- Part  7: member functions: time frame ------------
00536          //
00544       DayTime& setAllButTimeFrame(const DayTime& right) 
00545          throw(DayTimeException);
00546       
00548       DayTime& setTimeFrame(TimeFrame f)
00549          throw()
00550       { timeFrame = f ; return *this ; }
00551       
00553       TimeFrame getTimeFrame() const 
00554          throw() 
00555       { return timeFrame ; }
00556 
00557          // ----------- Part  8: member functions: get --------------
00558          // 
00559          // These routines retrieve elements of day, time or both.
00560          //
00562       double JD() const
00563          throw();
00564 
00568       double MJD() const
00569          throw();
00570 
00572       short year() const
00573          throw();
00574 
00576       short month() const
00577          throw();
00578 
00580       short day() const
00581          throw();
00582 
00584       short dayOfWeek() const
00585          throw();
00586 
00588       void getYMD(int& yy, int& mm, int& dd) const 
00589          throw() 
00590       { convertJDtoCalendar(jday, yy, mm, dd) ; }
00591 
00593       short hour() const
00594          throw();
00595 
00597       short minute() const
00598          throw();
00599 
00601       double second() const
00602          throw();
00603 
00605       double secOfDay() const
00606          throw() 
00607       { return (double(mSod) + mSec) / FACTOR ; }
00608 
00610       short GPS10bitweek() const
00611          throw();
00612 
00614       long GPSzcount() const
00615          throw();
00616 
00618       long GPSzcountFloor() const
00619          throw();
00620 
00622       double GPSsecond() const
00623          throw() 
00624       { return GPSsow(); }
00625 
00627       double GPSsow() const
00628          throw();
00629 
00631       short GPSday() const
00632          throw() 
00633       { return dayOfWeek(); }
00634 
00636       short GPSfullweek() const
00637          throw();
00638 
00640       short GPSyear() const
00641          throw() 
00642       { return year(); }
00643 
00645       short DOYyear() const
00646          throw() 
00647       { return year(); }
00648 
00650       short DOYday() const
00651          throw() 
00652       { return DOY(); }
00653 
00655       short DOY() const
00656          throw();
00657 
00659       double DOYsecond() const
00660          throw() 
00661       { return secOfDay(); }
00662 
00666       double MJDdate() const
00667          throw() 
00668       { return double(getMJDasLongDouble()) ; }
00669 
00673       long double getMJDasLongDouble() const
00674          throw();
00675 
00677       struct timeval unixTime() const
00678          throw(DayTimeException);
00679 
00685       unsigned long fullZcount() const
00686          throw();
00687 
00689       unsigned long fullZcountFloor() const
00690          throw();
00691 
00695       operator GPSZcount() const
00696          throw(DayTimeException) ;
00697 
00701       operator CommonTime() const
00702          throw(DayTimeException);
00703 
00704          // ----------- Part  9: member functions: set ------------
00705          //
00718       DayTime& setYMDHMS(short year,
00719                          short month,
00720                          short day, 
00721                          short hour, 
00722                          short min,
00723                          double sec, 
00724                          TimeFrame f = Unknown)
00725          throw(DayTimeException);
00726 
00737       DayTime& setGPS(short week,
00738                       double sow, 
00739                       TimeFrame f = Unknown)
00740          throw(DayTimeException);
00741 
00752       DayTime& setGPS(short week, 
00753                       long zcount, 
00754                       TimeFrame f = Unknown)
00755          throw(DayTimeException);
00756 
00768       DayTime& setGPS(short week, 
00769                       long zcount,
00770                       short year, 
00771                       TimeFrame f = Unknown)
00772          throw(DayTimeException);
00773 
00785       DayTime& setGPS(short week, 
00786                       double sow,
00787                       short year, 
00788                       TimeFrame f = Unknown)
00789          throw(DayTimeException);
00790 
00800       DayTime& setGPS(unsigned long Zcount, 
00801                       TimeFrame f = Unknown)
00802          throw(DayTimeException);
00803 
00811       DayTime& setGPSfullweek(short fullweek,
00812                               double sow,
00813                               TimeFrame f = Unknown)
00814          throw(DayTimeException);
00815 
00823       DayTime& setGPSfullweek(short fullweek,
00824                               long zcount, 
00825                               TimeFrame f = Unknown)
00826          throw(DayTimeException)
00827       { return setGPSfullweek(fullweek, double(zcount) * 1.5, f) ; }
00828 
00837       DayTime& setGPSZcount(const GPSZcount& z,
00838                             TimeFrame f = Unknown)
00839          throw(DayTimeException);
00840 
00847       DayTime& setCommonTime(const CommonTime& c,
00848                              TimeFrame f = Unknown)
00849          throw();
00850 
00859       DayTime& setYDoySod(short year,
00860                           short day_of_year, 
00861                           double sec_of_day, 
00862                           TimeFrame f = Unknown)
00863          throw(DayTimeException);
00864 
00872       DayTime& setMJD(long double mjd,
00873                       TimeFrame f = Unknown)
00874          throw(DayTimeException);
00875 
00877       DayTime& setMJDdate(long double mjd, 
00878                           TimeFrame f = Unknown)
00879          throw(DayTimeException) 
00880       { return setMJD(mjd, f); }
00881 
00883       DayTime& setMJD(double mjd, 
00884                       TimeFrame f = Unknown)
00885          throw(DayTimeException)
00886       { return setMJD((long double)(mjd), f) ; }
00887 
00889       DayTime& setMJDdate(double mjd, 
00890                           TimeFrame f = Unknown)
00891          throw(DayTimeException)
00892       { return setMJD((long double)(mjd), f) ; }
00893 
00903       DayTime& setUnix(const struct timeval& t, 
00904                        TimeFrame f = Unknown)
00905          throw(DayTimeException);
00906 
00916       DayTime& setANSI(const time_t& t,
00917                        TimeFrame f = Unknown)
00918          throw(DayTimeException);
00919 
00928       DayTime& setSystemTime()
00929          throw(DayTimeException);
00930 
00934       DayTime& setLocalTime()
00935          throw(DayTimeException);
00936 
00945       DayTime& setYMD(int yy,
00946                       int mm,
00947                       int dd,
00948                       TimeFrame f = Unknown)
00949          throw(DayTimeException);
00950 
00959       DayTime& setHMS(int hh,
00960                       int mm,
00961                       double sec, 
00962                       TimeFrame f = Unknown)
00963          throw(DayTimeException);
00964 
00969       DayTime& setSecOfDay(double sod,
00970                            TimeFrame f = Unknown)
00971          throw(DayTimeException);
00972 
00977       DayTime& setYDoy(int yy,
00978                        int doy,
00979                        TimeFrame f = Unknown)
00980          throw(DayTimeException);
00981 
00982          // ----------- Part 10: member functions: setToString, printf --------
00983          //
00984          // FIX someone figure out how to make the table below show up
00985          // nice in doxygen.
01036       DayTime& setToString(const std::string& str, 
01037                            const std::string& fmt)
01038          throw(DayTimeException, FormatException,
01039                gpstk::StringUtils::StringException);
01040 
01041 
01042          // if you can see this, ignore the \'s below, as they are for
01043          // the nasty html-ifying of doxygen.  Browsers try and
01044          // interpret the % and they get all messed up.
01090       std::string printf(const char *fmt) const
01091          throw(gpstk::StringUtils::StringException);
01092 
01094       std::string printf(const std::string& fmt) const
01095          throw(gpstk::StringUtils::StringException) 
01096       { return printf(fmt.c_str()) ; }
01097 
01099       std::string toString(int decimals=0,char datesep='-', char timesep=':') const
01100          throw(gpstk::StringUtils::StringException);
01101       
01102 
01104       std::string asString() const
01105          throw(gpstk::StringUtils::StringException);
01106 
01108       void dump(std::ostream& s) const
01109          throw(DayTimeException);
01110 
01112       void update() { setSystemTime(); }
01113 
01115       double elapsed() const {return ( DayTime() - (*this) );}
01116 
01118       bool isElapsed(double interval) const 
01119       { return elapsed()>interval?true:false; }
01120 
01121 
01122          // ----------- Part 11: functions: fundamental conversions ----------
01123          // 
01137       static void convertJDtoCalendar(long jd, 
01138                                       int& iyear, 
01139                                       int& imonth, 
01140                                       int& iday)
01141          throw();
01142 
01156       static long convertCalendarToJD(int iyear, 
01157                                       int imonth,
01158                                       int iday) 
01159          throw();
01160 
01167       static void convertSODtoTime(double sod,
01168                                    int& hh, 
01169                                    int& mm, 
01170                                    double& sec) 
01171          throw();
01172 
01179       static double convertTimeToSOD(int hh,
01180                                      int mm, 
01181                                      double sec) 
01182          throw();
01183 
01184          // ----------- Part 12: private functions and member data ----------
01185          //
01186    private:
01188       void init() 
01189          throw();
01190 
01192       DayTime(long jd,
01193               long sod,
01194               double ms,
01195               double tol, 
01196               TimeFrame f = Unknown)
01197          throw() 
01198             : jday(jd), mSod(sod), mSec(ms), tolerance(tol), timeFrame(f) 
01199       {}
01200 
01208       void addLongDeltaTime(long ldd,
01209                             long lds, 
01210                             double ds)
01211          throw(DayTimeException);
01212          
01215       void realignInternals(void)
01216          throw();
01217 
01219       long jday; 
01220           
01222       long mSod;           
01223 
01225       double mSec;         
01226 
01228       double tolerance;    
01229 
01231       TimeFrame timeFrame; 
01232 
01233    };   // end class DayTime
01234 
01235       // ----------- Part 13: operator<< -----------
01236       //
01243    std::ostream& operator<<( std::ostream& s,
01244                              const gpstk::DayTime& t );
01245 
01247 
01248 }  // namespace gpstk
01249 
01250 #endif   // GPSTK_DAYTIME_HPP

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