11 #include <hilti/rt/extension-points.h> 12 #include <hilti/rt/types/interval.h> 33 explicit Time(
const hilti::rt::integer::safe<uint64_t>& nsecs,
NanosecondTag ) : _nsecs(nsecs) {}
43 auto x = secs * 1
'000'000
'000; 45 using limits = std::numeric_limits<uint64_t>; 46 if ( x < static_cast<double>(limits::min()) || static_cast<double>(limits::max()) < x ) 47 throw OutOfRange("value cannot be represented as a time"); 49 return integer::safe<uint64_t>(x); 52 Time(const Time&) = default; 53 Time(Time&&) noexcept = default; 56 Time& operator=(const Time&) = default; 57 Time& operator=(Time&&) noexcept = default; 60 double seconds() const { return static_cast<double>(_nsecs.Ref()) / 1e9; } 63 uint64_t nanoseconds() const { return _nsecs; } 65 bool operator==(const Time& other) const { return _nsecs == other._nsecs; } 66 bool operator!=(const Time& other) const { return _nsecs != other._nsecs; } 67 bool operator<(const Time& other) const { return _nsecs < other._nsecs; } 68 bool operator<=(const Time& other) const { return _nsecs <= other._nsecs; } 69 bool operator>(const Time& other) const { return _nsecs > other._nsecs; } 70 bool operator>=(const Time& other) const { return _nsecs >= other._nsecs; } 72 Time operator+(const Interval& other) const { 73 if ( other.nanoseconds() < 0 && (integer::safe<int64_t>(_nsecs) < (-other.nanoseconds())) ) 74 throw RuntimeError(fmt("operation yielded negative time %d %d", _nsecs, other.nanoseconds())); 76 return Time(_nsecs + other.nanoseconds(), NanosecondTag{}); 79 Time operator-(const Interval& other) const { 80 if ( _nsecs < other.nanoseconds() ) 81 throw RuntimeError("operation yielded negative time"); 83 return Time(_nsecs - other.nanoseconds(), NanosecondTag()); 86 Interval operator-(const Time& other) const { 87 return Interval(integer::safe<int64_t>(_nsecs) - integer::safe<int64_t>(other._nsecs), 88 Interval::NanosecondTag()); 92 operator std::string() const; 95 hilti::rt::integer::safe<uint64_t> _nsecs = 0; ///< Nanoseconds since epoch. 99 extern Time current_time(); 100 extern Time mktime(uint64_t y, uint64_t m, uint64_t d, uint64_t H, uint64_t M, uint64_t S); 103 namespace detail::adl { 104 inline std::string to_string(const Time& x, adl::tag /*unused*/) { return x; } 105 } // namespace detail::adl 107 inline std::ostream& operator<<(std::ostream& out, const Time& x) { 112 } // namespace hilti::rt Time(const hilti::rt::integer::safe< uint64_t > &nsecs, NanosecondTag)
Definition: time.h:33
Time(double secs, SecondTag)
Definition: time.h:41