Time 如何检查当前时间是否介于其他两个时间之间?
如果当前时间在两个时间间隔之间,那么签入Lua的最佳方式是什么 例如,在上午5点到上午8点或晚上11点到凌晨1点之间?Time 如何检查当前时间是否介于其他两个时间之间?,time,lua,Time,Lua,如果当前时间在两个时间间隔之间,那么签入Lua的最佳方式是什么 例如,在上午5点到上午8点或晚上11点到凌晨1点之间?os.date(“*t”,os.time())获取一个表示当前时间的表,该表有一个小时字段(范围0-23),一个min字段,一个sec字段 local current = os.date("*t", os.time()) print(current.hour, current.min) 将时间与hour*60+min进行比较。您可以做一些算术运算,以分钟为基础比较每个值: lo
os.date(“*t”,os.time())
获取一个表示当前时间的表,该表有一个小时字段(范围0
-23
),一个min
字段,一个sec
字段
local current = os.date("*t", os.time())
print(current.hour, current.min)
将时间与hour*60+min
进行比较。您可以做一些算术运算,以分钟为基础比较每个值:
local function getMinutes(hours,minutes)
return (hours*60)+minutes
end
local value1 = getMinutes(time1.hours,time1.minutes)
local value2 = getMinutes(time2.hours,time2.minutes)
local currentTime = getMinutes(tonumber(os.date("%H"),tonumber(os.date("%M")))
local isBetween = false
if (currentTime >= value1 and currentTime <= value2) or (currentTime >= value2 and currentTime <= value1) then
isBetween = true
end
本地函数获取分钟数(小时,分钟)
返回时间(小时*60)+分钟
结束
本地值1=getMinutes(time1.hours,time1.minutes)
本地值2=getMinutes(time2.hours,time2.minutes)
local currentTime=getMinutes(操作系统日期(“%H”)、操作系统日期(“%M”))
本地isBetween=false
如果(currentTime>=value1和currentTime=value2且currentTime以上函数对我不起作用。如果第二次是在第二天,则该函数不起作用。如果扩展脚本,可能也会帮助其他人
local StartTimeHours=21
local StartTimeMinutes=11
local StopTimeHours=8
local StopTimeMinutes=30
time = os.date("*t")
local curhour=time.hour
local curmin=time.min
local function getMinutes(hours, minutes)
return (hours*60)+minutes
end
local function IsTimeBetween(StartH, StartM, StopH, StopM, TestH, TestM)
if (StopH < StartH) then -- add 23 hours if endhours < starthours
local StopHOrg=StopH
StopH=StopH+23
if (TestH <= StopHOrg) then -- if endhours has increased the currenthour should also increase
TestH=TestH+23
end
end
local StartTVal = getMinutes(StartH, StartM)
local StopTVal = getMinutes(StopH, StopM)
local curTVal = getMinutes(TestH, TestM)
if (curTVal >= StartTVal and curTVal <= StopTVal) then
return true
else
return false
end
end
local isBetween = IsTimeBetween(StartTimeHours, StartTimeMinutes, StopTimeHours, StopTimeMinutes, curhour, curmin)
commandArray = {}
if (isBetween) then
print("Yep: ".. curhour..":".. curmin .. " is between " .. StartTimeHours .. ":"..StartTimeMinutes .." and "..StopTimeHours..":"..StopTimeMinutes)
else
print("No: "..curhour..":".. curmin.." is not between "..StartTimeHours..":"..StartTimeMinutes.." and "..StopTimeHours..":"..StopTimeMinutes)
end
return commandArray
local StartTimeHours=21
本地StartTimeMinutes=11
本地停止时间小时数=8
本地停止时间分钟=30
时间=操作系统日期(“*t”)
本地curhour=time.hour
本地curmin=time.min
本地函数getMinutes(小时,分钟)
返回时间(小时*60)+分钟
结束
本地函数istimetween(StartH、StartM、StopH、StopM、TestH、TestM)
如果(停止时间<开始时间),则--如果结束时间<开始时间,则增加23小时
本地StopHOrg=StopH
StopH=StopH+23
如果(TestH=StartTVal和curTVal这是Ronalds的解决方案,但经过了调整(每天23小时),并添加了简单检查当前时间的功能:
local function getMinutes(hours, minutes)
return (hours*60)+minutes
end
local function IsTimeBetween(StartH, StartM, StopH, StopM, TestH, TestM)
if (StopH < StartH) then -- add 24 hours if endhours < starthours
local StopHOrg=StopH
StopH = StopH + 24
if (TestH <= StopHOrg) then -- if endhours has increased the currenthour should also increase
TestH = TestH + 24
end
end
local StartTVal = getMinutes(StartH, StartM)
local StopTVal = getMinutes(StopH, StopM)
local curTVal = getMinutes(TestH, TestM)
return (curTVal >= StartTVal and curTVal <= StopTVal)
end
local function IsNowBetween(StartH,StartM,StopH,StopM)
local time = os.date("*t")
return IsTimeBetween(StartH, StartM, StopH, StopM, time.hour, time.min)
end
本地函数获取分钟数(小时,分钟)
返回时间(小时*60)+分钟
结束
本地函数istimetween(StartH、StartM、StopH、StopM、TestH、TestM)
如果(停止时间<开始时间),则--如果结束时间<开始时间,则添加24小时
本地StopHOrg=StopH
StopH=StopH+24
如果(TestH=StartTVal和curTVal)可以,那么由于可能需要几分钟,所以它会稍微复杂一些。例如5:15和8:20。或者tonumber(os.date“%i%M”)
来使用普通的算术比较运算符。@Gerrit
local LuxTreshold=60
local StartTimeHours=20
local StartTimeMinutes=15
local StopTimeHours=8
local StopTimeMinutes=00
if (tonumber(fibaro:getValue(28, "value")) < LuxTreshold )
then
if (IsNowBetween(StartTimeHours, StartTimeMinutes, StopTimeHours, StopTimeMinutes))
then
fibaro:call(10, "setColor", "16","0","0","0")
fibaro:call(10, "turnOn")
else
fibaro:call(10, "setColor", "255","255","0","0")
fibaro:call(10, "turnOn")
end
end