37 double get_curr_time<double, CLOCK_REALTIME>(timespec & tsp)
39 clock_gettime(CLOCK_REALTIME, &tsp);
41 return ((
double)tsp.tv_sec) + ((double)tsp.tv_nsec)/1e9;
45 double get_curr_time<double, CLOCK_REALTIME>()
48 return get_curr_time<double,CLOCK_REALTIME>(tsp);
53 std::this_thread::sleep_for(std::chrono::seconds(sec));
58 std::this_thread::sleep_for(std::chrono::milliseconds(msec));
63 std::this_thread::sleep_for(std::chrono::microseconds(usec));
68 std::this_thread::sleep_for(std::chrono::nanoseconds(nsec));
75 ts.tv_nsec += nsec % 1000000000;
76 ts.tv_sec += nsec / 1000000000;
78 if(ts.tv_nsec > 999999999)
80 ts.tv_nsec -= 1000000000;
96 int rv = sofa::iauCal2jd(yr, mon,
day, &djm0, &djm);
98 if (rv < 0)
return (
double) rv;
101 djm0 = djm + (( (double) hr)/ (24.0) + ( (
double) min)/ (60.0*24.0) + sec / (3600.0*24.0));
112 const std::string & fdate
115 if(fdate.length() < 19)
return -4;
118 yr = atoi(fdate.substr(0,4).c_str());
119 mon = atoi(fdate.substr(5,2).c_str());
120 day = atoi(fdate.substr(8,2).c_str());
123 parse_hms( _hr, _min, sec, fdate.substr(11));
134 if(fdate.length() < 19)
return -4;
136 int yr, mon,
day, hr, min;
152 gmtime_r(&timeIn, &bdt);
156 strftime(tstr, 25,
"%FT%H:%M:%S", &bdt);
158 std::string result = tstr;
160 if(timeZone == 1) result +=
"Z";
161 if(timeZone == 2) result +=
"+00:00";
175 snprintf(tstr, 20,
".%09ld", timeIn.tv_nsec);
179 if(timeZone == 1) result +=
"Z";
180 if(timeZone == 2) result +=
"+00:00";
197 sofa::iauJd2cal( DJM0, timeIn, &iy, &im, &
id, &fd);
202 fd = (fd - hr/24.0)*24.0;
206 fd = (fd - mn/60.0)*3600.0;
210 snprintf(tstr, 32,
"%04d-%02d-%02dT%02d:%02d:%012.9f", iy,im,
id,hr,mn,fd);
212 std::string result = tstr;
214 if(timeZone == 1) result +=
"Z";
215 if(timeZone == 2) result +=
"+00:00";
226 time_t t0 = ts.tv_sec;
228 if(gmtime_r(&t0, &uttime) == 0)
230 std::cerr <<
"Error getting UT time (gmtime_r returned 0). At: " << __FILE__ <<
" " << __LINE__ <<
"\n";
236 snprintf(buffer,
sizeof(buffer),
"%04i%02i%02i%02i%02i%02i%09i", uttime.tm_year+1900, uttime.tm_mon+1, uttime.tm_mday, uttime.tm_hour, uttime.tm_min, uttime.tm_sec,
static_cast<int>(ts.tv_nsec));
245 const timespec & tsp,
254 tmrv = gmtime_r(&tsp.tv_sec, tm0);
255 if(tmrv == 0)
return -10;
258 rv1 = sofa::iauDat(1900+tm0->tm_year, 1+tm0->tm_mon, tm0->tm_mday, 0.0, &dat);
259 if(rv1 < 0)
return rv1;
262 rv2 = sofa::iauCal2jd(1900+tm0->tm_year, 1+tm0->tm_mon, tm0->tm_mday, &djm0, &djm);
263 if(rv2 < 0)
return rv2;
266 djmf = ((double) tm0->tm_hour)/24.0 + ((double) tm0->tm_min)/(24.0*60.) + (((
double) tm0->tm_sec)+((
double) tsp.tv_nsec/1e9) + dat )/(24.0*3600.0);
282 double means = (ts1.tv_sec + ts2.tv_sec)/2.0;
283 double meanns = (ts1.tv_nsec + ts2.tv_nsec)/2.0;
285 ts1.tv_sec = floor(means);
286 ts1.tv_nsec = round(meanns);
288 if( means != floor(means) )
292 if(ts1.tv_nsec >= 1e9)
310 return ( ((tsL.tv_sec == tsR.tv_sec) && (tsL.tv_nsec < tsR.tv_nsec)) || (tsL.tv_sec < tsR.tv_sec));
317 return ( ((tsL.tv_sec == tsR.tv_sec) && (tsL.tv_nsec > tsR.tv_nsec)) || (tsL.tv_sec > tsR.tv_sec));
320 bool operator==( timespec
const& tsL,
324 return ( (tsL.tv_sec == tsR.tv_sec) && (tsL.tv_nsec == tsR.tv_nsec) );
331 return ( tsL < tsR || tsL == tsR );
338 return ( tsL > tsR || tsL == tsR );
constexpr units::realT day()
Length of day.
void nanoSleep(unsigned nsec)
Sleep for a specified period in nanoseconds.
void sleep(unsigned sec)
Sleep for a specified period in seconds.
void milliSleep(unsigned msec)
Sleep for a specified period in milliseconds.
void microSleep(unsigned usec)
Sleep for a specified period in microseconds.
bool operator<(timespec const &tsL, timespec const &tsR)
Timespec comparison operator < (see caveats)
bool operator>(timespec const &tsL, timespec const &tsR)
Timespec comparison operator > (see caveats)
bool operator>=(timespec const &tsL, timespec const &tsR)
Timespec comparison operator >= (see caveats)
bool operator<=(timespec const &tsL, timespec const &tsR)
Timespec comparison operator <= (see caveats)
int ISO8601dateBreakdown(int &yr, int &mon, int &day, int &hr, int &min, double &sec, const std::string &fdate)
Parse an ISO8601 date of the form "YYYY-MM-DDTHH:MM:SS.S" into the individual components.
std::string ISO8601DateTimeStr(const timeT &timeIn, int timeZone=0)
Get a date-time string in ISO 8601 format.
std::string ISO8601DateTimeStrMJD(const double &timeIn, int timeZone=0)
Get a date-time string in ISO 8601 format for an MJD.
int timespecUTC2TAIMJD(double &djm, double &djmf, const timespec &tsp, tm *tm0)
Convert a UTC timespec to TAI modified Julian date.
std::string ISO8601DateTimeStr< timespec >(const timespec &timeIn, int timeZone)
Get a date-time string in ISO 8601 format for timespec.
void timespecAddNsec(timespec &ts, unsigned nsec)
Adds a time offset to an existing timespec.
void parse_hms(floatT &h, floatT &m, floatT &s, const std::string &hmsstr)
int timeStamp(std::string &tstamp, timespec &ts)
Get a timestamp string in the form YYYYMMDDHHMMSS.SSSSSSSSS.
double Cal2mjd(int yr, int mon, int day, int hr, int min, double sec)
Converts a Gregorian calendar date into modified Julian date (MJD).
double ISO8601date2mjd(const std::string &fdate)
Parse an ISO8601 date of the form "YYYY-MM-DDTHH:MM:SS.S" and return the modified Julian date (MJD)
std::string ISO8601DateTimeStr< time_t >(const time_t &timeIn, int timeZone)
Get a date-time string in ISO 8601 format for time_t.
Utilities for working with time.
timespec meanTimespec(timespec ts1, timespec ts2)
Calculate the mean time of two times given by timespecs.