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