41const double mjd_ref = 55197.000766018518519;
45#define G_CONSTRUCT "GTime::GTime(double&, std::string&)"
46#define G_SECS_GET "GTime::secs(std::string&)"
47#define G_SECS_SET "GTime::secs(double&, std::string&)"
48#define G_UTC "GTime::utc(std::string&)"
49#define G_UTC_GET "GTime::utc(int&)"
114 if (timeunit ==
"d" || timeunit ==
"day" || timeunit ==
"days") {
117 else if (timeunit ==
"s" || timeunit ==
"sec" || timeunit ==
"secs") {
121 std::string msg =
"Invalid time unit \""+unit+
"\" specified. Please "
122 "specify one of \"d\", \"day\", \"days\", \"s\", "
123 "\"sec\" or \"secs\"";
273 return new GTime(*
this);
361 if (timesys ==
"TT") {
366 else if (timesys ==
"TAI") {
374 else if (timesys ==
"UTC") {
385 std::string msg =
"Unknown time system \""+timesys+
"\". Either specify "
386 "\"TT\", \"TAI\" or \"UTC\".";
444 double julian_epoch = 2000.0 + (
jd(timesys) - 2451545.0) / 365.25;
469 std::string msg =
"Invalid precision \""+
gammalib::str(precision)+
"\""
470 " encountered. Please specify a precision >= 0.";
475 static int daymonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
490 double fraction =
mjd - (double)day;
507 for (
int i = 0; i < precision; ++i) {
516 int hour = (int)second / 3600;
517 second -= hour * 3600.0;
518 int minute = (int)second / 60;
519 second -= minute * 60.0;
556 if (day <= daymonth[month]) {
559 day -= daymonth[month];
566 int sec_width = precision + 2;
572 char utc_pattern[50];
573 #if ((defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L) || \
574 (defined __cplusplus && __cplusplus >= 201103L))
575 std::snprintf(utc_pattern, 50,
576 "%%4.4d-%%2.2d-%%2.2dT%%2.2d:%%2.2d:%%0%d.0%df",
577 sec_width, precision);
579 std::sprintf(utc_pattern,
"%%4.4d-%%2.2d-%%2.2dT%%2.2d:%%2.2d:%%0%d.0%df",
580 sec_width, precision);
585 #if ((defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L) || \
586 (defined __cplusplus && __cplusplus >= 201103L))
587 std::snprintf(
utc, 32, utc_pattern,
588 year, month, day, hour, minute, second);
590 std::sprintf(
utc, utc_pattern,
591 year, month, day, hour, minute, second);
595 return (std::string(
utc));
609 double d =
days(
"UTC") + 3652.50076602;
612 double gmst = 18.697374558 + 24.06570982441908 * d;
632 double d =
days(
"UTC") + 3652.50076602;
635 double Omega = 125.04 - 0.052954 * d;
638 double L = 280.47 + 0.98565 * d;
645 double epsilon = 23.4393 - 0.0000004 * d;
749 if (to_unit != 1.0) {
779void GTime::jd(
const double& time,
const std::string& timesys)
785 secs(seconds, timesys);
814void GTime::mjd(
const double& time,
const std::string& timesys)
820 secs(seconds, timesys);
833void GTime::secs(
const double& seconds,
const std::string& timesys)
836 if (timesys ==
"TT") {
841 else if (timesys ==
"TAI") {
847 else if (timesys ==
"UTC") {
855 std::string msg =
"Unknown time system \""+timesys+
"\". Either specify "
856 "\"TT\", \"TAI\" or \"UTC\".";
892 secs(seconds, timesys);
918 static int daymonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
927 int n = sscanf(time.c_str(),
"%ld-%d-%ldT%ld:%ld:%lg",
928 &year, &month, &day, &hour, &minute, &second);
939 if (n != 3 && n != 6) {
940 std::string msg =
"Invalid time string \""+time+
"\" encountered. "
941 "Please specify the time in the format YYYY-MM-DD "
942 "or YYYY-MM-DDThh:mm:ss.s.";
945 if (year < 1000 || year > 9999) {
946 std::string msg =
"Invalid year "+
gammalib::str(year)+
" specified. "
947 "The year needs to be a four-digit year.";
950 if (month < 1 || month > 12) {
951 std::string msg =
"Invalid month "+
gammalib::str(month)+
" specified. "
952 "The month needs to be comprised between 01 and 12";
955 if (day < 1 || day > daymonth[month-1]) {
956 std::string msg =
"Invalid day "+
gammalib::str(day)+
" specified. "
958 "to be comprised between 01 and "+
962 if (hour < 0 || hour > 23) {
963 std::string msg =
"Invalid hour "+
gammalib::str(hour)+
" specified. "
964 "The hour needs to be comprised between 00 and 23";
967 if (minute < 0 || minute > 59) {
968 std::string msg =
"Invalid minute "+
gammalib::str(minute)+
" specified. "
969 "The minute needs to be comprised between 00 and 59";
972 if (second < 0 || second >= 60.0) {
973 std::string msg =
"Invalid second "+
gammalib::str(second)+
" specified. "
974 "The second needs to be comprised between 00 and <60";
980 for (
int i = 0; i < month; ++i) {
983 day += (year - 1972) * 365 - 1;
984 day += (year - 1969) / 4;
985 day -= (year - 1901) / 100;
986 day += (year - 1601) / 400;
990 double fraction = ((double)hour * 3600.0 + (
double)minute * 60.0 + second) *
994 double mjd = (double)day + fraction;
1089 double second = 0.0;
1090 int n = sscanf(str.c_str(),
"%ld-%d-%ldT%ld:%ld:%lg",
1091 &year, &month, &day, &hour, &minute, &second);
1092 if (n == 3 || n == 6) {
1097 else if (str.find(
"MJD") == 0) {
1100 if (timesys.empty()) {
1103 mjd(timeval, timesys);
1107 else if (str.find(
"JD") == 0) {
1110 if (timesys.empty()) {
1113 jd(timeval, timesys);
1122 if (timesys.empty()) {
1126 set(timeval, timeref);
1142 struct std::tm timeStruct;
1147 now = std::time(NULL);
1148 #ifdef HAVE_GMTIME_R
1149 std::gmtime_r(&
now, &timeStruct);
1151 std::memcpy(&timeStruct, gmtime(&
now),
sizeof(
struct tm));
1155 #if ((defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L) || \
1156 (defined __cplusplus && __cplusplus >= 201103L))
1157 std::snprintf(buffer, 100,
"%04d-%02d-%02dT%02d:%02d:%02d",
1158 timeStruct.tm_year + 1900,
1159 timeStruct.tm_mon + 1,
1165 std::sprintf(buffer,
"%04d-%02d-%02dT%02d:%02d:%02d",
1166 timeStruct.tm_year + 1900,
1167 timeStruct.tm_mon + 1,
1175 std::string date = buffer;
1292 const long leapsmjd[] = {41317,
1320 const double leapsecs[] = {10.0, 11.0, 12.0, 13.0, 14.0,
1321 15.0, 16.0, 17.0, 18.0, 19.0,
1322 20.0, 21.0, 22.0, 23.0, 24.0,
1323 25.0, 26.0, 27.0, 28.0, 29.0,
1324 30.0, 31.0, 32.0, 33.0, 34.0,
1326 const int n_leapsecs =
sizeof(leapsmjd)/
sizeof(
long);
1329 long day = (long)
mjd;
1333 int i = n_leapsecs - 1;
1334 while ((day <= leapsmjd[i]) && i > 0) {
1339 return (leapsecs[i]);
1356 size_t length = time.length();
1357 size_t start = time.find_first_of(
"0123456789+-.");
1358 size_t stop = time.find(
"(");
1361 if (stop != std::string::npos) {
1366 if (start != std::string::npos) {
1371 std::string value = time.substr(start, length);
1398 std::string timesys =
"";
1404 if (str.find(
"(UTC)") != std::string::npos) {
1407 else if (str.find(
"(TAI)") != std::string::npos) {
1410 else if (str.find(
"(TT)") != std::string::npos) {
Exception handler interface definition.
Mathematical function definitions.
Time reference class interface definition.
const double jd_ref
JD of time=0.
const double mjd_ref
MJD of time=0.
Time class interface definition.
Implements a time reference.
double unitseconds(void) const
Return the time unit in seconds.
const double & mjdref(void) const
Return MJD reference (units: days)
const std::string & timeunit(void) const
Return time unit.
const std::string & timeref(void) const
Return time reference.
const std::string & timesys(void) const
Return time system.
void clear(void)
Clear time.
virtual ~GTime(void)
Destructor.
void copy_members(const GTime &time)
Copy class members.
std::string extract_timesys(const std::string &time) const
Extract time system from time string.
double extract_timeval(const std::string &time) const
Extract time value from time string.
double leap_seconds(void) const
Return number of leap seconds for current time.
double mjd(void) const
Return time in Modified Julian Days (TT)
GTimeReference reference(void) const
Returns native time reference.
bool is_leap_year(const int &year) const
Signals if year is a leap year.
double days(void) const
Return time in days in native reference (TT)
int days_in_year(const int &year) const
Returns number of days in year.
double gmst(void) const
Return Greenwich mean sidereal time in hours in a day.
std::string print(const GChatter &chatter=NORMAL) const
Print time.
double gast(void) const
Return Greenwich apparent sidereal time in hours in a day.
double m_time
Time in seconds in native reference (TT)
double convert(const GTimeReference &ref) const
Return time in specified reference.
std::string utc(const int &precision=0) const
Return time as string in UTC time system.
double lmst(const double &geolon) const
Return local mean sidereal time in hours in a day.
double last(const double &geolon) const
Return local apparent sidereal time in hours in a day.
void init_members(void)
Initialise class members.
void set(const double &time, const GTimeReference &ref)
Set time given in specified reference.
GTime * clone(void) const
Clone time.
double jd(void) const
Return time in Julian Days (TT)
GTime(void)
Void constructor.
void free_members(void)
Delete class members.
GTime & operator=(const GTime &time)
Assignment operator.
void now(void)
Set time to current time.
double julian_epoch(void) const
Return Julian epoch in native reference (TT)
const double & secs(void) const
Return time in seconds in native reference (TT)
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
double todouble(const std::string &arg)
Convert string into double precision value.
std::string tolower(const std::string &s)
Convert string to lower case.
std::string strip_whitespace(const std::string &arg)
Strip leading and trailing whitespace from string.
std::string toupper(const std::string &s)
Convert string to upper case.