local ffi = require("ffi")
ffi.cdef[[
typedef long time_t;
typedef struct tm {
int sec; /* secs. [0-60] (1 leap sec) */
int min; /* mins. [0-59] */
int hour; /* Hours. [0-23] */
int day; /* Day. [1-31] */
int month; /* Month. [0-11] */
int year; /* Year - 1900. */
int wday; /* Day of week. [0-6] */
int yday; /* Days in year.[0-365] */
int isdst; /* DST. [-1/0/1]*/
long int gmtoff; /* secs east of UTC. */
const char* zone; /* Timezone abbreviation. */
} tm;
struct tm* localtime_r(const time_t*, struct tm*);
]]
local TimeStructP = ffi.typeof("struct tm[1]")
local TimeStruct = ffi.typeof("struct tm")
local TimeP = ffi.typeof("time_t[1]")
local function localtime1(sec)
local c = ffi.C.localtime_r(TimeP(sec), TimeStruct())
return string.format('%s-%s-%s %s:%s:%s', c.year+1900, c.month+1, c.day,
c.hour, c.min, c.sec)
end
local function localtime2(sec)
local p = ffi.C.localtime_r(TimeP(sec), TimeStructP()) -- pointer here!
local c = p[0]
return string.format('%s-%s-%s %s:%s:%s', c.year+1900, c.month+1, c.day,
c.hour, c.min, c.sec)
end
print(localtime1(0))
print(localtime2(0))