36namespace timeUtilsDetail
39operations &operationsInstance()
41 static operations ops = { ::gmtime_r, sofa::iauDat, sofa::iauCal2jd };
47 operationsInstance() = { ::gmtime_r, sofa::iauDat, sofa::iauCal2jd };
55 clock_gettime( CLOCK_REALTIME, &tsp );
57 return ( (
double)tsp.tv_sec ) + ( (double)tsp.tv_nsec ) / 1e9;
69 std::this_thread::sleep_for( std::chrono::seconds( sec ) );
74 std::this_thread::sleep_for( std::chrono::milliseconds( msec ) );
79 std::this_thread::sleep_for( std::chrono::microseconds( usec ) );
84 std::this_thread::sleep_for( std::chrono::nanoseconds( nsec ) );
89 ts.tv_nsec += nsec % 1000000000;
90 ts.tv_sec += nsec / 1000000000;
92 if( ts.tv_nsec > 999999999 )
94 ts.tv_nsec -= 1000000000;
99double Cal2mjd(
int yr,
int mon,
int day,
int hr,
int min,
double sec )
104 int rv = timeUtilsDetail::operationsInstance().iauCal2jd( yr, mon, day, &djm0, &djm );
109 djm0 = djm + ( ( (double)hr ) / ( 24.0 ) + ( (double)min ) / ( 60.0 * 24.0 ) + sec / ( 3600.0 * 24.0 ) );
114int ISO8601dateBreakdown(
int &yr,
int &mon,
int &day,
int &hr,
int &min,
double &sec,
const std::string &fdate )
116 if( fdate.length() < 19 )
119 yr = atoi( fdate.substr( 0, 4 ).c_str() );
120 mon = atoi( fdate.substr( 5, 2 ).c_str() );
121 day = atoi( fdate.substr( 8, 2 ).c_str() );
124 parse_hms( _hr, _min, sec, fdate.substr( 11 ) );
134 if( fdate.length() < 19 )
137 int yr, mon, day, hr, min;
143 return Cal2mjd( yr, mon, day, hr, min, sec );
150 if( timeUtilsDetail::operationsInstance().gmtimeR( &timeIn, &bdt ) ==
nullptr )
157 strftime( tstr, 25,
"%FT%H:%M:%S", &bdt );
159 std::string result = tstr;
180 snprintf( tstr, 20,
".%09ld", timeIn.tv_nsec );
202 sofa::iauJd2cal(
DJM0, timeIn, &iy, &im, &
id, &fd );
206 hr = floor( fd * 24.0 );
207 fd = ( fd - hr / 24.0 ) * 24.0;
209 mn = floor( fd * 60. );
211 fd = ( fd - mn / 60.0 ) * 3600.0;
215 snprintf( tstr, 32,
"%04d-%02d-%02dT%02d:%02d:%012.9f", iy, im,
id, hr, mn, fd );
217 std::string result = tstr;
231 time_t t0 = ts.tv_sec;
233 if( timeUtilsDetail::operationsInstance().gmtimeR( &t0, &uttime ) ==
nullptr )
235 std::cerr <<
"Error getting UT time (gmtime_r returned 0). At: " << __FILE__ <<
" " << __LINE__ <<
"\n";
243 "%04i%02i%02i%02i%02i%02i%09i",
244 uttime.tm_year + 1900,
250 static_cast<int>( ts.tv_nsec ) );
270 tmrv = timeUtilsDetail::operationsInstance().gmtimeR( &tsp.tv_sec, tm0 );
275 rv1 = timeUtilsDetail::operationsInstance().iauDat( 1900 + tm0->tm_year, 1 + tm0->tm_mon, tm0->tm_mday, 0.0, &dat );
280 rv2 = timeUtilsDetail::operationsInstance().iauCal2jd( 1900 + tm0->tm_year,
289 djmf = ( (double)tm0->tm_hour ) / 24.0 + ( (
double)tm0->tm_min ) / ( 24.0 * 60. ) +
290 ( ( (
double)tm0->tm_sec ) + ( (
double)tsp.tv_nsec / 1e9 ) + dat ) / ( 24.0 * 3600.0 );
305 double means = ( ts1.tv_sec + ts2.tv_sec ) / 2.0;
306 double meanns = ( ts1.tv_nsec + ts2.tv_nsec ) / 2.0;
308 ts1.tv_sec = floor( means );
309 ts1.tv_nsec = round( meanns );
311 if( means != floor( means ) )
315 if( ts1.tv_nsec >= 1e9 )
328bool operator<( timespec
const &tsL, timespec
const &tsR )
330 return ( ( ( tsL.tv_sec == tsR.tv_sec ) && ( tsL.tv_nsec < tsR.tv_nsec ) ) || ( tsL.tv_sec < tsR.tv_sec ) );
333bool operator>( timespec
const &tsL, timespec
const &tsR )
335 return ( ( ( tsL.tv_sec == tsR.tv_sec ) && ( tsL.tv_nsec > tsR.tv_nsec ) ) || ( tsL.tv_sec > tsR.tv_sec ) );
340 return ( ( tsL.tv_sec == tsR.tv_sec ) && ( tsL.tv_nsec == tsR.tv_nsec ) );
345 return ( tsL < tsR || tsL == tsR );
350 return ( tsL > tsR || tsL == tsR );
#define DJM0
Julian Date of Modified Julian Date zero (defined in the SOFA library sofam.h).
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).
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).
typeT get_curr_time(timespec &tsp)
Get the current system time in seconds.
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.