Bdfy Bdfy - 1 year ago 75
Perl Question

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

I want to round current time to the nearest

minute interval.
So if it is currently
, it would read
as the start time.

How can I do that?

Answer Source

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 = 0 if($hr == 24); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download