41 const 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\".";
431 return (julian_epoch);
444 double julian_epoch = 2000.0 + (
jd(timesys) - 2451545.0) / 365.25;
447 return (julian_epoch);
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 sprintf(utc_pattern,
"%%4.4d-%%2.2d-%%2.2dT%%2.2d:%%2.2d:%%0%d.0%df",
574 sec_width, precision);
578 sprintf(utc, utc_pattern,
579 year, month, day, hour, minute, second);
582 return (std::string(utc));
596 double d =
days(
"UTC") + 3652.50076602;
599 double gmst = 18.697374558 + 24.06570982441908 * d;
602 gmst -= floor(gmst/24.0) * 24.0;
619 double d =
days(
"UTC") + 3652.50076602;
622 double Omega = 125.04 - 0.052954 * d;
625 double L = 280.47 + 0.98565 * d;
629 0.000024 *
std::sin(2.0 * L * gammalib::deg2rad);
632 double epsilon = 23.4393 - 0.0000004 * d;
635 double eqeq = DeltaPsi *
std::cos(epsilon * gammalib::deg2rad);
641 gast -= floor(gast/24.0) * 24.0;
662 lmst -= floor(lmst/24.0) * 24.0;
683 last -= floor(last/24.0) * 24.0;
736 if (to_unit != 1.0) {
766 void GTime::jd(
const double& time,
const std::string& timesys)
772 secs(seconds, timesys);
801 void GTime::mjd(
const double& time,
const std::string& timesys)
807 secs(seconds, timesys);
820 void GTime::secs(
const double& seconds,
const std::string& timesys)
823 if (timesys ==
"TT") {
828 else if (timesys ==
"TAI") {
834 else if (timesys ==
"UTC") {
842 std::string msg =
"Unknown time system \""+timesys+
"\". Either specify "
843 "\"TT\", \"TAI\" or \"UTC\".";
879 secs(seconds, timesys);
905 static int daymonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
914 int n = sscanf(time.c_str(),
"%ld-%d-%ldT%ld:%ld:%lg",
915 &year, &month, &day, &hour, &minute, &second);
926 if (n != 3 && n != 6) {
927 std::string msg =
"Invalid time string \""+time+
"\" encountered. "
928 "Please specify the time in the format YYYY-MM-DD "
929 "or YYYY-MM-DDThh:mm:ss.s.";
932 if (year < 1000 || year > 9999) {
933 std::string msg =
"Invalid year "+
gammalib::str(year)+
" specified. "
934 "The year needs to be a four-digit year.";
937 if (month < 1 || month > 12) {
938 std::string msg =
"Invalid month "+
gammalib::str(month)+
" specified. "
939 "The month needs to be comprised between 01 and 12";
942 if (day < 1 || day > daymonth[month-1]) {
943 std::string msg =
"Invalid day "+
gammalib::str(day)+
" specified. "
945 "to be comprised between 01 and "+
949 if (hour < 0 || hour > 23) {
950 std::string msg =
"Invalid hour "+
gammalib::str(hour)+
" specified. "
951 "The hour needs to be comprised between 00 and 23";
954 if (minute < 0 || minute > 59) {
955 std::string msg =
"Invalid minute "+
gammalib::str(minute)+
" specified. "
956 "The minute needs to be comprised between 00 and 59";
959 if (second < 0 || second >= 60.0) {
960 std::string msg =
"Invalid second "+
gammalib::str(second)+
" specified. "
961 "The second needs to be comprised between 00 and <60";
967 for (
int i = 0; i < month; ++i) {
970 day += (year - 1972) * 365 - 1;
971 day += (year - 1969) / 4;
972 day -= (year - 1901) / 100;
973 day += (year - 1601) / 400;
977 double fraction = ((double)hour * 3600.0 + (
double)minute * 60.0 + second) *
981 double mjd = (double)day + fraction;
1076 double second = 0.0;
1077 int n = sscanf(str.c_str(),
"%ld-%d-%ldT%ld:%ld:%lg",
1078 &year, &month, &day, &hour, &minute, &second);
1079 if (n == 3 || n == 6) {
1084 else if (str.find(
"MJD") == 0) {
1087 if (timesys.empty()) {
1090 mjd(timeval, timesys);
1094 else if (str.find(
"JD") == 0) {
1097 if (timesys.empty()) {
1100 jd(timeval, timesys);
1109 if (timesys.empty()) {
1113 set(timeval, timeref);
1129 struct std::tm timeStruct;
1134 now = std::time(NULL);
1135 #ifdef HAVE_GMTIME_R
1136 std::gmtime_r(&now, &timeStruct);
1138 std::memcpy(&timeStruct, gmtime(&now),
sizeof(
struct tm));
1142 std::sprintf(buffer,
"%04d-%02d-%02dT%02d:%02d:%02d",
1143 timeStruct.tm_year + 1900,
1144 timeStruct.tm_mon + 1,
1151 std::string date = buffer;
1268 const long leapsmjd[] = {41317,
1296 const double leapsecs[] = {10.0, 11.0, 12.0, 13.0, 14.0,
1297 15.0, 16.0, 17.0, 18.0, 19.0,
1298 20.0, 21.0, 22.0, 23.0, 24.0,
1299 25.0, 26.0, 27.0, 28.0, 29.0,
1300 30.0, 31.0, 32.0, 33.0, 34.0,
1302 const int n_leapsecs =
sizeof(leapsmjd)/
sizeof(
long);
1305 long day = (long)mjd;
1309 int i = n_leapsecs - 1;
1310 while ((day <= leapsmjd[i]) && i > 0) {
1315 return (leapsecs[i]);
1332 size_t length = time.length();
1333 size_t start = time.find_first_of(
"0123456789+-.");
1334 size_t stop = time.find(
"(");
1337 if (stop != std::string::npos) {
1342 if (start != std::string::npos) {
1347 std::string value = time.substr(start, length);
1374 std::string timesys =
"";
1380 if (str.find(
"(UTC)") != std::string::npos) {
1383 else if (str.find(
"(TAI)") != std::string::npos) {
1386 else if (str.find(
"(TT)") != std::string::npos) {
std::string extract_timesys(const std::string &time) const
Extract time system from time string.
void init_members(void)
Initialise class members.
void now(void)
Set time to current time.
GTimeReference reference(void) const
Returns native time reference.
const std::string & timeunit(void) const
Return time unit.
const double mjd_ref
MJD of time=0.
double lmst(const double &geolon) const
Return local mean sidereal time in hours in a day.
void copy_members(const GTime &time)
Copy class members.
GVector cos(const GVector &vector)
Computes cosine of vector elements.
double unitseconds(void) const
Return the time unit in seconds.
void clear(void)
Clear time.
std::string strip_whitespace(const std::string &arg)
Strip leading and trailing whitespace from string.
double extract_timeval(const std::string &time) const
Extract time value from time string.
double jd(void) const
Return time in Julian Days (TT)
bool is_leap_year(const int &year) const
Signals if year is a leap year.
virtual ~GTime(void)
Destructor.
const double & mjdref(void) const
Return MJD reference (units: days)
const double jd_ref
JD of time=0.
void free_members(void)
Delete class members.
GTime(void)
Void constructor.
Time reference class interface definition.
double leap_seconds(void) const
Return number of leap seconds for current time.
const std::string & timesys(void) const
Return time system.
std::string print(const GChatter &chatter=NORMAL) const
Print time.
const std::string & timeref(void) const
Return time reference.
GTime * clone(void) const
Clone time.
double gmst(void) const
Return Greenwich mean sidereal time in hours in a day.
const double & secs(void) const
Return time in seconds in native reference (TT)
double last(const double &geolon) const
Return local apparent sidereal time in hours in a day.
GTime & operator=(const GTime &time)
Assignment operator.
double gast(void) const
Return Greenwich apparent sidereal time in hours in a day.
GVector sin(const GVector &vector)
Computes sine of vector elements.
Exception handler interface definition.
std::string toupper(const std::string &s)
Convert string to upper case.
Implements a time reference.
std::string tolower(const std::string &s)
Convert string to lower case.
double mjd(void) const
Return time in Modified Julian Days (TT)
int days_in_year(const int &year) const
Returns number of days in year.
double julian_epoch(void) const
Return Julian epoch in native reference (TT)
std::string utc(const int &precision=0) const
Return time as string in UTC time system.
Time class interface definition.
void set(const double &time, const GTimeReference &ref)
Set time given in specified reference.
double convert(const GTimeReference &ref) const
Return time in specified reference.
double days(void) const
Return time in days in native reference (TT)
double m_time
Time in seconds in native reference (TT)
Mathematical function definitions.
double todouble(const std::string &arg)
Convert string into double precision value.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.