Re: elapsedTime - handler for returning time strings
Re: elapsedTime - handler for returning time strings
- Subject: Re: elapsedTime - handler for returning time strings
- From: Nigel Garvey <email@hidden>
- Date: Mon, 7 Jan 2002 16:44:02 +0000
Richard Morton wrote on Mon, 7 Jan 2002 10:39:52 +1100:
>
-- elapsedTime -- Richard Morton 2001-2002 --
>
-- pass a value in the range 0-8639999 or 0.0-8.639999E+6
>
-- and a separator character; returns string
>
on elapsedTime from secondTime against s
>
tell secondTime to ,
>
if it < 3600 then -- an hour
>
set iRes to "" -- to return empty hour string set to -> "00" & s
>
set mFac to it
>
else if it < 86400 then -- a day
>
set iRes to (text -2 thru -1 of ("0" & (it div 3600))) & s -- hours
>
set mFac to it mod 3600
>
else -- a long time
>
set iRes to (text -2 thru -1 of ("0" & (it div 86400))) &
>
s & [Option-L]
>
(text -2 thru -1 of ("0" & ((it mod 86400) div
>
3600))) & s -- days & hours
>
set mFac to (it mod 86400) mod 3600
>
end if
>
return iRes & (text -2 thru -1 of ("0" & (mFac div 60))) & s &
>
[Option-L]
>
(text -2 thru -1 of ("0" & ((mFac mod 60) as integer))) --
>
minutes & seconds
>
end elapsedTime
>
-- -- --
>
>
Hopefully Nigel will let us know if it's not fast enough...
Harrummphh! :-)
Not even I would bother with the miniscule speed advantage to be gained
from using integers instead of 'hours' and 'days'! However, I might be
tempted to reverse the flow of the handler so as not to have to bother
with the variables 'iRes' and 'fMac', and to use a slightly faster
leading-zero method that I've just invented in your honour:
on elapsedTime from secondTime against s
tell secondTime
text -2 thru -1 of ((100 + it mod hours div minutes) as string) & s
& [Option-L]
text -2 thru -1 of ((100 + it mod minutes as integer) as string)
if it is not less than days then
text -2 thru -1 of ((100 + it div days) as string) & s &
[Option-L]
text -2 thru -1 of ((100 + it mod days div hours) as string) &
s & result
else if it is not less than hours then
text -2 thru -1 of ((100 + it div hours) as string) & s & result
else
result
end if
end tell
end elapsedTime
The surgery needed to adapt this to produce an 'hours' figure even when
that's zero is a little more profound:
on elapsedTime from secondTime against s
tell secondTime
text -2 thru -1 of ((100 + it mod days div hours) as string) & s &
[Option-L]
text -2 thru -1 of ((100 + it mod hours div minutes) as string) &
s & [Option-L]
text -2 thru -1 of ((100 + it mod minutes as integer) as string)
if it is not less than days then
text -2 thru -1 of ((100 + it div hours) as string) & s & result
else
result
end if
end tell
end elapsedTime
NG