// ES6 语法: import koffi from 'koffi';
const koffi = require("koffi");
// 加载共享库
const lib = koffi.load("libc.so.6");
// 声明结构体类型
const timeval = koffi.struct("timeval", {
tv_sec: "unsigned int",
tv_usec: "unsigned int",
});
const timezone = koffi.struct("timezone", {
tz_minuteswest: "int",
tz_dsttime: "int",
});
const time_t = koffi.struct("time_t", { value: "int64_t" });
const tm = koffi.struct("tm", {
tm_sec: "int",
tm_min: "int",
tm_hour: "int",
tm_mday: "int",
tm_mon: "int",
tm_year: "int",
tm_wday: "int",
tm_yday: "int",
tm_isdst: "int",
});
// 查找函数
const gettimeofday = lib.func("int gettimeofday(_Out_ timeval *tv, _Out_ timezone *tz)");
const localtime_r = lib.func("tm *localtime_r(const time_t *timeval, _Out_ tm *result)");
const printf = lib.func("int printf(const char *format, ...)");
// 获取本地时间
let tv = {};
let now = {};
gettimeofday(tv, null);
localtime_r({ value: tv.tv_sec }, now);
// 使用 printf(变参函数)格式化输出
printf("Hello World!\n");
printf("Local time: %02d:%02d:%02d\n", "int", now.tm_hour, "int", now.tm_min, "int", now.tm_sec);