From 08a704d58fb2324d001a74e32a0397b24fdbf24c Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Sat, 10 Mar 2018 02:16:49 +0800 Subject: [PATCH] Add methods to get real world time --- util.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/util.lua b/util.lua index 25ee11d..f8c4e2f 100644 --- a/util.lua +++ b/util.lua @@ -1,5 +1,30 @@ local component=require("component") local serialization = require("serialization") +local filesystem=require("filesystem") + +function getWorldTimestamp() + local name=os.tmpname() + local f=io.open(name,"w") + f:close() + local ts=filesystem.lastModified(name) + filesystem.remove(name) + return ts +end + +function getWorldTimeInfo(gmt) + if(gmt==nil) then gmt=0 end + local x=getWorldTimestamp()+gmt*3600*1000 + local t=os.date("*t",math.ceil(x/1000)) + t.msec=x%1000 + t.unix=math.ceil(x/1000) + t.stamp=x + return t +end + +function getWorldDate(gmt) + local t=getWorldTimeInfo(gmt) + return t.year,t.month,t.day,t.hour,t.min,t.sec,t.msec +end function serialize(value) return serialization.serialize(value)