Fixposition SDK 0.0.0-heads/main-0-g2db0464
Collection of c++ libraries and apps for use with Fixposition products on Linux
Loading...
Searching...
No Matches
time.hpp
Go to the documentation of this file.
1/**
2 * \verbatim
3 * ___ ___
4 * \ \ / /
5 * \ \/ / Copyright (c) Fixposition AG (www.fixposition.com) and contributors
6 * / /\ \ License: see the LICENSE file
7 * /__/ \__\
8 *
9 * Parts copyright (c) 2008, Willow Garage, Inc., see time.cpp and the LICENSE file for details
10 * Written by flipflip (https://github.com/phkehl)
11 * \endverbatim
12 *
13 * @file
14 * @brief Fixposition SDK: Time utilities
15 *
16 * @page FPSDK_COMMON_TIME Time utilities
17 *
18 * **API**: fpsdk_common/time.hpp and fpsdk::common::time
19 *
20 */
21#ifndef __FPSDK_COMMON_TIME_HPP__
22#define __FPSDK_COMMON_TIME_HPP__
23
24/* LIBC/STL */
25#include <chrono>
26#include <cstdint>
27#include <ctime>
28#include <string>
29
30/* EXTERNAL */
31
32/* PACKAGE */
33
34namespace fpsdk {
35namespace common {
36/**
37 * @brief Time utilities
38 */
39namespace time {
40/* ****************************************************************************************************************** */
41
42/**
43 * @brief Constants
44 * @{
45 */
46
47// clang-format off
48static constexpr int SEC_IN_MIN_I = 60; //!< Number of seconds in a minute (integer) = 60
49static constexpr int SEC_IN_HOUR_I = 60 * SEC_IN_MIN_I; //!< Number of seconds in an hour (integer) = 3600
50static constexpr int SEC_IN_DAY_I = 24 * SEC_IN_HOUR_I; //!< Number of seconds in a day (integer) = 86400
51static constexpr int SEC_IN_WEEK_I = 7 * SEC_IN_DAY_I; //!< Number of seconds in a week (integer) = 604800
52static constexpr double SEC_IN_MIN_D = static_cast<double>(SEC_IN_MIN_I); //!< Number of seconds in a minute (double) = 60.0
53static constexpr double SEC_IN_HOUR_D = static_cast<double>(SEC_IN_HOUR_I); //!< Number of seconds in an hour (double) = 3600.0
54static constexpr double SEC_IN_DAY_D = static_cast<double>(SEC_IN_DAY_I); //!< Number of seconds in a day (double) = 86400.0
55static constexpr double SEC_IN_WEEK_D = static_cast<double>(SEC_IN_WEEK_I); //!< Number of seconds in a week (double) = 604800.0
56// clang-format on
57
58/**
59 * @brief Convert seconds to nanoseconds
60 *
61 * @note Depending on the \c ns value this can be lossy.
62 *
63 * @param[in] ns Nanoseconds alue
64 *
65 * @returns the seconds value
66 */
67static constexpr double NsecToSec(const uint64_t ns)
68{
69 return (double)ns * 1e-9; // (double)(ns % (uint64_t)1000000000) + ((double)(ns / (uint64_t)1000000000) * 1e-9);
70}
71
72/**
73 * @brief Convert nanoseconds to seconds
74 *
75 * @param[in] sec Seconds value
76 *
77 * @returns the nanoseconds value
78 */
79static constexpr uint64_t SecToNsec(const double sec)
80{
81 return sec * 1e9;
82}
83
84///@}
85
86// ---------------------------------------------------------------------------------------------------------------------
87
88/**
89 * @brief Get milliseconds [ms]
90 *
91 * Time is monotonic time since first call to GetMillis() or GetSecs().
92 *
93 * @returns the number of milliseconds (> 0)
94 */
95uint64_t GetMillis();
96
97/**
98 * @brief Get seconds [s]
99 *
100 * Time is monotonic time since first call to GetMillis() or GetSecs().
101 *
102 * @returns the number of seconds
103 */
104double GetSecs();
105
106// ---------------------------------------------------------------------------------------------------------------------
107
108class Duration; // forward declaration
109
110/**
111 * @brief Helper to measure wallclock time
112 */
114{
115 public:
116 /**
117 * @brief Constructor
118 *
119 * This also starts the measurements, like Tic().
120 */
122
123 /**
124 * @brief (Re-)start measurement
125 */
126 void Tic();
127
128 /**
129 * @brief Get elapsed wallclock time
130 *
131 * @param[in] reset Reset (restart) measurements
132 *
133 * @returns the elapsed time since object constructed resp. last call to Tic()
134 */
135 Duration Toc(const bool reset = false);
136
137 /**
138 * @brief Get elapsed wallclock time in [ms]
139 *
140 * @param[in] reset Reset (restart) measurements
141 *
142 * @returns the elapsed time since object constructed resp. last call to Tic() in [ms]
143 */
144 double TocMs(const bool reset = false);
145
146 private:
147 std::chrono::time_point<std::chrono::steady_clock> t0_;
148};
149
150// ---------------------------------------------------------------------------------------------------------------------
151
152/**
153 * @brief Minimal ros::Time() / rplcpp::Time implementation (that doesn't throw)
154 */
155struct RosTime
156{
157 RosTime();
158 /**
159 * @brief Constructor
160 *
161 * @param[in] sec Time value seconds
162 * @param[in] nsec Time value nanoseconds
163 */
164 RosTime(const uint32_t sec, const uint32_t nsec);
165
166 /**
167 * @brief 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 * See SetWnoTow() for details.
723 *
724 * @param[in] wnotow GNSS time
725 *
726 * @returns the Time object
727 */
728 static Time FromWnoTow(const WnoTow& wnotow);
729
730 /**
731 * @brief From GLONASS time (UTC + 3h)
732 *
733 * @param[in] glotime GLONASS time
734 *
735 * @returns the Time object
736 */
737 static Time FromGloTime(const GloTime& glotime);
738
739 /**
740 * @brief From UTC time (UTC)
741 *
742 * @param[in] utctime UTC time
743 *
744 * @returns the Time object
745 */
746 static Time FromUtcTime(const UtcTime& utctime);
747
748 /**
749 * @brief From TAI time (CLOCK_TAI)
750 *
751 * @returns true if successful, false otherwise (bad time)
752 */
753 static Time FromTai(const std::time_t tai);
754
755 /**
756 * @brief From TAI seconds (CLOCK_TAI)
757 *
758 * @param[in] tai_sec Time value seconds (> 0.0)
759 *
760 * @returns the Time object
761 */
762 static Time FromTaiSec(const double tai_sec);
763
764 /**
765 * @brief From TAI nanoseconds (CLOCK_TAI)
766 *
767 * @param[in] tai_ns Time value nanoseconds (> 0)
768 *
769 * @returns the Time object
770 */
771 static Time FromTaiNs(const uint64_t tai_ns);
772
773 /**
774 * @brief From system clock current (now) system time (CLOCK_REALTIME)
775 *
776 * @returns the Time object
777 */
779
780#ifdef CLOCK_TAI
781 /**
782 * @brief From system clock current (now) atomic time (CLOCK_TAI)
783 *
784 * @note This will only produce the right result if your system is configured accordingly (which may not be the
785 * case). See comments in the Time class description.
786 *
787 * @returns the Time object
788 */
789 static Time FromClockTai();
790#endif
791
792 ///@}
793 // -----------------------------------------------------------------------------------------------------------------
794 /**
795 * @name Set time
796 *
797 * These do not throw and instead return true/false.
798 * @{
799 */
800
801 /**
802 * @brief Set time from seconds and nanoseconds (atomic)
803 *
804 * @param[in] sec Time value seconds
805 * @param[in] nsec Time value nanoseconds
806 *
807 * @returns true if successful, false otherwise (bad time)
808 */
809 bool SetSecNSec(const uint32_t sec, const uint32_t nsec);
810
811 /**
812 * @brief Set time from nanoseconds (atomic)
813 *
814 * @param[in] nsec Time value nanoseconds (> 0)
815 *
816 * @returns true if successful, false otherwise (bad time)
817 */
818 bool SetNSec(const uint64_t nsec);
819
820 /**
821 * @brief Set time from seconds (atomic)
822 *
823 * @param[in] sec Time value seconds (> 0.0)
824 *
825 * @returns true if successful, false otherwise (bad time)
826 */
827 bool SetSec(const double sec);
828
829 /**
830 * @brief Set time from POSIX time (POSIX, seconds)
831 *
832 * @note See comments in the class description regarding POSIX time!
833 *
834 * @param[in] posix Time value seconds (> 0)
835 *
836 * @returns true if successful, false otherwise (bad time)
837 */
838 bool SetPosix(const std::time_t posix);
839
840 /**
841 * @brief Set time from POSIX seconds (POSIX)
842 *
843 * @note See comments in the class description regarding POSIX time!
844 *
845 * @param[in] posix_sec Time value seconds (> 0.0)
846 *
847 * @returns true if successful, false otherwise (bad time)
848 */
849 bool SetPosixSec(const double posix_sec);
850
851 /**
852 * @brief Set time from POSIX nanoseconds (POSIX)
853 *
854 * @note See comments in the class description regarding POSIX time!
855 *
856 * @param[in] posix_ns Time value nanoseconds (> 0)
857 *
858 * @returns true if successful, false otherwise (bad time)
859 */
860 bool SetPosixNs(const uint64_t posix_ns);
861
862 /**
863 * @brief Set time from ROS time (POSIX)
864 *
865 * @note See comments in the class description regarding POSIX time!
866 *
867 * @param[in] rostime Time value
868 *
869 * @returns true if successful, false otherwise (bad time)
870 */
871 bool SetRosTime(const RosTime& rostime);
872
873 /**
874 * @brief Set time from GNSS (GPS, Galileo, BeiDou) time (atomic)
875 *
876 * @note Input values are normalised, so tow < SANE_TOW_MIN, tow > SANE_TOW_MAX, and wno < SANE_WNO_MIN are
877 * acceptable. Compare GetWnoTow().
878 *
879 * @param[in] wnotow GNSS time
880 *
881 * @returns true if successful, false otherwise (bad time)
882 */
883 bool SetWnoTow(const WnoTow& wnotow);
884
885 /**
886 * @brief Set time from GLONASS time (UTC + 3h)
887 *
888 * @note Input values will be normalised, so e.g. Nt_ < SANE_NT_MIN or N4_ > SANE_N4_MAX, ... are acceptable.
889 * Compare GetGloTime().
890 *
891 * @param[in] glotime GLONASS time
892 *
893 * @returns true if successful, false otherwise (bad time)
894 */
895 bool SetGloTime(const GloTime& glotime);
896
897 /**
898 * @brief Set time from UTC time (UTC)
899 *
900 * @param[in] utctime UTC time
901 *
902 * @returns true if successful, false otherwise (bad time)
903 */
904 bool SetUtcTime(const UtcTime& utctime);
905
906 /**
907 * @brief Set time from TAI time (CLOCK_TAI)
908 *
909 * @param[in] tai The TAI time
910 *
911 * @returns true if successful, false otherwise (bad time)
912 */
913 bool SetTai(const std::time_t tai);
914
915 /**
916 * @brief Set time from TAI seconds (CLOCK_TAI)
917 *
918 * @param[in] tai_sec Time value seconds (> 0.0)
919 *
920 * @returns true if successful, false otherwise (bad time)
921 */
922 bool SetTaiSec(const double tai_sec);
923
924 /**
925 * @brief Set time from TAI nanoseconds (CLOCK_TAI)
926 *
927 * @param[in] tai_ns Time value nanoseconds (> 0)
928 *
929 * @returns true if successful, false otherwise (bad time)
930 */
931 bool SetTaiNs(const uint64_t tai_ns);
932
933 /**
934 * @brief Set time from system clock current (now) system time (CLOCK_REALTIME)
935 *
936 * @returns true if successful, false otherwise (bad time)
937 */
939
940#ifdef CLOCK_TAI
941 /**
942 * @brief Set time from system clock current (now) TAI time (CLOCK_TAI)
943 *
944 * @note This will only produce the right result if your system is configured accordingly (which is unlikely). See
945 * comments in the Time class description.
946 *
947 * @returns the Time object
948 */
949
950 bool SetClockTai();
951#endif
952
953 ///@}
954 // -----------------------------------------------------------------------------------------------------------------
955 /**
956 * @name Get time
957 * @{
958 */
959
960 /**
961 * @brief Get time as nanoseconds (atomic)
962 *
963 * @returns the time as nanoseconds
964 */
965 uint64_t GetNSec() const;
966
967 /**
968 * @brief Get time as seconds (atomic)
969 *
970 * @param[in] prec Round the seconds to this many fractional digits (0-9)
971 *
972 * @returns the time as seconds
973 */
974 double GetSec(const int prec = 9) const;
975
976 /**
977 * @brief Get time as POSIX time (POSIX, seconds)
978 *
979 * @note See comments in the class description regarding POSIX time!
980 *
981 * @returns the POSIX time, truncated (rounded down, sub-seconds ignored)
982 */
983 std::time_t GetPosix() const;
984
985 /**
986 * @brief Get time as POSIX seconds (POSIX)
987 *
988 * @note See comments in the class description regarding POSIX time!
989 *
990 * @param[in] prec Round the seconds to this many fractional digits (0-9)
991 *
992 * @returns the POSIX time in seconds
993 */
994 double GetPosixSec(const int prec = 9) const;
995
996 /**
997 * @brief Get time as POSIX nanoseconds (POSIX)
998 *
999 * @note See comments in the class description regarding POSIX time!
1000 *
1001 * @returns the POSIX time in nanoseconds
1002 */
1003 uint64_t GetPosixNs() const;
1004
1005 /**
1006 * @brief Get time as ROS time (POSIX)
1007 *
1008 * @note See comments in the class description regarding POSIX time!
1009 *
1010 * @returns the time
1011 */
1013
1014 /**
1015 * @brief Get time as GNSS (GPS, Galileo, BeiDou) time (atomic)
1016 *
1017 * @note For times before the respective GNSS epoch the result is not usable (e.g. wno_ may be < SANE_WNO_MIN).
1018 * Output tow_ is guaranteed to be within sane range (i.e. SANE_TOW_MIN <= tow_ < SANE_TOW_MAX). Compare
1019 * SetWnoTow()
1020 *
1021 * @param[in] sys GNSS
1022 * @param[in] prec Round the seconds to this many fractional digits (0-9)
1023 *
1024 * @returns the GNSS time for the selected GNSS
1025 */
1026 WnoTow GetWnoTow(const WnoTow::Sys sys = WnoTow::Sys::GPS, const int prec = 9) const;
1027
1028 /**
1029 * @brief Get time as GLONASS time (UTC + 3h)
1030 *
1031 * @note For times before the respective GLONASS epoch the result is not usable (e.g. N4_ may be < SANE_N4_MIN).
1032 * Output Nt_ and tod_ are guaranteed to be within sane range (e.g. SANE_NT_MIN <= Nt_ < SANE_NT_MAX). Compare
1033 * SetGloTime()
1034 *
1035 * @param[in] prec Round the seconds to this many fractional digits (0-9)
1036 *
1037 * @returns the GLONASS time
1038 */
1039 GloTime GetGloTime(const int prec = 9) const;
1040
1041 /**
1042 * @brief Get time as UTC time (UTC)
1043 *
1044 * @param[in] prec Round the seconds to this many fractional digits (0-9)
1045 *
1046 * @returns the UTC time
1047 */
1048 UtcTime GetUtcTime(const int prec = 9) const;
1049
1050 /**
1051 * @brief Get day of year
1052 *
1053 * @param[in] prec Round to this many fractional digits (0-12)
1054 *
1055 * @returns the day of the year
1056 */
1057 double GetDayOfYear(const int prec = 12) const;
1058
1059 /**
1060 * @brief Get time as TAI time (CLOCK_TAI)
1061 *
1062 * @returns the TAI time, truncated (rounded down, sub-seconds ignored)
1063 */
1064 std::time_t GetTai() const;
1065
1066 /**
1067 * @brief Get time as TAI seconds (CLOCK_TAI)
1068 *
1069 * @param[in] prec Round the seconds to this many fractional digits (0-9)
1070 *
1071 * @returns the TAI time in seconds
1072 */
1073 double GetTaiSec(const int prec = 9) const;
1074
1075 /**
1076 * @brief Get time as TAI nanoseconds (CLOCK_TAI)
1077 *
1078 * @returns the TAI time in nanoseconds
1079 */
1080 uint64_t GetTaiNs() const;
1081
1082 /**
1083 * @brief Get time as std::chrono::duration (milliseconds since epoch) (POSIX)
1084 *
1085 * @note See comments in the class description regarding POSIX time!
1086 *
1087 * @returns the time as std::chrono::milliseconds
1088 */
1089 std::chrono::milliseconds GetChronoMilli() const;
1090
1091 /**
1092 * @brief Get time as std::chrono::nanoseconds (nanoseconds since epoch) (POSIX)
1093 *
1094 * @note See comments in the class description regarding POSIX time!
1095 *
1096 * @returns the time as std::chrono::nanoseconds
1097 */
1098 std::chrono::nanoseconds GetChronoNano() const;
1099
1100 ///@}
1101 // -----------------------------------------------------------------------------------------------------------------
1102 /**
1103 * @name Misc
1104 * @{
1105 */
1106
1107 /**
1108 * @brief Check if time is zero (invalid)
1109 *
1110 * @returns true if time is exactly zero
1111 */
1112 bool IsZero() const;
1113
1114 ///@}
1115 // -----------------------------------------------------------------------------------------------------------------
1116 /**
1117 * @name Arithmetic methods
1118 *
1119 * These do not throw and instead return true/false.
1120 * @{
1121 */
1122
1123 /**
1124 * @brief Add duration to time
1125 *
1126 * @param[in] dur Duration to add
1127 *
1128 * @returns true if successful (dur value in range), false otherwise (bad sec value)
1129 */
1130 bool AddDur(const Duration& dur);
1131
1132 /**
1133 * @brief Add seconds to time
1134 *
1135 * @param[in] sec Seconds to add
1136 *
1137 * @returns true if successful (sec value in range), false otherwise (bad sec value)
1138 */
1139 bool AddSec(const double sec);
1140
1141 /**
1142 * @brief Add nanoseconds to time
1143 *
1144 * @param[in] nsec Nanoseconds to add
1145 *
1146 * @returns true if successful (sec value in range), false otherwise (bad sec value)
1147 */
1148 bool AddNSec(const int64_t nsec);
1149
1150 /**
1151 * @brief Substract duration from time
1152 *
1153 * @param[in] dur Duration to substract
1154 *
1155 * @returns true if successful (dur value in range), false otherwise (bad sec value)
1156 */
1157 bool SubDur(const Duration& dur);
1158
1159 /**
1160 * @brief Substract nanoseconds from time
1161 *
1162 * @param[in] nsec Nanoseconds to substract
1163 *
1164 * @returns true if successful (sec value in range), false otherwise (bad sec value)
1165 */
1166 bool SubNSec(const int64_t nsec);
1167
1168 /**
1169 * @brief Substract seconds from time
1170 *
1171 * @param[in] sec Seconds to substract
1172 *
1173 * @returns true if successful (sec value in range), false otherwise (bad sec value)
1174 */
1175 bool SubSec(const double sec);
1176
1177 /**
1178 * @brief Calculate difference between times
1179 *
1180 * @param[in] other The other time
1181 * @param[out] diff The difference to the other time (time - other)
1182 *
1183 * @returns true if the times and the difference are within range, false otherwise
1184 */
1185 bool Diff(const Time& other, Duration& diff) const;
1186
1187 ///@}
1188 // -----------------------------------------------------------------------------------------------------------------
1189 /**
1190 * @name Arithmetic operators
1191 *
1192 * @note These throw std::runtime_error if the arguments (the time) are out of range.
1193 * @{
1194 */
1195
1196 Time operator+(const Duration& rhs) const; //!< Sum time and duration
1197 Time operator+(const int64_t nsec) const; //!< Sum time and nanoseconds
1198 Time operator+(const double sec) const; //!< Sum time and seconds
1199 Time& operator+=(const Duration& rhs); //!< Add duration to time
1200 Time& operator+=(const int64_t nsec); //!< Add nanoseconds to time
1201 Time& operator+=(const double sec); //!< Add seconds to time
1202 Time operator-(const Duration& rhs) const; //!< Subtract time and duration
1203 Time operator-(const int64_t nsec) const; //!< Subtract time and nanoseconds
1204 Time operator-(const double sec) const; //!< Subtract time and seconds
1205 Time& operator-=(const Duration& rhs); //!< Subtract duration from time
1206 Time& operator-=(const int64_t nsec); //!< Subtract nanoseconds from time
1207 Time& operator-=(const double sec); //!< Subtract seconds from time
1208 Duration operator-(const Time& rhs) const; //!< Subtract time and time
1209
1210 ///@}
1211 // -----------------------------------------------------------------------------------------------------------------
1212 /**
1213 * @name Logic operators
1214 * @{
1215 */
1216
1217 bool operator==(const Time& rhs) const; //!< Equal
1218 bool operator!=(const Time& rhs) const; //!< Not equal
1219 bool operator>(const Time& rhs) const; //!< Greater than
1220 bool operator<(const Time& rhs) const; //!< Smaller than
1221 bool operator>=(const Time& rhs) const; //!< Greater or equal than
1222 bool operator<=(const Time& rhs) const; //!< Smaller or equal than
1223
1224 ///@}
1225 // -----------------------------------------------------------------------------------------------------------------
1226 /**
1227 * @name Stringification
1228 *
1229 * See also fpsdk::common::string::Strftime().
1230 *
1231 * @{
1232 */
1233
1234 /**
1235 * @brief Stringify as GNSS time (atomic)
1236 *
1237 * @param[in] sys The desired GNSS time system
1238 * @param[in] prec Number of fractional digits for the seconds (0-9)
1239 *
1240 * @returns a string with formatted week-number and time-of-week ("wwww:tttttt.ttt")
1241 */
1242 std::string StrWnoTow(const WnoTow::Sys sys = WnoTow::Sys::GPS, const int prec = 3) const;
1243
1244 /**
1245 * @brief Stringify as year, month, day, hour, minute and second time (UTC)
1246 *
1247 * @param[in] prec Number of fractional digits for the seconds (0-9)
1248 *
1249 * @returns a string with formatted UTC time ("yyyy-mm-dd hh:mm:ss.sss")
1250 */
1251 std::string StrUtcTime(const int prec = 3) const;
1252
1253 /**
1254 * @brief Stringify as ISO 8601 time (UTC)
1255 *
1256 * @param[in] prec Number of fractional digits for the seconds (0-9)
1257 *
1258 * @returns a string with formatted UTC time ("yyyy-dd-mmThh:mm:ssZ")
1259 */
1260 std::string StrIsoTime(const int prec = 0) const;
1261
1262 ///@}
1263 // -----------------------------------------------------------------------------------------------------------------
1264 /**
1265 * @name Leapseconds
1266 *
1267 * @{
1268 */
1269
1270 /**
1271 * @brief Set or change current leapseconds
1272 *
1273 * This extends the build-in leapseconds table with the current value. The given value is assumed to be the
1274 * leapseconds value (TAI - UTC) that is valid from the object's time (truncated to integer seconds). The object's
1275 * time must be past (later than) and the value must be greater or equal than the information in the latest entry in
1276 * the built-in table.
1277 *
1278 * The built-in table and the "current leapseconds" information are global per process. Setting or changing the
1279 * current leapseconds value affects all existing and future Time objects immediately.
1280 *
1281 * 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
1282 * be updated with a time of 2035-06-01 00:00:00 UTC and a value of 38.
1283 *
1284 * The idea is to use this in real-time applications where an upcoming leapseconds event can be learned from on-line
1285 * data, such as GNSS navigation data. Calling this method at and appropriate time with appropriate arguments is up
1286 * to the application. For example, if multiple future leapseconds events are known, the right event must be
1287 * "activated" at the right time.
1288 *
1289 * @param[in] value The current leapseconds value
1290 *
1291 * @returns true if the objects time and the leapseconds value were acceptable and set, false otherwise
1292 */
1293 bool SetCurrentLeapseconds(const int value) const;
1294
1295 /**
1296 * @brief Get leapseconds (TAI - UTC)
1297 *
1298 * @returns the leapseconds for the time
1299 */
1300 int GetLeapseconds() const;
1301
1302 /**
1303 * @brief Offset between GPS leapseconds and TAI-UTC leapseconds
1304 */
1305 static constexpr int GPS_LEAPSECONDS_OFFS = 19;
1306
1307 ///@}
1308 // -----------------------------------------------------------------------------------------------------------------
1309
1310 static const Time MIN; //!< Minimum representable time
1311 static const Time MAX; //!< Maximum representable time
1312 static const Time ZERO; //!< Zero (invalid, uninitialised) time
1313
1314 // Storage deliberately public
1315 uint32_t sec_; //!< Seconds part of time (atomic seconds since 1970-01-01 00:00:00 UTC)
1316 uint32_t nsec_; //!< Nanoseconds part of time (*should* be in range 0-999999999)
1317};
1318
1319/* ****************************************************************************************************************** */
1320} // namespace time
1321} // namespace common
1322} // namespace fpsdk
1323#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.
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:1310
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:1312
static constexpr int GPS_LEAPSECONDS_OFFS
Offset between GPS leapseconds and TAI-UTC leapseconds.
Definition time.hpp:1305
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:1316
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:1315
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:1311
bool operator>=(const Time &rhs) const
Greater or equal than.
std::time_t GetPosix() const
Get time as POSIX time (POSIX, seconds)
bool SetNSec(const uint64_t nsec)
Set time from nanoseconds (atomic)
Time operator-(const int64_t nsec) const
Subtract time and nanoseconds.
bool SetWnoTow(const WnoTow &wnotow)
Set time from GNSS (GPS, Galileo, BeiDou) time (atomic)
static Time FromPosixSec(const double posix_sec)
From POSIX seconds (POSIX)
std::time_t GetTai() const
Get time as TAI time (CLOCK_TAI)
bool SetPosixSec(const double posix_sec)
Set time from POSIX seconds (POSIX)
double GetTaiSec(const int prec=9) const
Get time as TAI seconds (CLOCK_TAI)
bool operator!=(const Time &rhs) const
Not equal.
double GetSec(const int prec=9) const
Get time as seconds (atomic)
WnoTow GetWnoTow(const WnoTow::Sys sys=WnoTow::Sys::GPS, const int prec=9) const
Get time as GNSS (GPS, Galileo, BeiDou) time (atomic)
uint64_t GetPosixNs() const
Get time as POSIX nanoseconds (POSIX)
std::string StrIsoTime(const int prec=0) const
Stringify as ISO 8601 time (UTC)
Time operator+(const double sec) const
Sum time and seconds.
Time utilities.
Definition time.hpp:39
static constexpr int SEC_IN_MIN_I
Constants.
Definition time.hpp:48
static constexpr double SEC_IN_MIN_D
Number of seconds in a minute (double) = 60.0.
Definition time.hpp:52
static constexpr int SEC_IN_WEEK_I
Number of seconds in a week (integer) = 604800.
Definition time.hpp:51
static constexpr double SEC_IN_WEEK_D
Number of seconds in a week (double) = 604800.0.
Definition time.hpp:55
uint64_t GetMillis()
Get milliseconds [ms].
static constexpr int SEC_IN_DAY_I
Number of seconds in a day (integer) = 86400.
Definition time.hpp:50
static constexpr double SEC_IN_HOUR_D
Number of seconds in an hour (double) = 3600.0.
Definition time.hpp:53
static constexpr double NsecToSec(const uint64_t ns)
Convert seconds to nanoseconds.
Definition time.hpp:67
static constexpr double SEC_IN_DAY_D
Number of seconds in a day (double) = 86400.0.
Definition time.hpp:54
double GetSecs()
Get seconds [s].
static constexpr uint64_t SecToNsec(const double sec)
Convert nanoseconds to seconds.
Definition time.hpp:79
static constexpr int SEC_IN_HOUR_I
Number of seconds in an hour (integer) = 3600.
Definition time.hpp:49
Fixposition SDK: Common library.
Definition doc.hpp:21
Fixposition SDK.
static constexpr int SANE_NT_MIN
Minimum sane Nt_ value.
Definition time.hpp: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)
Definition time.hpp:474
@ GAL
Galileo system time (GST)
Definition time.hpp:475
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)