Time.cpp

Go to the documentation of this file.
00001 
00002 
00008 /*  
00009 * \todo Finish implementing the rest of the time representatins.
00010 */
00012 
00013 #include "Time.h"
00014 namespace O_SESSAME {
00015 
00016 static int DaysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
00017  
00030 ssfJulianDate JulianDate(const ssfSeconds &_time)
00031 {
00032     time_t t = static_cast<time_t>(floor(_time));
00033     tm DateTime = *gmtime(&t);
00034     ssfJulianDate JD = 0;
00035     double uSec = _time - floor(_time);
00036     JD += 367 * (DateTime.tm_year + 1900);
00037     JD -= floor((7 * ((DateTime.tm_year + 1900) + floor(((DateTime.tm_mon + 1) + 9)/12))) / 4);
00038     JD += floor((275 * (DateTime.tm_mon + 1)) / 9);
00039     JD += DateTime.tm_mday;
00040     JD += 1721013.5;
00041     JD += (((DateTime.tm_sec + uSec)/60.0 + DateTime.tm_min) / 60.0 + DateTime.tm_hour) / 24;
00042     return JD;
00043 }
00044 
00045 void DayofYear2YMD(int _dayOfYear, int _year, int &_month, int &_day)
00046 {       
00047     int total = 0;
00048     // Is it a leap-year?
00049     if (_year % 4 == 0)
00050         DaysInMonth[1] = 29;
00051     for (_month = 0; total + DaysInMonth[_month+1] < _dayOfYear; ++_month)
00052     {
00053         total += DaysInMonth[_month];
00054     }
00055     ++_month;
00056     _day = _dayOfYear - total;
00057     return;
00058 }
00059 
00068 ssfJulianDate ssfTime::GetJulianDate() const
00069 {
00070 /*
00071     time_t t = static_cast<time_t>(floor(m_StoredTime));
00072     tm DateTime = *gmtime(&t);
00073     ssfJulianDate JD = 0;
00074     double uSec = m_StoredTime - floor(m_StoredTime);
00075     JD += 367 * (DateTime.tm_year + 1900);
00076     JD -= floor((7 * ((DateTime.tm_year + 1900) + floor(((DateTime.tm_mon + 1) + 9)/12))) / 4);
00077     JD += floor((275 * (DateTime.tm_mon + 1)) / 9);
00078     JD += DateTime.tm_mday;
00079     JD += 1721013.5;
00080     JD += (((DateTime.tm_sec + uSec)/60.0 + DateTime.tm_min) / 60.0 + DateTime.tm_hour) / 24;
00081     return JD;
00082 */
00083     return JulianDate(m_StoredTime);
00084 }
00085 
00086 ssfJulianDate ssfTime::GetEpochJulianDate() const
00087 {
00088     return JulianDate(m_EpochTime);
00089 }
00090 void ssfTime::Set(int year, int month, int day, int hour, int minute, double seconds)
00091 {
00092     tm DateTime;
00093     DateTime.tm_year = year - 1900;
00094     DateTime.tm_mon = month - 1;
00095     DateTime.tm_mday = day;
00096     DateTime.tm_hour = hour;
00097     DateTime.tm_min = minute;
00098     DateTime.tm_sec = static_cast<int>(floor(seconds));
00099     m_StoredTime = static_cast<ssfSeconds>(timegm(&DateTime));
00100     m_StoredTime += seconds - floor(seconds);
00101 }
00102 
00103 void ssfTime::SetEpoch(int year, int month, int day, int hour, int minute, double seconds)
00104 {
00105     tm DateTime;
00106     DateTime.tm_year = year - 1900;
00107     DateTime.tm_mon = month - 1;
00108     DateTime.tm_mday = day;
00109     DateTime.tm_hour = hour;
00110     DateTime.tm_min = minute;
00111     DateTime.tm_sec = static_cast<int>(floor(seconds));
00112     m_EpochTime = static_cast<ssfSeconds>(timegm(&DateTime));
00113     m_EpochTime += seconds - floor(seconds);
00114 }
00115 
00116 
00117 void ssfTime::SetJulianDate(ssfJulianDate _newJD)
00118 {
00119     // Since the Julian Date is measured in days, while the 
00120     // internal time is stored in seconds since the system epoch,
00121     // subtract the system Julian Date from the specified
00122     // Julian Date and convert to seconds.
00123     m_StoredTime = (_newJD - JulianDate(0)) * 24 * 60 * 60;
00124     return;
00125 }
00126 
00127 void ssfTime::SetEpochJulianDate(ssfJulianDate _newJD)
00128 {
00129     // Since the Julian Date is measured in days, while the 
00130     // internal time is stored in seconds since the system epoch,
00131     // subtract the system Julian Date from the specified
00132     // Julian Date and convert to seconds.
00133     m_EpochTime = (_newJD - JulianDate(0)) * 24 * 60 * 60;
00134     return;
00135 }
00136 
00137 Angle ssfTime::GetGreenwichMeanSiderealTime() const
00138 {
00139     double Tut1 = (this->GetJulianDate() - c_GreenwichSiderealEpochTime.GetJulianDate() - 2451545) / 36525;
00140     return  1.753368560 + 628.3319706889*Tut1 + 6.7707*pow(10,-6)*pow(Tut1,2) - 4.5*pow(10,-10)*pow(Tut1,3);
00141 }
00142     
00143 Angle ssfTime::GetEpochGreenwichMeanSiderealTime() const
00144 {
00145     double Tut1 = (this->GetEpochJulianDate() - c_GreenwichSiderealEpochTime.GetJulianDate() - 2451545) / 36525;
00146     return  1.753368560 + 628.3319706889*Tut1 + 6.7707*pow(10,-6)*pow(Tut1,2) - 4.5*pow(10,-10)*pow(Tut1,3);
00147 }
00148     
00149 ostream & operator << (ostream& s, ssfTime& t)
00150 {
00151     s << setprecision(TIME_PRECISION) << t.GetSeconds();
00152     return s;
00153 }
00154 } // close namespace O_SESSAME
00155 
00156 // Do not change the comments below - they will be added automatically by CVS
00157 /*****************************************************************************
00158 *       $Log: Time.cpp,v $
00159 *       Revision 1.8  2003/06/10 14:49:16  nilspace
00160 *       removed commas from numbers.
00161 *       
00162 *       Revision 1.7  2003/06/10 01:11:46  nilspace
00163 *       Fixed Time to build correctly (changed Greenwich epoch time to object instead of pointer)
00164 *       
00165 *       Revision 1.6  2003/06/09 19:41:30  nilspace
00166 *       Added SetEpoch(year,month,day,hour,minute,second)
00167 *       
00168 *       Revision 1.5  2003/05/21 13:35:59  nilspace
00169 *       Fixed the second GetJulianDate to be GetEpochJulianDate
00170 *       
00171 *       Revision 1.4  2003/05/21 03:57:52  nilspace
00172 *       Added GetEpochJulianDate and comments to JulianDate conversion function.
00173 *       
00174 *       Revision 1.3  2003/05/20 17:44:21  nilspace
00175 *       Updated comments.
00176 *       
00177 *       Revision 1.2  2003/04/27 22:04:34  nilspace
00178 *       Created the namespace O_SESSAME.
00179 *       
00180 *       Revision 1.1  2003/04/23 14:46:49  nilspace
00181 *       New Time class for simulation time.
00182 *       
00183 *
00184 ******************************************************************************/

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