Get specific UTC date and Time in Lua

2019-08-01 14:39发布

As an illustration, I want to grab the (UTC) +0000 UTC date and time in LUA.

I know there are some answers about this but none of them isn't my answer. The general approach to answering this issue is Find Local Time, then plus or minus the UTC number but I don't know my OS Time Zone because my program cab be run in different locations and I cannot get the timezone from my development environment.

Shortly, How can I get the UTC 0 date and time using LUA functions?

1条回答
ら.Afraid
2楼-- · 2019-08-01 15:02

If you need to generate epg, then:

local timestamp = os.time() 
local dt1 = os.date( "!*t", timestamp )  -- UTC
local dt2 = os.date( "*t" , timestamp )  -- local 

local shift_h  = dt2.hour - dt1.hour +  (dt1.isdst and 1 or 0)    -- +1 hour if daylight saving
local shift_m = 100 * (dt2.min  - dt1.min) / 60  
print( os.date("%Y%m%d%H%M%S ", timestamp) ..  string.format('%+05d' , shift_h*100 + shift_m ))
查看更多
登录 后发表回答