CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
PsyTime.h
Go to the documentation of this file.
1
21#if !defined(_PSYTIME_H_)
22#define _PSYTIME_H_
23
24//#include "MemoryManager.h"
25#include "Types.h"
26#include "sys/timeb.h"
27
28namespace cmlabs {
29
82
84#define USEC_YEAR_0_TO_1970 62167305600000000L
86#define SECS_PER_YEAR 31536000
88#define SECS_PER_MONTH 2629744
89#define SECS_PER_WEEK 604800
90#define SECS_PER_DAY 86400
91#define SECS_PER_HOUR 3600
92#define SECS_PER_MIN 60
94#define TIME_YEAR_1970 62167305600000000L
95
100#define SECOND_MS 1000
101#define MINUTE_MS SECS_PER_MIN * 1000
102#define HOUR_MS SECS_PER_HOUR * 1000
103#define DAY_MS SECS_PER_DAY * 1000
104#define WEEK_MS SECS_PER_WEEK * 1000
105#define MONTH_MS (uint64)SECS_PER_MONTH * 1000
106#define YEAR_MS (uint64)SECS_PER_YEAR * 1000
108
112#define SECOND_US (uint64)1000000
113#define MINUTE_US (uint64)SECS_PER_MIN * 1000000
114#define HOUR_US (uint64)SECS_PER_HOUR * 1000000
115#define DAY_US (uint64)SECS_PER_DAY * 1000000
116#define WEEK_US (uint64)SECS_PER_WEEK * 1000000
117#define MONTH_US (uint64)SECS_PER_MONTH * 1000000
118#define YEAR_US (uint64)SECS_PER_YEAR * 1000000
120
124#define PSYSECOND SECOND_US
125#define PSYMINUTE MINUTE_US
126#define PSYHOUR HOUR_US
127#define PSYDAY DAY_US
128#define PSYWEEK WEEK_US
129#define PSYMONTH MONTH_US
130#define PSYYEAR YEAR_US
132
134static char PsyDays[][5] = {"None", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
136static char PsyDaysFull[][10] = {"None", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
138static char PsyMonths[][5] = {"None", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
140static char PsyMonthsFull[][10] = {"None", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
141
151 uint32 usec; /* microseconds after the minute - [0,999999] */
152 uint32 msec; /* milliseconds after the minute - [0,999] */
153 uint16 sec; /* seconds after the minute - [0,59] */
154 uint16 min; /* minutes after the hour - [0,59] */
155 uint16 hour; /* hours since midnight - [0,23] */
156 uint16 day; /* day of the month - [1,31] */
157 uint16 mon; /* months since January - [1,12] */
158 uint16 year; /* years since 0000 */
159 uint16 wday; /* day of the week - [1,7] */
160 uint16 yday; /* day of the year - [1,366] */
161 bool negative; /* is the time value negative */
162 bool dst; /* daylight savings in effect */
163 int32 dstsec; /* daylight savings in minutes when in effect */
164 int32 tzsec; /* timezone in sec */
165 char zonename[64]; /* the name of the current time zone */
166};
167
187uint64 GetTimeNow();
188
194uint64 SyncToHardwareClock();
199bool SetCurrentNetSyncDif(int64 netTimeDif);
205bool SetCurrentTimeSyncData(uint64 tmc, int64 netTimeAdjust);
211bool GetCurrentTimeSyncData(uint64& tmc, int64& netTimeAdjust);
212//bool SetLocalSyncAdjustment(int64 adjust);
213//bool ChangeLocalSyncAdjustment(int64 adjust);
214
217#define TMC_MASTER 0
218#define TMC_SLAVE 1
219#define TMC_SYNC_INTERVAL 60000000
221
223static uint64 CurrentTMC = 0;
227static uint64 LastTMCSync = 0;
229static int64 NetTimeAdjust = 0;
230
234uint64 EstNextTMCWrap();
235
240int64 GetTimeAge(uint64 t);
245int32 GetTimeAgeMS(uint64 t);
255uint64 GetTimeFromString(const char* str);
262uint32 GetHTTPTime(uint64 time, char* buffer, uint32 size);
267uint64 GetTimeFromPsyDateAndTime(struct PsyDateAndTime &tad);
268
270uint32 GetTimeOffsetGMT();
271
277struct PsyDateAndTime GetDateAndTime(uint64 t, bool local = true);
279struct PsyDateAndTime GetDateAndTimeUTC(uint64 t);
280
285struct PsyDateAndTime GetTimeDifference(uint64 t1, uint64 t2);
287struct PsyDateAndTime GetTimeDifference(int64 dif);
288
297char* PrintTime(uint64 t, bool local = true, bool us = true, bool ms = true);
298char* PrintTimeOnly(uint64 t, bool local = true, bool us = true, bool ms = true);
299char* PrintTimeDif(uint64 t, bool us = true, bool ms = true);
300char* PrintDate(uint64 t, bool local = true);
301char* PrintDateSortable(uint64 t, bool local = true);
302char* PrintDateSortableDelimiter(uint64 t, const char* del, bool local = true);
303
304char* PrintTimeSortable(uint64 t, bool local);
305char* PrintTimeSortableMillisec(uint64 t, bool local);
306char* PrintTimeSortableMicrosec(uint64 t, bool local);
308
323std::string PrintTimeNowString(bool local = true, bool us = true, bool ms = true);
324std::string PrintTimeString(uint64 t, bool local = true, bool us = true, bool ms = true);
325std::string PrintTimeOnlyString(uint64 t, bool local = true, bool us = true, bool ms = true);
326std::string PrintTimeDifString(uint64 t, bool us = true, bool ms = true);
327std::string PrintDateString(uint64 t, bool local = true);
328std::string PrintDateStringSortable(uint64 t, bool local = true);
329std::string PrintDateStringSortableDelimiter(uint64 t, const char* del, bool local = true);
330
331std::string PrintTimeSortableString(uint64 t, bool local = true);
332std::string PrintTimeSortableMillisecString(uint64 t, bool local = true);
333std::string PrintTimeSortableMicrosecString(uint64 t, bool local = true);
335
340uint64 FTime2PsyTime(uint64 t);
341
343bool PsyTime_UnitTest();
344 // end of time
346
347} // namespace cmlabs
348
349#endif //_PSYTIME_H_
350
Core fixed-width scalar typedefs and the PsyType / PsyContext hierarchical identifiers.
static uint64 CurrentTMC
Current Time Mapping Constant (µs offset from monotonic clock to absolute PsyTime).
Definition PsyTime.h:223
std::string PrintTimeString(uint64 t, bool local=true, bool us=true, bool ms=true)
Definition PsyTime.cpp:676
static char PsyDaysFull[][10]
Full weekday names indexed 1..7 (Monday..Sunday); index 0 is "None".
Definition PsyTime.h:136
char * PrintDateSortable(uint64 t, bool local=true)
Date only, sortable (e.g.
Definition PsyTime.cpp:579
std::string PrintDateStringSortableDelimiter(uint64 t, const char *del, bool local=true)
Definition PsyTime.cpp:712
char * PrintTime(uint64 t, bool local=true, bool us=true, bool ms=true)
Full date + time-of-day.
Definition PsyTime.cpp:527
std::string PrintTimeSortableMillisecString(uint64 t, bool local=true)
Sortable date+time with milliseconds.
Definition PsyTime.cpp:740
char * PrintTimeSortableMicrosec(uint64 t, bool local)
Sortable date+time with microseconds.
Definition PsyTime.cpp:572
uint64 GetTimeFromPsyDateAndTime(struct PsyDateAndTime &tad)
Convert a broken-down PsyDateAndTime back into a µs timestamp.
Definition PsyTime.cpp:376
uint64 FTime2PsyTime(uint64 t)
Convert an ftime-style value (ms since Unix epoch) to a PsyTime µs timestamp.
Definition PsyTime.cpp:758
static uint8 CurrentTMCMode
Current TMC role: TMC_MASTER or TMC_SLAVE.
Definition PsyTime.h:225
char * PrintTimeDif(uint64 t, bool us=true, bool ms=true)
Format a µs duration with scale-adaptive units: "250us", "1.500ms", "2.750s", "2m 3s",...
Definition PsyTime.cpp:601
uint64 GetTimeNow()
Return the current absolute time (µs since year 0) according to the TMC.
Definition PsyTime.cpp:69
std::string PrintDateString(uint64 t, bool local=true)
Definition PsyTime.cpp:694
uint32 GetTimeOffsetGMT()
Definition PsyTime.cpp:397
static char PsyMonthsFull[][10]
Full month names indexed 1..12; index 0 is "None".
Definition PsyTime.h:140
char * PrintTimeSortableMillisec(uint64 t, bool local)
Sortable date+time with milliseconds.
Definition PsyTime.cpp:565
static char PsyDays[][5]
Abbreviated weekday names indexed 1..7 (Mon..Sun); index 0 is "None".
Definition PsyTime.h:134
char * PrintTimeOnly(uint64 t, bool local=true, bool us=true, bool ms=true)
Time-of-day only.
Definition PsyTime.cpp:539
static int64 NetTimeAdjust
Signed network time adjustment (µs) applied when slaved to a remote master.
Definition PsyTime.h:229
bool SetCurrentTimeSyncData(uint64 tmc, int64 netTimeAdjust)
Install externally computed sync data (e.g.
Definition PsyTime.cpp:140
bool PsyTime_UnitTest()
Run the PsyTime unit tests.
Definition PsyTime.cpp:764
uint32 GetHTTPTime(uint64 time, char *buffer, uint32 size)
Format a timestamp as an HTTP-date (RFC 7231) string, e.g.
Definition PsyTime.cpp:518
std::string PrintTimeSortableString(uint64 t, bool local=true)
Sortable date+time, second resolution.
Definition PsyTime.cpp:731
uint64 SyncToHardwareClock()
Recompute the Time Mapping Constant by anchoring the monotonic clock to the wall clock.
Definition PsyTime.cpp:168
std::string PrintTimeNowString(bool local=true, bool us=true, bool ms=true)
Format GetTimeNow().
Definition PsyTime.cpp:672
std::string PrintTimeDifString(uint64 t, bool us=true, bool ms=true)
Definition PsyTime.cpp:722
char * PrintDateSortableDelimiter(uint64 t, const char *del, bool local=true)
Sortable date with custom field delimiter.
Definition PsyTime.cpp:586
bool SetCurrentNetSyncDif(int64 netTimeDif)
Set the network time difference for a slave node so its GetTimeNow() matches the master.
Definition PsyTime.cpp:135
uint64 GetTimeFromString(const char *str)
Parse a textual date/time into a PsyTime timestamp.
Definition PsyTime.cpp:438
std::string PrintTimeOnlyString(uint64 t, bool local=true, bool us=true, bool ms=true)
Definition PsyTime.cpp:685
int32 GetTimeAgeMS(uint64 t)
Age of a timestamp relative to now, in milliseconds.
Definition PsyTime.cpp:35
#define TMC_MASTER
This node is the time master; others sync to it.
Definition PsyTime.h:217
struct PsyDateAndTime GetTimeDifference(uint64 t1, uint64 t2)
Express the difference t1 - t2 as calendar fields (negative flag set when t2 > t1).
Definition PsyTime.cpp:393
int64 GetTimeAge(uint64 t)
Age of a timestamp relative to now.
Definition PsyTime.cpp:25
static char PsyMonths[][5]
Abbreviated month names indexed 1..12 (Jan..Dec); index 0 is "None".
Definition PsyTime.h:138
struct PsyDateAndTime GetDateAndTimeUTC(uint64 t)
Break a timestamp into calendar fields in UTC.
Definition PsyTime.cpp:312
static uint64 LastTMCSync
PsyTime timestamp of the last hardware-clock sync.
Definition PsyTime.h:227
std::string PrintTimeSortableMicrosecString(uint64 t, bool local=true)
Sortable date+time with microseconds.
Definition PsyTime.cpp:749
struct PsyDateAndTime GetDateAndTime(uint64 t, bool local=true)
Break a timestamp into calendar fields.
Definition PsyTime.cpp:324
std::string PrintDateStringSortable(uint64 t, bool local=true)
Definition PsyTime.cpp:703
char * PrintDate(uint64 t, bool local=true)
Date only, human-readable.
Definition PsyTime.cpp:551
bool GetCurrentTimeSyncData(uint64 &tmc, int64 &netTimeAdjust)
Read the current sync data.
Definition PsyTime.cpp:147
char * PrintTimeSortable(uint64 t, bool local)
Sortable date+time, second resolution.
Definition PsyTime.cpp:558
uint64 EstNextTMCWrap()
Estimate when the underlying hardware counter will next wrap around.
Definition PsyTime.cpp:289
A PsyTime timestamp or time difference broken down into calendar fields.
Definition PsyTime.h:150