Page 1 of 1

Is this IF statement logic correct? (Time between)

Posted: Saturday 13 June 2020 23:52
by binbo
Hi guys,

does this look correct:

Code: Select all

time    = os.date("*t")
theHour = time.hour
                 if ((theHour > 23 and theHour < 24) or (theHour < 7 and theHour >= 0)) then
                    -- A time between 11pm and 7am???
                end
Does this look like it will trigger between 11pm and 7am?

Is there a better way to do this?

Many thanks.

Re: Is this IF statement logic correct? (Time between)

Posted: Sunday 14 June 2020 0:10
by waaren
binbo wrote: Saturday 13 June 2020 23:52 Does this look like it will trigger between 11pm and 7am?
Not really

try

Code: Select all

        time = os.date("*t")
	 if time.hour > 22 or time.hour < 7  then
            print ('between 23:00 and 6:59:59')
         else
            print ('NOT between 23:00 and 6:59:59')
         end

Re: Is this IF statement logic correct? (Time between)

Posted: Sunday 14 June 2020 10:24
by binbo
Thank you so much.

It seems so obvious. Lol. I don’t know how I came up with such a complicated non-working solution :lol: :lol:

Re: Is this IF statement logic correct? (Time between)

Posted: Saturday 20 June 2020 7:02
by tobydeteckel
Code:

Code: Select all

	function timebetween(s,e)
	   timenow = os.date("*t")
	   year = timenow.year
	   month = timenow.month
	   day = timenow.day
	   s = s .. ":00"  
	   e = e .. ":00"
	   shour = string.sub(s, 1, 2)
	   sminutes = string.sub(s, 4, 5)
	   sseconds = string.sub(s, 7, 8)
	   ehour = string.sub(e, 1, 2)
	   eminutes = string.sub(e, 4, 5)
	   eseconds = string.sub(e, 7, 8)
	   t1 = os.time()
	   t2 = os.time{year=year, month=month, day=day, hour=shour, min=sminutes, sec=sseconds}
	   t3 = os.time{year=year, month=month, day=day, hour=ehour, min=eminutes, sec=eseconds}
	   sdifference = os.difftime (t1, t2)
	   edifference = os.difftime (t1, t3)
	   isbetween = false
	   if sdifference >= 0 and edifference <= 0 then
		  isbetween = true
	   end
	   return isbetween
	end
Example:

Code: Select all

if timebetween("00:01:00","06:00:00") then
Example 2:

Code: Select all

if timebetween("04:14:00","06:23:00") then
Credits:
Siewert Lameijer / Siewert308SW

Re: Is this IF statement logic correct? (Time between)

Posted: Saturday 20 June 2020 8:39
by waaren
tobydeteckel wrote: Saturday 20 June 2020 7:02 Example:

Code: Select all

if timebetween("00:01:00","06:00:00") then
Example 2:

Code: Select all

if timebetween("04:14:00","06:23:00") then
This function does not work for the question from the op. It does not take date crossings into account.

try

Code: Select all

if timebetween("23:00:00", "11:59:00") then
in the morning