Human Readable Time snipit
Human Readable Time snipit
- Subject: Human Readable Time snipit
- From: "David A. Cox" <email@hidden>
- Date: Fri, 6 Oct 2006 13:10:15 -0700
I have had reason the last few days to want to do some countdown
timer stuff with applescript, and I was frustrated by not being able
to find a way to make a number of seconds more humanly readable
(minutes, hours, days). So I smacked together a little bit of code
that worked for what I was doing, and I thought I would post it out
here in case it is of use to anyone. Greater code writers than I
probably have more efficient samples, and I will not be offended by
seeing them :). Anyway, here is what I am using for the small project
I needed:
--Written by David A. Cox -- email@hidden --10.6.2006
(*
You should have a property in your script to make the final time
format available for use in other sections. This can be done with a
line like:
property timetext : ""
Then you can just call the handler below, giving it some number of
seconds. It will then convert that number of seconds into something a
bit easier to read.
*)
on HumanReadableTime(temperalpassage)
--first we clear out the negative time marker, assuming a positive
number of secons is provided
set NegativeTimeMarker to ""
--If a negative number of seconds is provided, we need to convert it
to a positive number, so our math works, and we set the negative
indicator to something we can use.
if temperalpassage < 0 then
set temperalpassage to temperalpassage * (-1)
set NegativeTimeMarker to "-"
end if
--Now we figure out how many minutes, hours, and days we have in our
number
set min_of_temperalpassage to temperalpassage div 60
set hour_of_temperalpassage to min_of_temperalpassage div 60
set day_of_temperalpassage to hour_of_temperalpassage div 24
--This section creates new variables for the hours, minutes, and
seconds that are as we expect (ie we can not have 61 minutes, as that
is more than an hour).
set MOD_hour_of_temperalpassage to hour_of_temperalpassage mod 24
set MOD_min_of_temperalpassage to min_of_temperalpassage mod 60
set MOD_sec_of_temperalpassage to temperalpassage mod 60
--This section adds a leading "0" to the seconds and minutes to make
them look more clock like
if MOD_sec_of_temperalpassage < 10 then
set MOD_sec_of_temperalpassageTEXT to "0" &
(MOD_sec_of_temperalpassage as string)
else
set MOD_sec_of_temperalpassageTEXT to MOD_sec_of_temperalpassage as
string
end if
if MOD_min_of_temperalpassage < 10 then
set Mod_min_of_temperalpassageTEXT to "0" &
(MOD_min_of_temperalpassage as string)
else
set Mod_min_of_temperalpassageTEXT to MOD_min_of_temperalpassage as
string
end if
if MOD_hour_of_temperalpassage < 10 then
set MOD_hour_of_temperalpassageTEXT to "0" &
(MOD_hour_of_temperalpassage as string)
else
set MOD_hour_of_temperalpassageTEXT to MOD_hour_of_temperalpassage
as string
end if
set timetext to NegativeTimeMarker & " " & day_of_temperalpassage &
"d " & MOD_hour_of_temperalpassageTEXT & ":" &
Mod_min_of_temperalpassageTEXT & ":" & MOD_sec_of_temperalpassageTEXT
as text
end HumanReadableTime
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden