On May 2, 9:46 am, lm7...@gmail.com (Matt) wrote:
> Have a date:
>
> 2011-05-02-16:40:51
>
> Using this to get it:
>
> $tm = gmtime;
> $time_stamp = sprintf "%04d-%02d-%02d-%02d:%02d:%02d",
>  $tm->year + 1900, $tm->mon + 1, $tm->mday, $tm->hour, $tm->min, $tm->sec;
> print "$time_stamp\n";
>
> I need to round it to nearest 5 minute point.
>
> 2011-05-02-16:40:51
>
> needs rounded like so.
>
> 2011-05-02-16:00:00
> 2011-05-02-16:45:00
> 2011-05-02-16:50:00
> 2011-05-02-16:55:00
>
> My thought is a bunch of if statements but that seems ugly.  Is there
> a better/easier way?

Yet another way using the core module List::Util:

use Time::localtime;
use List::Util qw/reduce/;

my @rounded_5mins = grep { not $_ % 5 } 0..60;
my $curr_min = localtime->min;

my $round_min =  reduce { $curr_min-$a < $b-$curr_min ? $a : $b }
                               @rounded_5mins;

etc...

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to