Round off to the quarter hour

I am experimenting with a time tracking script. I want to record the current time and then click a button and have it subtract the current time from the start time to get the amout of time worked. I did a search here and couldn’t find if anyone had come up with a simple solution to round off this number to the nearest quarter hour. I would always round up so hopefully it would be easy. I can think of a way with multiple “if theTime is greather than 0 but less than .25 then set theTime to 0.25” kinds of statements but I wondered if anyone already had some cool code to do this efficently.

I would post my script but really I am in the early planning stages so I don’t have much to post…

Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Alright. I have some code now. I got this far but I already got stuck:

set StartTime to (do shell script "date +%H%M")
with timeout of 9999 seconds
	display dialog "Click OK when finished task.")
end timeout
set EndTime to (do shell script "date +%H%M")
set FinalTime to (EndTime - StartTime)

Using the shell script already rounds off to the minute so I don’t have to worry about seconds. So, if you started at 8:30, the start time would be 0830, and then ended at 11:40 (1140), the FinalTime would be 310 by the calculation. What I want is 1) it to round up to the quarter hour, and 2) end up with a decimal value of hours. In this example, I want to end up with 3.25.

Has anyone figured this type of thing out already before I spend a ton of time coming up with a formula…

Browser: Safari 419.3
Operating System: Mac OS X (10.4)

If you use shell time as you have, you have to do some parsing to untangle hours and minutes, since the time difference in your example is not 310 minutes - you need a common unit, not a combined one.

Having said that, why not use StopWatch which is in ScriptBuilders.

something like this

set StartTime to (current date)
with timeout of 9999 seconds
	display dialog "Click OK when finished task."
end timeout
set EndTime to (current date)
tell (EndTime - StartTime) to set {hr, mn} to {it div 3600, (it mod 3600) / 60}
set mn to ((round mn / 15 rounding up) * 15) / 60
set FinalTime to hr + mn
set hoursWorked to ((current date) - startTime) / hours
if (hoursWorked mod 0.25 > 0.0) then set hoursWorked to (hoursWorked + 0.25) div 0.25 * 0.25

Cool. That’s just what I was looking for. I don’t know how long it would have taken me to get there.

Thanks a lot.

Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Doh! What was I thinking of? :rolleyes:

set hoursWorked to ((current date) - StartTime + 899) div 900 * 900 / hours

When I started to answer this post yesterday, my thought was: “No matter how I do this (similar to your first go), Nigel will come in with one that blows me a way with arcane numbers”. Thanks for living up to expectations - your second post redeemed you. :smiley:

pi / ((date "Monday 1 January 2001 00:00:00") - (get date "Tuesday 1 January 1901 00:00:00"))
--> 9.95510638828616E-10

pi / ((date "Saturday 1 January 2101 00:00:00") - (get date "Monday 1 January 2001 00:00:00"))
--> 9.95537895170715E-10

((220 as yards as centimeters as number) / (2 * weeks)) * minutes
--> 0.997857142857

Hmmm. Close! :slight_smile:

Those are the only two I recall. When I was learning to search in Fortran II approximately an eon ago, I wrote a program to search for unity relationships like that for a bunch of common constants.:lol:

An beguiling thought: some computer the size of a house taking a week of university time to work out each relationship. No wonder there’s global warming! :lol:

By the way, “Doh!” again:

set hoursWorked to ((current date) - StartTime + 899) div 900 / 4

900 seconds is, of course, a quarter of an hour. Divving by that gives the number of complete quarter hours in the time being divved. Adding 900 before the div would always increase the result by 1 (quarter of an hour), so it would always be rounded up rather than down. Adding 899 instead prevents exact multiples of 900 from being rounded up.

If it were desired not to credit an extra quarter-of-an-hour’s work when the button was clicked a mere second or so after a quarter-hour boundary, the addition amount could be reduced even further. An addition of 450 would round the result to the “nearest” quarter of an hour using the “.5 rounds up” convention.

It belonged to the Department, and was only the size of (and roughly the shape of) a large home freezer, or a washer and dryer side by side :o. Of course that doesn’t count the card punches, card sorter, Card reader, and a huge chain printer, all on a raised floor in an air conditioned room ;). And, btw, I had free use of it from 3 AM until 6 AM 3 nights a week. :frowning: