Richard Lee wrote:
What is the best way to indicate past hour from current time without
using a module?
so if it's 12:00, then 11:00-12:00
if it's 17:30 then 16:30 to 17:30
if it's 00:30 then 23:30 to 00:30
I wrote a below program and works but there has to be a better and
easier(not sure if I want easier as I am trying to learn) but I want to
do it the right perl way(despite
perl's motto)..
let me know please.
#!/usr/bin/perl -w
use strict;
use diagnostics;
my $time = localtime;
my @time_1 = split / /, $time;
my $start_time;
for ($time_1[4]) {
if ($_ =~ /00:([0-5][0-9]:[^ ])/) {
$start_time = join(':', 23,$1);
} else {
/(.*):(.*):(.*)/;
$start_time = join(':', $1 - 1, $2,$3);
}
}
print "$start_time to $time \n";
my $seconds_per_hour = 3600;
print scalar localtime, "\n"; # now
print scalar localtime time - $seconds_per_hour, "\n"; # one hour ago
Or if you just want to print the time only:
my $seconds_per_hour = 3600;
my ( $second, $minute, $hour ) = localtime;
printf "%02d:%02d:%02d\n", $hour, $minute, $second; # now
my ( $second, $minute, $hour ) = localtime time - $seconds_per_hour;
printf "%02d:%02d:%02d\n", $hour, $minute, $second; # one hour ago
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/