Add timer to cookbook
Case number: | 845813-992136 |
Topic: | Game: Tools |
Opened by: | salish99 |
Status: | Closed |
Type: | Suggestion |
Opened on: | Thursday, March 15, 2012 - 21:31 |
Last modified: | Wednesday, March 28, 2012 - 20:53 |
Sometimes, I donot know how long a cookbook recipe will run.
It would be nice to have a timer: "This will run for hh:mm" or "This will run until 3am".
This, of course, may vary on input paramaters and processing power, so a live updating timer would be nice.
Without this, once a recipe runs, I cannot see if it will terminate the next minute or in a week.
So, if I have a recipe not programmed in V2 script, it cannot be done.
If it is a V2 script, what do I add to it?
Just os.time
or should there be some more coding around it?
Thanks
Here is some code that I have added to V2 LUA recipes:
function report_time(start_clock,start_time,clock_msg,time_msg)
local seconds,minutes,hours,days
if clock_msg==nil then clock_msg="CPU time" end
if time_msg==nil then time_msg="Elapsed time" end
print(string.format("%s",os.date()))
days,remainder=math.modf((os.clock()-start_clock)/(24*60*60))
hours,remainder=math.modf(remainder*24)
minutes,remainder=math.modf(remainder*60)
seconds,remainder=math.modf(remainder*60)
print(string.format("%s(%02id:%02ih:%02im:%02is)",clock_msg,days,hours,minutes,seconds))
days,remainder=math.modf(os.difftime(os.time(),start_time)/(24*60*60))
hours,remainder=math.modf(remainder*24)
minutes,remainder=math.modf(remainder*60)
seconds,remainder=math.modf(remainder*60)
print(string.format("%s(%02id:%02ih:%02im:%02is)",time_msg,days,hours,minutes,seconds))
end
start_clock=os.clock()
start_time=os.time()
.... recipe code
then add
report_time(start_clock,start_time)
where you want the time to be printed.
This will print out like:
03/16/12 09:04:13
CPU time(00d:01h:19m:20s)
Elapsed time(00d:01h:19m:20s)
Note: in Windows the CPU time and Elapsed time are the same. I don't know if linux or OSX would display different values.
These are really cool ideas, and I'll add them to my scripts for sure. You can trick Lua into doing the math, as long as your script finishes in less than a year. :)
function difftime(start, finish)
if start > finish then start,finish = finish,start end
local t = os.date('!*t',finish-start) -- seconds since Jan 1 1970 UTC
return t.sec, t.min, t.hour, t.yday-1
end
d = 1 + 60 * 2+ 60*60 * 3 + 60*60*24 * 4 -- 1 sec, 2 min, 3 hr, 4 day
sec,min,hour,day = difftime(0,d)
print(string.format('Days: %d, Hours: %d, Minutes: %d, Seconds: %d',day,hour,min,sec))
Nice programming, thanks.
Status: Open » Closed |
As this can be covered by the lua libraries, we'll let you all use those.
It can be done in lua v2 script.
Proper use of os.time can give you this ability :)