Fixposition SDK 0.0.0-heads/main-0-g5656422
Collection of c++ libraries and apps for use with Fixposition products on Linux
Loading...
Searching...
No Matches
time.hpp
Go to the documentation of this file.
1/**
2 * \verbatim
3 * ___ ___
4 * \ \ / /
5 * \ \/ / Copyright (c) Fixposition AG (www.fixposition.com) and contributors
6 * / /\ \ License: see the LICENSE file
7 * /__/ \__\
8 *
9 * Parts copyright (c) 2008, Willow Garage, Inc., see time.cpp and the LICENSE file for details
10 * Written by flipflip (https://github.com/phkehl)
11 * \endverbatim
12 *
13 * @file
14 * @brief Fixposition SDK: Time utilities
15 *
16 * @page FPSDK_COMMON_TIME Time utilities
17 *
18 * **API**: fpsdk_common/time.hpp and fpsdk::common::time
19 *
20 */
21#ifndef __FPSDK_COMMON_TIME_HPP__
22#define __FPSDK_COMMON_TIME_HPP__
23
24/* LIBC/STL */
25#include <chrono>
26#include <cstdint>
27#include <ctime>
28#include <string>
29
30/* EXTERNAL */
31
32/* PACKAGE */
33
34namespace fpsdk {
35namespace common {
36/**
37 * @brief Time utilities
38 */
39namespace time {
40/* ****************************************************************************************************************** */
41
42/**
43 * @brief Constants
44 * @{
45 */
46
47// clang-format off
48static constexpr int SEC_IN_MIN_I = 60; //!< Number of seconds in a minute (integer) = 60
49static constexpr int SEC_IN_HOUR_I = 60 * SEC_IN_MIN_I; //!< Number of seconds in an hour (integer) = 3600
50static constexpr int SEC_IN_DAY_I = 24 * SEC_IN_HOUR_I; //!< Number of seconds in a day (integer) = 86400
51static constexpr int SEC_IN_WEEK_I = 7 * SEC_IN_DAY_I; //!< Number of seconds in a week (integer) = 604800
52static constexpr double SEC_IN_MIN_D = static_cast<double>(SEC_IN_MIN_I); //!< Number of seconds in a minute (double) = 60.0
53static constexpr double SEC_IN_HOUR_D = static_cast<double>(SEC_IN_HOUR_I); //!< Number of seconds in an hour (double) = 3600.0
54static constexpr double SEC_IN_DAY_D = static_cast<double>(SEC_IN_DAY_I); //!< Number of seconds in a day (double) = 86400.0
55static constexpr double SEC_IN_WEEK_D = static_cast<double>(SEC_IN_WEEK_I); //!< Number of seconds in a week (double) = 604800.0
56// clang-format on
57
58/**
59 * @brief Convert seconds to nanoseconds
60 *
61 * @note Depending on the \c ns value this can be lossy.
62 *
63 * @param[in] ns Nanoseconds alue
64 *
65 * @returns the seconds value
66 */
67static constexpr double NsecToSec(const uint64_t ns)
68{
69 return (double)ns * 1e-9; // (double)(ns % (uint64_t)1000000000) + ((double)(ns / (uint64_t)1000000000) * 1e-9);
70}
71
72/**
73 * @brief Convert nanoseconds to seconds
74 *
75 * @param[in] sec Seconds value
76 *
77 * @returns the nanoseconds value
78 */
79static constexpr uint64_t SecToNsec(const double sec)
80{
81 return sec * 1e9;
82}
83
84///@}
85
86// ---------------------------------------------------------------------------------------------------------------------
87
88/**
89 * @brief Get milliseconds [ms], monotonic time
90 *
91 * @returns the number of milliseconds
92 */
93uint64_t GetMillis();
94
95/**
96 * @brief Get seconds [s], monotonic time
97 *
98 * @returns the number of seconds
99 */
100double GetSecs();
101
102// ---------------------------------------------------------------------------------------------------------------------
103
104class Duration; // forward declaration
105
106/**
107 * @brief Helper to measure wallclock time
108 */
110{
111 public:
112 /**
113 * @brief Constructor
114 *
115 * This also starts the measurements, like Tic().
116 */
118
119 /**
120 * @brief (Re-)start measurement
121 */
122 void Tic();
123
124 /**
125 * @brief Get elapsed wallclock time
126 *
127 * @param[in] reset Reset (restart) measurements
128 *
129 * @returns the elapsed time since object constructed resp. last call to Tic()
130 */
131 Duration Toc(const bool reset = false);
132
133 /**
134 * @brief Get elapsed wallclock time in [ms]
135 *
136 * @param[in] reset Reset (restart) measurements
137 *
138 * @returns the elapsed time since object constructed resp. last call to Tic() in [ms]
139 */
140 double TocMs(const bool reset = false);
141
142 private:
143 std::chrono::time_point<std::chrono::steady_clock> t0_;
144};
145
146// ---------------------------------------------------------------------------------------------------------------------
147
148/**
149 * @brief Minimal ros::Time() / rplcpp::Time implementation (that doesn't throw)
150 */
152{
153 RosTime();
154 /**
155 * @brief Constructor
156 *
157 * @param[in] sec Time value seconds
158 * @param[in] nsec Time value nanoseconds
159 */
160 RosTime(const uint32_t sec, const uint32_t nsec);
161
162 /**
163 * @brief Convert to seconds
164 *
165 * @returns the time value (time since epoch) in [s]
166 */
167 double ToSec() const;
168
169 /**
170 * @brief Convert to nanoseconds
171 *
172 * @returns the time value (time since epoch) in [ns]
173 */
174 uint64_t ToNSec() const;
175
176 /**
177 * @brief Check if time is zero (invalid, unset)
178 *
179 * @returns true if time is zero (invalid, unset)
180 */
181 bool IsZero() const;
182
183 uint32_t sec_; //!< Seconds part of time
184 uint32_t nsec_; //!< Nanoseconds part of time (*should* be in range 0-999999999)
185
186 bool operator==(const RosTime& rhs) const; //!< Equal
187};
188
189// ---------------------------------------------------------------------------------------------------------------------
190
191/**
192 * @brief Time duration
193 *
194 * This is similar (and binary compatible) to ros::Duration and rpclpp::Duration. While some of the constructors and
195 * operators can throw, it also provides non-throwing methods to manipulate the duration.
196 */
198{
199 public:
200 // -----------------------------------------------------------------------------------------------------------------
201 /**
202 * @name Make Duration object
203 *
204 * @note These throw std::runtime_error if the values are out of range.
205 * @{
206 */
207
208 /**
209 * @brief Constructor, zero duration
210 */
212
213 /**
214 * @brief Make Duration from seconds and nanoseconds
215 *
216 * @param[in] sec Duration value seconds
217 * @param[in] nsec Duration value nanoseconds
218 */
219 static Duration FromSecNSec(const int32_t sec, const int32_t nsec);
220
221 /**
222 * @brief Make Duration from nanoseconds
223 *
224 * @param[in] nsec Duration value nanoseconds
225 */
226 static Duration FromNSec(const int64_t nsec);
227
228 /**
229 * @brief Make Duration from seconds
230 *
231 * @param[in] sec Duration value seconds
232 */
233 static Duration FromSec(const double sec);
234
235 ///@}
236 // -----------------------------------------------------------------------------------------------------------------
237 /**
238 * @name Set duration
239 *
240 * These do not throw and instead return true/false.
241 * @{
242 */
243
244 /**
245 * @brief Set duration from seconds and nanoseconds
246 *
247 * @param[in] sec Duration value seconds
248 * @param[in] nsec Duration value nanoseconds
249 *
250 * @returns true if successful (values in range), false otherwise (bad values)
251 */
252 bool SetSecNSec(const int32_t sec, const int32_t nsec);
253
254 /**
255 * @brief Set duration from nanoseconds
256 *
257 * @param[in] nsec Duration value nanoseconds
258 *
259 * @returns true if successful (sec value in range), false otherwise (bad sec value)
260 */
261 bool SetNSec(const int64_t nsec);
262
263 /**
264 * @brief Set duration from seconds
265 *
266 * @param[in] sec Duration value seconds
267 *
268 * @returns true if successful (sec value in range), false otherwise (bad sec value)
269 */
270 bool SetSec(const double sec);
271
272 ///@}
273 // -----------------------------------------------------------------------------------------------------------------
274 /**
275 * @name Get duration
276 * @{
277 */
278
279 /**
280 * @brief Get duration as nanoseconds
281 *
282 * @returns the duration as nanoseconds
283 */
284 int64_t GetNSec() const;
285
286 /**
287 * @brief Get duration as seconds
288 *
289 * @param[in] prec Round the seconds to this many fractional digits (0-9)
290 *
291 * @returns the duration as seconds
292 */
293 double GetSec(const int prec = 9) const;
294
295 /**
296 * @brief Get duration as std::chrono::milliseconds
297 *
298 * @returns the duration as std::chrono::milliseconds
299 */
300 std::chrono::milliseconds GetChronoMilli() const;
301
302 /**
303 * @brief Get duration as std::chrono::nanoseconds
304 *
305 * @returns the duration as std::chrono::nanoseconds
306 */
307 std::chrono::nanoseconds GetChronoNano() const;
308
309 ///@}
310 // -----------------------------------------------------------------------------------------------------------------
311 /**
312 * @name Misc
313 * @{
314 */
315
316 /**
317 * @brief Check if duration is zero
318 *
319 * @returns true if duration is exactly zero
320 */
321 bool IsZero() const;
322
323 /**
324 * @brief Stringify duration, for debugging
325 *
326 * @param[in] prec Number of fractional digits for the seconds (0-9)
327 *
328 * @returns The stringified duration ("HH:MM:SS.SSS", resp. "DDd HH:MM:SS.SSS" if duration > 1d)
329 */
330 std::string Stringify(const int prec = 3) const;
331
332 /**
333 * @brief Sleep for the duration (if > 0)
334 */
335 void Sleep() const;
336
337 ///@}
338 // -----------------------------------------------------------------------------------------------------------------
339 /**
340 * @name Arithmetic methods
341 *
342 * These do not throw and instead return true/false.
343 * @{
344 */
345
346 /**
347 * @brief Add duration to duration
348 *
349 * @param[in] dur Duration to add
350 *
351 * @returns true if successful (dur value in range), false otherwise (bad sec value)
352 */
353 bool AddDur(const Duration& dur);
354
355 /**
356 * @brief Add nanoseconds to duration
357 *
358 * @param[in] nsec Nanoseconds to add
359 *
360 * @returns true if successful (sec value in range), false otherwise (bad sec value)
361 */
362 bool AddNSec(const int64_t nsec);
363
364 /**
365 * @brief Add seconds to duration
366 *
367 * @param[in] sec Seconds to add
368 *
369 * @returns true if successful (sec value in range), false otherwise (bad sec value)
370 */
371 bool AddSec(const double sec);
372
373 /**
374 * @brief Substract duration from duration
375 *
376 * @param[in] dur Duration to substract
377 *
378 * @returns true if successful (dur value in range), false otherwise (bad sec value)
379 */
380 bool SubDur(const Duration& dur);
381
382 /**
383 * @brief Substract nanoseconds from duration
384 *
385 * @param[in] nsec Nanoseconds to substract
386 *
387 * @returns true if successful (sec value in range), false otherwise (bad sec value)
388 */
389 bool SubNSec(const int64_t nsec);
390
391 /**
392 * @brief Substract seconds from duration
393 *
394 * @param[in] sec Seconds to substract
395 *
396 * @returns true if successful (sec value in range), false otherwise (bad sec value)
397 */
398 bool SubSec(const double sec);
399
400 /**
401 * @brief Scale (multiply) duration
402 *
403 * @param[in] sec Scale factor
404 *
405 * @returns true if successful (scale value in range), false otherwise (bad scale value)
406 */
407 bool Scale(const double sec);
408
409 ///@}
410 // -----------------------------------------------------------------------------------------------------------------
411 /**
412 * @name Arithmetic operators
413 *
414 * @note These throw std::runtime_error if the values are out of range.
415 * @{
416 */
417
418 Duration operator+(const Duration& rhs) const; //!< Sum duration and duration
419 Duration operator+(const int64_t nsec) const; //!< Sum duration and nanoseconds
420 Duration operator+(const double sec) const; //!< Sum duration and seconds
421 Duration& operator+=(const Duration& rhs); //!< Add duration to duration
422 Duration& operator+=(const int64_t nsec); //!< Add nanoseconds to durationn
423 Duration& operator+=(const double sec); //!< Add seconds to duration
424 Duration operator-(const Duration& rhs) const; //!< Subtract duration and duration
425 Duration operator-(const int64_t nsec) const; //!< Subtract duration and nanoseconds
426 Duration operator-(const double sec) const; //!< Subtract duration and seconds
427 Duration& operator-=(const Duration& rhs); //!< Subtract duration from duration
428 Duration& operator-=(const int64_t nsec); //!< Subtract nanoseconds from duration
429 Duration& operator-=(const double sec); //!< Subtract seconds from durationn
430 Duration operator-() const; //!< Reverse sign
431 Duration operator*(double scale) const; //!< Multiply (scale) duration
432 Duration& operator*=(double scale); //!< Multiply (scale) duration
433
434 ///@}
435 // -----------------------------------------------------------------------------------------------------------------
436 /**
437 * @name Logic operators
438 * @{
439 */
440
441 bool operator==(const Duration& rhs) const; //!< Equal
442 bool operator!=(const Duration& rhs) const; //!< Not equal
443 bool operator>(const Duration& rhs) const; //!< Greater than
444 bool operator<(const Duration& rhs) const; //!< Smaller than
445 bool operator>=(const Duration& rhs) const; //!< Greater or equal than
446 bool operator<=(const Duration& rhs) const; //!< Smaller or equal than
447
448 ///@}
449 // -----------------------------------------------------------------------------------------------------------------
450
451 // Storage deliberately public
452 int32_t sec_; //!< Seconds part of duration
453 int32_t nsec_; //!< Nanoseconds part of duration (*should* be in range 0-999999999)
454
455 ///@
456};
457
458// ---------------------------------------------------------------------------------------------------------------------
459
460/**
461 * @brief GNSS atomic time representation: week number (wno) and time of week (tow) used by GPS, Galileo and BeiDou
462 */
463struct WnoTow
464{
465 /**
466 * @brief GNSS time system
467 */
468 enum class Sys
469 {
470 GPS, //!< GPS (also: SBAS, QZSS)
471 GAL, //!< Galileo system time (GST)
472 BDS, //!< BeiDou time
473 };
474
475 /**
476 * @brief Constructor
477 *
478 * @param[in] sys GNSS
479 */
480 WnoTow(const Sys sys = Sys::GPS);
481
482 /**
483 * @brief Constructor
484 *
485 * @note Input values are not normalised nor range checked here. See comments in Time::GetWnoTow() and
486 * Time::SetWnoTow()
487 *
488 * @param[in] wno Week number [-] (>= SANE_WNO_MIN)
489 * @param[in] tow Time of week [s] (SANE_TOW_MIN - SANE_TOW_MAX)
490 * @param[in] sys GNSS
491 */
492 WnoTow(const int wno, const double tow, const Sys sys = Sys::GPS);
493
494 // clang-format off
495 static constexpr int SANE_WNO_MIN = 0; //!< Minimum sane wno_ value
496 static constexpr int SANE_WNO_MAX = 9999; //!< Maximum sane wno_ value, good enough for the next centuries
497 static constexpr double SANE_TOW_MIN = 0.0; //!< Minimum sane tow_ value
498 static constexpr double SANE_TOW_MAX = SEC_IN_WEEK_D - std::numeric_limits<double>::epsilon(); //!< Maximum sane tow_ value
499
500 int wno_ = 0; //!< Week number [-] (>= SANE_WNO_MIN, if normalised)
501 double tow_ = 0.0; //!< Time of week [s] (SANE_TOW_MIN - SANE_TOW_MAX, if normalised)
502 Sys sys_; //!< Time system
503 // clang-format on
504
505 /**
506 * @brief Stringify time system (for debugging)
507 *
508 * @returns a string identifying the time system
509 */
510 const char* SysStr() const;
511};
512
513/**
514 * @brief GLONASS time
515 */
517{
518 GloTime() = default; //!< Ctor
519
520 /**
521 * @brief Constructor
522 *
523 * @note Input values are not normalised nor range checked here. See comments in Time::GetGloTime() and
524 * Time::SetGloTime()
525 *
526 * @param[in] N4 Four-year interval, 1=1996..1999, 2=2000..2003, ... (SANE_N4_MIN - SANE_N4_MAX)
527 * @param[in] Nt Day in four-year interval (SANE_NT_MIN - SANE_NT_MAX)
528 * @param[in] TOD Time of day (in Москва time zone) [s] (SANE_TOD_MIN - SANE_TOD_MAX)
529 */
530 GloTime(const int N4, const int Nt, const double TOD);
531
532 // clang-format off
533 static constexpr int SANE_N4_MIN = 1; //!< Minimum sane N4_ value
534 static constexpr int SANE_N4_MAX = 99; //!< Maximum sane N4_ value, good enough for the next centuries
535 static constexpr int SANE_NT_MIN = 1; //!< Minimum sane Nt_ value
536 static constexpr int SANE_NT_MAX = 1461; //!< Maximum sane Nt_ value
537 static constexpr double SANE_TOD_MIN = 0.0; //!< Minimum sane TOD_ value
538 static constexpr double SANE_TOD_MAX = SEC_IN_DAY_D - std::numeric_limits<double>::epsilon(); //!< Maximum sane TOD_ value
539
540 int N4_ = 0; //!< Four-year interval, 1=1996..1999, 2=2000..2003, ... (SANE_N4_MIN - SANE_N4_MAX)
541 int Nt_ = 0; //!< Day in four-year interval (SANE_NT_MIN - SANE_NT_MAX)
542 double TOD_ = 0.0; //!< Time of day (in Москва time zone) [s] (SANE_TOD_MIN - SANE_TOD_MAX)
543 // clang-format on
544};
545
546/**
547 * @brief UTC time representation
548 */
550{
551 UtcTime() = default; //!< Ctor
552 /**
553
554 * @brief Constructor
555 *
556 * @param[in] year Year
557 * @param[in] month Month (1-12)
558 * @param[in] day Day (1-31)
559 * @param[in] hour Hour (0-23)
560 * @param[in] min Minute (0-59)
561 * @param[in] sec Second (0-60)
562 */
563 UtcTime(const int year, const int month, const int day, const int hour, const int min, const double sec);
564
565 // clang-format off
566 int year_ = 0; //!< Year
567 int month_ = 0; //!< Month (1-12)
568 int day_ = 0; //!< Day (1-31)
569 int hour_ = 0; //!< Hour (0-23)
570 int min_ = 0; //!< Minute (0-59)
571 double sec_ = 0.0; //!< Second (0-60)
572 // clang-format on
573};
574
575// clang-format off
576/**
577 * @brief Time
578 *
579 * This class implements conversion from and to different time systems and time artithmetics. Internally it uses an
580 * *atomic* time in seconds and nanoseconds since 1970 as its representation of time. Conversion from and to UTC time or
581 * POSIX time are available. No timezones or local time are supported (use the ctime API for that).
582 *
583 * Some of the constructors and operators can throw. Non-throwing methods to manipulate the time are provided as well.
584 *
585 * This time object can represent the time from T_MIN to T_MAX, which is (the old) 32bit POSIX time. Note that for the
586 * different GNSS times you will get negative week numbers (GPS, Galileo, BeiDou) or a negative offset "M4" value
587 * (GLONASS) for timestamps before the beginning of respective timescales.
588 *
589 * ---1970-------------1980-------1996-1999----2006--------------------2106---//--fuuuuuture----> time
590 *
591 * 1970-01-01 00:00:00.0 UTC |<----- std::time_t ---------------------------------------------------//----->|
592 * |<----- ros::Time/rclcpp::Time----------------------------------->|
593 * 1980-01-06 00:00:00.0 UTC |<---- GPS time---------------------------------------//----->
594 * 1996-01-01 00:00:00.0 UTC(SU) |<---- GLONASS time -----------------------//----->
595 * 1999-08-21 23:59:47.0 UTC |<---- Galileo time ------------------//----->
596 * 2006-01-01 00:00:00.0 UTC |<---- BeiDou time -----------//----->
597 *
598 * |<======================= Time object ===========================>|
599 * T_MIN T_MAX
600 * 1970-01-01 00:00:0.0 UTC 2106-02-06 06:28:16.0 UTC (approximately!)
601 *
602 * Some notes:
603 *
604 * - "Strict" POSIX time is used for the methods marked with "POSIX". This time has a discontinuity and/or ambiguity
605 * at/during leapsecond events. It does not use the "Mills" or NTP style of handling these periods and is therefore,
606 * for those timestamps, likely not compatible with the system time (CLOCK_REALTIME) of a typical Linux system. See
607 * references below.
608 * - The from/to UTC calculations are not fully correct for times before 1972.
609 * - FromClockTai() and SetClockTai() likely will not give the expected result unless your system (Linux kernel) is
610 * configured for the correct leapsecond.
611 * - The precision of all integer getters, setters and operators should be [ns] in all cases
612 * - The precision of all double getters, setters and operators should be [ns] in most cases
613 * - The internal representation of time (the sec_ value) is atomic, but it is not CLOCK_TAI (which already "has" 10
614 * leapseconds at sec_ = 0).
615 *
616 * Some references:
617 *
618 * - https://en.wikipedia.org/wiki/Unix_time
619 * - https://en.wikipedia.org/wiki/Atomic_clock
620 * - https://en.wikipedia.org/wiki/Coordinated_Universal_Time
621 * - https://docs.ntpsec.org/latest/leapsmear.html
622 * - https://www.eecis.udel.edu/~mills/leap.html
623 * - https://manpages.org/adjtimex/2
624 *
625 */
626// clang-format on
627class Time
628{
629 public:
630 // -----------------------------------------------------------------------------------------------------------------
631 /**
632 * @name Make Time object
633 *
634 * @note These throw std::runtime_error if the time (the arguments) is out of range
635 * @{
636 */
637
638 /**
639 * @brief Constructor, empty (invalid) time
640 */
642
643 /**
644 * @brief Time from seconds and nanoseconds (atomic)
645 *
646 * @param[in] sec Time value seconds
647 * @param[in] nsec Time value nanoseconds
648 *
649 * @returns the Time object
650 */
651 static Time FromSecNSec(const uint32_t sec, const uint32_t nsec);
652
653 /**
654 * @brief Time from nanoseconds (atomic)
655 *
656 * @param[in] nsec Time value nanoseconds (> 0)
657 *
658 * @returns the Time object
659 */
660 static Time FromNSec(const uint64_t nsec);
661
662 /**
663 * @brief From seconds (atomic)
664 *
665 * @param[in] sec Time value seconds (> 0.0)
666 *
667 * @returns the Time object
668 */
669 static Time FromSec(const double sec);
670
671 /**
672 * @brief From POSIX time (POSIX, seconds)
673 *
674 * @note See comments in the class description regarding POSIX time!
675 *
676 * @param[in] posix Time value seconds (> 0)
677 *
678 * @returns the Time object
679 */
680 static Time FromPosix(const std::time_t posix);
681
682 /**
683 * @brief From POSIX seconds (POSIX)
684 *
685 * @note See comments in the class description regarding POSIX time!
686 *
687 * @param[in] posix_sec Time value seconds (> 0.0)
688 *
689 * @returns the Time object
690 */
691 static Time FromPosixSec(const double posix_sec);
692
693 /**
694 * @brief From POSIX nanoseconds (POSIX)
695 *
696 * @note See comments in the class description regarding POSIX time!
697 *
698 * @param[in] posix_ns Time value nanoseconds (> 0)
699 *
700 * @returns the Time object
701 */
702 static Time FromPosixNs(const uint64_t posix_ns);
703
704 /**
705 * @brief From ROS time (POSIX)
706 *
707 * @note See comments in the class description regarding POSIX time!
708 *
709 * @param[in] rostime Time value
710 *
711 * @returns the Time object
712 */
713 static Time FromRosTime(const RosTime& rostime);
714
715 /**
716 * @brief From GNSS time (atomic)
717 *
718 * @param[in] wnotow GNSS time
719 *
720 * @returns the Time object
721 */
722 static Time FromWnoTow(const WnoTow& wnotow);
723
724 /**
725 * @brief From GLONASS time (UTC + 3h)
726 *
727 * @param[in] glotime GLONASS time
728 *
729 * @returns the Time object
730 */
731 static Time FromGloTime(const GloTime& glotime);
732
733 /**
734 * @brief From UTC time (UTC)
735 *
736 * @param[in] utctime UTC time
737 *
738 * @returns the Time object
739 */
740 static Time FromUtcTime(const UtcTime& utctime);
741
742 /**
743 * @brief From TAI time (CLOCK_TAI)
744 *
745 * @returns true if successful, false otherwise (bad time)
746 */
747 static Time FromTai(const std::time_t tai);
748
749 /**
750 * @brief From TAI seconds (CLOCK_TAI)
751 *
752 * @param[in] tai_sec Time value seconds (> 0.0)
753 *
754 * @returns the Time object
755 */
756 static Time FromTaiSec(const double tai_sec);
757
758 /**
759 * @brief From TAI nanoseconds (CLOCK_TAI)
760 *
761 * @param[in] tai_ns Time value nanoseconds (> 0)
762 *
763 * @returns the Time object
764 */
765 static Time FromTaiNs(const uint64_t tai_ns);
766
767 /**
768 * @brief From system clock current (now) system time (CLOCK_REALTIME)
769 *
770 * @returns the Time object
771 */
773
774#ifdef CLOCK_TAI
775 /**
776 * @brief From system clock current (now) atomic time (CLOCK_TAI)
777 *
778 * @note This will only produce the right result if your system is configured accordingly (which may not be the
779 * case). See comments in the Time class description.
780 *
781 * @returns the Time object
782 */
783 static Time FromClockTai();
784#endif
785
786 ///@}
787 // -----------------------------------------------------------------------------------------------------------------
788 /**
789 * @name Set time
790 *
791 * These do not throw and instead return true/false.
792 * @{
793 */
794
795 /**
796 * @brief Set time from seconds and nanoseconds (atomic)
797 *
798 * @param[in] sec Time value seconds
799 * @param[in] nsec Time value nanoseconds
800 *
801 * @returns true if successful, false otherwise (bad time)
802 */
803 bool SetSecNSec(const uint32_t sec, const uint32_t nsec);
804
805 /**
806 * @brief Set time from nanoseconds (atomic)
807 *
808 * @param[in] nsec Time value nanoseconds (> 0)
809 *
810 * @returns true if successful, false otherwise (bad time)
811 */
812 bool SetNSec(const uint64_t nsec);
813
814 /**
815 * @brief Set time from seconds (atomic)
816 *
817 * @param[in] sec Time value seconds (> 0.0)
818 *
819 * @returns true if successful, false otherwise (bad time)
820 */
821 bool SetSec(const double sec);
822
823 /**
824 * @brief Set time from POSIX time (POSIX, seconds)
825 *
826 * @note See comments in the class description regarding POSIX time!
827 *
828 * @param[in] posix Time value seconds (> 0)
829 *
830 * @returns true if successful, false otherwise (bad time)
831 */
832 bool SetPosix(const std::time_t posix);
833
834 /**
835 * @brief Set time from POSIX seconds (POSIX)
836 *
837 * @note See comments in the class description regarding POSIX time!
838 *
839 * @param[in] posix_sec Time value seconds (> 0.0)
840 *
841 * @returns true if successful, false otherwise (bad time)
842 */
843 bool SetPosixSec(const double posix_sec);
844
845 /**
846 * @brief Set time from POSIX nanoseconds (POSIX)
847 *
848 * @note See comments in the class description regarding POSIX time!
849 *
850 * @param[in] posix_ns Time value nanoseconds (> 0)
851 *
852 * @returns true if successful, false otherwise (bad time)
853 */
854 bool SetPosixNs(const uint64_t posix_ns);
855
856 /**
857 * @brief Set time from ROS time (POSIX)
858 *
859 * @note See comments in the class description regarding POSIX time!
860 *
861 * @param[in] rostime Time value
862 *
863 * @returns true if successful, false otherwise (bad time)
864 */
865 bool SetRosTime(const RosTime& rostime);
866
867 /**
868 * @brief Set time from GNSS (GPS, Galileo, BeiDou) time (atomic)
869 *
870 * @note Input values are normalised, so tow < SANE_TOW_MIN, tow > SANE_TOW_MAX, and wno < SANE_WNO_MIN are
871 * acceptable. Compare GetWnoTow().
872 *
873 * @param[in] wnotow GNSS time
874 *
875 * @returns true if successful, false otherwise (bad time)
876 */
877 bool SetWnoTow(const WnoTow& wnotow);
878
879 /**
880 * @brief Set time from GLONASS time (UTC + 3h)
881 *
882 * @note Input values will be normalised, so e.g. Nt_ < SANE_NT_MIN or N4_ > SANE_N4_MAX, ... are acceptable.
883 * Compare GetGloTime().
884 *
885 * @param[in] glotime GLONASS time
886 *
887 * @returns true if successful, false otherwise (bad time)
888 */
889 bool SetGloTime(const GloTime& glotime);
890
891 /**
892 * @brief Set time from UTC time (UTC)
893 *
894 * @param[in] utctime UTC time
895 *
896 * @returns true if successful, false otherwise (bad time)
897 */
898 bool SetUtcTime(const UtcTime& utctime);
899
900 /**
901 * @brief Set time from TAI time (CLOCK_TAI)
902 *
903 * @param[in] tai The TAI time
904 *
905 * @returns true if successful, false otherwise (bad time)
906 */
907 bool SetTai(const std::time_t tai);
908
909 /**
910 * @brief Set time from TAI seconds (CLOCK_TAI)
911 *
912 * @param[in] tai_sec Time value seconds (> 0.0)
913 *
914 * @returns true if successful, false otherwise (bad time)
915 */
916 bool SetTaiSec(const double tai_sec);
917
918 /**
919 * @brief Set time from TAI nanoseconds (CLOCK_TAI)
920 *
921 * @param[in] tai_ns Time value nanoseconds (> 0)
922 *
923 * @returns true if successful, false otherwise (bad time)
924 */
925 bool SetTaiNs(const uint64_t tai_ns);
926
927 /**
928 * @brief Set time from system clock current (now) system time (CLOCK_REALTIME)
929 *
930 * @returns true if successful, false otherwise (bad time)
931 */
933
934#ifdef CLOCK_TAI
935 /**
936 * @brief Set time from system clock current (now) TAI time (CLOCK_TAI)
937 *
938 * @note This will only produce the right result if your system is configured accordingly (which is unlikely). See
939 * comments in the Time class description.
940 *
941 * @returns the Time object
942 */
943
944 bool SetClockTai();
945#endif
946
947 ///@}
948 // -----------------------------------------------------------------------------------------------------------------
949 /**
950 * @name Get time
951 * @{
952 */
953
954 /**
955 * @brief Get time as nanoseconds (atomic)
956 *
957 * @returns the time as nanoseconds
958 */
959 uint64_t GetNSec() const;
960
961 /**
962 * @brief Get time as seconds (atomic)
963 *
964 * @param[in] prec Round the seconds to this many fractional digits (0-9)
965 *
966 * @returns the time as seconds
967 */
968 double GetSec(const int prec = 9) const;
969
970 /**
971 * @brief Get time as POSIX time (POSIX, seconds)
972 *
973 * @note See comments in the class description regarding POSIX time!
974 *
975 * @returns the POSIX time, truncated (rounded down, sub-seconds ignored)
976 */
977 std::time_t GetPosix() const;
978
979 /**
980 * @brief Get time as POSIX seconds (POSIX)
981 *
982 * @note See comments in the class description regarding POSIX time!
983 *
984 * @param[in] prec Round the seconds to this many fractional digits (0-9)
985 *
986 * @returns the POSIX time in seconds
987 */
988 double GetPosixSec(const int prec = 9) const;
989
990 /**
991 * @brief Get time as POSIX nanoseconds (POSIX)
992 *
993 * @note See comments in the class description regarding POSIX time!
994 *
995 * @returns the POSIX time in nanoseconds
996 */
997 uint64_t GetPosixNs() const;
998
999 /**
1000 * @brief Get time as ROS time (POSIX)
1001 *
1002 * @note See comments in the class description regarding POSIX time!
1003 *
1004 * @returns the time
1005 */
1007
1008 /**
1009 * @brief Get time as GNSS (GPS, Galileo, BeiDou) time (atomic)
1010 *
1011 * @note For times before the respective GNSS epoch the result is not usable (e.g. wno_ may be < SANE_WNO_MIN).
1012 * Output tow_ is guaranteed to be within sane range (i.e. SANE_TOW_MIN <= tow_ < SANE_TOW_MAX). Compare
1013 * SetWnoTow()
1014 *
1015 * @param[in] sys GNSS
1016 * @param[in] prec Round the seconds to this many fractional digits (0-9)
1017 *
1018 * @returns the GNSS time for the selected GNSS
1019 */
1020 WnoTow GetWnoTow(const WnoTow::Sys sys = WnoTow::Sys::GPS, const int prec = 9) const;
1021
1022 /**
1023 * @brief Get time as GLONASS time (UTC + 3h)
1024 *
1025 * @note For times before the respective GLONASS epoch the result is not usable (e.g. N4_ may be < SANE_N4_MIN).
1026 * Output Nt_ and tod_ are guaranteed to be within sane range (e.g. SANE_NT_MIN <= Nt_ < SANE_NT_MAX). Compare
1027 * SetGloTime()
1028 *
1029 * @param[in] prec Round the seconds to this many fractional digits (0-9)
1030 *
1031 * @returns the GLONASS time
1032 */
1033 GloTime GetGloTime(const int prec = 9) const;
1034
1035 /**
1036 * @brief Get time as UTC time (UTC)
1037 *
1038 * @param[in] prec Round the seconds to this many fractional digits (0-9)
1039 *
1040 * @returns the UTC time
1041 */
1042 UtcTime GetUtcTime(const int prec = 9) const;
1043
1044 /**
1045 * @brief Get day of year
1046 *
1047 * @param[in] prec Round to this many fractional digits (0-12)
1048 *
1049 * @returns the day of the year
1050 */
1051 double GetDayOfYear(const int prec = 12) const;
1052
1053 /**
1054 * @brief Get time as TAI time (CLOCK_TAI)
1055 *
1056 * @returns the TAI time, truncated (rounded down, sub-seconds ignored)
1057 */
1058 std::time_t GetTai() const;
1059
1060 /**
1061 * @brief Get time as TAI seconds (CLOCK_TAI)
1062 *
1063 * @param[in] prec Round the seconds to this many fractional digits (0-9)
1064 *
1065 * @returns the TAI time in seconds
1066 */
1067 double GetTaiSec(const int prec = 9) const;
1068
1069 /**
1070 * @brief Get time as TAI nanoseconds (CLOCK_TAI)
1071 *
1072 * @returns the TAI time in nanoseconds
1073 */
1074 uint64_t GetTaiNs() const;
1075
1076 /**
1077 * @brief Get time as std::chrono::duration (milliseconds since epoch) (POSIX)
1078 *
1079 * @note See comments in the class description regarding POSIX time!
1080 *
1081 * @returns the time as std::chrono::milliseconds
1082 */
1083 std::chrono::milliseconds GetChronoMilli() const;
1084
1085 /**
1086 * @brief Get time as std::chrono::nanoseconds (nanoseconds since epoch) (POSIX)
1087 *
1088 * @note See comments in the class description regarding POSIX time!
1089 *
1090 * @returns the time as std::chrono::nanoseconds
1091 */
1092 std::chrono::nanoseconds GetChronoNano() const;
1093
1094 ///@}
1095 // -----------------------------------------------------------------------------------------------------------------
1096 /**
1097 * @name Misc
1098 * @{
1099 */
1100
1101 /**
1102 * @brief Check if time is zero (invalid)
1103 *
1104 * @returns true if time is exactly zero
1105 */
1106 bool IsZero() const;
1107
1108 ///@}
1109 // -----------------------------------------------------------------------------------------------------------------
1110 /**
1111 * @name Arithmetic methods
1112 *
1113 * These do not throw and instead return true/false.
1114 * @{
1115 */
1116
1117 /**
1118 * @brief Add duration to time
1119 *
1120 * @param[in] dur Duration to add
1121 *
1122 * @returns true if successful (dur value in range), false otherwise (bad sec value)
1123 */
1124 bool AddDur(const Duration& dur);
1125
1126 /**
1127 * @brief Add seconds to time
1128 *
1129 * @param[in] sec Seconds to add
1130 *
1131 * @returns true if successful (sec value in range), false otherwise (bad sec value)
1132 */
1133 bool AddSec(const double sec);
1134
1135 /**
1136 * @brief Add nanoseconds to time
1137 *
1138 * @param[in] nsec Nanoseconds to add
1139 *
1140 * @returns true if successful (sec value in range), false otherwise (bad sec value)
1141 */
1142 bool AddNSec(const int64_t nsec);
1143
1144 /**
1145 * @brief Substract duration from time
1146 *
1147 * @param[in] dur Duration to substract
1148 *
1149 * @returns true if successful (dur value in range), false otherwise (bad sec value)
1150 */
1151 bool SubDur(const Duration& dur);
1152
1153 /**
1154 * @brief Substract nanoseconds from time
1155 *
1156 * @param[in] nsec Nanoseconds to substract
1157 *
1158 * @returns true if successful (sec value in range), false otherwise (bad sec value)
1159 */
1160 bool SubNSec(const int64_t nsec);
1161
1162 /**
1163 * @brief Substract seconds from time
1164 *
1165 * @param[in] sec Seconds to substract
1166 *
1167 * @returns true if successful (sec value in range), false otherwise (bad sec value)
1168 */
1169 bool SubSec(const double sec);
1170
1171 /**
1172 * @brief Calculate difference between times
1173 *
1174 * @param[in] other The other time
1175 * @param[out] diff The difference to the other time (time - other)
1176 *
1177 * @returns true if the times and the difference are within range, false otherwise
1178 */
1179 bool Diff(const Time& other, Duration& diff) const;
1180
1181 ///@}
1182 // -----------------------------------------------------------------------------------------------------------------
1183 /**
1184 * @name Arithmetic operators
1185 *
1186 * @note These throw std::runtime_error if the arguments (the time) are out of range.
1187 * @{
1188 */
1189
1190 Time operator+(const Duration& rhs) const; //!< Sum time and duration
1191 Time operator+(const int64_t nsec) const; //!< Sum time and nanoseconds
1192 Time operator+(const double sec) const; //!< Sum time and seconds
1193 Time& operator+=(const Duration& rhs); //!< Add duration to time
1194 Time& operator+=(const int64_t nsec); //!< Add nanoseconds to time
1195 Time& operator+=(const double sec); //!< Add seconds to time
1196 Time operator-(const Duration& rhs) const; //!< Subtract time and duration
1197 Time operator-(const int64_t nsec) const; //!< Subtract time and nanoseconds
1198 Time operator-(const double sec) const; //!< Subtract time and seconds
1199 Time& operator-=(const Duration& rhs); //!< Subtract duration from time
1200 Time& operator-=(const int64_t nsec); //!< Subtract nanoseconds from time
1201 Time& operator-=(const double sec); //!< Subtract seconds from time
1202 Duration operator-(const Time& rhs) const; //!< Subtract time and time
1203
1204 ///@}
1205 // -----------------------------------------------------------------------------------------------------------------
1206 /**
1207 * @name Logic operators
1208 * @{
1209 */
1210
1211 bool operator==(const Time& rhs) const; //!< Equal
1212 bool operator!=(const Time& rhs) const; //!< Not equal
1213 bool operator>(const Time& rhs) const; //!< Greater than
1214 bool operator<(const Time& rhs) const; //!< Smaller than
1215 bool operator>=(const Time& rhs) const; //!< Greater or equal than
1216 bool operator<=(const Time& rhs) const; //!< Smaller or equal than
1217
1218 ///@}
1219 // -----------------------------------------------------------------------------------------------------------------
1220 /**
1221 * @name Stringification
1222 *
1223 * See also fpsdk::common::string::Strftime().
1224 *
1225 * @{
1226 */
1227
1228 /**
1229 * @brief Stringify as GNSS time (atomic)
1230 *
1231 * @param[in] sys The desired GNSS time system
1232 * @param[in] prec Number of fractional digits for the seconds (0-9)
1233 *
1234 * @returns a string with formatted week-number and time-of-week ("wwww:tttttt.ttt")
1235 */
1236 std::string StrWnoTow(const WnoTow::Sys sys = WnoTow::Sys::GPS, const int prec = 3) const;
1237
1238 /**
1239 * @brief Stringify as year, month, day, hour, minute and second time (UTC)
1240 *
1241 * @param[in] prec Number of fractional digits for the seconds (0-9)
1242 *
1243 * @returns a string with formatted UTC time ("yyyy-mm-dd hh:mm:ss.sss")
1244 */
1245 std::string StrUtcTime(const int prec = 3) const;
1246
1247 /**
1248 * @brief Stringify as ISO 8601 time (UTC)
1249 *
1250 * @param[in] prec Number of fractional digits for the seconds (0-9)
1251 *
1252 * @returns a string with formatted UTC time ("yyyyddmmThhmmssZ")
1253 */
1254 std::string StrIsoTime(const int prec = 0) const;
1255
1256 ///@}
1257 // -----------------------------------------------------------------------------------------------------------------
1258 /**
1259 * @name Leapseconds
1260 *
1261 * @{
1262 */
1263
1264 /**
1265 * @brief Set or change current leapseconds
1266 *
1267 * This extends the build-in leapseconds table with the current value. The given value is assumed to be the
1268 * leapseconds value (TAI - UTC) that is valid from the object's time (truncated to integer seconds). The object's
1269 * time must be past (later than) and the value must be greater or equal than the information in the latest entry in
1270 * the built-in table.
1271 *
1272 * The built-in table and the "current leapseconds" information are global per process. Setting or changing the
1273 * current leapseconds value affects all existing and future Time objects immediately.
1274 *
1275 * For example, the latest entry in the built-in table may be 2017-01-01 00:00:00 UTC with TAI-UTC = 37. So it could
1276 * be updated with a time of 2035-06-01 00:00:00 UTC and a value of 38.
1277 *
1278 * The idea is to use this in real-time applications where an upcoming leapseconds event can be learned from on-line
1279 * data, such as GNSS navigation data. Calling this method at and appropriate time with appropriate arguments is up
1280 * to the application. For example, if multiple future leapseconds events are known, the right event must be
1281 * "activated" at the right time.
1282 *
1283 * @param[in] value The current leapseconds value
1284 *
1285 * @returns true if the objects time and the leapseconds value were acceptable and set, false otherwise
1286 */
1287 bool SetCurrentLeapseconds(const int value) const;
1288
1289 /**
1290 * @brief Get leapseconds (TAI - UTC)
1291 *
1292 * @returns the leapseconds for the time
1293 */
1294 int GetLeapseconds() const;
1295
1296 /**
1297 * @brief Offset between GPS leapseconds and TAI-UTC leapseconds
1298 */
1299 static constexpr int GPS_LEAPSECONDS_OFFS = 19;
1300
1301 ///@}
1302 // -----------------------------------------------------------------------------------------------------------------
1303
1304 static const Time MIN; //!< Minimum representable time
1305 static const Time MAX; //!< Maximum representable time
1306 static const Time ZERO; //!< Zero (invalid, uninitialised) time
1307
1308 // Storage deliberately public
1309 uint32_t sec_; //!< Seconds part of time (atomic seconds since 1970-01-01 00:00:00 UTC)
1310 uint32_t nsec_; //!< Nanoseconds part of time (*should* be in range 0-999999999)
1311};
1312
1313/* ****************************************************************************************************************** */
1314} // namespace time
1315} // namespace common
1316} // namespace fpsdk
1317#endif // __FPSDK_COMMON_TIME_HPP__
Duration & operator+=(const int64_t nsec)
Add nanoseconds to durationn.
bool operator!=(const Duration &rhs) const
Not equal.
bool AddNSec(const int64_t nsec)
Add nanoseconds to duration.
Duration & operator+=(const double sec)
Add seconds to duration.
bool SetSecNSec(const int32_t sec, const int32_t nsec)
Set duration from seconds and nanoseconds.
Duration operator+(const double sec) const
Sum duration and seconds.
Duration operator*(double scale) const
Multiply (scale) duration.
Duration & operator-=(const int64_t nsec)
Subtract nanoseconds from duration.
static Duration FromSecNSec(const int32_t sec, const int32_t nsec)
Make Duration from seconds and nanoseconds.
int32_t sec_
Seconds part of duration.
Definition time.hpp:452
static Duration FromNSec(const int64_t nsec)
Make Duration from nanoseconds.
bool IsZero() const
Check if duration is zero.
Duration operator-(const Duration &rhs) const
Subtract duration and duration.
bool SubDur(const Duration &dur)
Substract duration from duration.
bool operator<=(const Duration &rhs) const
Smaller or equal than.
void Sleep() const
Sleep for the duration (if > 0)
Duration()
Constructor, zero duration.
Duration & operator*=(double scale)
Multiply (scale) duration.
Duration & operator-=(const Duration &rhs)
Subtract duration from duration.
std::chrono::nanoseconds GetChronoNano() const
Get duration as std::chrono::nanoseconds.
bool SetSec(const double sec)
Set duration from seconds.
std::chrono::milliseconds GetChronoMilli() const
Get duration as std::chrono::milliseconds.
double GetSec(const int prec=9) const
Get duration as seconds.
Duration operator-(const double sec) const
Subtract duration and seconds.
bool AddDur(const Duration &dur)
Add duration to duration.
int64_t GetNSec() const
Get duration as nanoseconds.
bool SubSec(const double sec)
Substract seconds from duration.
bool operator>(const Duration &rhs) const
Greater than.
Duration operator-(const int64_t nsec) const
Subtract duration and nanoseconds.
bool AddSec(const double sec)
Add seconds to duration.
Duration & operator+=(const Duration &rhs)
Add duration to duration.
int32_t nsec_
Nanoseconds part of duration (should be in range 0-999999999)
Definition time.hpp:453
bool Scale(const double sec)
Scale (multiply) duration.
Duration operator+(const Duration &rhs) const
Sum duration and duration.
std::string Stringify(const int prec=3) const
Stringify duration, for debugging.
Duration operator+(const int64_t nsec) const
Sum duration and nanoseconds.
bool SetNSec(const int64_t nsec)
Set duration from nanoseconds.
Duration operator-() const
Reverse sign.
bool SubNSec(const int64_t nsec)
Substract nanoseconds from duration.
static Duration FromSec(const double sec)
Make Duration from seconds.
bool operator==(const Duration &rhs) const
Equal.
Duration & operator-=(const double sec)
Subtract seconds from durationn.
bool operator>=(const Duration &rhs) const
Greater or equal than.
bool operator<(const Duration &rhs) const
Smaller than.
Helper to measure wallclock time.
Definition time.hpp:110
double TocMs(const bool reset=false)
Get elapsed wallclock time in [ms].
Duration Toc(const bool reset=false)
Get elapsed wallclock time.
void Tic()
(Re-)start measurement
UtcTime GetUtcTime(const int prec=9) const
Get time as UTC time (UTC)
double GetDayOfYear(const int prec=12) const
Get day of year.
Time operator-(const Duration &rhs) const
Subtract time and duration.
bool SetTaiNs(const uint64_t tai_ns)
Set time from TAI nanoseconds (CLOCK_TAI)
bool SetUtcTime(const UtcTime &utctime)
Set time from UTC time (UTC)
std::string StrUtcTime(const int prec=3) const
Stringify as year, month, day, hour, minute and second time (UTC)
Time & operator+=(const int64_t nsec)
Add nanoseconds to time.
static Time FromSec(const double sec)
From seconds (atomic)
bool SetTai(const std::time_t tai)
Set time from TAI time (CLOCK_TAI)
bool SetClockRealtime()
Set time from system clock current (now) system time (CLOCK_REALTIME)
Time operator+(const Duration &rhs) const
Sum time and duration.
Time & operator+=(const double sec)
Add seconds to time.
bool SetPosix(const std::time_t posix)
Set time from POSIX time (POSIX, seconds)
uint64_t GetTaiNs() const
Get time as TAI nanoseconds (CLOCK_TAI)
bool operator<=(const Time &rhs) const
Smaller or equal than.
static Time FromTaiSec(const double tai_sec)
From TAI seconds (CLOCK_TAI)
static const Time MIN
Minimum representable time.
Definition time.hpp:1304
GloTime GetGloTime(const int prec=9) const
Get time as GLONASS time (UTC + 3h)
static Time FromNSec(const uint64_t nsec)
Time from nanoseconds (atomic)
static const Time ZERO
Zero (invalid, uninitialised) time.
Definition time.hpp:1306
static constexpr int GPS_LEAPSECONDS_OFFS
Offset between GPS leapseconds and TAI-UTC leapseconds.
Definition time.hpp:1299
RosTime GetRosTime() const
Get time as ROS time (POSIX)
std::string StrWnoTow(const WnoTow::Sys sys=WnoTow::Sys::GPS, const int prec=3) const
Stringify as GNSS time (atomic)
static Time FromRosTime(const RosTime &rostime)
From ROS time (POSIX)
uint32_t nsec_
Nanoseconds part of time (should be in range 0-999999999)
Definition time.hpp:1310
int GetLeapseconds() const
Get leapseconds (TAI - UTC)
static Time FromPosix(const std::time_t posix)
From POSIX time (POSIX, seconds)
std::chrono::milliseconds GetChronoMilli() const
Get time as std::chrono::duration (milliseconds since epoch) (POSIX)
Time()
Constructor, empty (invalid) time.
bool SetCurrentLeapseconds(const int value) const
Set or change current leapseconds.
bool SetSec(const double sec)
Set time from seconds (atomic)
bool IsZero() const
Check if time is zero (invalid)
Time & operator-=(const int64_t nsec)
Subtract nanoseconds from time.
std::chrono::nanoseconds GetChronoNano() const
Get time as std::chrono::nanoseconds (nanoseconds since epoch) (POSIX)
bool SetGloTime(const GloTime &glotime)
Set time from GLONASS time (UTC + 3h)
bool SubDur(const Duration &dur)
Substract duration from time.
Duration operator-(const Time &rhs) const
Subtract time and time.
bool SetTaiSec(const double tai_sec)
Set time from TAI seconds (CLOCK_TAI)
static Time FromPosixNs(const uint64_t posix_ns)
From POSIX nanoseconds (POSIX)
bool operator>(const Time &rhs) const
Greater than.
Time & operator-=(const Duration &rhs)
Subtract duration from time.
uint32_t sec_
Seconds part of time (atomic seconds since 1970-01-01 00:00:00 UTC)
Definition time.hpp:1309
static Time FromUtcTime(const UtcTime &utctime)
From UTC time (UTC)
static Time FromGloTime(const GloTime &glotime)
From GLONASS time (UTC + 3h)
bool SetPosixNs(const uint64_t posix_ns)
Set time from POSIX nanoseconds (POSIX)
bool SubSec(const double sec)
Substract seconds from time.
static Time FromTaiNs(const uint64_t tai_ns)
From TAI nanoseconds (CLOCK_TAI)
Time operator+(const int64_t nsec) const
Sum time and nanoseconds.
bool AddSec(const double sec)
Add seconds to time.
bool AddNSec(const int64_t nsec)
Add nanoseconds to time.
bool SubNSec(const int64_t nsec)
Substract nanoseconds from time.
bool operator<(const Time &rhs) const
Smaller than.
static Time FromSecNSec(const uint32_t sec, const uint32_t nsec)
Time from seconds and nanoseconds (atomic)
static Time FromTai(const std::time_t tai)
From TAI time (CLOCK_TAI)
bool Diff(const Time &other, Duration &diff) const
Calculate difference between times.
bool SetSecNSec(const uint32_t sec, const uint32_t nsec)
Set time from seconds and nanoseconds (atomic)
Time & operator-=(const double sec)
Subtract seconds from time.
uint64_t GetNSec() const
Get time as nanoseconds (atomic)
static Time FromWnoTow(const WnoTow &wnotow)
From GNSS time (atomic)
Time operator-(const double sec) const
Subtract time and seconds.
static Time FromClockRealtime()
From system clock current (now) system time (CLOCK_REALTIME)
Time & operator+=(const Duration &rhs)
Add duration to time.
double GetPosixSec(const int prec=9) const
Get time as POSIX seconds (POSIX)
bool SetRosTime(const RosTime &rostime)
Set time from ROS time (POSIX)
bool AddDur(const Duration &dur)
Add duration to time.
bool operator==(const Time &rhs) const
Equal.
static const Time MAX
Maximum representable time.
Definition time.hpp:1305
bool operator>=(const Time &rhs) const
Greater or equal than.
std::time_t GetPosix() const
Get time as POSIX time (POSIX, seconds)
bool SetNSec(const uint64_t nsec)
Set time from nanoseconds (atomic)
Time operator-(const int64_t nsec) const
Subtract time and nanoseconds.
bool SetWnoTow(const WnoTow &wnotow)
Set time from GNSS (GPS, Galileo, BeiDou) time (atomic)
static Time FromPosixSec(const double posix_sec)
From POSIX seconds (POSIX)
std::time_t GetTai() const
Get time as TAI time (CLOCK_TAI)
bool SetPosixSec(const double posix_sec)
Set time from POSIX seconds (POSIX)
double GetTaiSec(const int prec=9) const
Get time as TAI seconds (CLOCK_TAI)
bool operator!=(const Time &rhs) const
Not equal.
double GetSec(const int prec=9) const
Get time as seconds (atomic)
WnoTow GetWnoTow(const WnoTow::Sys sys=WnoTow::Sys::GPS, const int prec=9) const
Get time as GNSS (GPS, Galileo, BeiDou) time (atomic)
uint64_t GetPosixNs() const
Get time as POSIX nanoseconds (POSIX)
std::string StrIsoTime(const int prec=0) const
Stringify as ISO 8601 time (UTC)
Time operator+(const double sec) const
Sum time and seconds.
static constexpr int SEC_IN_MIN_I
Constants.
Definition time.hpp:48
static constexpr double SEC_IN_MIN_D
Number of seconds in a minute (double) = 60.0.
Definition time.hpp:52
static constexpr int SEC_IN_WEEK_I
Number of seconds in a week (integer) = 604800.
Definition time.hpp:51
static constexpr double SEC_IN_WEEK_D
Number of seconds in a week (double) = 604800.0.
Definition time.hpp:55
uint64_t GetMillis()
Get milliseconds [ms], monotonic time.
static constexpr int SEC_IN_DAY_I
Number of seconds in a day (integer) = 86400.
Definition time.hpp:50
static constexpr double SEC_IN_HOUR_D
Number of seconds in an hour (double) = 3600.0.
Definition time.hpp:53
static constexpr double NsecToSec(const uint64_t ns)
Convert seconds to nanoseconds.
Definition time.hpp:67
static constexpr double SEC_IN_DAY_D
Number of seconds in a day (double) = 86400.0.
Definition time.hpp:54
double GetSecs()
Get seconds [s], monotonic time.
static constexpr uint64_t SecToNsec(const double sec)
Convert nanoseconds to seconds.
Definition time.hpp:79
static constexpr int SEC_IN_HOUR_I
Number of seconds in an hour (integer) = 3600.
Definition time.hpp:49
Fixposition SDK.
static constexpr int SANE_NT_MIN
Minimum sane Nt_ value.
Definition time.hpp:535
GloTime(const int N4, const int Nt, const double TOD)
Constructor.
static constexpr double SANE_TOD_MAX
Maximum sane TOD_ value.
Definition time.hpp:538
int Nt_
Day in four-year interval (SANE_NT_MIN - SANE_NT_MAX)
Definition time.hpp:541
double TOD_
Time of day (in Москва time zone) [s] (SANE_TOD_MIN - SANE_TOD_MAX)
Definition time.hpp:542
int N4_
Four-year interval, 1=1996..1999, 2=2000..2003, ... (SANE_N4_MIN - SANE_N4_MAX)
Definition time.hpp:540
static constexpr double SANE_TOD_MIN
Minimum sane TOD_ value.
Definition time.hpp:537
static constexpr int SANE_NT_MAX
Maximum sane Nt_ value.
Definition time.hpp:536
static constexpr int SANE_N4_MIN
Minimum sane N4_ value.
Definition time.hpp:533
static constexpr int SANE_N4_MAX
Maximum sane N4_ value, good enough for the next centuries.
Definition time.hpp:534
Minimal ros::Time() / rplcpp::Time implementation (that doesn't throw)
Definition time.hpp:152
uint64_t ToNSec() const
Convert to nanoseconds.
bool operator==(const RosTime &rhs) const
Equal.
RosTime(const uint32_t sec, const uint32_t nsec)
Constructor.
uint32_t nsec_
Nanoseconds part of time (should be in range 0-999999999)
Definition time.hpp:184
uint32_t sec_
Seconds part of time.
Definition time.hpp:183
double ToSec() const
Convert to seconds.
bool IsZero() const
Check if time is zero (invalid, unset)
UTC time representation.
Definition time.hpp:550
double sec_
Second (0-60)
Definition time.hpp:571
int min_
Minute (0-59)
Definition time.hpp:570
int month_
Month (1-12)
Definition time.hpp:567
UtcTime(const int year, const int month, const int day, const int hour, const int min, const double sec)
Constructor.
GNSS atomic time representation: week number (wno) and time of week (tow) used by GPS,...
Definition time.hpp:464
Sys sys_
Time system.
Definition time.hpp:502
double tow_
Time of week [s] (SANE_TOW_MIN - SANE_TOW_MAX, if normalised)
Definition time.hpp:501
static constexpr double SANE_TOW_MAX
Maximum sane tow_ value.
Definition time.hpp:498
int wno_
Week number [-] (>= SANE_WNO_MIN, if normalised)
Definition time.hpp:500
static constexpr int SANE_WNO_MIN
Minimum sane wno_ value.
Definition time.hpp:495
static constexpr double SANE_TOW_MIN
Minimum sane tow_ value.
Definition time.hpp:497
Sys
GNSS time system.
Definition time.hpp:469
@ GPS
GPS (also: SBAS, QZSS)
@ GAL
Galileo system time (GST)
WnoTow(const Sys sys=Sys::GPS)
Constructor.
static constexpr int SANE_WNO_MAX
Maximum sane wno_ value, good enough for the next centuries.
Definition time.hpp:496
WnoTow(const int wno, const double tow, const Sys sys=Sys::GPS)
Constructor.
const char * SysStr() const
Stringify time system (for debugging)