3 #include "../../catch2/catch.hpp"
6 #include "../../../include/sys/timeUtils.hpp"
13 SCENARIO(
"getting the current time in seconds",
"[timeutils]")
15 GIVEN(
"getting the time")
17 WHEN(
"the same timespec is used")
22 std::cout << std::endl;
29 WHEN(
"no timespec is provided")
33 std::cout << std::endl;
48 SCENARIO(
"putting a thread to sleep",
"[timeutils]" )
50 GIVEN(
"sleeping for 1 second")
52 WHEN(
"sleeping in seconds")
58 REQUIRE(t1 >= t0 + 1.0);
60 WHEN(
"sleeping in milliseconds")
66 REQUIRE(t1 >= t0 + 1.0);
69 WHEN(
"sleeping in microseconds")
75 REQUIRE(t1 >= t0 + 1.0);
77 WHEN(
"sleeping in nanoseconds")
83 REQUIRE(t1 >= t0 + 1.0);
93 SCENARIO(
"adding time to a timespec",
"[timeutils]" )
97 WHEN(
"adding less than 1e9 nanoseconds")
104 REQUIRE(ts.tv_sec == 1);
105 REQUIRE(ts.tv_nsec == 10);
108 REQUIRE(ts.tv_sec == 1);
109 REQUIRE(ts.tv_nsec == 110);
112 REQUIRE(ts.tv_sec == 1);
113 REQUIRE(ts.tv_nsec == 1110);
116 REQUIRE(ts.tv_sec == 1);
117 REQUIRE(ts.tv_nsec == 11110);
120 REQUIRE(ts.tv_sec == 1);
121 REQUIRE(ts.tv_nsec == 111110);
124 REQUIRE(ts.tv_sec == 1);
125 REQUIRE(ts.tv_nsec == 1111110);
128 REQUIRE(ts.tv_sec == 1);
129 REQUIRE(ts.tv_nsec == 11111110);
132 REQUIRE(ts.tv_sec == 1);
133 REQUIRE(ts.tv_nsec == 111111110);
136 WHEN(
"adding more than 1e9 nanoseconds but less than 2e9")
143 REQUIRE(ts.tv_sec == 2);
144 REQUIRE(ts.tv_nsec == 0);
147 REQUIRE(ts.tv_sec == 3);
148 REQUIRE(ts.tv_nsec == 10);
151 REQUIRE(ts.tv_sec == 4);
152 REQUIRE(ts.tv_nsec == 110);
155 REQUIRE(ts.tv_sec == 5);
156 REQUIRE(ts.tv_nsec == 1110);
159 REQUIRE(ts.tv_sec == 6);
160 REQUIRE(ts.tv_nsec == 11110);
163 REQUIRE(ts.tv_sec == 7);
164 REQUIRE(ts.tv_nsec == 111110);
167 REQUIRE(ts.tv_sec == 8);
168 REQUIRE(ts.tv_nsec == 1111110);
171 REQUIRE(ts.tv_sec == 9);
172 REQUIRE(ts.tv_nsec == 11111110);
175 REQUIRE(ts.tv_sec == 10);
176 REQUIRE(ts.tv_nsec == 111111110);
179 WHEN(
"adding more than 2e9")
186 REQUIRE(ts.tv_sec == 3);
187 REQUIRE(ts.tv_nsec == 10);
197 SCENARIO(
"parsing a formatted time string",
"[timeutils]" )
199 GIVEN(
"a valid time string")
201 WHEN(
"integer seconds")
215 WHEN(
"floating seconds")
225 REQUIRE(fabs(sec-3.23) < 1e-7);
229 WHEN(
"negative hour")
239 REQUIRE(fabs(sec - -3.23) < 1e-7);
253 REQUIRE(fabs(sec-3.23) < 1e-7);
263 SCENARIO(
"calculating MJD from a Gregorian date",
"[timeutils]" )
265 GIVEN(
"a valid Gregorian date")
267 WHEN(
"integer seconds")
270 REQUIRE(mjd == 59214.0);
274 GIVEN(
"a valid Gregorian date")
276 WHEN(
"floating seconds")
279 REQUIRE(fabs(mjd-59214.00011846875) < 1e-14);
288 SCENARIO(
"parsing an ISO 8601 time string",
"[timeutils]" )
290 GIVEN(
"a valid ISO 8601 time string")
292 WHEN(
"integer seconds")
294 int yr, mon,
day, hr, min;
310 WHEN(
"fractional seconds")
312 int yr, mon,
day, hr, min;
323 REQUIRE(fabs(sec-10.2357)<1e-14);
329 GIVEN(
"an invalid ISO 8601 time string")
331 WHEN(
"string too short")
333 int yr, mon,
day, hr, min;
347 SCENARIO(
"coverting an ISO 8601 time string to MJD",
"[timeutils]" )
349 GIVEN(
"a valid ISO 8601 time string")
351 WHEN(
"integer seconds")
355 REQUIRE(mjd == 59214.0);
358 WHEN(
"fractional seconds")
362 REQUIRE(fabs(mjd-59214.00011846875) < 1e-14);
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.
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.
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)
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)
SCENARIO("getting the current time in seconds", "[timeutils]")