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

时间:2016-02-15 06:41:31

标签: time lua

如果当前时间介于两个时间间隔内,检查Lua的最佳方法是什么?

即。早上5点到早上8点或晚上11点和凌晨1点?

4 个答案:

答案 0 :(得分:2)

os.date("*t", os.time())获取一个表格,代表当前时间,其中包含hour字段(范围0 - 23),min字段,{ {1}}字段。

sec

将时间与local current = os.date("*t", os.time()) print(current.hour, current.min) 进行比较。

答案 1 :(得分:0)

你可以做一些算术来根据分钟比较每个值:

from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        layout = QtGui.QVBoxLayout(self)
        self.buttonGroup = QtGui.QButtonGroup(self)
        for text in 'One Two Three'.split():
            button = QtGui.QPushButton(text)
            button.setCheckable(True)
            layout.addWidget(button)
            self.buttonGroup.addButton(button)
        self.buttonGroup.buttonClicked.connect(self.handleButtons)

    def handleButtons(self, button):
        print('Button %s Clicked' % button.text())

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

答案 2 :(得分:0)

以上功能对我不起作用。如果第二次是在第二天,它不起作用。如果扩展了脚本,也许它也可以帮助其他人。

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

GR, 罗纳德

答案 3 :(得分:0)

这是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

我用这个用我家里的运动传感器转动不同的灯。当它的傍晚我把灯转到100%,晚上转到5%(我有暖白光和冷白光LED条连接到RGBW控制器R和G连接器):

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