CppEphem
CETime.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * CETime.cpp: 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 
28 // C++ HEADERS
29 #include <stdio.h>
30 #include <chrono>
31 #include <ctime>
32 
33 // CPPEPHEM HEADERS
34 #include "CETime.h"
35 
36 /**********************************************************************/
40 {
41  init_members();
42 }
43 
44 /**********************************************************************/
50 CETime::CETime(const double& time,
51  CETimeType time_format)
52 {
53  init_members();
54  SetTime(time, time_format);
55 }
56 
57 
58 /**********************************************************************/
68 CETime::CETime(std::vector<double> time, CETimeType time_format)
69 {
70  init_members();
71  time_type_ = time_format;
72  // Just in case the passed "time" variable isnt the same length
73  for (size_t i=0; i<time.size(); i++) {
74  time_[i] = time[i] ;
75  // In case "time" has more than 4 elements,
76  // quit when we've stored the first 4
77  if (i==3) break ;
78  }
79 }
80 
81 
82 /**********************************************************************/
87 CETime::CETime(const CETime& other)
88 {
89  init_members();
90  copy_members(other);
91 }
92 
93 
94 /**********************************************************************/
98 {
99  free_members();
100 }
101 
102 
103 /**********************************************************************/
109 CETime& CETime::operator=(const CETime& other)
110 {
111  if (this != &other) {
112  free_members();
113  init_members();
114  copy_members(other);
115  }
116  return *this;
117 }
118 
119 
120 /**********************************************************************/
125 double CETime::CurrentUTC()
126 {
127  // Construct time_t of this moment
128  std::chrono::system_clock::time_point now(std::chrono::system_clock::now());
129  std::time_t now_t(std::chrono::system_clock::to_time_t(
130  std::chrono::system_clock::time_point(now)));
131 
132  // Get the time at midnight
133  struct tm midnight;
134  gmtime_r(&now_t, &midnight) ;
135  midnight.tm_hour = 0 ;
136  midnight.tm_min = 0 ;
137  midnight.tm_sec = 0 ;
138 
139  // Midnight object
140  auto millisec_start(std::chrono::system_clock::from_time_t(mktime(&midnight)));
141 
142  double start = std::chrono::duration_cast<std::chrono::microseconds>(millisec_start.time_since_epoch()).count()/1000000.0 ;
143  double stop = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count()/1000000.0 ;
144 
145  // Return seconds and fractions of a second since midnight
146  return stop-start-midnight.tm_gmtoff ;
147 }
148 
149 /**********************************************************************/
158 std::vector<double> CETime::CurrentUTC_vect()
159 {
160  return TimeSec2Vect( CurrentUTC() ) ;
161 }
162 
163 
164 /**********************************************************************/
170 double CETime::UTC(const double& mjd)
171 {
172  return (mjd - std::floor(mjd)) * CppEphem::sec_per_day();
173 }
174 
175 /**********************************************************************/
185 std::vector<double> CETime::UTC_vect(const double& mjd)
186 {
187  return TimeDbl2Vect( TimeSec2Time( UTC(mjd) ) ) ;
188 }
189 
190 
191 /**********************************************************************/
195 void CETime::SetTime(const double& time, CETimeType time_format)
196 {
197  // Convert the double into a vector
198  std::vector<double> time_vect = TimeDbl2Vect(time) ;
199 
200  // Call the vector version of SetTime
201  SetTime(time_vect, time_format) ;
202 }
203 
204 
205 /**********************************************************************/
214 void CETime::SetTime(std::vector<double> time_vect, CETimeType time_format)
215 {
216  // Convert the double into a vector
217  if (time_format==CETimeType::UTC) {
218  SetTime_UTC(time_vect) ;
219  } else if (time_format==CETimeType::GAST) {
220  SetTime_GAST(time_vect) ;
221  } else if (time_format==CETimeType::LAST) {
222  SetTime_LST(time_vect) ;
223  } else if (time_format==CETimeType::LOCALTIME) {
224  SetTime_LOCALTIME(time_vect) ;
225  }
226 }
227 
228 /**********************************************************************/
230 void CETime::UTC2GAST()
231 {
232  // TODO
233 }
234 
235 /**********************************************************************/
237 void CETime::UTC2LAST()
238 {
239  // TODO
240 }
241 
242 /**********************************************************************/
245 {
246 
247 }
248 
249 # pragma mark - Protected Methods
250 
251 /**********************************************************************/
261 double CETime::TimeVect2Dbl(std::vector<double> time)
262 {
263  return time[0] * 10000 +
264  time[1] * 100 +
265  time[2] + time[3] ;
266 }
267 
268 /**********************************************************************/
278 std::vector<double> CETime::TimeDbl2Vect(const double& time)
279 {
280  // Create a vector to hold the information
281  std::vector<double> time_vect(4,0.0) ;
282 
283  // Get the seconds fraction
284  time_vect[3] = time - std::floor(time) ;
285  // Get the seconds value
286  time_vect[2] = int(std::floor(time)) % 100 ;
287  // Get the minutes
288  time_vect[1] = int(std::floor(time - time_vect[2]))/100 % 100 ;
289  // Get the hours
290  time_vect[0] = int(std::floor(time - time_vect[2] - time_vect[1])/10000) ;
291 
292  return time_vect ;
293 }
294 
295 /**********************************************************************/
301 double CETime::TimeSec2Time(const double& seconds)
302 {
303  // Make sure the seconds are positive
304  double secs(seconds);
305  while (secs < 0.0) {
306  secs += CppEphem::sec_per_day();
307  }
308 
309  // Now do the actual conversion to a vector
310  double fracsec = secs - std::floor(secs) ; // Fractions of a second
311  double sec = int(std::floor(secs)) % 60 ; // Whole seconds
312  double min = int(std::floor(secs-sec)/60) % 60 ; // Whole minutes
313  double hrs = int(std::floor(secs-sec)/60)/60 ; // Whole hours
314  return (hrs*10000) + (min*100) + sec + fracsec ; // Formatted double (HHMMSS.S)
315 }
316 
317 /**********************************************************************/
327 std::vector<double> CETime::TimeSec2Vect(const double& seconds)
328 {
329  return TimeDbl2Vect( TimeSec2Time(seconds) ) ;
330 }
331 
332 /*----------------------------------------
333  * PRIVATE MEMBERS
334  *---------------------------------------*/
335 
336 /**********************************************************************/
341 void CETime::copy_members(const CETime& other)
342 {
343  time_ = other.time_;
344  time_type_ = other.time_type_;
345 }
346 
347 
348 /**********************************************************************/
351 void CETime::init_members(void)
352 {
353  // Initialize the time information
354  time_ = std::vector<double>(4,0.0);
356 }
357 
358 
359 /**********************************************************************/
362 void CETime::free_members(void)
363 {
364  time_.clear();
365 }
366 
367 
368 /**********************************************************************/
372 void CETime::SetTime_UTC(std::vector<double> time)
373 {
374  time_ = time;
376 }
377 
378 
379 /**********************************************************************/
384 void CETime::SetTime_GAST(std::vector<double> time)
385 {
386 }
387 
388 
389 /**********************************************************************/
398 void CETime::SetTime_LST(std::vector<double> time)
399 {
400 }
401 
402 
403 /**********************************************************************/
408 void CETime::SetTime_LOCALTIME(std::vector<double> time)
409 {
410 }
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
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::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
CppEphem::sec_per_day
double sec_per_day()
Seconds per day.
Definition: CENamespace.h:71
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.h
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::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::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