subtract seconds from seconds

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kresten
Posts: 153
Joined: Tue Sep 30, 2008 3:01 pm
Contact:

subtract seconds from seconds

Post by kresten » Thu Jun 02, 2016 2:58 am

Hello
I have been experimenting with this problem of returning the length of time elapsed between one button (H) clicked for going to sleep and another (9) clicked for standing up - and returning the result to a field "Trap5". But the result is only the actual time when button 9 is clicked.

Can somebody detect which errors I commit ?

if item 1 of parameterlist is "H" and item 2 of parameterlist is "Krop" then
put trap into chmode
put the seconds into tobed
put tab & item 1 of parameterlist & tab into trapbox
end if
if item 1 of parameterlist is "9" and item 2 of parameterlist is "Krop" then
put trap into chimed
-- set the standup of this stack to the seconds
put the seconds into standup
-- put (the seconds - the tobed of this stack) into sleeptime
subtract tobed from standup
-- subtract 3600 from sleeptime
put the value of standup into sleeptime
convert sleeptime to short time
if the number of chars of sleeptime = 8 then
put empty into char 6 to 8 of sleeptime
end if
put "Total sleep:" && sleeptime into field "Trap5"
put tab & item 1 of parameterlist & tab into trapbox
set the tobed of this stack to empty
end if

Hopefully
Kresten www.phenomenalog.dk

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10324
Joined: Wed May 06, 2009 2:28 pm

Re: subtract seconds from seconds

Post by dunbarx » Fri Jun 03, 2016 6:40 pm

Hi.

I am not getting what you really need, I guess, because if I place two buttons on a card, and in the first one:

Code: Select all

on mouseup
   set the bedTime of this stack to the seconds
end mouseup
and in the second:

Code: Select all

on mouseUp
   answer the seconds - the bedTime of this stack 
end mouseUp
I get how many seconds between clicks. I would use a single button and toggle its label. But that is just for show.

Craig Newman

kresten
Posts: 153
Joined: Tue Sep 30, 2008 3:01 pm
Contact:

Re: subtract seconds from seconds

Post by kresten » Sat Jun 04, 2016 12:43 am

Thanks,but....the result of the subtraction has to be transformed to an hour/minutes format, and shall arrive in a specified field

I tried

Code: Select all

if item 1 of parameterlist is "H" and item 2 of parameterlist is "Krop" then
      put trap into chmode
      set the tobed of this stack to the seconds
      put tab & item 1 of parameterlist & tab into trapbox
   end if
   if item 1 of parameterlist is "9" and item 2 of parameterlist is "Krop" then
      put trap into chmode
      put the seconds - the tobed of this stack into sleeptime
      convert sleeptime to short time
      if the number of chars of sleeptime = 8 then
         put empty into char 6 to 8 of sleeptime
      end if
      put "Total sleep:" && sleeptime into field "Trap5"
      put tab & item 1 of parameterlist & tab into trapbox
      set the tobed of this stack to empty
   end if
.....but only get "Total sleep:and the actual time" into the field.
Your solution is more or less identical with the original (dysfunctional), except we had the subtraction in a parenthesis. As I explore subtraction in the LC library I found no reference to the "-" character (the one on the key with the underline character). (I always wondered why genuine minus character is missing, when a "+" exists.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10324
Joined: Wed May 06, 2009 2:28 pm

Re: subtract seconds from seconds

Post by dunbarx » Sat Jun 04, 2016 1:15 am

A minus character certainly exists, It is an operator. Check the dictionary.

Remember that LC is a typeless language, A dash can be either text or an operator, depending entirely on context, where it is the task of the parser to figure it out.

To change the value expressed in seconds to hours/minutes/seconds simply requires something like: (pseudo)

Code: Select all

get the secondsDifference
put it div 3600 into numHours
subtract numHours * 3600 from it
put it div 60 into numMinutes
put it - numMinutes * 60 into numSeconds 
Notice the minus sign in the last line. 8)

Craig

Post Reply