Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^4: Round time to nearest 15 or 30 minutes

by bill_mcgonigle (Acolyte)
on May 18, 2010 at 01:04 UTC ( #840434=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Round time to nearest 15 or 30 minutes
in thread Round time to nearest 15 or 30 minutes

Ah, this was very helpful, thanks. Not being too clever in these matters, I didn't quite understand the above, but it got me close enough to experiment successfully (though I don't quite get the second bullet item). In my case I was after an offset to add to a unixtime, so that's how the code is oriented.

for (my $i = 0; $i < 60; $i++) { my $offset = getOffsetToNearestInterval($i,15); print "$i: " . ($i + $offset) . ' (' . $offset . ')' . "\n"; } sub getOffsetToNearestInterval { my $number = shift; my $interval = shift; my $nearest_interval = ( $interval * int( ($number + ($interval/2) + ) / $interval) ); my $offset_to_interval = $nearest_interval - $number; return $offset_to_interval; }
Hope this helps the next guy stumbling on this thread.


Comment on Re^4: Round time to nearest 15 or 30 minutes
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://840434]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-10-01 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (7 votes), past polls