Time.h

Go to the documentation of this file.
00001 
00002 
00008 /* \todo Finish implementing the rest of the time representatins.
00009 */
00011 
00012 #ifndef __SSF_TIME_H__
00013 #define __SSF_TIME_H__
00014 #include "MathUtils.h"
00015 #include <time.h>
00016 #include <sys/time.h>
00017 #include <math.h>
00018 #include <iostream.h>
00019 #include <iomanip.h>
00020 namespace O_SESSAME {
00022 
00037 typedef double ssfSeconds;      /*< Simulation representation of seconds \ingroup Utilities*/
00038 typedef double ssfJulianDate;   /*< Simulation representation of Julian Dates \ingroup Utilities */
00039 
00040 #define TIME_PRECISION 15       /*< Precision of time output when outputting seconds */
00041 
00043 class ssfTime;
00044 
00061 class ssfTime
00062 {
00063 public:
00065     ssfTime()                           
00066         {Set(ssfSeconds(0)); SetEpoch(ssfSeconds(0));};
00070     ssfTime(tm _Time)                   
00071         {Set(_Time);};
00081     ssfTime(timeval _Time)                      
00082         {Set(_Time);};
00086     ssfTime(ssfSeconds _Time)           
00087         {Set(_Time);};
00088     
00097     ssfTime(int year, int month, int day, int hour, int minute, double seconds)         
00098         {Set(year, month, day, hour, minute, seconds);};
00099     
00103     void Set(tm _newTime)               
00104         {Set(static_cast<ssfSeconds>(timegm(&_newTime)));};
00105     
00115     void Set(timeval _Time)                     
00116         {Set(_Time.tv_sec + _Time.tv_usec/1000000.);};
00117         
00122     void Set(ssfSeconds _newTime)       
00123         {m_StoredTime = _newTime;};
00124     
00128     void SetJulianDate(ssfJulianDate _newJD);
00129         
00133     void SetEpochJulianDate(ssfJulianDate _newJD);
00134     
00143     void Set(int year, int month, int day, int hour, int minute, double seconds);
00144         
00148     void SetTimeAfterEpoch(ssfSeconds _timeAfter)       
00149         {Set(m_EpochTime + _timeAfter);}; 
00150     
00154     void SetEpoch(ssfSeconds _newEpochTime)     
00155         {m_EpochTime = _newEpochTime;};
00156     
00160     void SetEpoch(tm _newEpochTime)     
00161         {SetEpoch(static_cast<ssfSeconds>(timegm(&_newEpochTime)));};
00162         
00172     void SetEpoch(const timeval &_Time)                         
00173         {SetEpoch(_Time.tv_sec + _Time.tv_usec/1000000.);};
00174     
00183     void SetEpoch(int year, int month, int day, int hour, int minute, double seconds);
00184     
00188     ssfSeconds GetSeconds()  const      
00189         {return m_StoredTime;};
00190 
00213     tm GetDateTime()  const     
00214         {time_t t = static_cast<time_t>(floor(m_StoredTime));return *gmtime(&t);};
00215     
00239     tm GetEpochDateTime()  const        
00240         {time_t t = static_cast<time_t>(floor(m_EpochTime));return *gmtime(&t);};
00241         
00245     ssfSeconds GetEpoch() const 
00246         {return m_EpochTime;};
00247         
00248         
00252     ssfJulianDate GetJulianDate() const;
00253     
00257     ssfJulianDate GetEpochJulianDate() const;
00258     
00259         
00263     Angle GetGreenwichMeanSiderealTime() const;
00264     
00271     Angle GetEpochGreenwichMeanSiderealTime() const;
00272 
00279 //    tm GetEpoch() const 
00280   //      {return *gmtime(&m_EpochTime);};
00281     
00285     ssfSeconds SecondsSinceEpoch() const
00286         {return static_cast<ssfSeconds>(m_StoredTime - m_EpochTime);};
00287     
00292     ssfTime operator+ (const int& rhsSeconds) const
00293         {return operator+(static_cast<ssfSeconds>(rhsSeconds));};
00294     
00299     ssfTime operator+= (const int& rhsSeconds)
00300         {return operator+=(static_cast<ssfSeconds>(rhsSeconds));};       
00301 
00306     ssfTime operator+ (const long& rhsSeconds) const
00307         {return operator+(static_cast<ssfSeconds>(rhsSeconds));};
00308 
00313     ssfTime operator+= (const long& rhsSeconds)
00314         {return operator+=(static_cast<ssfSeconds>(rhsSeconds));}; 
00315     
00320     ssfTime operator+ (const ssfSeconds& rhsSeconds) const
00321         {return ssfTime(m_StoredTime + rhsSeconds);};
00322     
00327     ssfTime operator+= (const ssfSeconds& rhsSeconds)
00328         {m_StoredTime += rhsSeconds; return *this;}; 
00329         
00334     ssfSeconds operator- (const ssfTime &rhs) const
00335         {return (m_StoredTime - rhs.GetSeconds());};
00336         
00341     bool operator> (const ssfTime &rhs) const
00342         {return (m_StoredTime > rhs.GetSeconds());};
00343         
00348     bool operator>= (const ssfTime &rhs) const
00349         {return (m_StoredTime >= rhs.GetSeconds());};
00350 
00355     bool operator< (const ssfTime &rhs) const
00356         {return (m_StoredTime < rhs.GetSeconds());};
00357         
00362     bool operator<= (const ssfTime &rhs) const
00363         {return (m_StoredTime <= rhs.GetSeconds());};
00364                 
00370     bool operator== (const ssfTime &rhs) const
00371         {return (m_StoredTime == rhs.GetSeconds());};
00372    
00378     bool operator!= (const ssfTime &rhs) const
00379         {return (m_StoredTime != rhs.GetSeconds());};
00380                 
00383     friend ostream & operator << (ostream& s, ssfTime& t);
00384 private:
00385     ssfSeconds m_StoredTime; 
00386     ssfSeconds m_EpochTime;  
00388 };
00389 
00394     static const ssfTime c_GreenwichSiderealEpochTime = ssfTime(1970,1,1,0,0,0);
00396     static const Angle c_GreenwichSiderealTimeAtEpoch = 1.74933340; 
00397     
00403 inline ssfSeconds Now()                 
00404         {timeval tv; gettimeofday(&tv, NULL); return (tv.tv_sec + tv.tv_usec/1000000.);}
00405        // {return time((time_t *)0);}; 
00406         
00407 
00425 static ssfTime tickTime(-1); /*< global variable used to store the initial tick() time */
00426 static ssfTime tockTime(-1); /*< global variable used to store the ending tock() time */
00427 
00428 inline void tick()
00429 {
00430     tickTime.Set(Now());
00431     return;
00432 }
00438 inline ssfSeconds tock()
00439 {
00440     if(tickTime.GetSeconds() > -1)
00441     {
00442         tockTime.Set(Now());
00443         return tockTime-tickTime;
00444     }
00445     else
00446         return -1;
00447 }
00448 
00453 void DayofYear2YMD(int _dayOfYear, int _year, int &_month, int &_day);
00454 
00455 } // close namespace O_SESSAME
00456 
00457 #endif 
00458 
00459 // Do not change the comments below - they will be added automatically by CVS
00460 /*****************************************************************************
00461 *       $Log: Time.h,v $
00462 *       Revision 1.13  2003/06/11 14:22:34  nilspace
00463 *       Added comments to GetDateTime() and GetEpochDateTime()
00464 *       
00465 *       Revision 1.12  2003/06/10 01:11:46  nilspace
00466 *       Fixed Time to build correctly (changed Greenwich epoch time to object instead of pointer)
00467 *       
00468 *       Revision 1.11  2003/06/09 19:41:30  nilspace
00469 *       Added SetEpoch(year,month,day,hour,minute,second)
00470 *       
00471 *       Revision 1.10  2003/06/06 00:34:47  nilspace
00472 *       ?
00473 *       
00474 *       Revision 1.9  2003/05/21 03:57:52  nilspace
00475 *       Added GetEpochJulianDate and comments to JulianDate conversion function.
00476 *       
00477 *       Revision 1.8  2003/05/19 21:19:49  nilspace
00478 *       Made calls to overloaded Set() and SetEpoch() functions recursive calls to Set(ssfSeconds) and SetEpoch(ssfSeconds)
00479 *       
00480 *       Revision 1.7  2003/05/19 19:21:37  nilspace
00481 *       Added #include<sys/time.h>
00482 *       
00483 *       Revision 1.6  2003/05/16 14:01:24  nilspace
00484 *       Called gettimeofday(timeval*) to have Now(), and tick/tock return seconds with subsecond decimal parts.
00485 *       
00486 *       Revision 1.5  2003/05/09 23:44:29  nilspace
00487 *       Added operator!=()
00488 *       
00489 *       Revision 1.4  2003/04/30 16:32:48  nilspace
00490 *       Added operator== to ssfTime
00491 *       
00492 *       Revision 1.3  2003/04/27 22:04:34  nilspace
00493 *       Created the namespace O_SESSAME.
00494 *       
00495 *       Revision 1.2  2003/04/25 13:45:56  nilspace
00496 *       const'd Get() functions.
00497 *       
00498 *       Revision 1.1  2003/04/23 14:14:45  nilspace
00499 *       New simulation time.
00500 *       
00501 *
00502 ******************************************************************************/

Generated on Wed Aug 6 12:58:54 2003 for Open-Sessame Framework by doxygen1.3