Bdfy Bdfy - 5 months ago 14
Perl Question

How can I round a date to nearest 15 minute interval in Perl?

I want to round current time to the nearest

15
minute interval.
So if it is currently
6:07
, it would read
6:15
as the start time.

How can I do that?

Answer

You can split the time into hours and minutes and then use the ceil function as:

use POSIX;

my ($hr,$min) = split/:/,$time;    
my $rounded_min = ceil($min/15) * 15;

if($rounded_min == 60) {
   $rounded_min = 0;
   $hr++;
   $hr = 0 if($hr == 24); 
}