且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从整数纪元时间值转换为本地时间

更新时间:2023-09-19 15:33:04

localtime()函数使用指向 const time_t 的指针,因此您必须先将整数纪元值转换为 time_t ,然后再调用 localtime

The localtime() function takes a pointer to const time_t so you have to first convert your integer epoch value to a time_t before calling localtime.

int epoch_time = SOME_VALUE;
struct tm * timeinfo;

/* Conversion to time_t as localtime() expects a time_t* */
time_t epoch_time_as_time_t = epoch_time;

/* Call to localtime() now operates on time_t */
timeinfo = localtime(&epoch_time_as_time_t);