Converting from ticks or milliseconds...

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: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Location: Khanom, Nakhon Si Thammarat, Thailand
Contact:

Converting from ticks or milliseconds...

Post by paulsr » Sun Oct 07, 2012 5:11 am

Greetings:

I'm trying to code a digital stopwatch that displays hh:mm:ss.s

i.e. to tenths of a second. So, I need to use either ticks or milliseconds.

If I wanted to work only with seconds, I could easily convert from seconds to hh:mm:ss using something like:

Code: Select all

convert tElapsed from seconds to long time
But the following (using either milliseconds or ticks) gives me a compile error:

Code: Select all

convert tElapsed from milliseconds to long time
So, is there a routine which will handle ticks/milliseconds conversions, or do I need to code this myself?

Many tkx...

--paul

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3361
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Converting from ticks or milliseconds...

Post by bn » Sun Oct 07, 2012 10:19 am

Hi Paul,

this is one way to do it:

Code: Select all

local sRunning = false
local sStart

on mouseUp
   if not sRunning then
      put true into sRunning
      put the seconds into sStart
      set the twelveHourTime to false -- eliminates AM/PM from display
      send timer to me in 0 milliseconds
   else
      put false into sRunning
      set the twelveHourTime to true
   end if
   
end mouseUp

on timer
   if not sRunning then exit timer
   put the seconds  - sStart into tElapsed
   convert tElapsed to long time
   set the itemDelimiter to ":" 
   subtract 1 from char 2 of item 1 of tElapsed -- needed because it always starts at 01:00:00 instead of 00:00:00

   -- char - 3 of the milliseconds is the tenth of a second
   put ":" & char - 3 of the milliseconds after tElapsed
   put tElapsed into field 1
   send timer to me in 100 milliseconds
end timer
Kind regards

Bernd

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Location: Thailand
Contact:

Re: Converting from ticks or milliseconds...

Post by dave_probertGA6e24 » Sun Oct 07, 2012 10:33 am

Along with bn's reply you could also try something like this:

Code: Select all

    put the seconds into tels
    put the milliseconds into telm
    put tels*1000 into tels2
    put telm-tels2 into teld
    convert tels from seconds to long time
    put tels & " : " & teld & cr into fld "foo"
Basically grab both the seconds and the milliseconds at the same time and subtract one from the other to get the millsecs amount.

Basically there is no one line solution (as far as I know) to getting a timer-like output. You need to manipulate numbers to suit your needs.

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Location: Khanom, Nakhon Si Thammarat, Thailand
Contact:

Re: Converting from ticks or milliseconds...

Post by paulsr » Sun Oct 07, 2012 10:44 am

Thanks Guys...

Need to absorb the code, but they look like good solutions.

--paul

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Location: Khanom, Nakhon Si Thammarat, Thailand
Contact:

Re: Converting from ticks or milliseconds...

Post by paulsr » Sun Oct 07, 2012 11:47 am

Hi Bernd,

In your solution you have the line...

Code: Select all

subtract 1 from char 2 of item 1 of tElapsed -- needed because it always starts at 01:00:00 instead of 00:00:00
I think this one hour offset is due to the timezone, because in my case, a time of zero seconds converts to 08:00:00.

Is there some way to persuade "convert" to use the time zone setting so that it converts to 00:00:00 ?

Thanks...

--paul

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

Re: Converting from ticks or milliseconds...

Post by Klaus » Sun Oct 07, 2012 12:11 pm

paulsr wrote:Is there some way to persuade "convert" to use the time zone setting so that it converts to 00:00:00 ?l
No, but why convert at all to TIME?

In an early project I also created a stopwatch and had this script which only used milliseconds:

Code: Select all

...
## !!!
set numberformat to 00

## startmillisecs = the time when the counting started
## I used a "send.. in 100 millisecs" to update the display
## As I said "early project" where I used german names for variables ;-)

  put the millisecs - startmillisecs into aktuellezeit
## current tme
  
  ## Calculate hours
  put aktuellezeit div(60*60*1000) into stunden
## stunden = hours
  
  ## Calculate remainig time - hours
  put aktuellezeit mod(60*60*1000) into restzeit
## restzeit = remaining time
  
  ## Calculate minutes
  put restzeit div (60*1000) into minuten
  
  ## Calculate remainig time - minutes
  put restzeit mod (60*1000) into restzeit
## restzeit = remaining time

  ## Calculate seconds
  put restzeit div(1000) into sekunden

## I only displayed 1/10 seconds, so I dropped the 1/100 and 1/1000
  put char -3 to -2 of aktuellezeit into the10th_secs
...
Now format this for display and you are done:
...
put stunden & ":" & minuten & ":" & sekunden &":" & the10th_secs into time_display
...

No converting and messing around with time zones 8)


Best

Klaus

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Location: Khanom, Nakhon Si Thammarat, Thailand
Contact:

Re: Converting from ticks or milliseconds...

