如何在 Lua 中获取最后修改的时间戳 [英] How can I get last modified timestamp in Lua

查看:55
本文介绍了如何在 Lua 中获取最后修改的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理 Lua 文件.

I am trying to work on Lua file handling.

因此,我可以打开、读取、写入、关闭文件.

So, I am able to open, read, write, close the files.

local session_debug = io.open("/root/session_debug.txt", "a")
session_debug:write("Some text
")
session_debug:close()

我怎么知道这个文件的最后修改日期时间戳.

How can I know the last modified date timestamp of this file.

推荐答案

标准 Lua 中没有内置函数可以做到这一点.在没有第三方库的情况下获得它的一种方法是使用 io.popen.

There's no built-in function in standard Lua that does this. One way to get it without third-party libraries is to take use of io.popen.

例如,在 Linux 上,您可以使用 stat:

For example, on Linux, you could use stat:

local f = io.popen("stat -c %Y testfile")
local last_modified = f:read()

现在last_modifiedtestfile最后修改时间的时间戳.在我的系统上,

Now last_modified is the timestamp of the last modified time of testfile. On my system,

print(os.date("%c", last_modified))

输出 2014 年 3 月 22 日星期六 08:36:50.

这篇关于如何在 Lua 中获取最后修改的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