Time
Xi::Time is a high-performance timestamp and calendar toolkit. It is the embedded-safe alternative to standard C++ <chrono> or the archaic <ctime> POSIX headers.
Architectural Overview: High-Performance Civil Time
Parsing and manipulating dates on an embedded system usually involves slow POSIX mktime functions, hidden dynamic allocations for strings, or reliance on heavy Real-Time Clock (RTC) libraries.
Xi::Time relies exclusively on heavily optimized, inline bit-math for converting between UNIX Epoch microseconds and Civil formats (Year, Month, Day, Hour, Minute).
Core Features
Epoch Centralized: All timestamps internally resolve down to a 64-bit signed integer (
i64 us), representing microseconds since the UNIX Epoch (1970-01-01T00:00:00Z).Auto-Dynamic Parsing: The
Xi::Timeconstructor accepts advanced format strings. It parses date formats like"2025-05-12T14:30.00"directly from raw payloads, with zero dynamic memory allocation.Hardware Abstraction: Through
Xi::epochMicros(),Xi::Timepulls the highest resolution monotonic tick available—whether that is theclock_gettime()on Linux, or FreeRTOS timers on ESP32 microcontrollers.
📖 Complete API Reference
1. Global Timestamping
i64 Xi::epochMicros()Returns the universally synced epoch in microseconds since 1970.int Xi::getGMT()Returns the globally tracked Timezone offset in hours (typically orchestrated viaXi::Spatial).void Xi::Time::sleep(double seconds)Suspends the current execution thread safely without busy-waiting. UsesvTaskDelayon ESP32/FreeRTOS,Sleepon Windows, andnanosleepon Linux.
2. Time Synchronization
Microcontrollers drift over time. Xi::Time offers built-in hardware synchronization tracking without overwriting low-level system monotonic timers.
void syncClock()Automatically polls the host OS (Linux/Windows) real-time clock to calibrate theXiepoch baseline.void syncClock(i64 now)Updates the system offset manually from an external source (like an NTP server or GPS module).void syncPPS()Snaps the current sub-second offset to the exact microsecond perfectly upon a hardware interrupt (Pulse Per Second), establishing atomic-level chronometry.
3. Instantiation & Parsing
Xi::Time()/Xi::Time(i64 unixMicroseconds)Constructs a timestamp. Defaults to "Now".Xi::Time(const Xi::String &date, const Xi::String &fmt)A powerful inline parser. Automatically derives the epoch microseconds from text without allocating strings dynamically. Format Specifiers:yyyy: 4-digit Yearmm: 2-digit Month or Minute (inferred via proximity toh)dd: 2-digit Dayhh: 2-digit Hourss: 2-digit Secondrr: AM/PM indicatorzz: Timezone Offset (e.g.+02:00or-05:00)
4. Civil Calendar Properties (C#-Style)
Instead of clumsy getters and setters, Xi::Time provides C#-style proxy properties for direct syntax manipulation:
t.year = 2025;int y = t.year;t.month = 5;t.day = 12; // (Day in month)t.hour = 14;t.minute = 30;t.second = 0;
These proxies instantly convert bounds (e.g., adding 90 to .minute correctly rolls over .hour and recalculates the microsecond epoch inline).
5. Advanced Component Output
int dayInYear()Returns the day of the year (0-365).Xi::String toString(const Xi::String &fmt = "yyyy/mm/dd hh:mm:ss", int targetTzHours = 0)Safely interpolates the timestamp back into a human-readableXi::Stringapplying the requestedtargetTzHoursoffset.
💻 Example: Parsing and Calculating Rollovers
Last updated