1 /****************************************************************************
2  * Name: gTime.hpp
3  *
4  * Description: gTime y gTimeSpan classes declaration.
5  *		 This classes implement the method and operators to manage
6  *		 calculation over dates and timestamps.
7  *
8  *
9  ***************************************************************************/
10 
11 /***************************************************************************
12  *   Copyright (C) 2006 by J. L. Canales                                   *
13  *   jlcanales@users.sourceforge.net                                       *
14  *                                                                         *
15  *   This program is free software; you can redistribute it and/or modify  *
16  *   it under the terms of the GNU General Public License as published by  *
17  *   the Free Software Foundation; either version 2 of the License, or     *
18  *   (at your option) any later version.                                   *
19  *                                                                         *
20  *   This program is distributed in the hope that it will be useful,       *
21  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
22  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
23  *   GNU General Public License for more details.                          *
24  *                                                                         *
25  *   You should have received a copy of the GNU General Public License     *
26  *   along with this program; if not, write to the                         *
27  *   Free Software Foundation, Inc.,                                       *
28  *   51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.             *
29  ***************************************************************************/
30 
31 /////////////////////////////////////////////////////////////////////////////
32 // gTimeSpan and gTime
33 #ifndef GTIME_HPP
34 #define GTIME_HPP
35 
36 #include <time.h>
37 #include <iostream> // for operator<<(), see below
38 
39 static const double JDAY_JAN1_00H_1900 = 2415019.5; // Jan 1.0 1900 = Jan 1 1900 00h UTC
40 static const double JDAY_JAN1_12H_1900 = 2415020.0; // Jan 1.5 1900 = Jan 1 1900 12h UTC
41 static const double JDAY_JAN1_12H_2000 = 2451545.0; // Jan 1.5 2000 = Jan 1 2000 12h UTC
42 static const double JDAY_JAN1_00H_1970 = 2440587.5;
43 
44 static const double OMEGA_E = 1.002737909350795; // earth rotation per sideral day
45 
46 static const int KSEC_PER_MIN = 60;
47 static const int KSEC_PER_HR  = 3600;
48 static const int KSEC_PER_DAY = 86400;
49 static const int KMIN_PER_HR  = 60;
50 static const int KMIN_PER_DAY = 1440;
51 static const int KHR_PER_DAY  = 60;
52 
53 //! @ingroup satellites
54 class gTimeSpan
55 {
56 public:
57 	// Constructors
58 	gTimeSpan(double timeSpanSrc = 0); // timeSpanSrc is mesured in days and fraction of day
59 	gTimeSpan(long lDays, int nHours, int nMins, double nSecs);
60 
61 	gTimeSpan(const gTimeSpan& timeSpanSrc);
62 	const gTimeSpan& operator=(const gTimeSpan& timeSpanSrc);
63 
64 	// Equal to time in Julian Days
operator =(const double & timeSpanSrc)65 	const gTimeSpan& operator=(const double& timeSpanSrc)
66 	{
67 		m_timeSpan=timeSpanSrc*KSEC_PER_DAY;
68 		return *this;
69 	}
70 
71 	// Attributes
72 	// extract parts
73 
74 	//! Operation: getDays()
75 	//!	This method returns the integer days number stored in the gTimeSpan object.
76 	//! @return
77 	//!    long  Total number of days
78 	long  getDays() const;
79 
80 	//! Operation: getHours()
81 	//!	This method returns the integer hours number stored in the gTimeSpan object.
82 	//! @return
83 	//!    int This is a value between 0 and 23 hours
84 	int   getHours() const;
85 
86 	//! Operation: getMinutes()
87 	//! This method returns the integer Minutes number stored in the gTimeSpan object.
88 	//! @return
89 	//!	int This is a value between 0 and 59 minutes.
90 	int   getMinutes() const;
91 
92 	//! Operation: getSeconds()
93 	//!  	This method returns the integer seconds number stored in the gTimeSpan object.
94 	//! @return
95 	//!	int This is a value between 0 and 59 seconds
96 	int   getSeconds() const;
97 
98 	//! Operation: getDblSeconds()
99 	//!	This method returns the total seconds number stored in the gTimeSpan
100 	//!	object.
101 	//! @return
102 	//!	double Total number of seconds in seconds and fraction of second.
103 	double getDblSeconds() const;
104 
105 
106 	//! Operation: getDblDays()
107 	//!	This method returns the total days number stored in the gTimeSpan
108 	//!	object.
109 	//! @return
110 	//!	double Total number of days in days and fraction of day.
111 	double getDblDays() const;
112 	// Operations
113 
114 	//////////////////////////////////////
115 	// TimeSpan Object Math operations
116 	//////////////////////////////////////
117 	gTimeSpan operator-(gTimeSpan timeSpan) const;
118 	gTimeSpan operator+(gTimeSpan timeSpan) const;
119 	const gTimeSpan& operator+=(gTimeSpan timeSpan);
120 	const gTimeSpan& operator-=(gTimeSpan timeSpan);
121 	bool operator==(gTimeSpan timeSpan) const;
122 	bool operator!=(gTimeSpan timeSpan) const;
123 	bool operator<(gTimeSpan timeSpan) const;
124 	bool operator>(gTimeSpan timeSpan) const;
125 	bool operator<=(gTimeSpan timeSpan) const;
126 	bool operator>=(gTimeSpan timeSpan) const;
127 
128 private:
129 	double m_timeSpan; //time span in julian days
130 };
131 
132 //! @class gTime
133 //! This class implements time calculations.
134 //! Time is stored in julian days
135 //! - Time getting in GMT, Local and Sidereal (earth rotational angle)
136 //! - Time Math operations.
137 
138 class gTime
139 {
140 public:
141 	// Constructors
142 	gTime(double ai_jDays = 0);
143 	gTime(int year, double day);
144 	gTime(int nYear, int nMonth, int nDay, int nHour, int nMin, double nSec);
145 	gTime(struct tm ai_timestruct);
146 
147 	// copy constructor
148 	gTime(const gTime& timeSrc);
149 
150 	//////////////////////////////////////
151 	// Time Object setting operations
152 	//////////////////////////////////////
153 
154 	// Operation setTime
155 	//! @brief Set the time value of the time object to the julian day of
156 	//!      year,  day.
157 	//! @details
158 	//!    References:\n
159 	//!      Astronomical Formulae for Calculators.\n
160 	//!	     Jean Meeus\n
161 	//!	     Chapter 3: Julian Day and Calendar Date. PG's 23, 24, 25\n
162 	//! @param[in] year date year
163 	//!	@param[in] day  date day and day fraction. Notice: 1 Jan is day=0 (not 1)
164 	void setTime(int year, double day);
165 
166 	// Operation operator=
167 	//! @brief overload de = operator to assign time values to he object.
168 	//! @param timeSrc Time to be assigned
169 	//!	@return const gTime&  Reference to *this object modified after operation
170 	const gTime& operator=(const gTime& timeSrc);
171 
172 	// Operation operator=
173 	//! @brief overload de = operator to assign time values to he object in
174 	//!   time_t (operating system) format.
175 	//! @param[in] t Time to be assigned
176 	//! @return
177 	//!   const gTime&  Reference to *this object modified after operation
178 	const gTime& operator=(time_t t);
179 
180 
181 	// Operation operator=
182 	//! @brief overload the = operator to assign time values to the object in
183 	//!   julian days.
184 	//! @param[in] t Time (JD) to be assigned
185 	//! @return
186 	//!   const gTime&  Reference to *this object modified after operation
operator =(double t)187 	const gTime& operator=(double t)
188 	{
189 		m_time = t;
190 		return *this;
191 	}
192 
193 	//////////////////////////////////////
194 	// Time Machine getting operations
195 	//////////////////////////////////////
196 
197 	// Operation  getCurrentTime();
198 	//! @brief Returns a gTime object setted with the actual machine time.
199 	//! @warning This method works with the operating system function time. So it doesn't support seconds fractions
200 	//! @return
201 	//!    gTime Object assigned with real machine time.
202 	static gTime     getCurrentTime();
203 
204 	// Operation  getTimeToUTC();
205 	//! @brief Returns a gTimeSpan object setted with the Local Time Span setted in the machine.
206 	//! @return
207 	//!    gTimeSpan Object assigned with the real machine GMT Diff.
208 	static gTimeSpan getTimeToUTC();
209 
210 	// Operation: isLeapYear
211 	//! @brief Leap Year Calculation.
212 	//! @details  This Operation use the standart algorithm to
213 	//!     calculate Leap Years. Reference: http://en.wikipedia.org/wiki/Leap_year
214 	//! @param ai_year Year number
215 	//! @return true if ai_year is a Leap Year
isLeapYear(int ai_year)216 	static bool isLeapYear(int ai_year)
217 	{
218 		return (ai_year % 4 == 0 && ai_year % 100 != 0) || (ai_year % 400 == 0);
219 	}
220 
221 
222 	//////////////////////////////////////
223 	// Time Object getting operations
224 	//////////////////////////////////////
225 
226 	// Operation:  getGmtTm();
227 	//! @brief Returns the time GMT value in Julian days.
228 	//! @return:
229 	//!    double Julian Days represented in the gTime Object
230 	double getGmtTm() const;
231 
232 	// Operation:  getLocalTm();
233 	//! @brief Returns the time Local value in Julian days.
234 	//! @return:
235 	//!    double Julian Days represented in the gTime Object
236 	double getLocalTm() const;
237 
238 
239 	//////////////////////////////////////
240 	// Time Object Converting operations
241 	//////////////////////////////////////
242 
243 	// Operation:  toTime();
244 	//! @brief Returns the time_t value of the gTime Object.
245 	//! @warning time_t object doesn't support seconds fractions. This method
246 	//!            must be avoid for astronomical calculation.
247 	//! @return:
248 	//!    time_t machine time represented in the gTime Object
249 	time_t toTime() const;
250 
251 
252 	void toCalendarDate(int *pYear, int *pMonth , double *pDom) const;
253 
254 	double toJCenturies() const;
255 
256 	// Operation:  toThetaGMST();
257 	//! @brief Calculate Theta Angle at Greenwich Mean Time for the Julian date.
258 	//! @details The return value is the angle, in radians, measuring eastward from
259 	//! the Vernal Equinox to the  prime meridian.\n
260 	//!   References:\n
261 	//!     Explanatory Supplement to the Astronomical Almanac, page 50.
262 	//!     http://books.google.com/books?id=uJ4JhGJANb4C&lpg=PA52&hl=es&pg=PA50#v=onepage&q&f=false
263 	//!     "Orbital Coordinate Systems, Part II."  Dr. T.Kelzo
264 	//!	     Satellite Times, 2, no. 2 (November/December 1995): 78-79.
265 	//!       http://www.celestrak.com/columns/v02n02/
266 	//! @return
267 	//!    Theta Angle in radians, measuring eastward from the Vernal Equinox to the
268 	//!    prime meridian
269 	double toThetaGMST() const;
270 
271 	// Operation:  toThetaLMST();
272 	//! @brief Calculate Theta Angle at Local Mean Time for the Julian date.
273 	//! @param[in] longitude Geographical longitude for the local meridian.\n
274 	//!     Positive longitude = east longitude\n
275 	//!     Negative longitude = west longitude\n
276 	//! @return Theta Angle in radians, measuring eastward from the Vernal Equinox to the
277 	//!    prime meridian
278 	double toThetaLMST(double longitude) const;
279 
280 
281 	//////////////////////////////////////
282 	// Time Object Math operations
283 	//////////////////////////////////////
284 
285 	gTimeSpan operator-(gTime time)         const;
286 	gTime operator- (gTimeSpan timeSpan) const;
287 	gTime operator+ (gTimeSpan timeSpan) const;
288 	const gTime& operator+= (gTimeSpan timeSpan);
289 	const gTime& operator-= (gTimeSpan timeSpan);
290 	bool operator== (gTime time)         const;
291 	bool operator!= (gTime time)         const;
292 	bool operator< (gTime time)         const;
293 	bool operator> (gTime time)         const;
294 	bool operator<= (gTime time)         const;
295 	bool operator>= (gTime time)         const;
296 
297 private:
298 	double m_time; //Time in Julian Days
299 };
300 
301 
302 
operator <<(std::ostream & s,gTime & ai_gTime)303 inline std::ostream& operator<<(std::ostream& s, gTime& ai_gTime)
304 {
305 	int    year, month;
306 	double Dom;
307 
308 	ai_gTime.toCalendarDate(&year, &month , &Dom);
309 
310 	s << "GMT " << year <<" " << month <<":"<<Dom;
311 	return s;
312 }
313 
operator <<(std::ostream & s,gTimeSpan & ai_gTimeSpan)314 inline std::ostream& operator<<(std::ostream& s, gTimeSpan& ai_gTimeSpan)
315 {
316 	s << "D " <<ai_gTimeSpan.getDays()<<" "<<ai_gTimeSpan.getHours()<<":"<<ai_gTimeSpan.getMinutes()<<":"<<ai_gTimeSpan.getSeconds()<<std::endl;
317 	return s;
318 }
319 
320 
321 #endif // GTIME_HPP
322