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