Get specific UTC date and Time in Lua

依然范特西╮ 提交于 2019-12-11 06:33:18

问题


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:


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 ))


来源:https://stackoverflow.com/questions/43600810/get-specific-utc-date-and-time-in-lua

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!