SatCat5
datetime.h
Go to the documentation of this file.
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
29 
30 
31 #pragma once
32 
33 #include <satcat5/io_core.h>
34 #include <satcat5/polling.h>
35 #include <satcat5/ptp_time.h>
36 #include <satcat5/timeref.h>
37 
38 namespace satcat5 {
39  namespace datetime {
43  static constexpr u32 ONE_SECOND = 1000;
44  static constexpr u32 ONE_MINUTE = 60 * ONE_SECOND;
45  static constexpr u32 ONE_HOUR = 60 * ONE_MINUTE;
46  static constexpr u32 ONE_DAY = 24 * ONE_HOUR;
47  static constexpr u32 ONE_WEEK = 7 * ONE_DAY;
49 
51  static constexpr s64 TIME_ERROR = 0;
52 
57  satcat5::ptp::Time to_ptp(s64 time);
60 
64  s64 from_gps(const satcat5::datetime::GpsTime& time);
65  s64 from_ptp(const satcat5::ptp::Time& time);
66  s64 from_rtc(const satcat5::datetime::RtcTime& time);
68 
77  struct GpsTime {
78  s32 wkn;
79  u32 tow;
80 
81  GpsTime() = default;
82  GpsTime(const GpsTime& t) = default;
83  GpsTime& operator=(const GpsTime& t) = default;
84 
86  inline s64 convert() const
87  { return satcat5::datetime::from_gps(*this); }
88 
89  bool operator==(const satcat5::datetime::GpsTime& other) const;
90  bool operator<(const satcat5::datetime::GpsTime& other) const;
91  inline bool operator!=(const satcat5::datetime::GpsTime& other) const
92  {return !operator==(other);}
93 
94  inline void write_to(satcat5::io::Writeable* wr) const
95  {wr->write_u32((u32)wkn); wr->write_u32(tow);}
96  bool read_from(satcat5::io::Readable* rd);
97  };
98 
109  struct RtcTime {
110  u8 dw;
111  u8 ct;
112  u8 yr;
113  u8 mo;
114  u8 dt;
115  u8 hr;
116  u8 mn;
117  u8 sc;
118  u8 ss;
119 
120  RtcTime() = default;
121  RtcTime(const RtcTime& t) = default;
122  RtcTime& operator=(const RtcTime& t) = default;
123 
125  inline s64 convert() const
126  { return satcat5::datetime::from_rtc(*this); }
127 
129  u32 days_since_epoch() const;
131  u32 msec_since_midnight() const;
133  bool validate() const;
134 
135  bool operator==(const satcat5::datetime::RtcTime& other) const;
136  bool operator<(const satcat5::datetime::RtcTime& other) const;
137 
140  void write_to(satcat5::io::Writeable* wr) const;
141 
145 
150  void log_to(satcat5::log::LogBuffer& wr) const;
151  };
152 
154  static const datetime::RtcTime RTC_ERROR = {0, 0, 0, 0, 0, 0, 0, 0, 0};
155 
158  static const u8 RTC_MIL_BIT = 0x80;
159 
169  class Clock : protected satcat5::poll::Timer {
170  public:
172  Clock();
173 
178  inline u32 uptime_msec() const {return m_tcount;}
179 
182  u32 uptime_usec() const;
183 
186  void reset(bool full = false);
187 
190  void set(s64 gps);
191 
193  inline s64 now() const {return m_gps;}
196  { return satcat5::datetime::to_gps(m_gps); }
198  inline satcat5::ptp::Time ptp() const
199  { return satcat5::datetime::to_ptp(m_gps); }
202  { return satcat5::datetime::to_rtc(m_gps); }
203 
204  protected:
206  void timer_event() override;
207 
208  satcat5::util::TimeVal m_tref;
209  u32 m_tcount;
210  s64 m_gps;
211  };
212 
216  extern satcat5::datetime::Clock clock;
217  }
218 }
Real-time clock for tracking date/time.
Definition: datetime.h:169
void set(s64 gps)
Set current GPS time.
Definition: datetime.cc:54
satcat5::ptp::Time ptp() const
Current time as Precision Time Protocol timestamp.
Definition: datetime.h:198
satcat5::datetime::GpsTime gps() const
Current time as GPS week number and time-of-week.
Definition: datetime.h:195
void reset(bool full=false)
Reset internals after changes to SATCAT5_CLOCK.
Definition: datetime.cc:49
s64 now() const
Current time as milliseconds since GPS epoch.
Definition: datetime.h:193
u32 uptime_msec() const
Get elapsed time since startup, in milliseconds.
Definition: datetime.h:178
satcat5::datetime::RtcTime rtc() const
Current time as ISL12082 real-time clock timestamp.
Definition: datetime.h:201
Clock()
Constructor defaults to T = 0 (unknown).
Definition: datetime.cc:33
u32 uptime_usec() const
Get elapsed time since startup, in microseconds.
Definition: datetime.cc:44
void timer_event() override
Child class MUST override this method.
Definition: datetime.cc:60
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
Global helper object that services all OnDemand objects.
Definition: polling.cc:64
Timer objects are polled after a fixed delay or at a regular interval.
Definition: polling.h:213
High-precision timestamp for use with PTP / IEEE1588.
Definition: ptp_time.h:41
static const u8 RTC_MIL_BIT
Bit-flag in HR field indicating 24-HOUR clock format.
Definition: datetime.h:158
static constexpr u32 ONE_WEEK
Common time-related constants, measured in milliseconds.
Definition: datetime.h:47
static constexpr s64 TIME_ERROR
A date/time of zero indicates an error.
Definition: datetime.h:51
satcat5::ptp::Time to_ptp(s64 time)
Convert an internal timestamp into the designated format.
Definition: datetime.cc:26
static constexpr u32 ONE_MINUTE
Common time-related constants, measured in milliseconds.
Definition: datetime.h:44
static constexpr u32 ONE_DAY
Common time-related constants, measured in milliseconds.
Definition: datetime.h:46
s64 from_rtc(const satcat5::datetime::RtcTime &time)
Convert the designated format into an internal timestamp.
Definition: datetime.cc:239
satcat5::datetime::RtcTime to_rtc(s64 time)
Convert an internal timestamp into the designated format.
Definition: datetime.cc:195
static constexpr u32 ONE_SECOND
Common time-related constants, measured in milliseconds.
Definition: datetime.h:43
static constexpr u32 ONE_HOUR
Common time-related constants, measured in milliseconds.
Definition: datetime.h:45
static const datetime::RtcTime RTC_ERROR
Special datetime::RtcTime value indicating an error.
Definition: datetime.h:154
satcat5::datetime::GpsTime to_gps(s64 time)
Convert an internal timestamp into the designated format.
Definition: datetime.cc:184
s64 from_ptp(const satcat5::ptp::Time &time)
Convert the designated format into an internal timestamp.
Definition: datetime.cc:22
s64 from_gps(const satcat5::datetime::GpsTime &time)
Convert the designated format into an internal timestamp.
Definition: datetime.cc:191
I/O interface core definitions.
Core event-processing loop for SatCat5 software.
High-precision "Time" object for use with PTP / IEEE1588.
GPS week-number and time-of-week.
Definition: datetime.h:77
s64 convert() const
Convert to standard form.
Definition: datetime.h:86
s32 wkn
Week number.
Definition: datetime.h:78
u32 tow
Time of week.
Definition: datetime.h:79
Hardware RTC (e.g., Renesas ISL12082).
Definition: datetime.h:109
u8 ss
Sub-seconds (0-99)
Definition: datetime.h:118
u8 dt
Day-of-month (1-31)
Definition: datetime.h:114
u8 hr
Hour (0-23) + MIL bit.
Definition: datetime.h:115
u8 ct
Century (20 = year 20xx)
Definition: datetime.h:111
u8 sc
Seconds (0-59)
Definition: datetime.h:117
u8 mn
Minutes (0-59)
Definition: datetime.h:116
u8 dw
Day of week (0-6, 0 = Sunday)
Definition: datetime.h:110
s64 convert() const
Convert to standard form.
Definition: datetime.h:125
void write_to(satcat5::io::Writeable *wr) const
Write legacy binary format (Deprecated).
Definition: datetime.cc:275
bool read_from(satcat5::io::Readable *rd)
Read legacy binary format (Deprecated).
Definition: datetime.cc:290
u32 days_since_epoch() const
Days since 2000 Jan 1 (a Saturday).
Definition: datetime.cc:156
void log_to(satcat5::log::LogBuffer &wr) const
Format as an ISO8601 / RFC3339 timestamp.
Definition: datetime.cc:315
bool validate() const
Are current contents valid?
Definition: datetime.cc:268
u32 msec_since_midnight() const
Milliseconds since midnight (0 - 86.4M).
Definition: datetime.cc:176
Timestamp for measuring elapsed time.
Definition: timeref.h:48
TimeRef and TimeVal define the API for monotonic timers.