GammaLib  2.1.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-2022 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 "GTools.hpp"
34 #include "GTimeReference.hpp"
35 
36 /* __ Forward declarations _______________________________________________ */
37 
38 
39 /***********************************************************************//**
40  * @class GTime
41  *
42  * @brief Time class
43  *
44  * The GTime class stores a time value in seconds in a GammaLib native time
45  * reference system. The GammaLib native time reference (i.e. time=0) is
46  * defined as
47  *
48  * January 1, 2010, 00:00:00 (TT)
49  *
50  * The time system is Terrestrial Time (TT). With respect to Coordinated
51  * Universal Time (UTC), TT time is greater than UTC time by 66.184 sec at
52  * January 1, 2010, 00:00:00. The difference is due to the introduction of
53  * leap seconds that synchronize TT with the Earth rotation (UTC).
54  ***************************************************************************/
55 class GTime : public GBase {
56 
57  // Operator friends
58  friend GTime operator+(const GTime& time, const double& seconds);
59  friend GTime operator+(const double& seconds, const GTime& time);
60  friend GTime operator-(const GTime& time, const double& seconds);
61  friend double 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  friend bool operator>=(const GTime& a, const GTime& b);
68 
69 public:
70  // Constructors and destructors
71  GTime(void);
72  GTime(const GTime& time);
73  GTime(const double& time, const std::string& unit = "sec");
74  GTime(const double& time, const GTimeReference& ref);
75  GTime(const std::string& time, const GTimeReference& ref);
76  explicit GTime(const std::string& time);
77  virtual ~GTime(void);
78 
79  // Operators
80  GTime& operator=(const GTime& time);
81  GTime& operator+=(const double& seconds);
82  GTime& operator-=(const double& seconds);
83 
84  // Methods
85  void clear(void);
86  GTime* clone(void) const;
87  std::string classname(void) const;
88  double jd(void) const;
89  double jd(const std::string& timesys) const;
90  double mjd(void) const;
91  double mjd(const std::string& timesys) const;
92  const double& secs(void) const;
93  double secs(const std::string& timesys) const;
94  double days(void) const;
95  double days(const std::string& timesys) const;
96  double julian_epoch(void) const;
97  double julian_epoch(const std::string& timesys) const;
98  std::string utc(const int& precision = 0) const;
99  double gmst(void) const;
100  double gast(void) const;
101  double lmst(const double& geolon) const;
102  double last(const double& geolon) const;
103  double leap_seconds(void) const;
104  double utc2tt(void) const;
105  double convert(const GTimeReference& ref) const;
106  void jd(const double& time);
107  void jd(const double& time, const std::string& timesys);
108  void mjd(const double& time);
109  void mjd(const double& time, const std::string& timesys);
110  void secs(const double& seconds);
111  void secs(const double& seconds, const std::string& timesys);
112  void days(const double& days);
113  void days(const double& days, const std::string& timesys);
114  void utc(const std::string& time);
115  void set(const double& time, const GTimeReference& ref);
116  void set(const std::string& time, const GTimeReference& ref);
117  void set(const std::string& time);
118  void now(void);
119  GTimeReference reference(void) const;
120  std::string print(const GChatter& chatter = NORMAL) const;
121 
122 protected:
123  // Protected methods
124  void init_members(void);
125  void copy_members(const GTime& time);
126  void free_members(void);
127  double leap_seconds(const double& mjd) const;
128  bool is_leap_year(const int& year) const;
129  int days_in_year(const int& year) const;
130  double extract_timeval(const std::string& time) const;
131  std::string extract_timesys(const std::string& time) const;
132 
133  // Protected data members
134  double m_time; //!< Time in seconds in native reference (TT)
135 };
136 
137 
138 /***********************************************************************//**
139  * @brief Return class name
140  *
141  * @return String containing the class name ("GTime").
142  ***************************************************************************/
143 inline
144 std::string GTime::classname(void) const
145 {
146  return ("GTime");
147 }
148 
149 
150 /***********************************************************************//**
151  * @brief Return time in seconds in native reference (TT)
152  *
153  * @return Time in native reference (seconds).
154  ***************************************************************************/
155 inline
156 const double& GTime::secs(void) const
157 {
158  return m_time;
159 }
160 
161 
162 /***********************************************************************//**
163  * @brief Set time in seconds in native reference (TT)
164  *
165  * @param[in] seconds Time in native reference (seconds).
166  ***************************************************************************/
167 inline
168 void GTime::secs(const double& seconds)
169 {
170  m_time = seconds;
171  return;
172 }
173 
174 
175 /***********************************************************************//**
176  * @brief Return number of leap seconds for current time
177  *
178  * @return Number of leap seconds for current time (seconds).
179  ***************************************************************************/
180 inline
181 double GTime::leap_seconds(void) const
182 {
183  return (leap_seconds(this->mjd()));
184 }
185 
186 
187 /***********************************************************************//**
188  * @brief Return time difference between UTC and TT (seconds)
189  *
190  * @return Time difference between UTC and TT (seconds).
191  *
192  * Returns the time difference between UTC and TT. The time difference is
193  * a positive number and is given by the sum of 32.184 s and the number
194  * of leap seconds.
195  ***************************************************************************/
196 inline
197 double GTime::utc2tt(void) const
198 {
199  return (leap_seconds()+gammalib::tai2tt);
200 }
201 
202 
203 /***********************************************************************//**
204  * @brief Signals if year is a leap year
205  *
206  * @param[in] year Year (four digits integer).
207  * @return True if year is a leap year.
208  ***************************************************************************/
209 inline
210 bool GTime::is_leap_year(const int& year) const
211 {
212  bool is_leap_year = (((year % 400) == 0) ||
213  (((year % 100) != 0) && ((year % 4) == 0)));
214  return (is_leap_year);
215 }
216 
217 
218 /***********************************************************************//**
219  * @brief Returns number of days in year
220  *
221  * @param[in] year Year (four digits integer).
222  * @return Number of days in year.
223  ***************************************************************************/
224 inline
225 int GTime::days_in_year(const int& year) const
226 {
227  int days = (is_leap_year(year)) ? 366 : 365;
228  return (days);
229 }
230 
231 
232 /***********************************************************************//**
233  * @brief Add seconds to time
234  *
235  * @param[in] seconds Seconds.
236  * @return Time.
237  *
238  * Adds @p seconds to the time.
239  ***************************************************************************/
240 inline
241 GTime& GTime::operator+=(const double& seconds)
242 {
243  m_time += seconds;
244  return *this;
245 }
246 
247 
248 /***********************************************************************//**
249  * @brief Subtract seconds from time
250  *
251  * @param[in] seconds Seconds.
252  * @return Time.
253  *
254  * Subtracts @p seconds from the time.
255  ***************************************************************************/
256 inline
257 GTime& GTime::operator-=(const double& seconds)
258 {
259  m_time -= seconds;
260  return *this;
261 }
262 
263 
264 /***********************************************************************//**
265  * @brief Add seconds to time
266  *
267  * @param[in] time Time.
268  * @param[in] seconds Seconds.
269  * @return Time.
270  *
271  * Adds @p seconds to the @p time.
272  ***************************************************************************/
273 inline
274 GTime operator+(const GTime& time, const double& seconds)
275 {
276  GTime result;
277  result.m_time = time.m_time + seconds;
278  return result;
279 }
280 
281 
282 /***********************************************************************//**
283  * @brief Add seconds to time
284  *
285  * @param[in] seconds Seconds.
286  * @param[in] time Time.
287  * @return Time.
288  *
289  * Adds @p seconds to the @p time.
290  ***************************************************************************/
291 inline
292 GTime operator+(const double& seconds, const GTime& time)
293 {
294  GTime result;
295  result.m_time = time.m_time + seconds;
296  return result;
297 }
298 
299 
300 /***********************************************************************//**
301  * @brief Subtract seconds from time
302  *
303  * @param[in] time Time.
304  * @param[in] seconds Seconds.
305  * @return Time.
306  *
307  * Subtracts @p seconds from the @p time.
308  ***************************************************************************/
309 inline
310 GTime operator-(const GTime& time, const double& seconds)
311 {
312  GTime result;
313  result.m_time = time.m_time - seconds;
314  return result;
315 }
316 
317 
318 /***********************************************************************//**
319  * @brief Subtract times
320  *
321  * @param[in] a First time.
322  * @param[in] b Second time.
323  * @return Time difference in seconds.
324  *
325  * Subtracts time @p b from time @p a and returns the difference in seconds.
326  ***************************************************************************/
327 inline
328 double operator-(const GTime& a, const GTime& b)
329 {
330  return (a.m_time - b.m_time);
331 }
332 
333 
334 /***********************************************************************//**
335  * @brief Check if times are equal
336  *
337  * @param[in] a First time.
338  * @param[in] b Second time.
339  * @return True if @p a equals @p b.
340  ***************************************************************************/
341 inline
342 bool operator==(const GTime &a, const GTime &b)
343 {
344  return (a.m_time == b.m_time);
345 }
346 
347 
348 /***********************************************************************//**
349  * @brief Check if times are not equal
350  *
351  * @param[in] a First time.
352  * @param[in] b Second time.
353  * @return True if @p a is not equal to @p b.
354  ***************************************************************************/
355 inline
356 bool operator!=(const GTime &a, const GTime &b)
357 {
358  return (a.m_time != b.m_time);
359 }
360 
361 
362 /***********************************************************************//**
363  * @brief Check if time is smaller than other time
364  *
365  * @param[in] a First time.
366  * @param[in] b Second time.
367  * @return True if @p a is smaller than @p b.
368  ***************************************************************************/
369 inline
370 bool operator<(const GTime &a, const GTime &b)
371 {
372  return (a.m_time < b.m_time);
373 }
374 
375 
376 /***********************************************************************//**
377  * @brief Check if time is smaller than or equal to other time
378  *
379  * @param[in] a First time.
380  * @param[in] b Second time.
381  * @return True if @p a is smaller than or equal to @p b.
382  ***************************************************************************/
383 inline
384 bool operator<=(const GTime &a, const GTime &b)
385 {
386  return (a.m_time <= b.m_time);
387 }
388 
389 
390 /***********************************************************************//**
391  * @brief Check if time is larger than other time
392  *
393  * @param[in] a First time.
394  * @param[in] b Second time.
395  * @return True if @p a is larger than @p b.
396  ***************************************************************************/
397 inline
398 bool operator>(const GTime &a, const GTime &b)
399 {
400  return (a.m_time > b.m_time);
401 }
402 
403 
404 /***********************************************************************//**
405  * @brief Check if time is larger than or equal to other time
406  *
407  * @param[in] a First time.
408  * @param[in] b Second time.
409  * @return True if @p a is larger than or equal to @p b.
410  ***************************************************************************/
411 inline
412 bool operator>=(const GTime &a, const GTime &b)
413 {
414  return (a.m_time >= b.m_time);
415 }
416 
417 
418 /***********************************************************************//**
419  * @brief Set time for string for native time reference system
420  *
421  * @param[in] time Time string.
422  ***************************************************************************/
423 inline
424 void GTime::set(const std::string& time)
425 {
426  set(time, this->reference());
427  return;
428 }
429 
430 #endif /* GTIME_HPP */
std::string extract_timesys(const std::string &time) const
Extract time system from time string.
Definition: GTime.cpp:1371
friend bool operator<=(const GTime &a, const GTime &b)
Check if time is smaller than or equal to other time.
Definition: GTime.hpp:384
friend bool operator<(const GTime &a, const GTime &b)
Check if time is smaller than other time.
Definition: GTime.hpp:370
void init_members(void)
Initialise class members.
Definition: GTime.cpp:1215
void now(void)
Set time to current time.
Definition: GTime.cpp:1126
GTimeReference reference(void) const
Returns native time reference.
Definition: GTime.cpp:1170
friend GTime operator-(const GTime &time, const double &seconds)
Subtract seconds from time.
Definition: GTime.hpp:310
double lmst(const double &geolon) const
Return local mean sidereal time in hours in a day.
Definition: GTime.cpp:656
void copy_members(const GTime &time)
Copy class members.
Definition: GTime.cpp:1230
Definition of interface for all GammaLib classes.
friend bool operator==(const GTime &a, const GTime &b)
Check if times are equal.
Definition: GTime.hpp:342
void clear(void)
Clear time.
Definition: GTime.cpp:252
Time class.
Definition: GTime.hpp:55
Gammalib tools definition.
friend bool operator>=(const GTime &a, const GTime &b)
Check if time is larger than or equal to other time.
Definition: GTime.hpp:412
friend bool operator!=(const GTime &a, const GTime &b)
Check if times are not equal.
Definition: GTime.hpp:356
GTime & operator-=(const double &seconds)
Subtract seconds from time.
Definition: GTime.hpp:257
friend GTime operator+(const GTime &time, const double &seconds)
Add seconds to time.
Definition: GTime.hpp:274
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:1329
double jd(void) const
Return time in Julian Days (TT)
Definition: GTime.cpp:284
bool is_leap_year(const int &year) const
Signals if year is a leap year.
Definition: GTime.hpp:210
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:200
std::string classname(void) const
Return class name.
Definition: GTime.hpp:144
void free_members(void)
Delete class members.
Definition: GTime.cpp:1243
GTime(void)
Void constructor.
Definition: GTime.cpp:67
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
const double tai2tt
Definition: GTools.hpp:51
Time reference class interface definition.
double leap_seconds(void) const
Return number of leap seconds for current time.
Definition: GTime.hpp:181
GChatter
Definition: GTypemaps.hpp:33
std::string print(const GChatter &chatter=NORMAL) const
Print time.
Definition: GTime.cpp:1188
GTime * clone(void) const
Clone time.
Definition: GTime.cpp:270
double gmst(void) const
Return Greenwich mean sidereal time in hours in a day.
Definition: GTime.cpp:593
const double & secs(void) const
Return time in seconds in native reference (TT)
Definition: GTime.hpp:156
double last(const double &geolon) const
Return local apparent sidereal time in hours in a day.
Definition: GTime.cpp:677
GTime & operator=(const GTime &time)
Assignment operator.
Definition: GTime.cpp:222
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:616
friend bool operator>(const GTime &a, const GTime &b)
Check if time is larger than other time.
Definition: GTime.hpp:398
GTime & operator+=(const double &seconds)
Add seconds to time.
Definition: GTime.hpp:241
Implements a time reference.
double mjd(void) const
Return time in Modified Julian Days (TT)
Definition: GTime.cpp:320
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:225
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:425
std::string utc(const int &precision=0) const
Return time as string in UTC time system.
Definition: GTime.cpp:465
bool operator!=(const GEbounds &a, const GEbounds &b)
Energy boundaries inequality operator friend.
Definition: GEbounds.hpp:213
void set(const double &time, const GTimeReference &ref)
Set time given in specified reference.
Definition: GTime.cpp:1005
double utc2tt(void) const
Return time difference between UTC and TT (seconds)
Definition: GTime.hpp:197
double convert(const GTimeReference &ref) const
Return time in specified reference.
Definition: GTime.cpp:698
double days(void) const
Return time in days in native reference (TT)
Definition: GTime.cpp:400
double m_time
Time in seconds in native reference (TT)
Definition: GTime.hpp:134