Page 1 of 1

Adding two 'times' togehter

Posted: Fri May 28, 2021 2:11 pm
by rado
Hi,

i have two variables who represent duration of a song.
The one is for example: "00:03:06" and the other one: "00:02:15" and i am trying to add them together.
I have converted them both into "long time" and other formats and tried stuff like:

timeOne + timeTwo
put timeOne+timeTwo into ...
put sum(timeOne,timeTwo) into ....
add timeOne to timeTwo

but nothing seems to work for me... can anyone please help?


Rado

Re: Adding two 'times' togehter

Posted: Fri May 28, 2021 2:23 pm
by Klaus
Hi rado,

we had a similar problem recently:
viewtopic.php?f=7&t=35882

Try this, tested and works:

Code: Select all

on mouseUp
   put "00:03:25" into tDate1
   put "00:02:15" into tDate2
   
   ## We need to subtract the whole day from the seconds or we will get something like: 
   ## 81289:05:40 instead of the desired result: 00:05:40
   put the date into tDatum
   convert tDatum to seconds
   convert tDate1 to seconds
   convert tDate2 to seconds
   
   ## remove all days etc. and just leave HH:MM:SS
   subtract tDatum from tDate1
   subtract tDatum from tDate2
   
   ## Now do the math:
   put tDate1 + tDate2 into tDate3
   
   ## Format as desired as SMPTE-lite:
   put smpt_lite(tDate3) 
end mouseUp

function smpt_lite tSecs
   return format("%02d:%02d:%02d", tSecs div 3600, (tSecs mod 3600) div 60, tSecs mod 60)
end smpt_lite
You can also turn this into a function where you pass just tDate1 and tDate2 and it returns the sum of both times.
Drop a line if you need help with this!


Best

Klaus

Re: Adding two 'times' togehter

Posted: Fri May 28, 2021 2:29 pm
by dunbarx
Hi.

Fun, fun.

The "long time" is not really appropriate here. You have to deconstruct the string yourself. With the two times you posted in a field 1, and this In a button script:

Code: Select all

on mouseUp
   answer addSongLength(fld 1)
end mouseUp

function addSongLength var
   set the itemDel to ":"
   add item 3 of  line 1 of var + item 3 of line 2 of var to totSeconds
   add (item 2 of  line 1 of var + item 2 of line 2 of var) * 60 to totSeconds
   add (item 1 of  line 1 of var + item 1 of line 2 of var) * 3600  to totSeconds
   return totSeconds 
end addSongLength
I get 321 seconds.

Craig

Re: Adding two 'times' togehter

Posted: Fri May 28, 2021 2:37 pm
by Klaus
Hi Craig,
dunbarx wrote:
Fri May 28, 2021 2:29 pm
...
I get 321 seconds.
which resolves to 00:05:21 but the correct result is 00:05:40.


Best

Klaus

Re: Adding two 'times' togehter

Posted: Fri May 28, 2021 2:46 pm
by dunbarx
@Klaus.

What do you mean by "00:05:40"? That is not 321 seconds.

@ Rado.

Once we figure that out, do you need help to change to the "xx:yy:zz" format?

Craig

Re: Adding two 'times' togehter

Posted: Fri May 28, 2021 2:47 pm
by dunbarx
Klaus.

I see, you used a slightly different time than I did.

I used what the OP posted 8)

Craig

Re: Adding two 'times' togehter

Posted: Fri May 28, 2021 2:54 pm
by Klaus
Hi Craig,
dunbarx wrote:
Fri May 28, 2021 2:46 pm
@Klaus.

What do you mean by "00:05:40"? That is not 321 seconds.
...
even with my poor calculation skills I can see at a glance that this:
00:03:25 + 00:02:15 = 00:05:40, which resolves to 340 seconds.
Are we really using identical times?


Best

Klaus

Re: Adding two 'times' togehter

Posted: Fri May 28, 2021 2:55 pm
by Klaus
AHA!
Sorry, I wear glasses... 8)

Re: Adding two 'times' togehter

Posted: Tue Jun 01, 2021 8:47 am
by rado
Thank you very much guys!

It works perfect with the code form Klaus.


Best regards,
Rado