00001
00002
00008
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;
00038 typedef double ssfJulianDate;
00039
00040 #define TIME_PRECISION 15
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
00280
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
00406
00407
00425 static ssfTime tickTime(-1);
00426 static ssfTime tockTime(-1);
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 }
00456
00457 #endif
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502