CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
Functions dealing with time

The CMSDK time layer: µs-resolution 64-bit timestamps, one shared timeline for every thread, process and machine in a Psyclone system. More...

Detailed Description

The CMSDK time layer: µs-resolution 64-bit timestamps, one shared timeline for every thread, process and machine in a Psyclone system.

Why its own time system
Timestamps are stamped into every DataMessage, stored in shared-memory segments and sent over the network, so they must be a fixed-width wire type that means the same thing everywhere: an unsigned 64-bit count of microseconds since year 0000 (proleptic Gregorian). A uint64 of µs does not wrap for ~584,000 years, comparisons and arithmetic are plain integer ops, and unix_us = psytime - USEC_YEAR_0_TO_1970 converts to/from the Unix epoch.
Why not std::chrono or raw OS calls
std::chrono clocks have implementation-defined epochs and representations, so their values cannot be written into a shared-memory struct or a network packet and read by another binary; raw OS clocks differ per platform (QueryPerformanceCounter/GetSystemTime on Windows, clock_gettime/gettimeofday on POSIX) and wall clocks can step backwards under NTP. PsyTime instead reads the machine's monotonic clock and maps it onto the absolute timescale with the Time Mapping Constant (TMC): GetTimeNow() = monotonic + CurrentTMC (+ NetTimeAdjust when this node is a TMC_SLAVE synced to a remote master) — giving timestamps that are both absolute (calendar-meaningful) and monotonic between re-syncs, and that agree across all nodes of a distributed system.
Measuring an interval and formatting timestamps
uint64 start = GetTimeNow(); // µs since year 0
DoSomeWork();
int64 elapsed = GetTimeAge(start); // now - start, in µs
printf("took %s\n", PrintTimeDifString(elapsed).c_str()); // "1.500ms", "2m 3s", ...
uint64 deadline = start + 5 * PSYSECOND; // duration constants in µs
if (GetTimeNow() > deadline) { } // plain integer comparison
std::string stamp = PrintTimeString(start); // "21/07/2026 15:30:02.123.456"
std::string sortable = PrintTimeSortableMillisecString(start); // "20260721-153002.123"
std::string logdate = PrintDateStringSortableDelimiter(start, "-"); // "2026-07-21"
uint64 parsed = GetTimeFromString("Sun, 06 Nov 1994 08:49:37 GMT"); // RFC dates too
std::string PrintTimeString(uint64 t, bool local=true, bool us=true, bool ms=true)
Definition PsyTime.cpp:676
std::string PrintDateStringSortableDelimiter(uint64 t, const char *del, bool local=true)
Definition PsyTime.cpp:712
std::string PrintTimeSortableMillisecString(uint64 t, bool local=true)
Sortable date+time with milliseconds.
Definition PsyTime.cpp:740
uint64 GetTimeNow()
Return the current absolute time (µs since year 0) according to the TMC.
Definition PsyTime.cpp:69
#define PSYSECOND
Definition PsyTime.h:124
std::string PrintTimeDifString(uint64 t, bool us=true, bool ms=true)
Definition PsyTime.cpp:722
uint64 GetTimeFromString(const char *str)
Parse a textual date/time into a PsyTime timestamp.
Definition PsyTime.cpp:438
int64 GetTimeAge(uint64 t)
Age of a timestamp relative to now.
Definition PsyTime.cpp:25
For calendar fields use GetDateAndTime() / GetTimeFromPsyDateAndTime(); see PsyDateAndTime for the 1-based day/month conventions.
Note
Prefer the std::string Print*String() formatters; the char* variants return a new char[] buffer the caller must delete [].
Warning
Durations are unsigned in most APIs: subtracting a later timestamp from an earlier one underflows. Use GetTimeAge()/GetTimeDifference() for signed differences. See the file documentation in PsyTime.h for TMC synchronisation details and per-translation-unit state caveats.

Classes

struct  cmlabs::PsyDateAndTime
 A PsyTime timestamp or time difference broken down into calendar fields. More...

Macros

#define USEC_YEAR_0_TO_1970   62167305600000000L
 Microseconds between year 0000 and the Unix epoch (1970-01-01).
#define SECS_PER_YEAR   31536000
 Seconds in a (non-leap) 365-day year.
#define SECS_PER_MONTH   2629744
 Average seconds per Gregorian month (365.2425/12 days).
#define SECS_PER_WEEK   604800
 Seconds per week.
#define SECS_PER_DAY   86400
 Seconds per day.
#define SECS_PER_HOUR   3600
 Seconds per hour.
#define SECS_PER_MIN   60
 Seconds per minute.
#define TIME_YEAR_1970   62167305600000000L
 Same value as USEC_YEAR_0_TO_1970: the PsyTime timestamp of 1970-01-01 00:00:00 UTC.

Functions

uint64 cmlabs::GetTimeNow ()
 Return the current absolute time (µs since year 0) according to the TMC.
uint64 cmlabs::SyncToHardwareClock ()
 Recompute the Time Mapping Constant by anchoring the monotonic clock to the wall clock.
bool cmlabs::SetCurrentNetSyncDif (int64 netTimeDif)
 Set the network time difference for a slave node so its GetTimeNow() matches the master.