Post by paulsr » Sun Oct 07, 2012 12:28 pm

Klaus wrote:No, but why convert at all to TIME?
Laziness!

Thanks Klaus, I'll steal yr code, and apply translations :D

--paul

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3361
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Converting from ticks or milliseconds...

Post by bn » Sun Oct 07, 2012 12:29 pm

Hi,

I think Klaus's solution is the best. No messing around with UTC, daylight saving etc.

I changed the code he posted a bit and here it is:

Code: Select all

local sRunning = false
local sStart

on mouseUp
   if not sRunning then
      put true into sRunning
      put the milliseconds into sStart
      send timer to me in 0 milliseconds
   else
      put false into sRunning
   end if
end mouseUp

on timer
   if not sRunning then exit timer
   set the numberformat to "00"
   put the millisecs - sStart into tElapsed
   ## current tme
     
     ## Calculate hours
     put tElapsed div(60*60*1000) into tHour
   ## stunden = hours
     
     ## Calculate remainig time - hours
     put tElapsed mod(60*60*1000) into tRemaining
   ## restzeit = remaining time
     
     ## Calculate minutes
     put tRemaining div (60*1000) into tMinutes
     
     ## Calculate remainig time - minutes
     put tRemaining mod (60*1000) into tRemaining
   ## restzeit = remaining time
   
     ## Calculate seconds
     put tRemaining div(1000) into tSeconds
   
   ## I only displayed 1/10 seconds, so I dropped the 1/100 and 1/1000
     put char -3  of tElapsed into the10the_secs
   put tHour & ":" & tMinutes & ":" & tSeconds & ":" & the10the_secs into field 1
   send timer to me in 100 milliseconds
end timer
Kind regards
Bernd

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

Re: Converting from ticks or milliseconds...

Post by Klaus » Sun Oct 07, 2012 12:31 pm

Hi Paul,
paulsr wrote:
Klaus wrote:No, but why convert at all to TIME?
Laziness!
As we experienced here again, Lazyness is no guarantee to do it the shortest way, it only looks like 8)
paulsr wrote:Thanks Klaus, I'll steal your code, and apply translations :D
Yes please :D


Best

Klaus

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Converting from ticks or milliseconds...

Post by Mag » Thu Feb 13, 2014 12:42 pm

Great job guys, but if I wanted to pause and then restart the timer? I guess it would not be easy to find a way...

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Location: Khanom, Nakhon Si Thammarat, Thailand
Contact:

Re: Converting from ticks or milliseconds...

Post by paulsr » Thu Feb 13, 2014 12:58 pm

Mag wrote:Great job guys, but if I wanted to pause and then restart the timer? I guess it would not be easy to find a way...
It's not so hard Mag. It depends what you want the display to do when it's restarted... continue from where the timer was paused, or show actual elapsed time ignoring the amount paused. The latter is easy because you just show current time minus the start time. But if you want the timer to continue from the paused point, then calculate the amount of time it was paused, and add this to the start time.

--paul

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Converting from ticks or milliseconds...

Post by Mag » Thu Feb 13, 2014 7:11 pm

Hi Paul, I would like to create a stopwatch with a stop/start button that continue from where the timer was paused. So I have to calculate the amount of time it was paused, and add this to the start time? Seems not easy... :oops:

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Location: Khanom, Nakhon Si Thammarat, Thailand
Contact:

Re: Converting from ticks or milliseconds...

Post by paulsr » Fri Feb 14, 2014 3:14 am

Mag wrote:Hi Paul, I would like to create a stopwatch with a stop/start button that continue from where the timer was paused. So I have to calculate the amount of time it was paused, and add this to the start time? Seems not easy... :oops:
Mag, if I can do it, anyone can do it :-)

I could post my code, but my timer app does lots more things so I think it would confuse you more than help you. But, here's the rough logic...

When the user starts the timer "put the ticks into tStart".

(I use ticks because my timer displays tenths of seconds.)

To display the elapsed time "put the ticks into tCurrent" and display tCurrent - tStart. You'll need to convert from elapsed ticks into whatever format you want for your display.

When the user pauses the timer "put the ticks into tPause" and when they restart "put the ticks into tRestart." tRestart - tPause is the amount of time the timer was paused, so just add this to tStart.

You can then continue to display tCurrent - tStart.

Does that help?

--paul

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Converting from ticks or milliseconds...

Post by Mag » Sun Feb 16, 2014 9:24 pm

Hi Paul, very clear explanation. Now I will try! Thank you.

BTW
Based on your experience, it's save to display also hundredths of a second or maybe it's better to just show tenths of a second?

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Location: Khanom, Nakhon Si Thammarat, Thailand
Contact:

Re: Converting from ticks or milliseconds...

Post by paulsr » Mon Feb 17, 2014 2:41 am

I haven't tried hundredths Mag, but tenths work fine.

Why not give it a try and let us know? ;-)

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”