Page 1 of 2
Converting from ticks or milliseconds...
Posted: Sun Oct 07, 2012 5:11 am
by paulsr
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
Re: Converting from ticks or milliseconds...
Posted: Sun Oct 07, 2012 10:19 am
by bn
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
Re: Converting from ticks or milliseconds...
Posted: Sun Oct 07, 2012 10:33 am
by dave_probertGA6e24
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
Re: Converting from ticks or milliseconds...
Posted: Sun Oct 07, 2012 10:44 am
by paulsr
Thanks Guys...
Need to absorb the code, but they look like good solutions.
--paul
Re: Converting from ticks or milliseconds...
Posted: Sun Oct 07, 2012 11:47 am
by paulsr
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
Re: Converting from ticks or milliseconds...
Posted: Sun Oct 07, 2012 12:11 pm
by Klaus
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
Best
Klaus
Re: Converting from ticks or milliseconds...
Posted: Sun Oct 07, 2012 12:28 pm
by paulsr
Klaus wrote:No, but why convert at all to TIME?
Laziness!
Thanks Klaus, I'll steal yr code, and apply translations
--paul
Re: Converting from ticks or milliseconds...
Posted: Sun Oct 07, 2012 12:29 pm
by bn
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
Re: Converting from ticks or milliseconds...
Posted: Sun Oct 07, 2012 12:31 pm
by Klaus
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
paulsr wrote:Thanks Klaus, I'll steal your code, and apply translations
Yes please
Best
Klaus
Re: Converting from ticks or milliseconds...
Posted: Thu Feb 13, 2014 12:42 pm
by Mag
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...
Re: Converting from ticks or milliseconds...
Posted: Thu Feb 13, 2014 12:58 pm
by paulsr
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
Re: Converting from ticks or milliseconds...
Posted: Thu Feb 13, 2014 7:11 pm
by Mag
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...

Re: Converting from ticks or milliseconds...
Posted: Fri Feb 14, 2014 3:14 am
by paulsr
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...

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
Re: Converting from ticks or milliseconds...
Posted: Sun Feb 16, 2014 9:24 pm
by Mag
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?
Re: Converting from ticks or milliseconds...
Posted: Mon Feb 17, 2014 2:41 am
by paulsr
I haven't tried hundredths Mag, but tenths work fine.
Why not give it a try and let us know?
