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