From 6adf412a256181e07b4859c3703fb4dc6023007e Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sat, 23 Jan 2021 15:16:20 -0300 Subject: [PATCH] return higher precision times in lfs.attributes --- src/lfs.c | 21 ++++++++++++++++++--- tests/test.lua | 4 ++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/lfs.c b/src/lfs.c index 95ab63b..1b74a31 100644 --- a/src/lfs.c +++ b/src/lfs.c @@ -876,22 +876,37 @@ static void push_st_rdev(lua_State * L, STAT_STRUCT * info) lua_pushinteger(L, (lua_Integer) info->st_rdev); } +#define push_stat_timespec(field) \ + lua_pushnumber(L, info->FIELDNAME(field).tv_sec \ + + info->FIELDNAME(field).tv_nsec / 1000000000.0) + +#if _POSIX_VERSION >= 200809L +#define FIELDNAME(field) field +#define push_stat_time(field) push_stat_timespec(field) +#elif __APPLE__ +#define FIELDNAME(field) field ## espec +#define push_stat_time(field) push_stat_timespec(field) +#else +#define FIELDNAME(field) field ## e +#define push_stat_time(field) lua_pushinteger(L, info->FIELDNAME(field)) +#endif + /* time of last access */ static void push_st_atime(lua_State * L, STAT_STRUCT * info) { - lua_pushinteger(L, (lua_Integer) info->st_atime); + push_stat_time(st_atim); } /* time of last data modification */ static void push_st_mtime(lua_State * L, STAT_STRUCT * info) { - lua_pushinteger(L, (lua_Integer) info->st_mtime); + push_stat_time(st_mtim); } /* time of last file status change */ static void push_st_ctime(lua_State * L, STAT_STRUCT * info) { - lua_pushinteger(L, (lua_Integer) info->st_ctime); + push_stat_time(st_ctim); } /* file size, in bytes */ diff --git a/tests/test.lua b/tests/test.lua index ed154c0..3ace810 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -151,8 +151,8 @@ io.flush() -- Restore access time to current value assert (lfs.touch (tmpfile, attrib.access, attrib.modification)) new_att = assert (lfs.attributes (tmpfile)) -assert (new_att.access == attrib.access) -assert (new_att.modification == attrib.modification) +assert (new_att.access - attrib.access < 1) +assert (new_att.modification - attrib.modification < 1) io.write(".") io.flush()