COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
timelib.h
Go to the documentation of this file.
1 /********************************************************************
2 * Copyright (C) 2015 by Interstel Technologies, Inc.
3 * and Hawaii Space Flight Laboratory.
4 *
5 * This file is part of the COSMOS/core that is the central
6 * module for COSMOS. For more information on COSMOS go to
7 * <http://cosmos-project.com>
8 *
9 * The COSMOS/core software is licenced under the
10 * GNU Lesser General Public License (LGPL) version 3 licence.
11 *
12 * You should have received a copy of the
13 * GNU Lesser General Public License
14 * If not, go to <http://www.gnu.org/licenses/>
15 *
16 * COSMOS/core is free software: you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public License
18 * as published by the Free Software Foundation, either version 3 of
19 * the License, or (at your option) any later version.
20 *
21 * COSMOS/core is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * Refer to the "licences" folder for further information on the
27 * condititons and terms to use this software.
28 ********************************************************************/
29 
35 
51 #ifndef _TIMELIB_H
52 #define _TIMELIB_H 1
53 
54 #include "support/configCosmos.h"
55 #include "support/elapsedtime.h"
56 #include "math/mathlib.h"
57 
58 #ifdef COSMOS_WIN_BUILD_MSVC
59 #include "support/timeutils.h"
60 #endif
61 
62 #include <cmath>
63 #include <time.h>
64 #include <errno.h>
65 #include <stdio.h>
66 #include <ctime>
67 #include <ratio>
68 
72 
73 #define MJD_UNIX_OFFSET 40587.
74 #define JD_MJD_OFFSET 2400000.5
75 #define MJD2JD(mjd) (double)((mjd) + JD_MJD_OFFSET)
76 #define JD2MJD(jd) (double)((jd) - JD_MJD_OFFSET)
77 #define DAY_TO_SECONDS 86400.
78 //#define CURR_MJD current_mjd()
79 //#define CURRENT_TIME_us CURR_MJD*DAY_TO_SECONDS*1000*1000
80 #define SECONDS_TO_DAYS(x) ((double)(x) / DAY_TO_SECONDS)
81 #define DAYS_TO_SECONDS(x) ((double)(x) * DAY_TO_SECONDS)
82 #define TIME_SECS_TO_DAYS(x) ((double)(x) / DAY_TO_SECONDS)
83 #define TIME_DAYS_TO_SECS(x) ((double)(x) * DAY_TO_SECONDS)
84 #define TIME_SECS_SINCE_MJD(x) ( ( (currentmjd(0.)-(x)) ) * DAY_TO_SECONDS )
85 #define TIME_UNIXs_TO_MJD(unixSecs) ( ( (unixSecs) / DAY_TO_SECONDS ) + MJD_UNIX_OFFSET ) // From http://stackoverflow.com/questions/466321/convert-unix-timestamp-to-julian
86 #define TIME_MJD_TO_UNIXs(mjd) (((mjd) - MJD_UNIX_OFFSET) * DAY_TO_SECONDS) // Inverse operation of above
87 #define TIME_UNIX_TV_TO_DOUBLE_SECS(x) ( ((double)(x.tv_sec)) + ((double)(x.tv_usec) / 1000000. ) )
88 #define UPTIME (DAY_TO_SECONDS*(currentmjd(0.)-mjd_start_time))
89 
91 
95 struct timestruc
96 {
97  double mjd;
98  int32_t year;
99  int32_t month;
100  int32_t day;
101  double fd;
102  double tt_mjd;
103  double gmst_rad;
104 };
105 
106 struct calstruc
107 {
108  int32_t year;
109  int32_t month;
110  int32_t dom;
111  int32_t doy;
112  int32_t hour;
113  int32_t minute;
114  int32_t second;
115  int32_t nsecond;
116 };
117 
118 class DateTime {
119 
120 public:
121  double mjd; // represented in Modified Julian Date
122 
123  DateTime();
124  DateTime(int year, int month, int day, int hour, int minute, double seconds);
125 // ~DateTime();
126 
127 };
128 
130 
134 
135 double currentmjd(double offset);
136 double currentmjd();
137 unsigned long get_unix_time();
138 string get_local_time();
139 
140 // gregorian calendar (year, month, day) to another format
141 double cal2mjd(calstruc date);
142 double cal2mjd(double year);
143 double cal2mjd(int32_t year, double dayOfYear=1.);
144 double cal2mjd(int32_t year, int32_t month=1, double day=1.);
145 double cal2mjd(int32_t year, int32_t month=1, int32_t day=1, int32_t hour=0, int32_t minute=0, int32_t second=0, int32_t nsecond=0);
146 double gregorianToModJulianDate(int32_t year, int32_t month, int32_t day,
147  int32_t hour, int32_t minute, double second);
148 
149 // utc to another format
150 struct timeval utc2unix(double utc);
151 double utc2unixseconds(double utc);
152 double utc2epsilon(double mjd);
153 double utc2depsilon(double mjd);
154 double utc2dpsi(double mjd);
155 double utc2L(double mjd);
156 double utc2Lp(double mjd);
157 double utc2F(double mjd);
158 double utc2D(double mjd);
159 double utc2omega(double mjd);
160 double utc2zeta(double mjd);
161 double utc2z(double mjd);
162 double utc2era(double mjd);
163 double utc2tt(double mjd);
164 double utc2gps(double utc);
165 double utc2ut1(double mjd);
166 double utc2dut1(double mjd);
167 double utc2tdb(double mjd);
168 double utc2tdb(double mjd);
169 double utc2gmst1982(double mjd);
170 double utc2gmst2000(double mjd);
171 double utc2gast(double mjd);
172 rvector utc2nuts(double mjd);
173 double utc2theta(double mjd);
174 double utc2jcentt(double mjd);
175 double utc2jcenut1(double mjd);
176 string utc2unixdate(double utc);
177 string utc2iso8601(double mjd);
178 double iso86012utc(string date);
179 
180 // gps to another format
181 double gps2utc(double gps);
182 void gps2week(double gps, uint32_t& week, double& seconds);
183 double week2gps(uint32_t week, double seconds);
184 
185 // mjd to another format
186 double mjd2year(double mjd);
187 double mjd2doy(double mjd);
188 calstruc mjd2cal(double mjd);
189 int32_t mjd2ymd(double mjd, int32_t &year, int32_t &month, double &day);
190 int32_t mjd2ymd(double mjd, int32_t &year, int32_t &month, double &day, double &doy);
191 string mjdToGregorian(double mjd);
192 int32_t mjdToGregorian(double mjd, int32_t *year, int32_t *month, int32_t *day,
193  int32_t *hour, int32_t *minute, int32_t *second);
194 string mjdToGregorianFormat(double mjd);
195 string mjdToGregorianDDMMMYYYY(double mjd);
196 string mjdToGregorianDDMmmYYYY(double mjd);
197 string mjd2iso8601(double mjd);
198 double mjd2jd(double mjd);
199 double jd2mjd(double jd);
200 int32_t mjd2tlef(double mjd, string &tle);
201 
202 // other
203 double unix2utc(struct timeval unixtime);
204 double unix2utc(double unixtime);
205 double tt2utc(double mjd);
206 double tt2tdb(double mjd);
207 double julcen(double mjd);
208 cvector polar_motion(double mjd);
209 int32_t leap_seconds(double mjd);
210 double ranrm(double angle);
211 int16_t isleap(int32_t year);
212 int32_t load_iers();
213 string seconds2DDHHMMSS(double elapsed_seconds);
214 int32_t timed_countdown(int32_t seconds, int32_t step=10., string message="");
215 double set_local_clock(double utc_to);
216 
217 
219 
220 #endif
double mjd2jd(double mjd)
Modified Julian Day to Julian Day.
Definition: timelib.cpp:1563
int32_t dom
Definition: timelib.h:110
string mjdToGregorianFormat(double mjd)
Definition: timelib.cpp:1417
double utc2tdb(double mjd)
Convert UTC to TDB.
Definition: timelib.cpp:826
calstruc mjd2cal(double mjd)
MJD to Calendar.
Definition: timelib.cpp:180
double utc2gps(double utc)
Convert UTC to GPS.
Definition: timelib.cpp:925
double utc2dut1(double mjd)
Calculate DUT1.
Definition: timelib.cpp:751
static std::vector< tlestruc > tle
Definition: agent_antenna.cpp:177
double julcen(double mjd)
Julian Century.
Definition: timelib.cpp:815
double utc2era(double mjd)
Earth Rotation Angle.
Definition: timelib.cpp:1027
double utc2theta(double utc)
Precession theta value.
Definition: timelib.cpp:737
double utc2D(double mjd)
Nutation D value.
Definition: timelib.cpp:658
3 element generic row vector
Definition: vector.h:53
double utc2jcenut1(double mjd)
UT1 Julian Century.
Definition: timelib.cpp:488
string seconds2DDHHMMSS(double elapsed_seconds)
Convert Elapsed Time in Seconds to Human Readable Format (used for GPS simulator) ...
Definition: timelib.cpp:1514
double utc2omega(double mjd)
Nutation omega value.
Definition: timelib.cpp:679
int32_t month
Definition: timelib.h:99
int32_t mjdToGregorian(double mjd, int32_t *year, int32_t *month, int32_t *day, int32_t *hour, int32_t *minute, int32_t *second)
Definition: timelib.h:118
double unix2utc(struct timeval unixtime)
Unix time to UTC.
Definition: timelib.cpp:120
int32_t day
Definition: timelib.h:100
double utc2z(double utc)
Precession z value.
Definition: timelib.cpp:720
double ranrm(double angle)
Definition: timelib.cpp:1109
int32_t hour
Definition: timelib.h:112
double fd
Definition: timelib.h:101
double utc2F(double mjd)
Nutation F value.
Definition: timelib.cpp:637
double utc2unixseconds(double utc)
UTC to Unix time.
Definition: timelib.cpp:167
double utc2gast(double mjd)
UTC to GAST.
Definition: timelib.cpp:1048
double utc2jcentt(double mjd)
TT Julian Century.
Definition: timelib.cpp:462
int32_t load_iers()
Load IERS Polar Motion, UT1-UTC, Leap Seconds.
Definition: timelib.cpp:1158
int32_t nsecond
Definition: timelib.h:115
string mjdToGregorianDDMMMYYYY(double mjd)
Definition: timelib.cpp:1442
double iso86012utc(string date)
Definition: timelib.cpp:1305
Definition: timelib.h:106
double tt_mjd
Definition: timelib.h:102
cvector polar_motion(double mjd)
Polar motion.
Definition: timelib.cpp:1221
string utc2unixdate(double utc)
Time for setting unix date.
Definition: timelib.cpp:1260
double set_local_clock(double utc_to)
Definition: timelib.cpp:1626
int32_t month
Definition: timelib.h:109
double utc2L(double mjd)
Nutation L value.
Definition: timelib.cpp:595
double utc2depsilon(double mjd)
Nutation Delta Epsilon value.
Definition: timelib.cpp:552
uint8_t message[300]
Definition: kpc9612p_send.cpp:36
int32_t mjd2ymd(double mjd, int32_t &year, int32_t &month, double &day)
MJD to Year, Month, and Decimal Day (overloaded)
Definition: timelib.cpp:217
double mjd
Definition: timelib.h:97
Headers and definitions common to all COSMOS.
int16_t isleap(int32_t year)
Check for Leap year.
Definition: timelib.cpp:1122
double cal2mjd(int32_t year, int32_t month, int32_t day, int32_t hour, int32_t minute, int32_t second, int32_t nsecond)
Calendar representation to Modified Julian Day - full.
Definition: timelib.cpp:294
double gps2utc(double gps)
Convert GPS to UTC.
Definition: timelib.cpp:946
void gps2week(double gps, uint32_t &week, double &seconds)
GPS Weeks and Seconds from GPS time.
Definition: timelib.cpp:967
string get_local_time()
Definition: timelib.cpp:92
int32_t mjd2tlef(double mjd, string &tle)
Convert mjd to the TLE epoch format.
Definition: timelib.cpp:1613
Definition: timelib.h:95
double jd2mjd(double jd)
Julian Day to Modified Julian Day.
Definition: timelib.cpp:1572
int32_t minute
Definition: timelib.h:113
int32_t timed_countdown(int32_t seconds, int32_t step, string message)
Definition: timelib.cpp:1576
double mjd
Definition: timelib.h:121
struct timeval utc2unix(double utc)
UTC to Unix time.
Definition: timelib.cpp:152
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
mathlib include file
double tt2tdb(double mjd)
Elapsed Time header file.
double mjd2doy(double mjd)
Day of Year from MJD.
Definition: timelib.cpp:1012
string utc2iso8601(double utc)
ISO 8601 version of time.
Definition: timelib.cpp:1286
double utc2epsilon(double mjd)
Nutation Epsilon value.
Definition: timelib.cpp:573
double utc2zeta(double utc)
Precession zeta value.
Definition: timelib.cpp:703
unsigned long int get_unix_time()
Definition: timelib.cpp:86
int32_t year
Definition: timelib.h:108
int32_t year
Definition: timelib.h:98
double gregorianToModJulianDate(int32_t year, int32_t month, int32_t day, int32_t hour, int32_t minute, double second)
Definition: timelib.cpp:448
string mjdToGregorianDDMmmYYYY(double mjd)
Definition: timelib.cpp:1479
double utc2gmst1982(double mjd)
UTC (Modified Julian Day) to GMST.
Definition: timelib.cpp:1075
int32_t second
Definition: timelib.h:114
double week2gps(uint32_t week, double seconds)
GPS Time from GPS Week and Seconds.
Definition: timelib.cpp:983
double utc2ut1(double mjd)
Convert UTC to UT1.
Definition: timelib.cpp:792
rvector utc2nuts(double mjd)
Nutation values.
Definition: timelib.cpp:508
string mjd2iso8601(double mjd)
Definition: timelib.cpp:1316
double gmst_rad
Definition: timelib.h:103
int32_t doy
Definition: timelib.h:111
double utc2dpsi(double mjd)
Nutation Delta Psi value.
Definition: timelib.cpp:531
double utc2Lp(double mjd)
Nutation L prime value.
Definition: timelib.cpp:616
double utc2gmst2000(double utc)
Definition: timelib.cpp:1092
double tt2utc(double mjd)
Convert TT to UTC.
Definition: timelib.cpp:852
int32_t leap_seconds(double mjd)
Leap Seconds.
Definition: timelib.cpp:1193
3 element cartesian vector
Definition: vector.h:107
double utc2tt(double mjd)
Convert UTC to TT.
Definition: timelib.cpp:884
double mjd2year(double mjd)
Year from MJD.
Definition: timelib.cpp:997