Re: Human Readable Time snipit
Re: Human Readable Time snipit
- Subject: Re: Human Readable Time snipit
- From: Nir Soffer <email@hidden>
- Date: Thu, 12 Oct 2006 00:51:56 +0200
Here is a shorter, simpler and more readable version:
on daysHoursMinutesSeconds(theSeconds)
set secondsPerDay to 60 * 60 * 24
set {theDays, theRest} to divmod(theSeconds, secondsPerDay)
set secondsPerHour to 60 * 60
set {theHours, theRest} to divmod(theRest, secondsPerHour)
set secondsPerMinute to 60
set {theMinutes, theRest} to divmod(theRest, secondsPerMinute)
return theDays & ":" & pad(theHours) & ":" & pad(theMinutes) & ":" &
pad(theRest) as string
end daysHoursMinutesSeconds
on divmod(a, b)
set division to a div b
set theRest to a mod b
return {division, theRest}
end divmod
on pad(aNumber)
set aString to aNumber as string
if length of aString < 2 then return "0" & aString
return aString
end pad
Negative time values does not make much sense for me. I guess you
display either time passed, or time left, in both cases the time
value is not negative.
On Oct 6, 2006, at 10:10 PM, David A. Cox wrote:
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 (Applescript-
email@hidden)
Help/Unsubscribe/Update your Subscription:
40freeshell.org
This email sent to email@hidden
Best Regards,
Nir Soffer
_______________________________________________
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