如何检查当前时间是否在两个时间段之间?

在 Lua 中,检查当前时间是否处于时间间隔之间的最佳方法是什么?

也就是说,在早上 5 点到 8 点之间或在晚上 11 点到 1 点之间?

点赞
用户1009479
用户1009479

os.date("*t", os.time())得到一个表示当前时间的表格,其中包含hour字段(取值范围为0-23)、min字段和sec字段。

local current = os.date("*t", os.time())
print(current.hour, current.min)

比较时间与hour * 60 + min

2016-02-15 06:47:59
用户3353400
用户3353400

你可以通过对分钟数进行一些算术运算来比较每个值:

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
2016-02-15 20:28:37
用户3158613
用户3158613

上述函数对我无效。如果第二次是在第二天, 这个函数就无效了。如果扩展这个脚本, 或许其他人也能从中受益。

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 -- 如果结束小时小于开始小时则增加 23 小时
        local StopHOrg=StopH
        StopH=StopH+23
        if (TestH <= StopHOrg) then -- 如果结束小时增加了,则当前小时也应该增加
            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
2016-11-16 21:24:47
用户5592367
用户5592367

这是 Ronald 的解决方案,但稍作修改(23 小时制),并添加了一个函数来简单地检查当前时间:

local function getMinutes(hours, minutes)
    return (hours*60)+minutes
end

local function IsTimeBetween(StartH, StartM, StopH, StopM, TestH, TestM)
    if (StopH < StartH) then -- 如果结束时间小于开始时间,则加上 24 小时
        local StopHOrg=StopH
        StopH = StopH + 24
        if (TestH <= StopHOrg) then -- 如果结束时间已增加,则当前时间也应该增加
            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

我用这个在我的房子里用运动传感器开不同的灯。当天还早时,我把灯光调到 100%;晚上调到 5%(我连接了 RGBW 控制器的暖白 LED 和冷白 LED 条):

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
2017-01-29 10:29:12
用户7169586
用户7169586

我找到了这个简单的方法:

``` 本地当前分钟数=(tonumber(os.time("% H"))60+tonumber(os.time(% M)) 本地开始时间=(760)+30 - 07:30分钟过去 本地结束时间=(21*60)+30 - 21:30

如果当前分钟数小于等于开始时间和当前分钟数大于等于结束时间,则 print("白天") 否则 print("夜晚") 结束```

2022-02-02 17:04:59