Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Synchronizing script with real world clock

Synchronizing script with real world clock

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Frazmi   (3 posts)  Bio
Date Sun 12 Oct 2008 02:34 PM (UTC)
Message
I want to display the "real world" time once per minute. I have a simple function that I call using a timer. The function is:
function fn_echotime ()
Note (" RW time is: " .. os.date () )
end

I would like to augment this simple function so that it "holds" until the real world clock hits "00" seconds. So I wrote the following function:

function fn_echotime ()
secs = os.date ("%S")
if secs ~= "00" then
nrsecs = 60 - tonumber(secs)
Note (nrsecs)
require "wait"
wait.make (function ()
Note ("Inside timer")
wait.time (nrsecs)
end)
end

ColourNote ("grey", "darkblue", " RW time is: " .. os.date () .. " ")
end

The two Note functions return expected values. But the timer does not seem to run, as the function never returns :00 as seconds. I'm sure I'm doing something utterly simple to fix, but I'm stumped.

TIA
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 12 Oct 2008 08:52 PM (UTC)

Amended on Mon 13 Oct 2008 05:29 AM (UTC) by Nick Gammon

Message
The problem here is the asynchronous nature of the way wait.time works.

It actually creates a timer, and that timer resumes the coroutine when the time is up. If you change it to:


wait.make (function ()
  Note ("Inside timer")
  wait.time (nrsecs)
  Note ("time's up!")
end)	


... you will see what I mean. The words "time's up" appear at the correct moment, although your function fn_echotime returns immediately.

Thus, replace the line 'Note ("time's up!")' with whatever you want to do when the minute is completed. (eg. Note (" RW time is: " .. os.date () )).

However to achieve your effect of showing the time once a minute you still need to call it once a minute, so perhaps something like this instead:


function fn_echotime ()
  require "wait"
  wait.make (function ()
    while true do  -- loop forever
      local secs = tonumber (os.date ("%S"))
      if secs > 0 then
        wait.time (60 - secs)  -- wait till exact minute
      end -- if
      ColourNote ("grey", "darkblue", " RW time is: " .. os.date () .. " ")
      wait.time (55)  -- wait almost a minute
    end -- while
  end)	-- wait.make function
end  -- function fn_echotime


The version above will echo the time, on the minute, indefinitely (and you only need to call it once). I wait 55 seconds rather than a minute, in case some other processing takes so long that it misses the occasional minute.



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Frazmi   (3 posts)  Bio
Date Reply #2 on Mon 13 Oct 2008 03:46 AM (UTC)
Message
* Slaps forehead with palm of hand.

Thanks!
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


12,008 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.