Shaun Mccran

My digital playground

11
J
U
N
2009

Rounding up to the nearest timed value - Ex every 15 minutes

A watercooler moment threw up an interesting dilemma yesterday. I was talking to a colleague about a scheduled application that would run through a timed loop over the course of a day. It would chop off the top N records from a record set and perform an operation on them. Rather than having to manually run the task every 15 minutes, it would be much better to have it run automatically, at 00,15,30 and 45min markers.

So how to do this?

Initial thoughts wandered through various looping-over type conversations, and creating structures of the timed triggers etc.

First stab coding function looks a little something like this:

view plain print about
1<cf script>
2function RoundUp(input){
3var roundval = 15;
4var result = 0;
5
6if(ArrayLen(arguments) GTE 2)
7roundval = arguments[2];
8if(roundval EQ 0)
9roundval = 1;
10
11if((input MOD roundval) NEQ 0)
12{
13result = input + (roundval - (input MOD roundval));
14}
15else
16{
17result = input;
18}
19return result;
20}
21</cfscript>
22
23<cfset variables.time = RoundUp(#timeformat(NOW(),"mm")#)>
24<cfoutput>variables.time = #variables.time#</cfoutput>

Which successfully returns a rounded up value to the nearest 15 minutes.

It is a bit long winded though and after some more thought and consultation with more team members another solution was reached.

view plain print about
1dateadd("n", 15-(minute(now()) mod 15), now())

Which does exactly the same thing! Maybe not as extensible, you can't exactly build it into a dynamic function, but considerably smaller.

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Back to top