00001
00002
00008
00009
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
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
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
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
00120
00121
00122
00123 m_StoredTime = (_newJD - JulianDate(0)) * 24 * 60 * 60;
00124 return;
00125 }
00126
00127 void ssfTime::SetEpochJulianDate(ssfJulianDate _newJD)
00128 {
00129
00130
00131
00132
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 }
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184