PDA

View Full Version : seconds to minutes ..?


reteset
01-21-2008, 05:52 AM
hi
i have a problem

for example:
seconds = 186;
186 secons = 3 minutes and 6 seconds
how can i convert 186 to 03:06 with a simplest way

because i am using a timer
this routine will be called every 50 milliseconds
and my solutions are using %10 - %15 of my 2.5 CPU

i need a simplest solution that reduce CPU usage

this is a media player project
it calculates length and current position when playing an MP3 file
thanks

RizlaUK
01-21-2008, 06:06 AM
Here, try this function

place this in globals
------------------------------------------------------------
-- this function will Convert seconds into minutes/seconds
function sec2Min(secs)
if secs > 59 then
myMinutes = Math.Floor(secs/60);
mySeconds = secs-(Math.Floor(secs/60)*60);
if mySeconds < 10 then
mySeconds = "0"..mySeconds;
end
myTime = myMinutes..":"..mySeconds;
else
if secs < 10 then
mySeconds = "0"..secs;
end
myTime = "0:"..mySeconds;
end
return myTime;
end
------------------------------------------------------------


-- Example Use

Dialog.Message("Time:", "186 Seconds = "..sec2Min(186));

Worm
01-21-2008, 07:14 AM
You could try the DateDiff plugin. It formats seconds into HH:MM:SS

http://www.indigorose.com/forums/showpost.php?p=63018&postcount=17

reteset
01-21-2008, 07:45 AM
thank a lot

i.m using BASS (http://un4seen.com/bass.html) sound system
i made an action plugin for my self. this plugin calls functions from bass.dll

actually my problem is not conversion
also i can create DLLs Action plugins for myself
but i want to do it interlally
i.m using
Page.StartTimer(50);
and my internal procedures using %10 - %15 of CPU
when i am using external routine (dll ,lmd) also
On Timer:
sec2Min(186);

finally, i am looking for simplest calculation of this

RizlaUK thanks i will try it
your method is different from my methods

Bags
01-21-2008, 09:44 AM
Just want to make a code change here for performance. I know it isn't much probably about 4 cycles or so if I remember right from my old Assembler days...

All I did was used the already calculated value of secs/60 in myMinutes in place of doing all the math again. no point in using up more cycles to do that math when it is already completed and stored. So just use the variable.

I should also note that you should use local variables when ever possible. Lua by default makes all variables global, and global variables have a slower access time then local variables. It also helps in garbage collection as well. If the variables are not used by any other function, or anywhere else when created then make them local to the current working scope for speed and memory usage reasons.

------------------------------------------------------------
-- this function will Convert seconds into minutes/seconds
function sec2Min(secs)

local myMinutes = 0;
local mySeconds = 0;
local myTime =0;

if secs > 59 then
myMinutes = Math.Floor(secs/60);
mySeconds = secs-(myMinutes*60); -- Changes here.
if mySeconds < 10 then
mySeconds = "0"..mySeconds;
end
myTime = myMinutes..":"..mySeconds;
else
if secs < 10 then
mySeconds = "0"..secs;
end
myTime = "0:"..mySeconds;
end
return myTime;
end
------------------------------------------------------------

clueless
01-21-2008, 11:16 PM
This could be done in a single line of code..
-Divide the seconds by 60 and convert to an integer (this is the mins).
-Multiply the integer by 60 and Subtract from the orig numb (this is the secs).

or:
RealTime = ConvertToInt(OrigNumb/60)..":"..OrigNumb-ConvertToInt(OrigNumb/60);

But there is no Integer command in AMS so you would need to add your own conversion function to your globals;


function ConvertToInt(Anumb)
if String.Find(Anumb.."", ".", 1, false) > 1 then
Anumb = String.ToNumber(String.Left(Anumb.."" , String.Find(Anumb.."", ".", 1, false)-1));
end
return Anumb
end

reteset
01-23-2008, 04:41 AM
thanks to all.
i will increase timer interval
like : Page.StartTimer(90);