L
Lax
Hi all,
Could someone please critic my solution to a date/time rounding off
solution?
It works, but I think its really ugly and maybe Date::Manip itself has
some useful routine to do this. I havent been able to find one.
Issue: Given current time, I'd like to find the ten-minute interval
it'd be in.
e.g, if my current time is: "20060206 16:35:43", I'd like the upper and
lower ends of its interval, like "Upper: 20060206 16:40:00" and "Lower:
20060206 16:30:00"
This is what I've come up with, but was wondering if there are more
elegant soultions?
1. I get the current time-stamp from Date::Manip's ParseDate.
2. I convert the result into a number by removing the semi-colons ":"
3. To get the lower limit, I substitute the last three digits with
zeroes.
4. To get upper limit, I substitute the last three digits with zeroes
and add one to the "fourth from last" digit.
====================================================
#!/usr/local/bin/perl
use strict ;
use warnings ;
use Date::Manip ;
my $date = ParseDate("today") ;
$date =~ s/\://g ;
print "Orig : $date\n" ; # Original time stamp, e.g, 20060206163543
my @arr = split(//,$date) ;
# Make last three digits zeroes.
for ( my $i = 0 ; $i <= 2 ; $i++ )
{
$arr[$#arr - $i] = 0 ;
}
my $lower = join("",@arr) ;
print "Lower: $lower\n" ; # 20060206163000
# Add one to the last but fourth.
$arr[$#arr - 3]++ ;
my $upper = join("",@arr) ;
print "Upper: $upper\n" ; # 20060206164000
Could someone please critic my solution to a date/time rounding off
solution?
It works, but I think its really ugly and maybe Date::Manip itself has
some useful routine to do this. I havent been able to find one.
Issue: Given current time, I'd like to find the ten-minute interval
it'd be in.
e.g, if my current time is: "20060206 16:35:43", I'd like the upper and
lower ends of its interval, like "Upper: 20060206 16:40:00" and "Lower:
20060206 16:30:00"
This is what I've come up with, but was wondering if there are more
elegant soultions?
1. I get the current time-stamp from Date::Manip's ParseDate.
2. I convert the result into a number by removing the semi-colons ":"
3. To get the lower limit, I substitute the last three digits with
zeroes.
4. To get upper limit, I substitute the last three digits with zeroes
and add one to the "fourth from last" digit.
====================================================
#!/usr/local/bin/perl
use strict ;
use warnings ;
use Date::Manip ;
my $date = ParseDate("today") ;
$date =~ s/\://g ;
print "Orig : $date\n" ; # Original time stamp, e.g, 20060206163543
my @arr = split(//,$date) ;
# Make last three digits zeroes.
for ( my $i = 0 ; $i <= 2 ; $i++ )
{
$arr[$#arr - $i] = 0 ;
}
my $lower = join("",@arr) ;
print "Lower: $lower\n" ; # 20060206163000
# Add one to the last but fourth.
$arr[$#arr - 3]++ ;
my $upper = join("",@arr) ;
print "Upper: $upper\n" ; # 20060206164000