Skip to content

Commit

Permalink
Use union for xtime to u64 conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Sep 8, 2023
1 parent 076a54b commit 2815439
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"type_traits": "c",
"utility": "c",
"xcli.h": "c",
"xlog.h": "c"
"xlog.h": "c",
"xtime.h": "c"
}
}
1 change: 1 addition & 0 deletions examples/xlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "xstd.h"
#include "xstr.h"
#include "xlog.h"
#include "xtime.h"

int logCallback(const char *pLog, size_t nLength, xlog_flag_t eFlag, void *pCtx)
{
Expand Down
30 changes: 26 additions & 4 deletions src/sys/xtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void XTime_FromEpoch(xtime_t *pTime, const time_t nTime)
pTime->nFraq = 0;
}

void XTime_FromU64(xtime_t *pTime, const uint64_t nTime)
void XTime_Deserialize(xtime_t *pTime, const uint64_t nTime)
{
pTime->nYear = XTIME_U64_YEAR(nTime);
pTime->nMonth = XTIME_U64_MONTH(nTime);
Expand All @@ -120,6 +120,13 @@ void XTime_FromU64(xtime_t *pTime, const uint64_t nTime)
pTime->nFraq = XTIME_U64_FRAQ(nTime);
}

void XTime_FromU64(xtime_t *pTime, const uint64_t nTime)
{
xtimeu_t xtime;
xtime.uTime = nTime;
*pTime = xtime.time;
}

void XTime_ToTm(const xtime_t *pTime, struct tm *pTm)
{
pTm->tm_year = pTime->nYear - 1900;
Expand Down Expand Up @@ -179,6 +186,14 @@ size_t XTime_ToHTTP(const xtime_t *pTime, char *pStr, size_t nSize)
}

uint64_t XTime_ToU64(const xtime_t *pTime)
{
XASSERT_RET(pTime, 0);
xtimeu_t xtime;
xtime.time = *pTime;
return xtime.uTime;
}

uint64_t XTime_Serialize(const xtime_t *pTime)
{
uint64_t nTime;
nTime = (uint64_t)pTime->nYear;
Expand Down Expand Up @@ -266,8 +281,8 @@ double XTime_Diff(const xtime_t *pSrc1, const xtime_t *pSrc2, xtime_diff_t eDiff

void XTime_Copy(xtime_t *pDst, const xtime_t *pSrc)
{
if (pDst == NULL || pSrc == NULL) return;
XTime_FromU64(pDst, XTime_ToU64(pSrc));
XASSERT_VOID_RET((pDst && pSrc));
*pDst = *pSrc;
}

void XTime_Make(xtime_t *pSrc)
Expand Down Expand Up @@ -353,10 +368,17 @@ size_t XTime_GetStr(char *pDst, size_t nSize, xtime_fmt_t eFmt)
}

uint64_t XTime_GetU64(void)
{
xtimeu_t xtime;
XTime_Get(&xtime.time);
return xtime.uTime;
}

uint64_t XTime_Serialized(void)
{
xtime_t xtime;
XTime_Get(&xtime);
return XTime_ToU64(&xtime);
return XTime_Serialize(&xtime);
}

void XTime_GetTm(struct tm *pTm)
Expand Down
8 changes: 8 additions & 0 deletions src/sys/xtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ typedef struct XTime {
uint8_t nFraq;
} xtime_t;

typedef union {
xtime_t time;
uint64_t uTime;
} xtimeu_t;

/* Empty initializer of xtime_t structure */
#define XTIME_EMPTY (xtime_t){ 0, 0, 0, 0, 0, 0, 0}

Expand All @@ -72,6 +77,7 @@ extern "C" {
void XTime_Init(xtime_t *pTime);
void XTime_Get(xtime_t *pTime);
void XTime_GetTm(struct tm *pTm);
uint64_t XTime_Serialized(void);
uint64_t XTime_GetU64(void);
uint32_t XTime_GetUsec(void);
uint64_t XTime_GetStamp(void);
Expand All @@ -89,6 +95,7 @@ void XTime_Copy(xtime_t *pDst, const xtime_t *pSrc);
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);

uint64_t XTime_Serialize(const xtime_t *pTime);
uint64_t XTime_ToU64(const xtime_t *pTime);
time_t XTime_ToEpoch(const xtime_t *pTime);
size_t XTime_ToStr(const xtime_t *pTime, char *pStr, size_t nSize);
Expand All @@ -98,6 +105,7 @@ 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);
void XTime_ToTm(const xtime_t* pTime, struct tm *pTm);

void XTime_Deserialize(xtime_t *pTime, const uint64_t nTime);
void XTime_FromEpoch(xtime_t *pTime, const time_t nTime);
void XTime_FromU64(xtime_t *pTime, const uint64_t nTime);
void XTime_FromTm(xtime_t *pTime, const struct tm *pTm);
Expand Down
2 changes: 1 addition & 1 deletion src/xver.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#define XUTILS_VERSION_MAX 2
#define XUTILS_VERSION_MIN 5
#define XUTILS_BUILD_NUMBER 47
#define XUTILS_BUILD_NUMBER 48

#ifdef __cplusplus
extern "C" {
Expand Down

0 comments on commit 2815439

Please sign in to comment.