- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
class NTPTime {
public:
static NTPTime getInvalidTime() { NTPTime t; t.setInvalid(); return t; }
public:
NTPTime() : m_Time(0) {}
NTPTime(const uint64_t time) : m_Time(time) {}
NTPTime(const uint32_t sec, const uint32_t frac)
: m_Time(0)
{
this->set(sec, frac);
}
NTPTime(const boost::posix_time::ptime& timestamp);
public: // Assignment operators
NTPTime& operator=(const uint64_t u) { m_Time = u; return *this; }
NTPTime& operator+= (const NTPTime& Q) { m_Time += Q.m_Time; return *this; }
NTPTime& operator-= (const NTPTime& Q) { m_Time -= Q.m_Time; return *this; }
public: // Cast operators
operator uint64_t() { return this->m_Time; }
public: // comparison operators
bool operator==(const NTPTime& other) const { return (m_Time == other.m_Time); }
bool operator!=(const NTPTime& other) const { return (m_Time != other.m_Time); }
bool operator>=(const NTPTime& other) const { return (m_Time >= other.m_Time); }
bool operator>(const NTPTime& other) const { return (m_Time > other.m_Time); }
bool operator<=(const NTPTime& other) const { return (m_Time <= other.m_Time); }
bool operator<(const NTPTime& other) const { return (m_Time < other.m_Time); }
public: // arithmetic operators
NTPTime operator+(const NTPTime& other) const
{
NTPTime result = *this;
result += other;
return result;
}
NTPTime operator-(const NTPTime& other) const
{
NTPTime result = *this;
result -= other;
return result;
}
public:
uint32_t getSeconds() const { return ((uint32_t)(m_Time >> 32));}
uint32_t getFracSeconds() const { return ((uint32_t)(m_Time & 0xFFFFFFFF));}
uint32_t getMilliseconds() const { const uint64_t t = 1000*m_Time ; return (uint32_t)((t>>32)&0xFFFFFFFF);}
uint32_t getMicroseconds() const { const uint64_t t = 125*m_Time/536871; return (uint32_t)(t&0xFFFFFFFF);}
void getTime_s_us(uint32_t& sec, uint32_t& us) const { sec = getSeconds(); us = getFracSeconds()/4295;}
uint64_t getTime(void) const {return m_Time;}
public:
/// set the time in seconds and microseconds (micros: 0..1000 0000)
///This routine uses the factorization: 2^32/10^6 = 4096 + 256 - 1825/32
void setTime_s_us(const uint32_t sec, const uint32_t us) { m_Time = ((uint64_t)sec<<32) | ((us<<12)-((us*1852)>>5)+(us<<8));}
void set(const uint64_t& u) {m_Time = u;}
void set(const uint32_t sec, const uint32_t frac)
{
m_Time = sec;
m_Time = m_Time<<32;
m_Time |= frac;
}
void setInvalid() { m_Time = uint64_t(NOT_A_DATE_TIME) << 32; }
/// 2^32/10^6 = 4096 + 256 - 1825/32
void setMicroseconds(const uint32_t u) { const uint64_t t = ((uint64_t)u * 1825) >> 5; m_Time = ((uint64_t)u << 12) + ((uint64_t)u << 8) - t;}
void setMicroseconds(const uint64_t u) { const uint64_t t = (u * 1825) >> 5; m_Time = (u << 12) + (u << 8) - t;}
void setMilliseconds(const uint32_t u) { m_Time = (uint64_t)u * 536870912 / 125;}
void addMilliseconds(const uint32_t u) { NTPTime t; t.setMilliseconds(u); *this += t;}
void addMicroseconds(const uint32_t u) { NTPTime t; t.setMicroseconds(u); *this += t;}
protected:
static double round(const double v);
private:
static const double secondFractionNTPtoNanoseconds;
static const double nanosecondsToSecondFractionNTP;
static const uint32_t NOT_A_DATE_TIME;
static const uint64_t NOT_A_DATE_TIME64;
static const boost::posix_time::ptime m_epoch;
protected:
uint64_t m_Time; ///< NTP time in 1/2^32 seconds (~233 ps)
}; // NTPTime