bool cmlabs::SetCurrentTimeSyncData (uint64 tmc, int64 netTimeAdjust)
 Install externally computed sync data (e.g.
bool cmlabs::GetCurrentTimeSyncData (uint64 &tmc, int64 &netTimeAdjust)
 Read the current sync data.
uint64 cmlabs::EstNextTMCWrap ()
 Estimate when the underlying hardware counter will next wrap around.
int64 cmlabs::GetTimeAge (uint64 t)
 Age of a timestamp relative to now.
int32 cmlabs::GetTimeAgeMS (uint64 t)
 Age of a timestamp relative to now, in milliseconds.
uint64 cmlabs::GetTimeFromString (const char *str)
 Parse a textual date/time into a PsyTime timestamp.
uint32 cmlabs::GetHTTPTime (uint64 time, char *buffer, uint32 size)
 Format a timestamp as an HTTP-date (RFC 7231) string, e.g.
uint64 cmlabs::GetTimeFromPsyDateAndTime (struct PsyDateAndTime &tad)
 Convert a broken-down PsyDateAndTime back into a µs timestamp.
uint32 cmlabs::GetTimeOffsetGMT ()
struct PsyDateAndTime cmlabs::GetDateAndTime (uint64 t, bool local=true)
 Break a timestamp into calendar fields.
struct PsyDateAndTime cmlabs::GetDateAndTimeUTC (uint64 t)
 Break a timestamp into calendar fields in UTC.
struct PsyDateAndTime cmlabs::GetTimeDifference (uint64 t1, uint64 t2)
 Express the difference t1 - t2 as calendar fields (negative flag set when t2 > t1).
struct PsyDateAndTime cmlabs::GetTimeDifference (int64 dif)
 Express a signed µs difference as calendar fields.
uint64 cmlabs::FTime2PsyTime (uint64 t)
 Convert an ftime-style value (ms since Unix epoch) to a PsyTime µs timestamp.
bool cmlabs::PsyTime_UnitTest ()
 Run the PsyTime unit tests.

Variables

static char cmlabs::PsyDays [][5] = {"None", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
 Abbreviated weekday names indexed 1..7 (Mon..Sun); index 0 is "None".
static char cmlabs::PsyDaysFull [][10] = {"None", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
 Full weekday names indexed 1..7 (Monday..Sunday); index 0 is "None".
static char cmlabs::PsyMonths [][5] = {"None", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
 Abbreviated month names indexed 1..12 (Jan..Dec); index 0 is "None".
static char cmlabs::PsyMonthsFull [][10] = {"None", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
 Full month names indexed 1..12; index 0 is "None".
static uint64 cmlabs::CurrentTMC = 0
 Current Time Mapping Constant (µs offset from monotonic clock to absolute PsyTime).
static uint8 cmlabs::CurrentTMCMode = TMC_MASTER
 Current TMC role: TMC_MASTER or TMC_SLAVE.
static uint64 cmlabs::LastTMCSync = 0
 PsyTime timestamp of the last hardware-clock sync.
static int64 cmlabs::NetTimeAdjust = 0
 Signed network time adjustment (µs) applied when slaved to a remote master.

char* time formatters

Format timestamps into newly allocated char buffers.

Warning
Ownership: each of these allocates a fresh 1024-byte buffer with new char[] and returns it — the CALLER must delete [] the result or it leaks. Prefer the std::string variants below, which have no such footgun. Common parameters: t timestamp in µs; local = local time vs UTC; us / ms = include microseconds / milliseconds in the output.
char * cmlabs::PrintTime (uint64 t, bool local=true, bool us=true, bool ms=true)
 Full date + time-of-day.
char * cmlabs::PrintTimeOnly (uint64 t, bool local=true, bool us=true, bool ms=true)
 Time-of-day only.
char * cmlabs::PrintTimeDif (uint64 t, bool us=true, bool ms=true)
 Format a µs duration with scale-adaptive units: "250us", "1.500ms", "2.750s", "2m 3s", "01:02:03", "3 days 01:02:03".
char * cmlabs::PrintDate (uint64 t, bool local=true)
 Date only, human-readable.
char * cmlabs::PrintDateSortable (uint64 t, bool local=true)
 Date only, sortable (e.g.
char * cmlabs::PrintDateSortableDelimiter (uint64 t, const char *del, bool local=true)
 Sortable date with custom field delimiter.
char * cmlabs::PrintTimeSortable (uint64 t, bool local)
 Sortable date+time, second resolution.
char * cmlabs::PrintTimeSortableMillisec (uint64 t, bool local)
 Sortable date+time with milliseconds.
char * cmlabs::PrintTimeSortableMicrosec (uint64 t, bool local)
 Sortable date+time with microseconds.

std::string time formatters

Thread-safe, leak-free equivalents of the char* formatters above (they wrap the char* variants and free the buffer); same parameter conventions (t µs timestamp, local, us, ms).

Preferred in application code.

uint64 t = GetTimeNow();
std::string full = PrintTimeString(t); // "21/07/2026 15:30:02.123.456"
std::string clock = PrintTimeOnlyString(t, true, false, true); // "15:30:02.123"
std::string sortable = PrintTimeSortableMillisecString(t); // "20260721-153002.123"
std::string logname = PrintDateStringSortableDelimiter(t, "-"); // "2026-07-21"
std::string age = PrintTimeDifString(GetTimeAge(msgTime)); // e.g. "2m 3s"
std::string PrintTimeOnlyString(uint64 t, bool local=true, bool us=true, bool ms=true)
Definition PsyTime.cpp:685
std::string cmlabs::PrintTimeNowString (bool local=true, bool us=true, bool ms=true)
 Format GetTimeNow().
std::string cmlabs::PrintTimeString (uint64 t, bool local=true, bool us=true, bool ms=true)
std::string cmlabs::PrintTimeOnlyString (uint64 t, bool local=true, bool us=true, bool ms=true)
std::string cmlabs::PrintTimeDifString (uint64 t, bool us=true, bool ms=true)
std::string cmlabs::PrintDateString (uint64 t, bool local=true)
std::string cmlabs::PrintDateStringSortable (uint64 t, bool local=true)
std::string cmlabs::PrintDateStringSortableDelimiter (uint64 t, const char *del, bool local=true)
std::string cmlabs::PrintTimeSortableString (uint64 t, bool local=true)
 Sortable date+time, second resolution.
std::string cmlabs::PrintTimeSortableMillisecString (uint64 t, bool local=true)
 Sortable date+time with milliseconds.
std::string cmlabs::PrintTimeSortableMicrosecString (uint64 t, bool local=true)
 Sortable date+time with microseconds.

Millisecond duration constants

Common durations expressed in milliseconds.

Warning
MINUTE_MS..WEEK_MS expand as SECS_PER_X * 1000 without parentheses — beware when used inside larger expressions.
#define SECOND_MS   1000
#define MINUTE_MS   SECS_PER_MIN * 1000
#define HOUR_MS   SECS_PER_HOUR * 1000
#define DAY_MS   SECS_PER_DAY * 1000
#define WEEK_MS   SECS_PER_WEEK * 1000
#define MONTH_MS   (uint64)SECS_PER_MONTH * 1000
#define YEAR_MS   (uint64)SECS_PER_YEAR * 1000

Microsecond duration constants

Common durations expressed in microseconds — the native PsyTime unit.

#define SECOND_US   (uint64)1000000
#define MINUTE_US   (uint64)SECS_PER_MIN * 1000000
#define HOUR_US   (uint64)SECS_PER_HOUR * 1000000
#define DAY_US   (uint64)SECS_PER_DAY * 1000000
#define WEEK_US   (uint64)SECS_PER_WEEK * 1000000
#define MONTH_US   (uint64)SECS_PER_MONTH * 1000000
#define YEAR_US   (uint64)SECS_PER_YEAR * 1000000

PSY* duration aliases

Preferred spelling of the µs duration constants in application code, e.g.

5*PSYSECOND.

#define PSYSECOND   SECOND_US
#define PSYMINUTE   MINUTE_US
#define PSYHOUR   HOUR_US
#define PSYDAY   DAY_US
#define PSYWEEK   WEEK_US
#define PSYMONTH   MONTH_US
#define PSYYEAR   YEAR_US

TMC roles and sync interval

#define TMC_MASTER   0
 This node is the time master; others sync to it.
#define TMC_SLAVE   1
 This node slaves its clock to a remote master.
#define TMC_SYNC_INTERVAL   60000000
 Re-sync interval in µs (1 minute).

Macro Definition Documentation

◆ DAY_MS

#define DAY_MS   SECS_PER_DAY * 1000

Definition at line 103 of file PsyTime.h.

◆ DAY_US

#define DAY_US   (uint64)SECS_PER_DAY * 1000000

Definition at line 115 of file PsyTime.h.

◆ HOUR_MS

#define HOUR_MS   SECS_PER_HOUR * 1000

Definition at line 102 of file PsyTime.h.

◆ HOUR_US

#define HOUR_US   (uint64)SECS_PER_HOUR * 1000000

Definition at line 114 of file PsyTime.h.

◆ MINUTE_MS

#define MINUTE_MS   SECS_PER_MIN * 1000

Definition at line 101 of file PsyTime.h.

◆ MINUTE_US

#define MINUTE_US   (uint64)SECS_PER_MIN * 1000000

Definition at line 113 of file PsyTime.h.

◆ MONTH_MS

#define MONTH_MS   (uint64)SECS_PER_MONTH * 1000

Definition at line 105 of file PsyTime.h.

◆ MONTH_US

#define MONTH_US   (uint64)SECS_PER_MONTH * 1000000

Definition at line 117 of file PsyTime.h.

◆ PSYDAY

#define PSYDAY   DAY_US

Definition at line 127 of file PsyTime.h.

◆ PSYHOUR

#define PSYHOUR   HOUR_US

Definition at line 126 of file PsyTime.h.

◆ PSYMINUTE

#define PSYMINUTE   MINUTE_US

Definition at line 125 of file PsyTime.h.

Referenced by cmlabs::MessageIndex::MessageIndex().

◆ PSYMONTH

#define PSYMONTH   MONTH_US

Definition at line 129 of file PsyTime.h.

◆ PSYSECOND

#define PSYSECOND   SECOND_US

Definition at line 124 of file PsyTime.h.

◆ PSYWEEK

#define PSYWEEK   WEEK_US

Definition at line 128 of file PsyTime.h.

◆ PSYYEAR

#define PSYYEAR   YEAR_US

Definition at line 130 of file PsyTime.h.

◆ SECOND_MS

#define SECOND_MS   1000

Definition at line 100 of file PsyTime.h.

◆ SECOND_US

#define SECOND_US   (uint64)1000000

Definition at line 112 of file PsyTime.h.

◆ SECS_PER_DAY

#define SECS_PER_DAY   86400

Seconds per day.

Definition at line 90 of file PsyTime.h.

Referenced by cmlabs::GetTimeDifference(), and cmlabs::PrintTimeDif().

◆ SECS_PER_HOUR

#define SECS_PER_HOUR   3600

Seconds per hour.

Definition at line 91 of file PsyTime.h.

Referenced by cmlabs::GetDateAndTime(), cmlabs::GetTimeDifference(), and cmlabs::PrintTimeDif().

◆ SECS_PER_MIN

#define SECS_PER_MIN   60

Seconds per minute.

Definition at line 92 of file PsyTime.h.

Referenced by cmlabs::GetTimeDifference().

◆ SECS_PER_MONTH

#define SECS_PER_MONTH   2629744

Average seconds per Gregorian month (365.2425/12 days).

Definition at line 88 of file PsyTime.h.

Referenced by cmlabs::GetTimeDifference().

◆ SECS_PER_WEEK

#define SECS_PER_WEEK   604800

Seconds per week.

Definition at line 89 of file PsyTime.h.

◆ SECS_PER_YEAR

#define SECS_PER_YEAR   31536000

Seconds in a (non-leap) 365-day year.

Definition at line 86 of file PsyTime.h.

Referenced by cmlabs::GetTimeDifference().

◆ TIME_YEAR_1970

#define TIME_YEAR_1970   62167305600000000L

Same value as USEC_YEAR_0_TO_1970: the PsyTime timestamp of 1970-01-01 00:00:00 UTC.

Definition at line 94 of file PsyTime.h.

Referenced by cmlabs::HTTPRequest::createMultipartRequest(), cmlabs::HTTPRequest::createRequest(), cmlabs::HTTPRequest::createRequest(), and cmlabs::HTTPRequest::createRequest().

◆ TMC_MASTER

#define TMC_MASTER   0

This node is the time master; others sync to it.

Definition at line 217 of file PsyTime.h.

Referenced by cmlabs::GetTimeNow().

◆ TMC_SLAVE

#define TMC_SLAVE   1

This node slaves its clock to a remote master.

Definition at line 218 of file PsyTime.h.

Referenced by cmlabs::SetCurrentTimeSyncData().

◆ TMC_SYNC_INTERVAL

#define TMC_SYNC_INTERVAL   60000000

Re-sync interval in µs (1 minute).

Definition at line 219 of file PsyTime.h.

Referenced by cmlabs::GetTimeNow().

◆ USEC_YEAR_0_TO_1970

#define USEC_YEAR_0_TO_1970   62167305600000000L

Microseconds between year 0000 and the Unix epoch (1970-01-01).

Subtract to convert PsyTime → Unix µs.

Definition at line 84 of file PsyTime.h.

Referenced by cmlabs::FTime2PsyTime(), cmlabs::GetDateAndTime(), cmlabs::GetTimeFromPsyDateAndTime(), and cmlabs::SyncToHardwareClock().

◆ WEEK_MS

#define WEEK_MS   SECS_PER_WEEK * 1000

Definition at line 104 of file PsyTime.h.

◆ WEEK_US

#define WEEK_US   (uint64)SECS_PER_WEEK * 1000000

Definition at line 116 of file PsyTime.h.

◆ YEAR_MS

#define YEAR_MS   (uint64)SECS_PER_YEAR * 1000

Definition at line 106 of file PsyTime.h.

◆ YEAR_US

#define YEAR_US   (uint64)SECS_PER_YEAR * 1000000

Definition at line 118 of file PsyTime.h.

Function Documentation

◆ EstNextTMCWrap()

uint64 cmlabs::EstNextTMCWrap ( )

Estimate when the underlying hardware counter will next wrap around.

Returns
Estimated PsyTime timestamp of the next hardware-counter wrap.

Definition at line 289 of file PsyTime.cpp.

Referenced by _wrap_EstNextTMCWrap(), _wrap_EstNextTMCWrap(), and Java_com_cmlabs_cmsdk_cmsdkJNI_EstNextTMCWrap().

◆ FTime2PsyTime()

uint64 cmlabs::FTime2PsyTime ( uint64 t)

Convert an ftime-style value (ms since Unix epoch) to a PsyTime µs timestamp.

Parameters
tMilliseconds since 1970-01-01.
Returns
µs since year 0 (adds USEC_YEAR_0_TO_1970).

Definition at line 758 of file PsyTime.cpp.

References USEC_YEAR_0_TO_1970.

Referenced by _wrap_FTime2PsyTime(), _wrap_FTime2PsyTime(), cmlabs::utils::GetFileDetails(), and Java_com_cmlabs_cmsdk_cmsdkJNI_FTime2PsyTime().

◆ GetCurrentTimeSyncData()

bool cmlabs::GetCurrentTimeSyncData ( uint64 & tmc,
int64 & netTimeAdjust )

Read the current sync data.

Parameters
tmcReceives the current Time Mapping Constant.
netTimeAdjustReceives the current network adjustment in µs.
Returns
true on success.

Definition at line 147 of file PsyTime.cpp.

References CurrentTMC, and NetTimeAdjust.

Referenced by _wrap_GetCurrentTimeSyncData(), _wrap_GetCurrentTimeSyncData(), and Java_com_cmlabs_cmsdk_cmsdkJNI_GetCurrentTimeSyncData().

◆ GetDateAndTime()

◆ GetDateAndTimeUTC()

struct PsyDateAndTime cmlabs::GetDateAndTimeUTC ( uint64 t)

Break a timestamp into calendar fields in UTC.

Parameters
tTimestamp in µs.

Definition at line 312 of file PsyTime.cpp.

References GetDateAndTime(), and GetDateAndTimeUTC().

Referenced by _wrap_GetDateAndTimeUTC(), _wrap_GetDateAndTimeUTC(), GetDateAndTimeUTC(), and Java_com_cmlabs_cmsdk_cmsdkJNI_GetDateAndTimeUTC().

◆ GetHTTPTime()

◆ GetTimeAge()

◆ GetTimeAgeMS()

int32 cmlabs::GetTimeAgeMS ( uint64 t)

Age of a timestamp relative to now, in milliseconds.

Parameters
tTimestamp to compare (0 returns 0).
Returns
now - t in ms; negative when t is in the future.

Definition at line 35 of file PsyTime.cpp.

References GetTimeNow().

Referenced by _wrap_GetTimeAgeMS(), _wrap_GetTimeAgeMS(), cmlabs::MessageIndex::addMessage(), cmlabs::RequestGateway::addRequestReplyToRequestQueue(), cmlabs::ProcessMemory::checkProcessHeartbeats(), cmlabs::MemoryManager::connect(), cmlabs::NetworkChannel::ConnectionAutodetectRun(), cmlabs::MemoryManager::create(), Internal_BitmapPoster(), Internal_MessageScript(), Internal_QueryTest(), Internal_RetrieveTest(), cmlabs::PsySpace::isConnected(), Java_com_cmlabs_cmsdk_cmsdkJNI_GetTimeAgeMS(), cmlabs::TestRequestExecutor::longRequestRun(), cmlabs::MessageIndex::queryMessages(), cmlabs::NetworkConnection::receive(), cmlabs::SSLConnection::receive(), cmlabs::NetworkConnection::receiveAvailable(), cmlabs::HTTPProtocol::ReceiveHTTPReply(), cmlabs::HTTPProtocol::ReceiveHTTPRequest(), cmlabs::TelnetProtocol::ReceiveTelnetLine(), cmlabs::HTTPProtocol::ReceiveWebsocketData(), cmlabs::RequestQueue::removeStaleRequests(), cmlabs::VantagePoints::reserveNextPoint(), cmlabs::RequestClient::run(), cmlabs::RequestExecutor::run(), cmlabs::TestRequestClient::run(), cmlabs::TestWebRequestClient::run(), cmlabs::TestWebSocketRequestClient::run(), cmlabs::RequestGateway::runClient(), cmlabs::RequestGateway::runExec(), cmlabs::utils::RunOSCommand(), cmlabs::TCPConnection::send(), cmlabs::UDPConnection::send(), cmlabs::ThreadManager::shutdown(), cmlabs::ThreadManager::terminateThread(), cmlabs::RequestQueue::toJSON(), cmlabs::RequestQueue::toXML(), cmlabs::NetworkManager::UnitTest(), cmlabs::RequestGateway::UnitTest(), cmlabs::RequestClient::waitForConnection(), cmlabs::NetworkChannel::waitForHTTPReply(), cmlabs::NetworkChannel::waitForHTTPRequest(), cmlabs::NetworkChannel::waitForMessage(), cmlabs::NetworkChannel::waitForNetworkEvent(), cmlabs::utils::WaitForProcess(), cmlabs::DataMapsMemory::waitForRequestReply(), cmlabs::MemoryRequestConnection::waitForRequestReply(), cmlabs::NetworkChannel::waitForTelnetLine(), cmlabs::utils::Timer::waitForTimer(), and cmlabs::NetworkChannel::waitForWebsocketData().

◆ GetTimeDifference() [1/2]

◆ GetTimeDifference() [2/2]

struct PsyDateAndTime cmlabs::GetTimeDifference ( uint64 t1,
uint64 t2 )

Express the difference t1 - t2 as calendar fields (negative flag set when t2 > t1).

Parameters
t1First timestamp.
t2Second timestamp.
Returns
Difference as a PsyDateAndTime (duration semantics).

Definition at line 393 of file PsyTime.cpp.

References GetTimeDifference().

Referenced by _wrap_GetTimeDifference__SWIG_0(), _wrap_GetTimeDifference__SWIG_0(), _wrap_GetTimeDifference__SWIG_1(), _wrap_GetTimeDifference__SWIG_1(), GetTimeDifference(), GetTimeDifference(), Java_com_cmlabs_cmsdk_cmsdkJNI_GetTimeDifference_1_1SWIG_10(), Java_com_cmlabs_cmsdk_cmsdkJNI_GetTimeDifference_1_1SWIG_11(), PrintTimeDif(), and PsyTime_UnitTest().

◆ GetTimeFromPsyDateAndTime()

uint64 cmlabs::GetTimeFromPsyDateAndTime ( struct PsyDateAndTime & tad)

◆ GetTimeFromString()

uint64 cmlabs::GetTimeFromString ( const char * str)

Parse a textual date/time into a PsyTime timestamp.

Accepted formats (tried in order): RFC 822/1123 ("Sun, 06 Nov 1994 08:49:37 GMT"), RFC 850 ("Sunday, 06-Nov-94 08:49:37 GMT"), ANSI C asctime() ("Sun Nov 6 08:49:37 1994"), and the sortable forms produced by the PrintTimeSortable* functions ("YYYYMMDD-HHMMSS[.mmm[.uuu]]") — sortable input is interpreted as GMT.

Parameters
strDate/time string.
Returns
Timestamp in µs, or 0 on parse failure.

Definition at line 438 of file PsyTime.cpp.

References cmlabs::PsyDateAndTime::day, GetTimeFromPsyDateAndTime(), cmlabs::PsyDateAndTime::hour, cmlabs::PsyDateAndTime::min, cmlabs::PsyDateAndTime::mon, cmlabs::PsyDateAndTime::msec, PsyMonths, PsyMonthsFull, cmlabs::PsyDateAndTime::sec, cmlabs::utils::strcpyavail(), stricmp, cmlabs::PsyDateAndTime::usec, and cmlabs::PsyDateAndTime::year.

Referenced by _wrap_GetTimeFromString(), _wrap_GetTimeFromString(), Java_com_cmlabs_cmsdk_cmsdkJNI_GetTimeFromString(), cmlabs::HTTPReply::processHeader(), and cmlabs::HTTPRequest::processHeader().

◆ GetTimeNow()

uint64 cmlabs::GetTimeNow ( )

Return the current absolute time (µs since year 0) according to the TMC.

Reads the platform monotonic clock (QueryPerformanceCounter on Windows, CLOCK_MONOTONIC on POSIX) and adds CurrentTMC plus any network adjustment.

Because timestamps are plain uint64 microseconds, time arithmetic is ordinary integer arithmetic using the PSY* duration constants:

uint64 now = GetTimeNow();
uint64 deadline = now + 5 * PSYSECOND; // 5 s from now
uint64 anHourAgo = now - PSYHOUR;
if (GetTimeAge(msg->getCreatedTime()) > 2 * PSYMINUTE)
; // message is older than two minutes
printf("%s\n", PrintTimeString(now).c_str()); // "21/07/2026 15:30:02.123.456"
printf("%s\n", PrintTimeSortableString(now).c_str()); // "20260721-153002"
#define PSYHOUR
Definition PsyTime.h:126
std::string PrintTimeSortableString(uint64 t, bool local=true)
Sortable date+time, second resolution.
Definition PsyTime.cpp:731
#define PSYMINUTE
Definition PsyTime.h:125
Returns
Current PsyTime timestamp in microseconds.
Note
Fast and non-blocking; safe to call from any thread.

Definition at line 69 of file PsyTime.cpp.

References CurrentTMC, CurrentTMCMode, LastTMCSync, NetTimeAdjust, SyncToHardwareClock(), TMC_MASTER, and TMC_SYNC_INTERVAL.

Referenced by _wrap_GetTimeNow(), _wrap_GetTimeNow(), cmlabs::TCPListener::acceptConnection(), cmlabs::RequestGateway::addAuthUser(), cmlabs::ComponentData::AddComponentStats(), cmlabs::ComponentMemory::addComponentStats(), cmlabs::ThreadManager::addLocalThreadStats(), cmlabs::MemoryRequestQueues::AddNewRequest(), cmlabs::utils::Timer::addTimer(), cmlabs::ProcessMemory::addToProcessStats(), Base64_UnitTest(), cmlabs::RequestGateway::callExternalAPI(), cmlabs::RequestGateway::callInternalAPI(), cmlabs::MemoryRequestConnection::connect(), cmlabs::NetworkChannel::ConnectionAutodetectRun(), cmlabs::ControlMessage::ControlMessage(), cmlabs::JSONM::convertToMessage(), cmlabs::ComponentMemory::create(), cmlabs::MasterMemory::create(), cmlabs::MemoryManager::create(), cmlabs::ProcessMemory::create(), cmlabs::TemporalMemory::create(), cmlabs::ComponentData::CreateComponent(), cmlabs::ComponentMemory::createComponent(), cmlabs::utils::CreateDrumBeat(), cmlabs::GenericMemoryMap< T, ID >::CreateEntry(), cmlabs::GenericMemoryMap< T, ID >::CreateFirstFreeEntry(), cmlabs::HTTPReply::createFromFile(), cmlabs::ProcessMemory::createNewProcess(), cmlabs::DataMapsMemory::createNewRequest(), cmlabs::ThreadManager::createThread(), cmlabs::DataMessage::DataMessage(), cmlabs::DataMessage::DataMessage(), cmlabs::DataMessage::DataMessage(), cmlabs::DataMessage::DataMessage(), cmlabs::MessageIndex::doMaintenance(), cmlabs::NetworkChannel::enterNetworkEvent(), cmlabs::RandomPathGenerator::generateNextPoint(), cmlabs::GenericObservation< T >::GenericObservation(), cmlabs::GenericObservation< T >::GenericObservation(), cmlabs::utils::GetFileDetails(), cmlabs::utils::TimeQueue< T >::getNextEntryDue(), cmlabs::MovingAverage::getPerfJSON(), cmlabs::MovingAverage::getPerfXML(), GetTimeAge(), GetTimeAgeMS(), Hash_UnitTest(), cmlabs::html::HTML_UnitTest(), cmlabs::HTTPReply::HTTPReply(), cmlabs::HTTPRequest::HTTPRequest(), cmlabs::MemoryRequestServer::init(), cmlabs::VantagePoints::init(), cmlabs::MemoryRequestQueues::InitQueue(), cmlabs::MemoryRequestQueues::InitRequestMap(), cmlabs::TemporalMemory::insertMessage(), Internal_BitmapPoster(), Internal_MessageScript(), Internal_Ping(), Internal_QueryTest(), Internal_RetrieveTest(), Internal_SignalPing(), Internal_SignalPong(), Internal_StatsLog(), Internal_Time(), Java_com_cmlabs_cmsdk_cmsdkJNI_GetTimeNow(), cmlabs::PsyAPI::logPrint(), cmlabs::LogSystem::LogSystemDebug(), cmlabs::LogSystem::LogSystemPrint(), cmlabs::TestRequestExecutor::longRequestRun(), cmlabs::TemporalMemory::maintenance(), MathClasses_UnitTest(), cmlabs::NetworkChannel::MessageConnectionRun(), cmlabs::MessageIndex::MessageIndex(), cmlabs::MessagePlayer::MessagePlayer(), NetworkProtocols_UnitTest(), NetworkTest_TCPClient(), cmlabs::ProcessMemory::PerfTest(), cmlabs::PsyAPI::postOutputMessage(), PrintTimeNowString(), cmlabs::HTTPRequest::processContent(), cmlabs::WebsocketData::processContent(), cmlabs::HTTPRequest::processHeader(), cmlabs::WebsocketData::processHeader(), cmlabs::PsySpace::PsySpace(), PsyTime_UnitTest(), cmlabs::NetworkConnection::readIntoBuffer(), cmlabs::NetworkConnection::receive(), cmlabs::SSLConnection::receive(), cmlabs::NetworkConnection::receiveAvailable(), cmlabs::SSLConnection::receiveAvailable(), cmlabs::HTTPProtocol::ReceiveHTTPReply(), cmlabs::HTTPProtocol::ReceiveHTTPRequest(), cmlabs::HTTPTestServer::receiveHTTPRequest(), cmlabs::RequestGateway::receiveHTTPRequest(), cmlabs::RequestExecutor::receiveMessage(), cmlabs::RequestGateway::receiveMessage(), cmlabs::RequestClient::receiveNetworkEvent(), cmlabs::RequestExecutor::receiveNetworkEvent(), cmlabs::RequestGateway::receiveNetworkEvent(), cmlabs::TelnetProtocol::ReceiveTelnetLine(), cmlabs::HTTPProtocol::ReceiveWebsocketData(), cmlabs::RequestGateway::receiveWebsocketData(), cmlabs::RequestGateway::replyToClient(), cmlabs::RequestReply::replyToRequest(), cmlabs::RequestGateway::RequestGateway(), cmlabs::RequestReply::RequestReply(), cmlabs::VantagePoints::reserveNextPoint(), cmlabs::RequestClient::run(), cmlabs::TestRequestClient::run(), cmlabs::TestWebRequestClient::run(), cmlabs::TestWebSocketRequestClient::run(), cmlabs::RequestGateway::runClient(), cmlabs::RequestGateway::runExec(), cmlabs::utils::RunOSCommand(), cmlabs::utils::SeedRandomValues(), cmlabs::SSLConnection::send(), cmlabs::TCPConnection::send(), cmlabs::UDPConnection::send(), cmlabs::RequestClient::sendMessageToGateway(), cmlabs::RequestExecutor::sendMessageToGateway(), cmlabs::RequestClient::sendRequest(), cmlabs::RequestExecutor::sendStatusNow(), cmlabs::ProcessMemory::setProcessStatus(), cmlabs::DataMapsMemory::setRequestStatus(), cmlabs::DataMapsMemory::setRequestStatus(), cmlabs::RandomPathGenerator::setStartPoint(), cmlabs::PsycloneIndex::setStatus(), cmlabs::ThreadManager::shutdown(), cmlabs::TelnetLine::TelnetLine(), cmlabs::ThreadManager::terminateThread(), Test_RequestClient(), cmlabs::ThreadManager::threadMonitoring(), cmlabs::utils::Timer::triggerTimer(), cmlabs::Bitmap::UnitTest(), cmlabs::ControlMessage::UnitTest(), cmlabs::MemoryManager::UnitTest(), cmlabs::MemoryRequestServer::UnitTest(), cmlabs::MessageIndex::UnitTest(), cmlabs::MovingAverage::UnitTest(), cmlabs::NetworkManager::UnitTest(), cmlabs::ProcessMemory::UnitTest(), cmlabs::RequestGateway::UnitTest(), cmlabs::RequestQueue::UnitTest(), cmlabs::RESTParser::UnitTest(), cmlabs::TemporalMemory::UnitTest(), cmlabs::ThreadManager::UnitTest(), cmlabs::TriggerSpec::UnitTest(), cmlabs::VantagePoints::UnitTest(), cmlabs::utils::UnitTest_Timer(), cmlabs::NetworkManager::UnitTestHTTP(), cmlabs::RequestClient::waitForConnection(), cmlabs::NetworkChannel::waitForHTTPReply(), cmlabs::NetworkChannel::waitForHTTPRequest(), cmlabs::RequestExecutor::waitForLongRequest(), cmlabs::NetworkChannel::waitForMessage(), cmlabs::NetworkChannel::waitForNetworkEvent(), cmlabs::PsyAPI::waitForNewMessage(), cmlabs::MessagePlayer::waitForNextMessage(), cmlabs::utils::WaitForProcess(), cmlabs::DataMapsMemory::waitForRequestReply(), cmlabs::MemoryRequestConnection::waitForRequestReply(), cmlabs::RequestExecutor::waitForShortRequest(), cmlabs::NetworkChannel::waitForTelnetLine(), cmlabs::utils::Timer::waitForTimer(), and cmlabs::NetworkChannel::waitForWebsocketData().

◆ GetTimeOffsetGMT()

uint32 cmlabs::GetTimeOffsetGMT ( )
Returns
The local timezone's offset from GMT/UTC, in seconds.

Definition at line 397 of file PsyTime.cpp.

Referenced by _wrap_GetTimeOffsetGMT(), _wrap_GetTimeOffsetGMT(), and Java_com_cmlabs_cmsdk_cmsdkJNI_GetTimeOffsetGMT().

◆ PrintDate()

◆ PrintDateSortable()

◆ PrintDateSortableDelimiter()

◆ PrintDateString()

◆ PrintDateStringSortable()

◆ PrintDateStringSortableDelimiter()

◆ PrintTime()

◆ PrintTimeDif()

◆ PrintTimeDifString()

◆ PrintTimeNowString()

◆ PrintTimeOnly()

◆ PrintTimeOnlyString()

◆ PrintTimeSortable()

◆ PrintTimeSortableMicrosec()

◆ PrintTimeSortableMicrosecString()

◆ PrintTimeSortableMillisec()

◆ PrintTimeSortableMillisecString()

◆ PrintTimeSortableString()

◆ PrintTimeString()

◆ PsyTime_UnitTest()

◆ SetCurrentNetSyncDif()

bool cmlabs::SetCurrentNetSyncDif ( int64 netTimeDif)

Set the network time difference for a slave node so its GetTimeNow() matches the master.

Parameters
netTimeDifSigned offset in µs between this node and the time master.
Returns
true on success.

Definition at line 135 of file PsyTime.cpp.

References NetTimeAdjust.

Referenced by _wrap_SetCurrentNetSyncDif(), _wrap_SetCurrentNetSyncDif(), and Java_com_cmlabs_cmsdk_cmsdkJNI_SetCurrentNetSyncDif().

◆ SetCurrentTimeSyncData()

bool cmlabs::SetCurrentTimeSyncData ( uint64 tmc,
int64 netTimeAdjust )

Install externally computed sync data (e.g.

received from the time master).

Parameters
tmcTime Mapping Constant to use.
netTimeAdjustSigned network adjustment in µs.
Returns
true on success.

Definition at line 140 of file PsyTime.cpp.

References CurrentTMC, CurrentTMCMode, NetTimeAdjust, and TMC_SLAVE.

Referenced by _wrap_SetCurrentTimeSyncData(), _wrap_SetCurrentTimeSyncData(), and Java_com_cmlabs_cmsdk_cmsdkJNI_SetCurrentTimeSyncData().

◆ SyncToHardwareClock()

uint64 cmlabs::SyncToHardwareClock ( )

Recompute the Time Mapping Constant by anchoring the monotonic clock to the wall clock.

Waits for a wall-clock tick edge for accuracy (gettimeofday loop on POSIX).

Returns
The new TMC value (also stored in CurrentTMC).
Note
May block briefly (up to one clock tick). Call sparingly — typically once at startup and at TMC_SYNC_INTERVAL.

Definition at line 168 of file PsyTime.cpp.

References CurrentTMC, and USEC_YEAR_0_TO_1970.

Referenced by _wrap_SyncToHardwareClock(), _wrap_SyncToHardwareClock(), GetTimeNow(), and Java_com_cmlabs_cmsdk_cmsdkJNI_SyncToHardwareClock().

Variable Documentation

◆ CurrentTMC

uint64 cmlabs::CurrentTMC = 0
static

Current Time Mapping Constant (µs offset from monotonic clock to absolute PsyTime).

Note
file-static: each translation unit gets its own copy.

Definition at line 223 of file PsyTime.h.

Referenced by GetCurrentTimeSyncData(), GetTimeNow(), Java_com_cmlabs_cmsdk_cmsdkJNI_CurrentTMC_1get(), Java_com_cmlabs_cmsdk_cmsdkJNI_CurrentTMC_1set(), SetCurrentTimeSyncData(), Swig_var_CurrentTMC_get(), Swig_var_CurrentTMC_set(), and SyncToHardwareClock().

◆ CurrentTMCMode

◆ LastTMCSync

uint64 cmlabs::LastTMCSync = 0
static

◆ NetTimeAdjust

int64 cmlabs::NetTimeAdjust = 0
static

◆ PsyDays

char cmlabs::PsyDays[][5] = {"None", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
static

Abbreviated weekday names indexed 1..7 (Mon..Sun); index 0 is "None".

Definition at line 134 of file PsyTime.h.

Referenced by GetHTTPTime(), Java_com_cmlabs_cmsdk_cmsdkJNI_PsyDays_1get(), and Swig_var_PsyDays_get().

◆ PsyDaysFull

char cmlabs::PsyDaysFull[][10] = {"None", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
static

Full weekday names indexed 1..7 (Monday..Sunday); index 0 is "None".

Definition at line 136 of file PsyTime.h.

Referenced by Java_com_cmlabs_cmsdk_cmsdkJNI_PsyDaysFull_1get(), and Swig_var_PsyDaysFull_get().

◆ PsyMonths

char cmlabs::PsyMonths[][5] = {"None", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
static

Abbreviated month names indexed 1..12 (Jan..Dec); index 0 is "None".

Definition at line 138 of file PsyTime.h.

Referenced by GetHTTPTime(), GetTimeFromString(), Java_com_cmlabs_cmsdk_cmsdkJNI_PsyMonths_1get(), and Swig_var_PsyMonths_get().

◆ PsyMonthsFull

char cmlabs::PsyMonthsFull[][10] = {"None", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
static

Full month names indexed 1..12; index 0 is "None".

Definition at line 140 of file PsyTime.h.

Referenced by GetTimeFromString(), Java_com_cmlabs_cmsdk_cmsdkJNI_PsyMonthsFull_1get(), and Swig_var_PsyMonthsFull_get().