1. 我的需求
通过lua将某个时间字符串转换成时间戳,但实际验证后发现精度是个问题,得到结果的并不是正确的时间戳。
2. 我的代码如下
function unixtimestamp(datestr)
-- StrSplit函数类似python里面的split
local datelist = StrSplit(datestr, " ");
local datebegin = StrSplit(datelist[1], "-");
local dateend = StrSplit(datelist[2], ":");
local day = datebegin[3];
local month = datebegin[2];
local year = datebegin[1];
local hour = dateend[1];
local minute = dateend[2];
local second = dateend[3];
print("use shell date command: ");
os.execute("date +%s -d " .. "\"" .. datestr .. "\"");
print("use lua os.time() command: ");
print(os.time({day = day, month = month, year = year, hour = hour, minute = minute, second = second}));
end
unixtimestamp("2017-12-20 22:47:57");
print("\n");
unixtimestamp("2017-12-20 22:12:57");
print("\n");
unixtimestamp("2017-12-20 20:13:57");
3. 以下是我运行之后的结果