SatCat5
ptp_time.h
Go to the documentation of this file.
1 // Copyright 2022-2024 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
13 
14 #pragma once
15 
16 #include <satcat5/types.h>
17 #include <satcat5/utils.h>
18 
19 namespace satcat5 {
20  namespace ptp {
23  constexpr s64 NSEC_PER_SEC = 1000000000LL;
24  constexpr s64 NSEC_PER_MSEC = 1000000LL;
25  constexpr s64 NSEC_PER_USEC = 1000LL;
26  constexpr s64 USEC_PER_SEC = 1000000LL;
27  constexpr s64 MSEC_PER_SEC = 1000LL;
28  constexpr s64 SUBNS_PER_NSEC = 65536LL;
33 
41  class Time {
42  public:
44  constexpr Time()
45  : m_secs(0), m_subns(0) {}
46 
49  constexpr explicit Time(s64 subnanoseconds)
50  : m_secs (satcat5::util::divide(subnanoseconds, SUBNS_PER_SEC))
51  , m_subns(satcat5::util::modulo(subnanoseconds, SUBNS_PER_SEC)) {}
52 
54  constexpr Time(const Time& other)
55  : m_secs(other.m_secs), m_subns(other.m_subns) {}
56 
59  Time(u64 seconds, u32 nanoseconds, u16 subnanoseconds = 0);
60 
62  inline s64 field_secs() const
63  {return m_secs;}
66  inline u32 field_nsec() const
67  {return (u32)satcat5::util::div_floor(m_subns, SUBNS_PER_NSEC);}
70  inline u64 field_subns() const
71  {return (u64)m_subns;}
72 
75  inline s64 round_secs() const
76  {return (*this + Time(SUBNS_PER_NSEC / 2)).field_secs();}
78  inline u32 round_nsec() const
79  {return (*this + Time(SUBNS_PER_NSEC / 2)).field_nsec();}
80 
85  s64 delta_subns() const;
86  s64 delta_nsec() const;
87  s64 delta_usec() const;
88  s64 delta_msec() const;
90 
95 
100  void write_to(satcat5::io::Writeable* dst) const;
101 
103  void log_to(satcat5::log::LogBuffer& wr) const;
104 
108  inline u64 correction() const
109  {return (u64)satcat5::util::modulo(m_subns, SUBNS_PER_NSEC);}
110 
112  s64 to_datetime() const;
113 
116  satcat5::ptp::Time abs() const;
117  void operator+=(const satcat5::ptp::Time& other);
118  void operator-=(const satcat5::ptp::Time& other);
120  satcat5::ptp::Time operator+(const satcat5::ptp::Time& other) const;
121  satcat5::ptp::Time operator-(const satcat5::ptp::Time& other) const;
124 
128  void operator*=(unsigned scale);
129  void operator/=(unsigned scale);
130  satcat5::ptp::Time operator*(unsigned scale) const;
131  satcat5::ptp::Time operator/(unsigned scale) const;
133 
136  bool operator==(const satcat5::ptp::Time& other) const;
137  bool operator<(const satcat5::ptp::Time& other) const;
138  bool operator>(const satcat5::ptp::Time& other) const;
139  inline bool operator!=(const satcat5::ptp::Time& other) const {return !operator==(other);}
140  inline bool operator<=(const satcat5::ptp::Time& other) const {return !operator>(other);}
141  inline bool operator>=(const satcat5::ptp::Time& other) const {return !operator<(other);}
143 
144  protected:
145  void normalize(); // Reduce to canonical form
146  s64 m_secs; // Seconds since epoch (may be negative)
147  s64 m_subns; // Subnanoseconds, range [0 .. SUBNS_PER_SEC)
148  };
149 
151  satcat5::ptp::Time from_datetime(s64 gps_msec);
152 
156  constexpr satcat5::ptp::Time ONE_NANOSECOND(SUBNS_PER_NSEC);
157  constexpr satcat5::ptp::Time ONE_MICROSECOND(SUBNS_PER_USEC);
158  constexpr satcat5::ptp::Time ONE_MILLISECOND(SUBNS_PER_MSEC);
159  constexpr satcat5::ptp::Time ONE_SECOND(SUBNS_PER_SEC);
160  constexpr satcat5::ptp::Time ONE_MINUTE(SUBNS_PER_SEC * 60);
161  constexpr satcat5::ptp::Time ONE_HOUR(SUBNS_PER_SEC * 3600);
162  constexpr satcat5::ptp::Time ONE_DAY(SUBNS_PER_SEC * 3600 * 24);
164  }
165 }
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Internal buffer used by the Log class.
Definition: log.h:134
High-precision timestamp for use with PTP / IEEE1588.
Definition: ptp_time.h:41
s64 delta_subns() const
Convert time-differences to the designated unit.
Definition: ptp_time.cc:55
satcat5::ptp::Time operator=(const satcat5::ptp::Time &other)
Standard arithmetic operations.
Definition: ptp_time.cc:171
bool operator!=(const satcat5::ptp::Time &other) const
Standard comparison operators.
Definition: ptp_time.h:139
constexpr Time(const Time &other)
Copy constructor.
Definition: ptp_time.h:54
bool operator<=(const satcat5::ptp::Time &other) const
Standard comparison operators.
Definition: ptp_time.h:140
s64 delta_nsec() const
Convert time-differences to the designated unit.
Definition: ptp_time.cc:57
void operator*=(unsigned scale)
Scalar multiply and divide are used for weighted averaging.
Definition: ptp_time.cc:157
bool read_from(satcat5::io::Readable *src)
Read the standard 10-byte timestamp from a PTP message (e.g., originTimestamp: u48 seconds + u32 nano...
Definition: ptp_time.cc:64
u32 field_nsec() const
Read the "nanoseconds" field, rounding down.
Definition: ptp_time.h:66
s64 field_secs() const
Read the "seconds" field without intermediate rounding.
Definition: ptp_time.h:62
satcat5::ptp::Time operator+(const satcat5::ptp::Time &other) const
Standard arithmetic operations.
Definition: ptp_time.cc:177
bool operator>=(const satcat5::ptp::Time &other) const
Standard comparison operators.
Definition: ptp_time.h:141
satcat5::ptp::Time abs() const
Standard arithmetic operations.
Definition: ptp_time.cc:112
void log_to(satcat5::log::LogBuffer &wr) const
User-readable format for logging.
Definition: ptp_time.cc:83
u64 field_subns() const
Read the raw "subnanoseconds" field.
Definition: ptp_time.h:70
bool operator>(const satcat5::ptp::Time &other) const
Standard comparison operators.
Definition: ptp_time.cc:139
u32 round_nsec() const
Return "nanoseconds" field after rounding to the nearest nanosecond.
Definition: ptp_time.h:78
satcat5::ptp::Time operator/(unsigned scale) const
Scalar multiply and divide are used for weighted averaging.
Definition: ptp_time.cc:201
void operator/=(unsigned scale)
Scalar multiply and divide are used for weighted averaging.
Definition: ptp_time.cc:164
satcat5::ptp::Time operator-() const
Standard arithmetic operations.
Definition: ptp_time.cc:189
s64 delta_usec() const
Convert time-differences to the designated unit.
Definition: ptp_time.cc:59
constexpr Time(s64 subnanoseconds)
Single argument constructor is scaled in subnanoseconds.
Definition: ptp_time.h:49
s64 round_secs() const
Return "seconds" field after rounding to the nearest nanosecond.
Definition: ptp_time.h:75
u64 correction() const
Get the correctionField value in subnanoseconds.
Definition: ptp_time.h:108
s64 to_datetime() const
Convert to SatCat5 date/time.
Definition: ptp_time.cc:96
void operator+=(const satcat5::ptp::Time &other)
Standard arithmetic operations.
Definition: ptp_time.cc:145
void write_to(satcat5::io::Writeable *dst) const
Read or write the 10-byte timestamp from a PTP message.
Definition: ptp_time.cc:77
satcat5::ptp::Time operator*(unsigned scale) const
Scalar multiply and divide are used for weighted averaging.
Definition: ptp_time.cc:195
constexpr Time()
Default constructor.
Definition: ptp_time.h:44
void operator-=(const satcat5::ptp::Time &other)
Standard arithmetic operations.
Definition: ptp_time.cc:151
s64 delta_msec() const
Convert time-differences to the designated unit.
Definition: ptp_time.cc:61
bool operator==(const satcat5::ptp::Time &other) const
Standard comparison operators.
Definition: ptp_time.cc:129
bool operator<(const satcat5::ptp::Time &other) const
Standard comparison operators.
Definition: ptp_time.cc:133
constexpr satcat5::ptp::Time ONE_MILLISECOND(SUBNS_PER_MSEC)
Common time-related constants.
constexpr s64 SUBNS_PER_MSEC
Define commonly used scaling factors.
Definition: ptp_time.h:30
constexpr s64 SUBNS_PER_SEC
Define commonly used scaling factors.
Definition: ptp_time.h:31
constexpr s64 USEC_PER_SEC
Define commonly used scaling factors.
Definition: ptp_time.h:26
constexpr satcat5::ptp::Time ONE_HOUR(SUBNS_PER_SEC *3600)
Common time-related constants.
constexpr satcat5::ptp::Time ONE_SECOND(SUBNS_PER_SEC)
Common time-related constants.
constexpr s64 NSEC_PER_MSEC
Define commonly used scaling factors.
Definition: ptp_time.h:24
constexpr s64 NSEC_PER_SEC
Define commonly used scaling factors.
Definition: ptp_time.h:23
constexpr satcat5::ptp::Time ONE_MINUTE(SUBNS_PER_SEC *60)
Common time-related constants.
constexpr s64 SUBNS_PER_NSEC
Define commonly used scaling factors.
Definition: ptp_time.h:28
constexpr satcat5::ptp::Time ONE_DAY(SUBNS_PER_SEC *3600 *24)
Common time-related constants.
constexpr s64 NSEC_PER_USEC
Define commonly used scaling factors.
Definition: ptp_time.h:25
constexpr satcat5::ptp::Time TIME_ZERO(0LL)
Common time-related constants.
constexpr satcat5::ptp::Time ONE_NANOSECOND(SUBNS_PER_NSEC)
Common time-related constants.
constexpr s64 MSEC_PER_SEC
Define commonly used scaling factors.
Definition: ptp_time.h:27
constexpr s64 SUBNS_PER_USEC
Define commonly used scaling factors.
Definition: ptp_time.h:29
constexpr satcat5::ptp::Time ONE_MICROSECOND(SUBNS_PER_USEC)
Common time-related constants.
Basic type aliases and prototypes used throughout SatCat5.
Miscellaneous mathematical utility functions.