Stellarium 0.14.3
gTime.hpp
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 
32 // gTimeSpan and gTime
33 #ifndef _GTIME_HPP_
34 #define _GTIME_HPP_ 1
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 
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
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 
78  long getDays() const;
79 
84  int getHours() const;
85 
90  int getMinutes() const;
91 
96  int getSeconds() const;
97 
103  double getDblSeconds() const;
104 
105 
111  double getDblDays() const;
112  // Operations
113 
115  // TimeSpan Object Math operations
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 
133 
134 
135 
136 
142 
143 class gTime
144 {
145 public:
146 
147  // Constructors
148  gTime(double ai_jDays = 0);
149  gTime(int year, double day);
150  gTime(int nYear, int nMonth, int nDay, int nHour, int nMin, double nSec);
151  gTime(struct tm ai_timestruct);
152 
153  // copy constructor
154  gTime(const gTime& timeSrc);
155 
157  // Time Object setting operations
159 
160  // Operation setTime
170  void setTime(int year, double day);
171 
172  // Operation operator=
176  const gTime& operator=(const gTime& timeSrc);
177 
178  // Operation operator=
184  const gTime& operator=(time_t t);
185 
186 
187  // Operation operator=
193  const gTime& operator=(double t)
194  {
195  m_time = t;
196  return *this;
197  }
198 
199 
200 
202  // Time Machine getting operations
204 
205  // Operation getCurrentTime();
210  static gTime getCurrentTime();
211 
212  // Operation getTimeToUTC();
216  static gTimeSpan getTimeToUTC();
217 
218  // Operation: isLeapYear
224  static bool isLeapYear(int ai_year)
225  {
226  return (ai_year % 4 == 0 && ai_year % 100 != 0) || (ai_year % 400 == 0);
227  }
228 
229 
231  // Time Object getting operations
233 
234  // Operation: getGmtTm();
238  double getGmtTm() const;
239 
240  // Operation: getLocalTm();
244  double getLocalTm() const;
245 
246 
248  // Time Object Converting operations
250 
251  // Operation: toTime();
257  time_t toTime() const;
258 
259 
260  void toCalendarDate(int *pYear, int *pMonth , double *pDom) const;
261 
262  double toJCenturies() const;
263 
264  // Operation: toThetaGMST();
277  double toThetaGMST() const;
278 
279  // Operation: toThetaLMST();
286  double toThetaLMST(double longitude) const;
287 
288 
290  // Time Object Math operations
292 
293  gTimeSpan operator-(gTime time) const;
294  gTime operator- (gTimeSpan timeSpan) const;
295  gTime operator+ (gTimeSpan timeSpan) const;
296  const gTime& operator+= (gTimeSpan timeSpan);
297  const gTime& operator-= (gTimeSpan timeSpan);
298  bool operator== (gTime time) const;
299  bool operator!= (gTime time) const;
300  bool operator< (gTime time) const;
301  bool operator> (gTime time) const;
302  bool operator<= (gTime time) const;
303  bool operator>= (gTime time) const;
304 
305 private:
306  double m_time; //Time in Julian Days
307 
308 };
309 
310 
311 
312 inline std::ostream& operator<<(std::ostream& s, gTime& ai_gTime)
313 {
314  int year, month;
315  double Dom;
316 
317  ai_gTime.toCalendarDate(&year, &month , &Dom);
318 
319  s << "GMT " << year <<" " << month <<":"<<Dom;
320  return s;
321 }
322 
323 inline std::ostream& operator<<(std::ostream& s, gTimeSpan& ai_gTimeSpan)
324 {
325  s << "D " <<ai_gTimeSpan.getDays()<<" "<<ai_gTimeSpan.getHours()<<":"<<ai_gTimeSpan.getMinutes()<<":"<<ai_gTimeSpan.getSeconds()<<std::endl;
326  return s;
327 }
328 
329 
330 #endif // _GTIME_HPP_
long getDays() const
Operation: getDays() This method returns the integer days number stored in the gTimeSpan object...
int getHours() const
Operation: getHours() This method returns the integer hours number stored in the gTimeSpan object...
double toThetaGMST() const
Calculate Theta Angle at Greenwich Mean Time for the Julian date.
void setTime(int year, double day)
Set the time value of the time object to the julian day of year, day.
double getDblDays() const
Operation: getDblDays() This method returns the total days number stored in the gTimeSpan object...
static gTimeSpan getTimeToUTC()
Returns a gTimeSpan object setted with the Local Time Span setted in the machine. ...
int getMinutes() const
Operation: getMinutes() This method returns the integer Minutes number stored in the gTimeSpan object...
double toThetaLMST(double longitude) const
Calculate Theta Angle at Local Mean Time for the Julian date.
const gTime & operator=(double t)
overload the = operator to assign time values to the object in julian days.
Definition: gTime.hpp:193
double getDblSeconds() const
Operation: getDblSeconds() This method returns the total seconds number stored in the gTimeSpan objec...
time_t toTime() const
Returns the time_t value of the gTime Object.
static gTime getCurrentTime()
Returns a gTime object setted with the actual machine time.
This class implements time calculations.
Definition: gTime.hpp:143
double getGmtTm() const
Returns the time GMT value in Julian days.
const gTime & operator=(const gTime &timeSrc)
overload de = operator to assign time values to he object.
double getLocalTm() const
Returns the time Local value in Julian days.
int getSeconds() const
Operation: getSeconds() This method returns the integer seconds number stored in the gTimeSpan object...
static bool isLeapYear(int ai_year)
Leap Year Calculation.
Definition: gTime.hpp:224