Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Sign in to follow this  
Pheggas

Compare times

Question

Hello. I'm struggling with comparing times in Open Computers. Currently i have

local Hour = os.date("%H")
local Min = os.date("ˇ%M")
local Sec = os.date("%S")
while true do
if (Hour, ":", Min, ":", Sec) == (Hour, ":", Min+15, ":", Sec) then print("anything here")
end

So first half of the if should be (eg.) 16:15:25 and the second half should be 16:30:25. But it's writting error that on line 5 (where is the if) and the error is: ")" expected near ,

So should i first write that times into local variables and after then i can compare the variables or what i must to do?

The main project for which i want to do this is monitoring movement (using Radar) and when Radar detect any movement, it will enable redstone signal. When not, it will count 15 seconds and then it should turn off redstone signal.

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Okay, so, for a start, I ... think you're trying to concatenate times to compare?

To concatenate in Lua, you use the .. operator, like so:

(Hour .. ":" .. Minute .. ":" .. Second)

Though using string.format may be saner

string.format("%s:%s:%s", Hour, Minute, Second)

Next, you're trying to add a number to a string, which just isn't going to fly;

Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> "a"+2
stdin:1: attempt to perform arithmetic on a string value
stack traceback:
	stdin:1: in main chunk
	[C]: in ?

You'd need to use tonumber on the value somewhere, like

local Min = tonumber(os.date("%M"))

And then turn it back into a string to concatenate it

(Hour .. ":", tostring(Min+15) .. ":" .. Sec)

Or use the %i - integer - pattern for string.format

string.format("%s:%i:%s", Hour, Minute, Second)

Though this could all be done much more simply:

os.sleep(15)

Which will sleep the computer for 15 seconds, saving power, rather than busy-waiting - and a time that long would probably result in a too long without yielding error if you don't yield in the middle.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.