Zth (libzth)
Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1#ifndef ZTH_TIME_H
2#define ZTH_TIME_H
3/*
4 * SPDX-FileCopyrightText: 2019-2026 Jochem Rutgers
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 */
8
18#include <libzth/macros.h>
19
20#ifdef __cplusplus
21
22# include <libzth/allocator.h>
23# include <libzth/config.h>
24# include <libzth/util.h>
25
26# include <algorithm>
27# include <cmath>
28# include <cstdio>
29# include <ctime>
30# include <inttypes.h>
31# include <limits>
32
33# if __cplusplus >= 201103L
34# include <chrono>
35# endif // C++11
36
37# if defined(ZTH_OS_MAC) || defined(ZTH_OS_BAREMETAL)
38# ifdef ZTH_CUSTOM_CLOCK_GETTIME
39extern "C" int clock_gettime(int clk_id, struct timespec* res);
40# endif
41extern "C" int
42clock_nanosleep(int clk_id, int flags, struct timespec const* request, struct timespec* remain);
43# ifndef CLOCK_MONOTONIC
44# define CLOCK_MONOTONIC 1
45# endif
46# ifndef TIMER_ABSTIME
47# define TIMER_ABSTIME 1
48# endif
49# endif
50
51namespace zth {
52
59ZTH_EXPORT inline void now(struct timespec& ts)
60{
61 int res = clock_gettime(CLOCK_MONOTONIC, &ts);
62 (void)res;
63 zth_assert(res == 0);
64}
65
74ZTH_EXPORT int realsleep(struct timespec const& ts);
75
76class Timestamp;
77
84private:
85 // Do not convert automatically.
86 TimeInterval(Timestamp const&);
87
88public:
89 static long const BILLION = 1000000000L;
90
91 constexpr static TimeInterval infinity() noexcept
92 {
93 return TimeInterval(std::numeric_limits<time_t>::max());
94 }
95
96 constexpr static TimeInterval null() noexcept
97 {
98 return TimeInterval();
99 }
100
101 constexpr TimeInterval() noexcept
102 : m_t()
103 , m_negative()
104 {}
105
106# if __cplusplus >= 201103L
107 // cppcheck-suppress noExplicitConstructor
108 constexpr TimeInterval(time_t s, long ns = 0, bool negative = false) noexcept
109 : m_t{s, ns}
110 , m_negative{negative}
111 {}
112# else
113 // cppcheck-suppress noExplicitConstructor
114 TimeInterval(time_t s, long ns = 0, bool negative = false) noexcept
115 : m_t()
116 , m_negative(negative)
117 {
118 m_t.tv_sec = s;
119 m_t.tv_nsec = ns;
121 }
122# endif
123
124 // cppcheck-suppress noExplicitConstructor
125 constexpr TimeInterval(struct timespec const& ts) noexcept
126 : m_t(ts)
127 , m_negative()
128 {}
129
131 {
132 m_t = t.m_t;
133 m_negative = t.m_negative;
134 return *this;
135 }
136
137 constexpr TimeInterval(TimeInterval const& t) noexcept
138 : m_t(t.ts())
139 , m_negative(t.isNegative())
140 {}
141
142 // cppcheck-suppress noExplicitConstructor
143 TimeInterval(float dt)
144 : m_t()
145 , m_negative()
146 {
147 init_float<float>(dt);
148 }
149
150 // cppcheck-suppress noExplicitConstructor
151 TimeInterval(double dt)
152 : m_t()
153 , m_negative()
154 {
155 init_float<double>(dt);
156 }
157
158 // cppcheck-suppress noExplicitConstructor
159 TimeInterval(long double dt)
160 : m_t()
161 , m_negative()
162 {
163 init_float<long double>(dt);
164 }
165
166 template <typename T>
167 // cppcheck-suppress noExplicitConstructor
169 : m_t()
170 , m_negative()
171 {
172 init_int<T>(dt);
173 }
174
175 template <typename T>
177 {
178 if(s > std::numeric_limits<time_t>::max())
179 return TimeInterval(std::numeric_limits<time_t>::max(), 999999999L);
180
181 return TimeInterval(s);
182 }
183
184 template <typename T>
186 {
187 T s_ = ms / (T)1000;
188 if((unsigned long long)s_ > (unsigned long long)std::numeric_limits<time_t>::max())
189 return TimeInterval(std::numeric_limits<time_t>::max(), 999999999L);
190
191 return TimeInterval((time_t)s_, ((long)ms % 1000L) * 1000000L);
192 }
193
194 template <typename T>
196 {
197 T s_ = us / (T)1000000;
198 if((unsigned long long)s_ > (unsigned long long)std::numeric_limits<time_t>::max())
199 return TimeInterval(std::numeric_limits<time_t>::max(), 999999999L);
200
201 return TimeInterval((time_t)s_, ((long)us % 1000000L) * 1000L);
202 }
203
204 template <typename T>
206 {
207 T s_ = ns / (T)1000000000L;
208 if(s_ > std::numeric_limits<time_t>::max())
209 return TimeInterval(std::numeric_limits<time_t>::max(), 999999999L);
210
211 return TimeInterval((time_t)s_, (long)ns % 1000000000L);
212 }
213
214# if __cplusplus >= 201103L
215 template <typename Rep>
216 // cppcheck-suppress noExplicitConstructor
217 TimeInterval(std::chrono::duration<Rep, std::nano> ns)
218 {
219 *this = from_ns(ns.count());
220 }
221
222 template <typename Rep>
223 // cppcheck-suppress noExplicitConstructor
224 TimeInterval(std::chrono::duration<Rep, std::micro> us)
225 {
226 *this = from_us(us.count());
227 }
228
229 template <typename Rep>
230 // cppcheck-suppress noExplicitConstructor
231 TimeInterval(std::chrono::duration<Rep, std::milli> ms)
232 {
233 *this = from_ms(ms.count());
234 }
235
236 template <typename Rep>
237 // cppcheck-suppress noExplicitConstructor
238 TimeInterval(std::chrono::duration<Rep, std::ratio<1>> s)
239 {
240 *this = from_s(s.count());
241 }
242
243 template <typename Rep, typename Period>
244 // cppcheck-suppress noExplicitConstructor
245 TimeInterval(std::chrono::duration<Rep, Period> d)
246 : TimeInterval{std::chrono::duration_cast<std::chrono::nanoseconds>(d)}
247 {}
248
249 operator std::chrono::nanoseconds() const
250 {
251 return std::chrono::nanoseconds(
252 (std::chrono::nanoseconds::rep)m_t.tv_sec * BILLION
253 + (std::chrono::nanoseconds::rep)m_t.tv_nsec);
254 }
255
256 template <typename Rep, typename Period>
257 operator std::chrono::duration<Rep, Period>() const
258 {
259 return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(
260 (std::chrono::nanoseconds) * this);
261 }
262# endif // C++11
263
264private:
265 template <typename T>
266 void init_float(T dt)
267 {
268# pragma GCC diagnostic push
269# pragma GCC diagnostic ignored "-Wfloat-equal"
270 zth_assert(dt == dt); // Should not be NaN
271# pragma GCC diagnostic pop
272
273 m_negative = dt < 0;
274
275 if(std::fabs(dt) > (T)(std::numeric_limits<time_t>::max() / 2 - 1))
276 m_t.tv_sec = std::numeric_limits<time_t>::max();
277 else
278 m_t.tv_sec = (time_t)std::fabs(dt);
279 // cppcheck-suppress suspiciousFloatingPointCast
280 m_t.tv_nsec = (long)(std::fmod(std::fabs(dt), (T)1.0) * (T)1e9);
281
282 if(m_t.tv_nsec >= BILLION) {
283 m_t.tv_nsec -= BILLION;
284 m_t.tv_sec++;
285 }
286 }
287
288 template <typename T>
289 constexpr14 void init_int(T dt) noexcept
290 {
291 // seconds only.
292 m_negative = dt < 0;
293 m_t.tv_sec = (time_t)(dt >= 0 ? dt : -dt);
294 m_t.tv_nsec = 0;
295 }
296
297public:
298 constexpr bool isNormal() const noexcept
299 {
300 return m_t.tv_sec >= 0 && m_t.tv_nsec >= 0 && m_t.tv_nsec < BILLION;
301 }
302
303 constexpr bool isNegative() const noexcept
304 {
305 return m_negative;
306 }
307
308 constexpr bool isPositive() const noexcept
309 {
310 return !isNegative();
311 }
312
313 constexpr bool isNull() const noexcept
314 {
315 return m_t.tv_sec == 0 && m_t.tv_nsec == 0;
316 }
317
318 constexpr bool isInfinite() const noexcept
319 {
320 return m_t.tv_sec > std::numeric_limits<time_t>::max() / 2;
321 }
322
323 constexpr bool hasPassed() const noexcept
324 {
325 return isNegative() || isNull();
326 }
327
328 constexpr struct timespec const& ts() const noexcept
329 {
330 return m_t;
331 }
332
333 constexpr14 double s() const noexcept
334 {
335 return s<double>();
336 }
337
338 template <typename T>
339 constexpr14 T s() const noexcept
340 {
341 T t = (T)m_t.tv_sec + (T)m_t.tv_nsec * (T)1e-9;
342 if(m_negative)
343 t = -t;
344 return t;
345 }
346
347 constexpr bool isAbsBiggerThan(TimeInterval const& t) const noexcept
348 {
349 return m_t.tv_sec > t.m_t.tv_sec
350 || (m_t.tv_sec == t.m_t.tv_sec && m_t.tv_nsec > t.m_t.tv_nsec);
351 }
352
353 constexpr bool isBiggerThan(TimeInterval const& t) const noexcept
354 {
355 return (!m_negative && t.m_negative)
356 || (!m_negative && !t.m_negative && this->isAbsBiggerThan(t))
357 || (m_negative && t.m_negative && t.isAbsBiggerThan(*this));
358 }
359
360 constexpr bool operator==(TimeInterval const& rhs) const noexcept
361 {
362 return m_t.tv_nsec == rhs.m_t.tv_nsec && m_t.tv_sec == rhs.m_t.tv_sec
363 && m_negative == rhs.m_negative;
364 }
365
366 constexpr bool operator>(TimeInterval const& rhs) const noexcept
367 {
368 return this->isBiggerThan(rhs);
369 }
370
371 constexpr bool operator>=(TimeInterval const& rhs) const noexcept
372 {
373 return *this == rhs || this->isBiggerThan(rhs);
374 }
375
376 constexpr bool operator<(TimeInterval const& rhs) const noexcept
377 {
378 return !(*this >= rhs);
379 }
380
381 constexpr bool operator<=(TimeInterval const& rhs) const noexcept
382 {
383 return *this == rhs || !(*this > rhs);
384 }
385
386 constexpr14 void add(TimeInterval const& t) noexcept
387 {
388 if(t.m_negative == m_negative) {
389 // Check for overflow.
390 if(isInfinite() || t.isInfinite()
391 || std::numeric_limits<time_t>::max() - m_t.tv_sec - 1 <= t.m_t.tv_sec) {
392 m_t.tv_sec = std::numeric_limits<time_t>::max(); // infinite
393 } else {
394 // Add in same sign direction.
395 m_t.tv_sec += t.m_t.tv_sec;
396 m_t.tv_nsec += t.m_t.tv_nsec;
397 if(m_t.tv_nsec > BILLION) {
398 m_t.tv_nsec -= BILLION;
399 m_t.tv_sec++;
400 }
401 }
402 } else {
403 if(t.isAbsBiggerThan(*this)) {
404 // Add in other sign direction with sign flip.
405 m_t.tv_sec = t.m_t.tv_sec - m_t.tv_sec;
406 m_t.tv_nsec = t.m_t.tv_nsec - m_t.tv_nsec;
407 m_negative = !m_negative;
408 } else {
409 // Add in other sign direction (but without sign flip).
410 m_t.tv_sec -= t.m_t.tv_sec;
411 m_t.tv_nsec -= t.m_t.tv_nsec;
412 }
413 if(m_t.tv_nsec < 0) {
414 m_t.tv_nsec += BILLION;
415 m_t.tv_sec--;
416 }
417 }
418 }
419
420 constexpr14 void sub(TimeInterval const& t) noexcept
421 {
422 add(TimeInterval(t.ts().tv_sec, t.ts().tv_nsec, !t.isNegative()));
423 }
424
425 template <typename T>
426 constexpr14 void mul(T x) noexcept
427 {
428 *this = TimeInterval(s<T>() * x);
429 }
430
432 {
433 add(rhs);
434 return *this;
435 }
436
438 {
439 TimeInterval ti(*this);
440 ti += rhs;
441 return ti;
442 }
443
445 {
446 sub(rhs);
447 return *this;
448 }
449
451 {
452 TimeInterval ti(*this);
453 ti -= rhs;
454 return ti;
455 }
456
458 {
459 return TimeInterval(ts().tv_sec, ts().tv_nsec, !isNegative());
460 }
461
462 template <typename T>
464 {
465 mul<T>(x);
466 return *this;
467 }
468
469 template <typename T>
471 {
472 return TimeInterval(s<T>() * x);
473 }
474
475 template <typename T>
477 {
478 mul<T>((T)1 / x);
479 return *this;
480 }
481
482 template <typename T>
484 {
485 return TimeInterval(s<T>() / x);
486 }
487
488 string str() const
489 {
490 string res;
491 if(isInfinite()) {
492 res = "infinity";
493 return res;
494 }
495
496 if(m_negative)
497 res = "-";
498
500 // Do a simplified print without float formatting support.
501 if(m_t.tv_sec >= 60)
502 res += format("%u s", (unsigned int)m_t.tv_sec);
503 else if(m_t.tv_sec > 0)
504 res +=
505 format("%u.%03u s", (unsigned int)m_t.tv_sec,
506 (unsigned int)(m_t.tv_nsec / 1000000L));
507 else
508 res += format("%u us", (unsigned int)m_t.tv_nsec / 1000U);
509 } else {
510 uint64_t d = (uint64_t)(m_t.tv_sec / 3600 / 24);
511 time_t rest = m_t.tv_sec - d * 3600 * 24;
512 bool doPrint = d > 0;
513 if(doPrint)
514 res += format("%" PRIu64 "d:", d);
515
516 int h = int(rest / 3600);
517 rest -= h * 3600;
518 if(doPrint) {
519 res += format("%02d:", h);
520 } else if(h > 0) {
521 res += format("%d:", h);
522 doPrint = true;
523 }
524
525 int m = int(rest / 60);
526 rest -= m * 60;
527 if(doPrint) {
528 res += format("%02d:", m);
529 } else if(m > 0) {
530 res += format("%d:", m);
531 doPrint = true;
532 }
533
534 double sec = (double)rest + (double)m_t.tv_nsec * 1e-9;
535 if(doPrint) {
536 res += format("%06.3f", sec);
537 } else {
538 res += format("%g s", sec);
539 }
540 }
541
542 return res;
543 }
544
545private:
546 struct timespec m_t;
547 bool m_negative;
548};
549
550template <>
551inline cow_string str<TimeInterval const&>(TimeInterval const& value)
552{
553 return value.str();
554}
555
556# if __cplusplus >= 201103L
564ZTH_EXPORT constexpr14 inline TimeInterval operator"" _s(unsigned long long int x) noexcept
565{
566 return TimeInterval((time_t)std::min<unsigned long long int>(
567 x, (unsigned long long int)std::numeric_limits<time_t>::max()));
568}
569
577ZTH_EXPORT constexpr14 inline TimeInterval operator"" _ms(unsigned long long int x) noexcept
578{
579 return TimeInterval::from_ms(x);
580}
581
589ZTH_EXPORT constexpr14 inline TimeInterval operator"" _us(unsigned long long int x) noexcept
590{
591 return TimeInterval::from_us(x);
592}
593
601ZTH_EXPORT inline TimeInterval operator"" _s(long double x)
602{
603 return TimeInterval(x);
604}
605# endif // C++11
606
607# if __cplusplus >= 201103L
609 using duration = std::chrono::nanoseconds;
610 using rep = duration::rep;
611 using period = duration::period;
612 using time_point = std::chrono::time_point<monotonic_clock>;
613 static constexpr bool is_steady = true;
614
615 static time_point now() noexcept
616 {
617 struct timespec ts;
618 zth::now(ts);
619 return time_point{
620 duration((rep)ts.tv_sec * TimeInterval::BILLION + (rep)ts.tv_nsec)};
621 }
622};
623# endif // C++11
624
631public:
632 // null
633 constexpr Timestamp() noexcept
634 : m_t()
635 {}
636
637 // cppcheck-suppress noExplicitConstructor
638 constexpr Timestamp(struct timespec const& ts) noexcept
639 : m_t(ts)
640 {}
641
642 constexpr14 explicit Timestamp(time_t sec, long nsec = 0) noexcept
643 : m_t()
644 {
645 m_t.tv_sec = sec;
646 m_t.tv_nsec = nsec;
647 }
648
649 // cppcheck-suppress noExplicitConstructor
651 : m_t()
652 {
653 *this = now() + ti;
654 }
655
656 static Timestamp now()
657 {
658 Timestamp t;
659 zth::now(t.m_t);
660 zth_assert(!t.isNull());
661 return t;
662 }
663
664 constexpr struct timespec const& ts() const noexcept
665 {
666 return m_t;
667 }
668 constexpr operator struct timespec const&() const noexcept
669 {
670 return ts();
671 }
672
673 constexpr bool isBefore(Timestamp const& t) const noexcept
674 {
675 return ts().tv_sec < t.ts().tv_sec
676 || (ts().tv_sec == t.ts().tv_sec && ts().tv_nsec < t.ts().tv_nsec);
677 }
678
679 constexpr bool isAfter(Timestamp const& t) const noexcept
680 {
681 return t.isBefore(*this);
682 }
683
684 bool hasPassed() const noexcept
685 {
686 return isBefore(now());
687 }
688
689 constexpr bool operator==(Timestamp const& rhs) const noexcept
690 {
691 return ts().tv_nsec == rhs.ts().tv_nsec && ts().tv_sec == rhs.ts().tv_sec;
692 }
693
694 constexpr bool operator!=(Timestamp const& rhs) const noexcept
695 {
696 return !(*this == rhs);
697 }
698 constexpr bool operator<(Timestamp const& rhs) const noexcept
699 {
700 return this->isBefore(rhs);
701 }
702 constexpr bool operator<=(Timestamp const& rhs) const noexcept
703 {
704 return *this == rhs || this->isBefore(rhs);
705 }
706 constexpr bool operator>(Timestamp const& rhs) const noexcept
707 {
708 return rhs.isBefore(*this);
709 }
710 constexpr bool operator>=(Timestamp const& rhs) const noexcept
711 {
712 return *this == rhs || rhs.isBefore(*this);
713 }
714
716 {
717 return TimeInterval(t.ts().tv_sec, t.ts().tv_nsec)
718 - TimeInterval(ts().tv_sec, ts().tv_nsec);
719 }
720
722 {
723 // Assume we are in the past. If so, interval calculations are
724 // simpler than in the general timeTo() case.
725 Timestamp t = now();
726 zth_assert(!isAfter(t));
727
728 t.m_t.tv_sec -= m_t.tv_sec;
729 t.m_t.tv_nsec -= m_t.tv_nsec;
730 if(t.m_t.tv_nsec < 0) {
731 t.m_t.tv_sec--;
732 t.m_t.tv_nsec += TimeInterval::BILLION;
733 }
734
735 return TimeInterval(t.m_t.tv_sec, t.m_t.tv_nsec);
736 }
737
738 constexpr14 void add(TimeInterval const& dt) noexcept
739 {
740 TimeInterval t(m_t.tv_sec, m_t.tv_nsec);
741 t += dt;
743 m_t = t.ts();
744 }
745
747 {
748 add(dt);
749 return *this;
750 }
751
752 constexpr14 Timestamp operator+(TimeInterval const& dt) const noexcept
753 {
754 Timestamp t(*this);
755 return t += dt;
756 }
757
759 {
760 add(-dt);
761 return *this;
762 }
763
764 constexpr14 Timestamp operator-(TimeInterval const& dt) const noexcept
765 {
766 Timestamp t(*this);
767 return t -= dt;
768 }
769
770 constexpr14 TimeInterval operator-(Timestamp const& rhs) const noexcept
771 {
772 return rhs.timeTo(*this);
773 }
774
775 static constexpr Timestamp null() noexcept
776 {
777 return Timestamp();
778 }
779
780 constexpr bool isNull() const noexcept
781 {
782 return m_t.tv_sec == 0 && m_t.tv_nsec == 0;
783 }
784
785# if __cplusplus >= 201103L
786 template <typename Duration>
787 // cppcheck-suppress noExplicitConstructor
788 Timestamp(std::chrono::time_point<monotonic_clock, Duration> tp)
789 {
790 auto d = tp.time_since_epoch();
791 auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(d).count();
792 m_t.tv_sec = (time_t)(ns / TimeInterval::BILLION);
793 m_t.tv_nsec = (long)(ns % TimeInterval::BILLION);
794 }
795
796 operator std::chrono::time_point<monotonic_clock>() const
797 {
798 return std::chrono::time_point<monotonic_clock>{monotonic_clock::duration{
800 + (monotonic_clock::rep)m_t.tv_nsec}};
801 }
802# endif // C++11
803
804private:
805 struct timespec m_t;
806};
807
808# ifdef ZTH_OS_MAC
809// Should be const, but the alias-trick does not work on OSX.
810extern Timestamp /* const */ startTime;
811# else
812extern Timestamp const startTime;
813# endif
814
815} // namespace zth
816
822EXTERN_C ZTH_EXPORT ZTH_INLINE void zth_now(struct timespec* ts)
823{
824 if(ts)
825 zth::now(*ts);
826}
827
833EXTERN_C ZTH_EXPORT ZTH_INLINE void zth_now2(uint64_t* s, uint32_t* ns)
834{
835 struct timespec ts = {};
836 zth_now(&ts);
837 if(s)
838 *s = (uint64_t)ts.tv_sec;
839 if(ns)
840 *ns = (uint32_t)ts.tv_nsec;
841}
842
843#else // !__cplusplus
844
845# include <sys/time.h>
846
847ZTH_EXPORT void zth_now(struct timespec const* ts);
848ZTH_EXPORT void zth_now2(uint64_t* s, uint32_t* ns);
849
850#endif // __cplusplus
851#endif // ZTH_TIME_H
Convenient wrapper around struct timespec that contains a time interval.
Definition time.h:82
constexpr bool operator==(TimeInterval const &rhs) const noexcept
Definition time.h:360
constexpr TimeInterval operator+(TimeInterval const &rhs) const noexcept
Definition time.h:437
constexpr TimeInterval(T dt) noexcept
Definition time.h:168
static constexpr TimeInterval from_s(T s)
Definition time.h:176
TimeInterval(double dt)
Definition time.h:151
constexpr bool operator>(TimeInterval const &rhs) const noexcept
Definition time.h:366
static constexpr TimeInterval null() noexcept
Definition time.h:96
TimeInterval(std::chrono::duration< Rep, Period > d)
Definition time.h:245
constexpr TimeInterval operator-(TimeInterval const &rhs) const noexcept
Definition time.h:450
static constexpr TimeInterval from_ms(T ms)
Definition time.h:185
constexpr bool isInfinite() const noexcept
Definition time.h:318
static constexpr TimeInterval from_ns(T ns)
Definition time.h:205
static constexpr TimeInterval infinity() noexcept
Definition time.h:91
constexpr void sub(TimeInterval const &t) noexcept
Definition time.h:420
constexpr bool hasPassed() const noexcept
Definition time.h:323
TimeInterval(std::chrono::duration< Rep, std::ratio< 1 > > s)
Definition time.h:238
constexpr TimeInterval() noexcept
Definition time.h:101
constexpr TimeInterval operator/(T x) const noexcept
Definition time.h:483
constexpr void add(TimeInterval const &t) noexcept
Definition time.h:386
constexpr void mul(T x) noexcept
Definition time.h:426
static constexpr TimeInterval from_us(T us)
Definition time.h:195
string str() const
Definition time.h:488
constexpr bool operator<=(TimeInterval const &rhs) const noexcept
Definition time.h:381
constexpr TimeInterval & operator*=(T x) noexcept
Definition time.h:463
constexpr bool isPositive() const noexcept
Definition time.h:308
constexpr TimeInterval & operator-=(TimeInterval const &rhs) noexcept
Definition time.h:444
constexpr bool operator>=(TimeInterval const &rhs) const noexcept
Definition time.h:371
TimeInterval & operator=(TimeInterval const &t) noexcept
Definition time.h:130
constexpr TimeInterval(time_t s, long ns=0, bool negative=false) noexcept
Definition time.h:108
constexpr double s() const noexcept
Definition time.h:333
TimeInterval(std::chrono::duration< Rep, std::micro > us)
Definition time.h:224
constexpr T s() const noexcept
Definition time.h:339
TimeInterval(std::chrono::duration< Rep, std::milli > ms)
Definition time.h:231
constexpr bool isNegative() const noexcept
Definition time.h:303
TimeInterval(long double dt)
Definition time.h:159
static long const BILLION
Definition time.h:89
constexpr bool isNull() const noexcept
Definition time.h:313
constexpr TimeInterval & operator+=(TimeInterval const &rhs) noexcept
Definition time.h:431
constexpr bool operator<(TimeInterval const &rhs) const noexcept
Definition time.h:376
constexpr bool isAbsBiggerThan(TimeInterval const &t) const noexcept
Definition time.h:347
constexpr TimeInterval operator-() const noexcept
Definition time.h:457
TimeInterval(float dt)
Definition time.h:143
constexpr TimeInterval(TimeInterval const &t) noexcept
Definition time.h:137
constexpr TimeInterval & operator/=(T x) noexcept
Definition time.h:476
constexpr TimeInterval operator*(T x) const noexcept
Definition time.h:470
constexpr struct timespec const & ts() const noexcept
Definition time.h:328
constexpr TimeInterval(struct timespec const &ts) noexcept
Definition time.h:125
constexpr bool isNormal() const noexcept
Definition time.h:298
operator std::chrono::nanoseconds() const
Definition time.h:249
TimeInterval(std::chrono::duration< Rep, std::nano > ns)
Definition time.h:217
constexpr bool isBiggerThan(TimeInterval const &t) const noexcept
Definition time.h:353
Convenient wrapper around struct timespec that contains an absolute timestamp.
Definition time.h:629
constexpr Timestamp(struct timespec const &ts) noexcept
Definition time.h:638
static Timestamp now()
Definition time.h:656
constexpr TimeInterval operator-(Timestamp const &rhs) const noexcept
Definition time.h:770
constexpr bool operator==(Timestamp const &rhs) const noexcept
Definition time.h:689
constexpr Timestamp operator-(TimeInterval const &dt) const noexcept
Definition time.h:764
constexpr struct timespec const & ts() const noexcept
Definition time.h:664
constexpr bool isNull() const noexcept
Definition time.h:780
constexpr Timestamp & operator-=(TimeInterval const &dt) noexcept
Definition time.h:758
Timestamp(TimeInterval const &ti)
Definition time.h:650
constexpr bool operator>=(Timestamp const &rhs) const noexcept
Definition time.h:710
constexpr bool isAfter(Timestamp const &t) const noexcept
Definition time.h:679
constexpr bool operator!=(Timestamp const &rhs) const noexcept
Definition time.h:694
constexpr bool operator<(Timestamp const &rhs) const noexcept
Definition time.h:698
bool hasPassed() const noexcept
Definition time.h:684
constexpr void add(TimeInterval const &dt) noexcept
Definition time.h:738
Timestamp(std::chrono::time_point< monotonic_clock, Duration > tp)
Definition time.h:788
static constexpr Timestamp null() noexcept
Definition time.h:775
constexpr bool operator>(Timestamp const &rhs) const noexcept
Definition time.h:706
constexpr Timestamp(time_t sec, long nsec=0) noexcept
Definition time.h:642
constexpr Timestamp() noexcept
Definition time.h:633
constexpr bool isBefore(Timestamp const &t) const noexcept
Definition time.h:673
constexpr TimeInterval timeTo(Timestamp const &t) const
Definition time.h:715
TimeInterval passed() const
Definition time.h:721
constexpr bool operator<=(Timestamp const &rhs) const noexcept
Definition time.h:702
constexpr Timestamp & operator+=(TimeInterval const &dt) noexcept
Definition time.h:746
constexpr Timestamp operator+(TimeInterval const &dt) const noexcept
Definition time.h:752
Copy-on-write string.
Definition util.h:339
void zth_now(struct timespec *ts)
Returns the current timestamp.
Definition time.h:822
void zth_now2(uint64_t *s, uint32_t *ns)
Returns the current timestamp.
Definition time.h:833
#define ZTH_CLASS_NEW_DELETE(T)
Define new/delete operators for a class, which are allocator-aware.
Definition allocator.h:160
#define constexpr14
Definition macros.h:219
#define ZTH_INLINE
Definition macros.h:139
STL namespace.
void now(struct timespec &ts)
Returns the current timestamp.
Definition time.h:59
string format(char const *fmt,...)
Format like sprintf(), but save the result in an zth::string.
Definition util.h:498
static bool const UseLimitedFormatSpecifiers
Use limited formatting specifiers.
Definition config.h:297
std::chrono::time_point< monotonic_clock > time_point
Definition time.h:612
static time_point now() noexcept
Definition time.h:615
static constexpr bool is_steady
Definition time.h:613
std::chrono::nanoseconds duration
Definition time.h:609
duration::rep rep
Definition time.h:610
duration::period period
Definition time.h:611
#define zth_assert(expr)
assert(), but better integrated in Zth.
Definition util.h:217