forked from CiscoDevNet/n3550-timestamp-decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pstime.hpp
43 lines (35 loc) · 1.08 KB
/
pstime.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#pragma once
struct pstime_t
{
time_t sec;
uint64_t psec;
unsigned precision;
pstime_t(time_t s, uint64_t ps, unsigned prec = 12)
: sec(s)
, psec(ps)
, precision(prec)
{}
operator bool() const { return sec || psec; }
explicit operator double() const { return sec + (psec / 1e12); }
pstime_t operator-(const pstime_t& rhs) const
{
unsigned p = precision < rhs.precision ? precision : rhs.precision;
if (psec < rhs.psec)
return pstime_t(sec - rhs.sec - 1, 1000000000000ULL + psec - rhs.psec, p);
else
return pstime_t(sec - rhs.sec, psec - rhs.psec, p);
}
bool operator<(const pstime_t& rhs) const
{
return sec < rhs.sec || (sec == rhs.sec && psec < rhs.psec);
}
bool operator>(const pstime_t& rhs) const
{
return sec > rhs.sec || (sec == rhs.sec && psec > rhs.psec);
}
int64_t ns() const { return (sec * 1000000000LL) + (psec / 1000); }
};
static inline pstime_t ns_to_pstime(uint64_t ns)
{
return pstime_t(ns / 1000000000, (ns % 1000000000) * 1000, 9);
}