CppEphem
CETime.h
Go to the documentation of this file.
1 /***************************************************************************
2  * CETime.h: CppEphem *
3  * ----------------------------------------------------------------------- *
4  * Copyright © 2016 JCardenzana *
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 #ifndef CETime_h
23 #define CETime_h
24 
25 #include <cmath>
26 #include <stdio.h>
27 #include <vector>
28 
29 #include "CENamespace.h"
30 
31 // Time types are defined as:
32 // UTC - Coordinate Universal Time
33 // GAST - Greenwich Apparent Sidereal Time
34 // LAST - Local Apparent Sidereal Time
35 // LOCALTIME - Local time (defined as the UTC + timezone_shift_)
36 enum CETimeType {UTC, GAST, LAST, LOCALTIME} ;
37 
38 class CETime {
39 public:
40  // Default constructor
41  CETime() ;
42  // Primary constructor
43  CETime(const double& time,
44  CETimeType time_format=CETimeType::UTC) ;
45  CETime(std::vector<double> time,
46  CETimeType time_format=CETimeType::UTC) ;
47  // Copy constructor
48  CETime(const CETime& other) ;
49  // Destructor
50  virtual ~CETime() ;
51 
52  CETime& operator=(const CETime& other);
53 
54  /*******************************************
55  * Get the time
56  *******************************************/
57 
58  double Hour(void) const;
59  double Min(void) const;
60  double Sec(void) const;
61 
62  static double CurrentUTC() ;
63  static std::vector<double> CurrentUTC_vect() ;
64  static double UTC(const double& jd) ;
65  static std::vector<double> UTC_vect(const double& jd) ;
66 
67  // Convert a double of the form HHMMSS.S to a vector with
68  // the same format as 'time_'
69  static std::vector<double> TimeDbl2Vect(const double& time) ;
70  static double TimeVect2Dbl(std::vector<double> time) ;
71 
72  // Convert number of seconds since midnight to HHMMSS.S formatted double
73  static double TimeSec2Time(const double& seconds) ;
74  static std::vector<double> TimeSec2Vect(const double& seconds) ;
75 
76  static double SystemUTCOffset_hrs()
77  {
78  time_t now ;
79  time (&now) ;
80  struct tm local;
81  localtime_r(&now, &local) ;
82  return local.tm_gmtoff/3600.0;
83  }
84 
85  /*******************************************
86  * Convert between the various time types
87  *******************************************/
88 
89  void SetTime(const double& time,
90  CETimeType time_format=CETimeType::UTC) ;
91  void SetTime(std::vector<double> time_vect,
92  CETimeType time_format=CETimeType::UTC) ;
93  void SetHours(const double& hours)
94  {time_[0] = hours ;}
95  void SetMinutes(const double& minutes)
96  {time_[1] = minutes ;}
97  void SetSeconds(const double& seconds)
98  {time_[2] = std::floor(seconds) ;
99  time_[3] = seconds-time_[2] ;}
100 
101  /*******************************************
102  * Convert between the various time types
103  *******************************************/
104 
105  // UTC conversions
106  static void UTC2GAST() ;
107  static void UTC2LAST() ;
108  static void UTC2LOCALTIME() ;
109 
110  // GAST conversions
111 
112 private:
113 
114  void copy_members(const CETime& other);
115  void init_members(void);
116  void free_members(void);
117 
118  // Internal methods for setting the time
119  void SetTime_UTC(std::vector<double> time);
120  void SetTime_GAST(std::vector<double> time);
121  void SetTime_LST(std::vector<double> time);
122  void SetTime_LOCALTIME(std::vector<double> time);
123 
124  // Variables for storing the time in various formats
125  // The vectors store the time in the following format:
126  // element 0 - hours
127  // element 1 - minutes
128  // element 2 - seconds
129  // element 3 - fractional seconds
130  // Note that the internal stored time is UTC
131  std::vector<double> time_ ;
133 };
134 
135 
136 /**********************************************************************/
141 inline
142 double CETime::Hour(void) const
143 {
144  return time_[0];
145 }
146 
147 
148 /**********************************************************************/
153 inline
154 double CETime::Min(void) const
155 {
156  return time_[1];
157 }
158 
159 
160 /**********************************************************************/
165 inline
166 double CETime::Sec(void) const
167 {
168  return time_[2]+time_[3];
169 }
170 
171 #endif /* CETime_h */
CETime::SetHours
void SetHours(const double &hours)
Definition: CETime.h:90
CETime::SetSeconds
void SetSeconds(const double &seconds)
Definition: CETime.h:94
UTC
Definition: CETime.h:53
CETime::init_members
void init_members(void)
Initialize data members.
Definition: CETime.cpp:350
CETime::copy_members
void copy_members(const CETime &other)
Copy data members from another object of the same type.
Definition: CETime.cpp:340
CENamespace.h
CETime::UTC
static double UTC(const double &jd)
Get the current UTC time.
Definition: CETime.cpp:169
CETime::UTC2LOCALTIME
static void UTC2LOCALTIME()
Definition: CETime.cpp:243
CETime::Hour
double Hour(void) const
Return the Hour associated with this time object.
Definition: CETime.h:141
CETime::TimeDbl2Vect
static std::vector< double > TimeDbl2Vect(const double &time)
Convert a time formatted as HHMMSS.SS into a vector.
Definition: CETime.cpp:277
CETime::UTC2LAST
static void UTC2LAST()
Definition: CETime.cpp:236
CETime
Definition: CETime.h:37
CETime::UTC2GAST
static void UTC2GAST()
Definition: CETime.cpp:229
CETimeType
CETimeType
Definition: CETime.h:35
CETime::SetTime_LOCALTIME
void SetTime_LOCALTIME(std::vector< double > time)
Set the time from a vector representing local observer time.
Definition: CETime.cpp:407
CETime::UTC_vect
static std::vector< double > UTC_vect(const double &jd)
Get the UTC time of a given julian date as a vector.
Definition: CETime.cpp:184
CETime::operator=
CETime & operator=(const CETime &other)
Copy assignment operator.
Definition: CETime.cpp:108
CETime::SetTime_GAST
void SetTime_GAST(std::vector< double > time)
Set the time from a vector representing Greenwich Apparent Sidereal Time.
Definition: CETime.cpp:383
CETime::Sec
double Sec(void) const
Return the Second associated with this time object.
Definition: CETime.h:165
CETime::SetMinutes
void SetMinutes(const double &minutes)
Definition: CETime.h:92
CETime::SetTime
void SetTime(const double &time, CETimeType time_format=CETimeType::UTC)
Set time from double of the form HHMMSS.SS and a specified time format.
Definition: CETime.cpp:194
CETime::TimeSec2Time
static double TimeSec2Time(const double &seconds)
Convert number of seconds since midnight to HHMMSS.S formatted double.
Definition: CETime.cpp:300
CETime::~CETime
virtual ~CETime()
Destructor.
Definition: CETime.cpp:96
CETime::CurrentUTC
static double CurrentUTC()
Get the current UTC time as seconds since midnight.
Definition: CETime.cpp:124
CETime::CETime
CETime()
Default constructor.
Definition: CETime.cpp:38
CETime::free_members
void free_members(void)
Deallocate data members if necessary.
Definition: CETime.cpp:361
CETime::Min
double Min(void) const
Return the Minute associated with this time object.
Definition: CETime.h:153
CETime::SystemUTCOffset_hrs
static double SystemUTCOffset_hrs()
Definition: CETime.h:74
CETime::TimeVect2Dbl
static double TimeVect2Dbl(std::vector< double > time)
Convert a time formatted as HHMMSS.SS into a vector.
Definition: CETime.cpp:260
CETime::time_type_
CETimeType time_type_
Definition: CETime.h:128
LAST
Definition: CETime.h:53
CETime::SetTime_UTC
void SetTime_UTC(std::vector< double > time)
Set the time from a vector representing UTC time.
Definition: CETime.cpp:371
LOCALTIME
Definition: CETime.h:53
CETime::TimeSec2Vect
static std::vector< double > TimeSec2Vect(const double &seconds)
Convert number of seconds since midnight to HHMMSS.S formatted double.
Definition: CETime.cpp:326
CETime::SetTime_LST
void SetTime_LST(std::vector< double > time)
Set the time from a vector representing Local Sidereal Time.
Definition: CETime.cpp:397
CETime::CurrentUTC_vect
static std::vector< double > CurrentUTC_vect()
Get the current UTC time as a vector.
Definition: CETime.cpp:157
GAST
Definition: CETime.h:53
CETime::time_
std::vector< double > time_
Definition: CETime.h:127