本文介绍了在Lua中获取特定的UTC日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个例子,我想在LUA中获取(UTC)+0000 UTC日期和时间.

我知道对此有一些答案,但是没有一个不是我的答案.解决此问题的一般方法是查找本地时间",然后加上或减去UTC编号,但是我不知道我的操作系统时区,因为我的程序室运行在不同的位置,并且无法从开发环境中获取时区. /p>

很快,如何使用LUA函数获取UTC 0日期和时间?

解决方案

如果您需要生成epg,则:

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

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?

解决方案

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

这篇关于在Lua中获取特定的UTC日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:44