GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GTime.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GTime.hpp - Time class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-2019 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GTime.hpp
23  * @brief Time class interface definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GTIME_HPP
28 #define GTIME_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include "GBase.hpp"
33 #include "GTimeReference.hpp"
34 
35 /* __ Forward declarations _______________________________________________ */
36 
37 
38 /***********************************************************************//**
39  * @class GTime
40  *
41  * @brief Time class
42  *
43  * The GTime class stores a time value in seconds in a GammaLib native time
44  * reference system. The GammaLib native time reference (i.e. time=0) is
45  * defined as
46  *
47  * January 1, 2010, 00:00:00 (TT)
48  *
49  * The time system is Terrestrial Time (TT). With respect to Coordinated
50  * Universal Time (UTC), TT time is greater than UTC time by 66.184 sec at
51  * January 1, 2010, 00:00:00. The difference is due to the introduction of
52  * leap seconds that synchronize TT with the Earth rotation (UTC).
53  ***************************************************************************/
54 class GTime : public GBase {
55 
56  // Operator friends
57  friend GTime operator+(const GTime& time, const double& seconds);
58  friend GTime operator+(const double& seconds, const GTime& time);
59  friend GTime operator-(const GTime& time, const double& seconds);
60  friend double operator-(const GTime& a, const GTime& b);
61  friend bool operator==(const GTime& a, const GTime& b);
62  friend bool operator!=(const GTime& a, const GTime& b);
63  friend bool operator<(const GTime& a, const GTime& b);
64  friend bool operator<=(const GTime& a, const GTime& b);
65  friend bool operator>(const GTime& a, const GTime& b);
66  friend bool operator>=(const GTime& a, const GTime& b);
67 
68 public:
69  // Constructors and destructors
70  GTime(void);
71  GTime(const GTime& time);
72  GTime(const double& time, const std::string& unit = "sec");
73  GTime(const double& time, const GTimeReference& ref);
74  GTime(const std::string& time, const GTimeReference& ref);
75  explicit GTime(const std::string& time);
76  virtual ~GTime(void);
77 
78  // Operators
79  GTime& operator=(const GTime& time);
80  GTime& operator+=(const double& seconds);
81  GTime& operator-=(const double& seconds);
82 
83  // Methods
84  void clear(void);
85  GTime* clone(void) const;
86  std::string classname(void) const;
87  double jd(void) const;
88  double jd(const std::string& timesys) const;
89  double mjd(void) const;
90  double mjd(const std::string& timesys) const;
91  const double& secs(void) const;
92  double secs(const std::string& timesys) const;
93  double days(void) const;
94  double days(const std::string& timesys) const;
95  double julian_epoch(void) const;
96  double julian_epoch(const std::string& timesys) const;
97  std::string utc(const int& precision = 0) const;
98  double gmst(void) const;
99  double gast(void) const;
100  double lmst(const double& geolon) const;
101  double last(const double& geolon) const;
102  double convert(const GTimeReference& ref) const;
103  void jd(const double& time);
104  void jd(const double& time, const std::string& timesys);
105  void mjd(const double& time);
106  void mjd(const double& time, const std::string& timesys);
107  void secs(const double& seconds);
108  void secs(const double& seconds, const std::string& timesys);
109  void days(const double& days);
110  void days(const double& days, const std::string& timesys);
111  void utc(const std::string& time);
112  void set(const double& time, const GTimeReference& ref);
113  void set(const std::string& time, const GTimeReference& ref);
114  void set(const std::string& time);
115  void now(void);
116  GTimeReference reference(void) const;
117  std::string print(const GChatter& chatter = NORMAL) const;
118 
119 protected:
120  // Protected methods
121  void init_members(void);
122  void copy_members(const GTime& time);
123  void free_members(void);
124  double leap_seconds(const double& mjd) const;
125  bool is_leap_year(const int& year) const;
126  int days_in_year(const int& year) const;
127  double extract_timeval(const std::string& time) const;
128  std::string extract_timesys(const std::string& time) const;
129 
130  // Protected data members
131  double m_time; //!< Time in seconds in native reference (TT)
132 };
133 
134 
135 /***********************************************************************//**
136  * @brief Return class name
137  *
138  * @return String containing the class name ("GTime").
139  ***************************************************************************/
140 inline
141 std::string GTime::classname(void) const
142 {
143  return ("GTime");
144 }
145 
146 
147 /***********************************************************************//**
148  * @brief Return time in seconds in native reference (TT)
149  *
150  * @return Time in native reference (seconds).
151  ***************************************************************************/
152 inline
153 const double& GTime::secs(void) const
154 {
155  return m_time;
156 }
157 
158 
159 /***********************************************************************//**
160  * @brief Set time in seconds in native reference (TT)
161  *
162  * @param[in] seconds Time in native reference (seconds).
163  ***************************************************************************/
164 inline
165 void GTime::secs(const double& seconds)
166 {
167  m_time = seconds;
168  return;
169 }
170 
171 
172 /***********************************************************************//**
173  * @brief Signals if year is a leap year
174  *
175  * @param[in] year Year (four digits integer).
176  * @return True if year is a leap year.
177  ***************************************************************************/
178 inline
179 bool GTime::is_leap_year(const int& year) const
180 {
181  bool is_leap_year = (((year % 400) == 0) ||
182  (((year % 100) != 0) && ((year % 4) == 0)));
183  return (is_leap_year);
184 }
185 
186 
187 /***********************************************************************//**
188  * @brief Returns number of days in year
189  *
190  * @param[in] year Year (four digits integer).
191  * @return Number of days in year.
192  ***************************************************************************/
193 inline
194 int GTime::days_in_year(const int& year) const
195 {
196  int days = (is_leap_year(year)) ? 366 : 365;
197  return (days);
198 }
199 
200 
201 /***********************************************************************//**
202  * @brief Add seconds to time
203  *
204  * @param[in] seconds Seconds.
205  * @return Time.
206  *
207  * Adds @p seconds to the time.
208  ***************************************************************************/
209 inline
210 GTime& GTime::operator+=(const double& seconds)
211 {
212  m_time += seconds;
213  return *this;
214 }
215 
216 
217 /***********************************************************************//**
218  * @brief Subtract seconds from time
219  *
220  * @param[in] seconds Seconds.
221  * @return Time.
222  *
223  * Subtracts @p seconds from the time.
224  ***************************************************************************/
225 inline
226 GTime& GTime::operator-=(const double& seconds)
227 {
228  m_time -= seconds;
229  return *this;
230 }
231 
232 
233 /***********************************************************************//**
234  * @brief Add seconds to time
235  *
236  * @param[in] time Time.
237  * @param[in] seconds Seconds.
238  * @return Time.
239  *
240  * Adds @p seconds to the @p time.
241  ***************************************************************************/
242 inline
243 GTime operator+(const GTime& time, const double& seconds)
244 {
245  GTime result;
246  result.m_time = time.m_time + seconds;
247  return result;
248 }
249 
250 
251 /***********************************************************************//**
252  * @brief Add seconds to time
253  *
254  * @param[in] seconds Seconds.
255  * @param[in] time Time.
256  * @return Time.
257  *
258  * Adds @p seconds to the @p time.
259  ***************************************************************************/
260 inline
261 GTime operator+(const double& seconds, const GTime& time)
262 {
263  GTime result;
264  result.m_time = time.m_time + seconds;
265  return result;
266 }
267 
268 
269 /***********************************************************************//**
270  * @brief Subtract seconds from time
271  *
272  * @param[in] time Time.
273  * @param[in] seconds Seconds.
274  * @return Time.
275  *
276  * Subtracts @p seconds from the @p time.
277  ***************************************************************************/
278 inline
279 GTime operator-(const GTime& time, const double& seconds)
280 {
281  GTime result;
282  result.m_time = time.m_time - seconds;
283  return result;
284 }
285 
286 
287 /***********************************************************************//**
288  * @brief Subtract times
289  *
290  * @param[in] a First time.
291  * @param[in] b Second time.
292  * @return Time difference in seconds.
293  *
294  * Subtracts time @p b from time @p a and returns the difference in seconds.
295  ***************************************************************************/
296 inline
297 double operator-(const GTime& a, const GTime& b)
298 {
299  return (a.m_time - b.m_time);
300 }
301 
302 
303 /***********************************************************************//**
304  * @brief Check if times are equal
305  *
306  * @param[in] a First time.
307  * @param[in] b Second time.
308  * @return True if @p a equals @p b.
309  ***************************************************************************/
310 inline
311 bool operator==(const GTime &a, const GTime &b)
312 {
313  return (a.m_time == b.m_time);
314 }
315 
316 
317 /***********************************************************************//**
318  * @brief Check if times are not equal
319  *
320  * @param[in] a First time.
321  * @param[in] b Second time.
322  * @return True if @p a is not equal to @p b.
323  ***************************************************************************/
324 inline
325 bool operator!=(const GTime &a, const GTime &b)
326 {
327  return (a.m_time != b.m_time);
328 }
329 
330 
331 /***********************************************************************//**
332  * @brief Check if time is smaller than other time
333  *
334  * @param[in] a First time.
335  * @param[in] b Second time.
336  * @return True if @p a is smaller than @p b.
337  ***************************************************************************/
338 inline
339 bool operator<(const GTime &a, const GTime &b)
340 {
341  return (a.m_time < b.m_time);
342 }
343 
344 
345 /***********************************************************************//**
346  * @brief Check if time is smaller than or equal to other time
347  *
348  * @param[in] a First time.
349  * @param[in] b Second time.
350  * @return True if @p a is smaller than or equal to @p b.
351  ***************************************************************************/
352 inline
353 bool operator<=(const GTime &a, const GTime &b)
354 {
355  return (a.m_time <= b.m_time);
356 }
357 
358 
359 /***********************************************************************//**
360  * @brief Check if time is larger than other time
361  *
362  * @param[in] a First time.
363  * @param[in] b Second time.
364  * @return True if @p a is larger than @p b.
365  ***************************************************************************/
366 inline
367 bool operator>(const GTime &a, const GTime &b)
368 {
369  return (a.m_time > b.m_time);
370 }
371 
372 
373 /***********************************************************************//**
374  * @brief Check if time is larger than or equal to other time
375  *
376  * @param[in] a First time.
377  * @param[in] b Second time.
378  * @return True if @p a is larger than or equal to @p b.
379  ***************************************************************************/
380 inline
381 bool operator>=(const GTime &a, const GTime &b)
382 {
383  return (a.m_time >= b.m_time);
384 }
385 
386 
387 /***********************************************************************//**
388  * @brief Set time for string for native time reference system
389  *
390  * @param[in] time Time string.
391  ***************************************************************************/
392 inline
393 void GTime::set(const std::string& time)
394 {
395  set(time, this->reference());
396  return;
397 }
398 
399 #endif /* GTIME_HPP */
std::string extract_timesys(const std::string &time) const
Extract time system from time string.
Definition: GTime.cpp:1344
friend bool operator<=(const GTime &a, const GTime &b)
Check if time is smaller than or equal to other time.
Definition: GTime.hpp:353
friend bool operator<(const GTime &a, const GTime &b)
Check if time is smaller than other time.
Definition: GTime.hpp:339
void init_members(void)
Initialise class members.
Definition: GTime.cpp:1188
void now(void)
Set time to current time.
Definition: GTime.cpp:1099
GTimeReference reference(void) const
Returns native time reference.
Definition: GTime.cpp:1143
friend GTime operator-(const GTime &time, const double &seconds)
Subtract seconds from time.
Definition: GTime.hpp:279
double lmst(const double &geolon) const
Return local mean sidereal time in hours in a day.
Definition: GTime.cpp:629
void copy_members(const GTime &time)
Copy class members.
Definition: GTime.cpp:1203
Definition of interface for all GammaLib classes.
friend bool operator==(const GTime &a, const GTime &b)
Check if times are equal.
Definition: GTime.hpp:311
void clear(void)
Clear time.
Definition: GTime.cpp:251
Time class.
Definition: GTime.hpp:54
friend bool operator>=(const GTime &a, const GTime &b)
Check if time is larger than or equal to other time.
Definition: GTime.hpp:381
friend bool operator!=(const GTime &a, const GTime &b)
Check if times are not equal.
Definition: GTime.hpp:325
GTime & operator-=(const double &seconds)
Subtract seconds from time.
Definition: GTime.hpp:226
friend GTime operator+(const GTime &time, const double &seconds)
Add seconds to time.
Definition: GTime.hpp:243
GArf operator+(const GArf &a, const GArf &b)
Auxiliary Response File addition operator friend.
Definition: GArf.hpp:293
double extract_timeval(const std::string &time) const
Extract time value from time string.
Definition: GTime.cpp:1302
double jd(void) const
Return time in Julian Days (TT)
Definition: GTime.cpp:283
bool is_leap_year(const int &year) const
Signals if year is a leap year.
Definition: GTime.hpp:179
bool operator<=(const GEnergy &a, const GEnergy &b)
Energy smaller than or equal to operator friend.
Definition: GEnergy.hpp:339
bool operator<(const GEnergy &a, const GEnergy &b)
Energy smaller than operator friend.
Definition: GEnergy.hpp:325
virtual ~GTime(void)
Destructor.
Definition: GTime.cpp:199
std::string classname(void) const
Return class name.
Definition: GTime.hpp:141
void free_members(void)
Delete class members.
Definition: GTime.cpp:1216
GTime(void)
Void constructor.
Definition: GTime.cpp:67
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
Time reference class interface definition.
GChatter
Definition: GTypemaps.hpp:33
std::string print(const GChatter &chatter=NORMAL) const
Print time.
Definition: GTime.cpp:1161
double leap_seconds(const double &mjd) const
Returns number of leap seconds for a given MJD.
Definition: GTime.cpp:1236
GTime * clone(void) const
Clone time.
Definition: GTime.cpp:269
double gmst(void) const
Return Greenwich mean sidereal time in hours in a day.
Definition: GTime.cpp:566
const double & secs(void) const
Return time in seconds in native reference (TT)
Definition: GTime.hpp:153
double last(const double &geolon) const
Return local apparent sidereal time in hours in a day.
Definition: GTime.cpp:650
GTime & operator=(const GTime &time)
Assignment operator.
Definition: GTime.cpp:221
bool operator>=(const GEnergy &a, const GEnergy &b)
Energy larger than or equal to operator friend.
Definition: GEnergy.hpp:367
double gast(void) const
Return Greenwich apparent sidereal time in hours in a day.
Definition: GTime.cpp:589
friend bool operator>(const GTime &a, const GTime &b)
Check if time is larger than other time.
Definition: GTime.hpp:367
GTime & operator+=(const double &seconds)
Add seconds to time.
Definition: GTime.hpp:210
Implements a time reference.
double mjd(void) const
Return time in Modified Julian Days (TT)
Definition: GTime.cpp:319
bool operator>(const GEnergy &a, const GEnergy &b)
Energy larger than operator friend.
Definition: GEnergy.hpp:353
bool operator==(const GEnergy &a, const GEnergy &b)
Energy equality operator friend.
Definition: GEnergy.hpp:297
int days_in_year(const int &year) const
Returns number of days in year.
Definition: GTime.hpp:194
GArf operator-(const GArf &a, const GArf &b)
Auxiliary Response File subtraction operator friend.
Definition: GArf.hpp:309
double julian_epoch(void) const
Return Julian epoch in native reference (TT)
Definition: GTime.cpp:424
std::string utc(const int &precision=0) const
Return time as string in UTC time system.
Definition: GTime.cpp:464
bool operator!=(const GEbounds &a, const GEbounds &b)
Energy boundaries inequality operator friend.
Definition: GEbounds.hpp:207
void set(const double &time, const GTimeReference &ref)
Set time given in specified reference.
Definition: GTime.cpp:978
double convert(const GTimeReference &ref) const
Return time in specified reference.
Definition: GTime.cpp:671
double days(void) const
Return time in days in native reference (TT)
Definition: GTime.cpp:399
double m_time
Time in seconds in native reference (TT)
Definition: GTime.hpp:131