On May 3, 4:12 pm, rob.di...@gmx.com (Rob Dixon) wrote:
> On 03/05/2011 19:49, C.DeRykus wrote:
>
>
>
>
>
>
>
>
>
> > 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;
>
> Both this and Uri's earlier suggestion ignore the seconds field. I would
> be surprised if it was acceptable to round 10:02:59
> to 10:00:00 rather than 10:05:00.
>

Depends on how precise the partition counts need to be...
maybe just determining the approx. spread of timestamps
in 5 min. increments suffices.  And of course, if there's
some other criteria, maybe, even a biasing that causes
10:02:30 to fall  into a 10:00:00 rather than a 10:05:00
slot,  won't be acceptable either.

--
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