xtime.c
April 3, 2026 ยท View on GitHub
Purpose
Time parsing, formatting, serialization, epoch conversion and current-clock helpers.
API Reference
Basic struct helpers
void XTime_Init(xtime_t *pTime)
- Arguments:
- time struct.
- Does:
- zeroes all fields.
- Returns:
- no return value.
void XTime_Copy(xtime_t *pDst, const xtime_t *pSrc)
- Arguments:
- destination and source time structs.
- Does:
- copies the whole struct when both pointers are valid.
- Returns:
- no return value.
void XTime_Make(xtime_t *pTime)
- Arguments:
- time struct.
- Does:
- normalizes the fields through
mktime()and writes the normalized values back.
- normalizes the fields through
- Returns:
- no return value.
void XTime_ToTm(const xtime_t *pTime, struct tm *pTm)
void XTime_FromTm(xtime_t *pTime, const struct tm *pTm)
- Arguments:
xtime_tandstruct tmobjects.
- Does:
- converts between the project struct and
struct tm.
- converts between the project struct and
- Returns:
- no return value.
Current time helpers
void XTime_GetTm(struct tm *pTm)
- Arguments:
- destination
struct tm.
- destination
- Does:
- gets current local time and stores it as
struct tm.
- gets current local time and stores it as
- Returns:
- no return value.
int XTime_GetClock(xtime_spec_t *pTs)
- Arguments:
- destination clock struct containing seconds and nanoseconds.
- Does:
- reads the current real-time clock with:
clock_gettime(CLOCK_REALTIME)on Linux when available- Windows system time APIs
gettimeofday()elsewhere
- reads the current real-time clock with:
- Returns:
XSTDOKon success.XSTDERRon clock read failure.
uint32_t XTime_Get(xtime_t *pTime)
- Arguments:
- destination
xtime_t.
- destination
- Does:
- reads current time, converts epoch seconds to local time and stores hundredths of a second in
nFraq.
- reads current time, converts epoch seconds to local time and stores hundredths of a second in
- Returns:
- current microseconds component.
uint32_t XTime_GetUsec(void)
uint64_t XTime_GetMs(void)
uint64_t XTime_GetStamp(void)
uint64_t XTime_GetU64(void)
uint64_t XTime_Serialized(void)
- Arguments:
- none.
- Does:
- returns the current time as:
- microseconds component only
- epoch milliseconds
- epoch microseconds
- union-packed
xtime_t - manually serialized
uint64_t
- returns the current time as:
- Returns:
- the requested numeric representation.
size_t XTime_GetStr(char *pDst, size_t nSize, xtime_fmt_t eFmt)
- Arguments:
- destination buffer, size and desired format enum.
- Does:
- gets current time and formats it using one of the
XTime_To*()helpers.
- gets current time and formats it using one of the
- Returns:
- written length.
0for invalid args or unknown format.
Calendar helpers
int XTime_GetLeapYear(int nYear)
int XTime_LeapYear(const xtime_t *pTime)
- Arguments:
- raw year or
xtime_t.
- raw year or
- Does:
- checks leap-year rules.
- Returns:
1for leap year.0otherwise.
int XTime_GetMonthDays(int nYear, int nMonth)
int XTime_MonthDays(const xtime_t *pTime)
- Arguments:
- raw year/month or
xtime_t.
- raw year/month or
- Does:
- returns the number of days in the requested month.
- Returns:
28,29,30or31.
- Caveat:
- current February logic is reversed: leap years return
28and non-leap years return29.
- current February logic is reversed: leap years return
Difference helpers
double XTime_DiffSec(const xtime_t *pSrc1, const xtime_t *pSrc2)
double XTime_Diff(const xtime_t *pSrc1, const xtime_t *pSrc2, xtime_diff_t eDiff)
- Arguments:
- two time values and an optional scaling unit.
- Does:
- computes difference through
mktime()anddifftime(). - optionally rescales the result to years, months, weeks, days, hours or minutes using fixed constants.
- computes difference through
- Returns:
- floating-point difference value.
Serialization and epoch conversion
uint64_t XTime_Serialize(const xtime_t *pTime)
void XTime_Deserialize(xtime_t *pTime, const uint64_t nTime)
- Arguments:
- time struct and serialized integer.
- Does:
- manually packs or unpacks fields into a
uint64_t.
- manually packs or unpacks fields into a
- Returns:
- serializer returns the packed value.
- deserializer has no return value.
uint64_t XTime_ToU64(const xtime_t *pTime)
void XTime_FromU64(xtime_t *pTime, const uint64_t nTime)
- Arguments:
- time struct and union-packed integer.
- Does:
- reinterprets the struct through
xtimeu_t.
- reinterprets the struct through
- Returns:
- packed integer from
ToU64(). - no return value from
FromU64().
- packed integer from
uint64_t XTime_ToEpochLocal(const xtime_t *pTime)
uint64_t XTime_ToEpochUTC(const xtime_t *pTime)
void XTime_FromEpoch(xtime_t *pTime, const time_t nTime)
uint64_t XTime_ISOToEpochUTC(const char *pStr)
- Arguments:
- time struct, epoch value or ISO string.
- Does:
- converts between
xtime_tand local/UTC epoch seconds. FromEpoch()uses local time.ISOToEpochUTC()parses ISO then converts to UTC epoch seconds.
- converts between
- Returns:
- epoch value for converters.
0fromISOToEpochUTC()when parsing fails.
Formatting helpers
size_t XTime_ToStr(const xtime_t *pTime, char *pStr, size_t nSize)
size_t XTime_ToHstr(const xtime_t *pTime, char *pStr, size_t nSize)
size_t XTime_ToLstr(const xtime_t *pTime, char *pStr, size_t nSize)
size_t XTime_ToRstr(const xtime_t *pTime, char *pStr, size_t nSize)
size_t XTime_ToHTTP(const xtime_t *pTime, char *pStr, size_t nSize)
size_t XTime_ToISO(const xtime_t *pTime, char *pStr, size_t nSize)
size_t XTime_ToISO8601(const xtime_t *pTime, char *pStr, size_t nSize)
- Arguments:
- time struct, destination buffer and size.
- Does:
- writes one of the supported text forms:
- compact numeric
- dotted human format
- slash-separated format
- US-style format
- HTTP GMT string
- ISO
- ISO8601 UTC-with-
.000Z
- writes one of the supported text forms:
- Returns:
- written length from
xstrncpyf()orstrftime().
- written length from
Parse helpers
int XTime_FromStr(xtime_t *pTime, const char *pStr)
int XTime_FromLstr(xtime_t *pTime, const char *pStr)
int XTime_FromRstr(xtime_t *pTime, const char *pStr)
int XTime_FromISO(xtime_t *pTime, const char *pStr)
- Arguments:
- destination time struct and source string.
- Does:
- clears the destination and parses the string with
sscanf().
- clears the destination and parses the string with
- Returns:
sscanf()field count.0when the source string is empty.
int XTime_FromHstr(xtime_t *pTime, const char *pStr)
- Header status:
- declared in
xtime.h.
- declared in
- Implementation status:
- the source file implements
XTime_FromHStr()with a capitalS, notXTime_FromHstr().
- the source file implements
- Actual behavior of the implemented function:
- clears the destination and parses
%04d.%02d.%02d-%02d:%02d:%02d.%02d. - returns the
sscanf()field count or0for empty input.
- clears the destination and parses
Important Notes
-
FromEpoch()uses local time, not UTC. -
XTime_GetMonthDays()has reversed February leap-year logic in the current source. -
xtime.handxtime.cdisagree onXTime_FromHstrvsXTime_FromHStr. -
Decide explicitly whether your caller wants local time or UTC before mixing
FromEpoch(),ToEpochUTC()and HTTP/ISO helpers.