Adding two 'times' togehter

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
rado
Posts: 5
Joined: Tue May 25, 2021 9:33 am

Adding two 'times' togehter

Post by rado » Fri May 28, 2021 2:11 pm

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
Attachments
timeProblem.png

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Adding two 'times' togehter

Post by Klaus » Fri May 28, 2021 2:23 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Adding two 'times' togehter

Post by dunbarx » Fri May 28, 2021 2:29 pm

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

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Adding two 'times' togehter

Post by Klaus » Fri May 28, 2021 2:37 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Adding two 'times' togehter

Post by dunbarx » Fri May 28, 2021 2:46 pm

@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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Adding two 'times' togehter

Post by dunbarx » Fri May 28, 2021 2:47 pm

Klaus.

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

I used what the OP posted 8)

Craig

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Adding two 'times' togehter

Post by Klaus » Fri May 28, 2021 2:54 pm

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

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Adding two 'times' togehter

Post by Klaus » Fri May 28, 2021 2:55 pm

AHA!
Sorry, I wear glasses... 8)

rado
Posts: 5
Joined: Tue May 25, 2021 9:33 am

Re: Adding two 'times' togehter

Post by rado » Tue Jun 01, 2021 8:47 am

Thank you very much guys!

It works perfect with the code form Klaus.


Best regards,
Rado

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”