add char to variable on condition

Posted by Dagaswolf on Thu 26 Feb 2009 03:10 AM — 11 posts, 43,786 views.

USA #0
hey there all...


this is what i am trying to do...
i have the following code...

tsth = tonumber(tsth)
tstm = tonumber(tstm)
tsts = tonumber(tsts)
teth = tonumber(teth)
tetm = tonumber(tetm)
tets = tonumber(tets)
	
local tch = (teth - tsth)
local tcm = (tetm - tstm)
local tcs = (tets - tsts)


what i am trying to do is calculate time... how long something takes basically...

**note** h=hour, m=minute, s=second **end note**

i want to have the output to look something like this:
00:00:00

and i know to output what i want i gotta do:

world.Send("echo "..tch..":"..tcm..":"..tcs)


the problem that i am finding is that when i get ONLY a 0-9 value in any of the variables, it outputs "0:0:0"

how do i do a check like:

if (string-length(tch) == 1) then
    tch = ([add-to-the-start-of-var]0 + tch)
end


any and all help is appreciated.
USA #1
string.format is what you're looking for.


print(  string.format( "%02d", 5) )  -->  05

%d is a number
%2d is two places for that number
%02d is pad with zeros if the number doesn't fill 2 places.

More information is in the help or
http://www.gammon.com.au/scripts/doc.php?lua=string.format


USA #2
AWESOME!!!

that worked wonderfully!


might i ask another question?

i am now running into getting "-" values in my time.
how do i compensate for calculating time? like when it says 17 for the seconds in the start time, and 14 for the seconds in the end time... i get a value set of -3 seconds...

you know of any way to compensate for that?
USA #3
have you thought about something like:


tsth = tonumber(tsth)
tstm = tonumber(tstm)
tsts = tonumber(tsts)
teth = tonumber(teth)
tetm = tonumber(tetm)
tets = tonumber(tets)
starttime= tsth * 3600  + tstm * 60 + tsts
endtime  = teth * 3600  + tetm * 60 + tets

elapsedtime = endtime - start time
elh= math.floor(elapsedtime/3600)
elm= math.floor(elapsedtime - elh*3600)
els= elapsedtime - (elh*3600) - (elm*60)

print ( string.format("Time Taken: %0d:%02d:%02d", elh, elm, els) )



USA #4
i think i see where your going with that...

but here is the output from that with the following times:

start time: 23:45:43
end time: 00:02:15

Time Taken: -24:992:-58528

the output don't make any sense to me ???

the output should say something like:
00:17:32
i think that my calculation is right for the time... yes?
Australia Forum Administrator #5
No, if it wraps around midnight. You might check that if the number is negative, add 24 hours to it.
USA #6
ok... understand about the 24hour thing... but this is what i am getting now...

start time: 00:38:32
end time: 00:39:05

using the scripting that was given earlier:

local starttime = (tsth * 3600  + tstm * 60 + tsts)
local endtime = (teth * 3600  + tetm * 60 + tets)

local elapsedtime = (endtime - starttime)
local elh = math.floor(elapsedtime/3600)
local elm = math.floor(elapsedtime - elh*3600)
local els = (elapsedtime - (elh*3600) - (elm*60))

print ( string.format("Time Taken: %0d:%02d:%02d", elh, elm, els) )


this is the output that i am getting...
Time Taken: 0:33:-1947


shouldn't the "Time Taken:" show 00:00:35???

USA #7
ok breaking it down... this is what i get for calculations.

local starttime = (tsth * 3600  + tstm * 60 + tsts)
local endtime = (teth * 3600  + tetm * 60 + tets)

local elapsedtime = (endtime - starttime)
local elh = math.floor(elapsedtime/3600)
local elm = math.floor(elapsedtime - elh*3600)
local els = (elapsedtime - (elh*3600) - (elm*60))

print ( string.format("Time Taken: %0d:%02d:%02d", elh, elm, els) )


starttime = ((0*3600) + (38*60) + 32) which = 2312
endtime = ((0*3600) + (39*60) + 5) which = 2345

elapsedtime = (2345 - 2312) which = 33

elh = 33/0 which = 0
elm = (33 - (0*60) = 0
els = (33 - (0*3600) - (0*60) = 33

so when i do the print i should get:
"Time Taken: 00:00:33"

is that not correct? but the output is: "Time Taken: 0:33:-1947"

Am i missing something???
Australia Forum Administrator #8
Try this:


tsth = 23
tstm = 45
tsts = 43

teth = 0
tetm = 02
tets = 15


local starttime = tsth * 3600  + tstm * 60 + tsts
local endtime = teth * 3600  + tetm * 60 + tets

local elapsedtime = endtime - starttime

if elapsedtime < 0 then
  elapsedtime = elapsedtime + 24 * 60 * 60
end -- if midnight wrap

local elh = math.floor (elapsedtime/3600)  -- hours
elapsedtime = elapsedtime - elh * 3600 -- remove hours
local elm = math.floor (elapsedtime/60)  -- minutes
elapsedtime = elapsedtime - elm * 60 -- remove minutes
local els = elapsedtime   -- seconds

print ( string.format("Time Taken: %0d:%02d:%02d", elh, elm, els) )


Testing on the test fields gave me:


Time Taken: 0:16:32


... which looks about right.
USA #9
I was just about to reply that I forgot to divide by 60 in elm...

USA #10
AWESOME!!!! that worked...

thank you so much for your help!!! i really do appreciate it.

i am still really new to this scripting stuff. and that is why i am trying to do as much of it as i can instead of using others scripts. i want to learn

thanks again to both of you!