Something like this should work.  It takes the hour as an optional argument,
but defaults to the current hour if no parameter is supplied.  The optional
argument makes it easier to test and makes it possible to use an hour of a
different timezone.

print last_hour(), "\n";   # current hour minus 1
print last_hour(0), "\n";  # prints 23
print last_hour(1), "\n";  # prints 00
print last_hour(5), "\n";  # prints 04
print last_hour(15), "\n"; # prints 14
print last_hour(23), "\n"; # prints 22

sub last_hour {
  my $hour = shift;
  $hour = (localtime)[2] unless defined $hour;
  $hour--; #adjust hour to previous
  $hour += 24 if $hour < 0;
  return sprintf("%02d", $hour);
}


Rob

-----Original Message-----
From: Kevin Old [mailto:[EMAIL PROTECTED]]
Sent: Monday, July 29, 2002 11:33 AM
To: [EMAIL PROTECTED]
Subject: Calculate previous hour?


Hello all,

Well, I've got a very simple problem and can't seem to get the answer I
want.

Basically I just want to find the previous hour in one of my scripts.

This code does it, but I need to maintain the format "09".

 $hour = (localtime)[2];
 $hour--; #adjust hour to previous

I know about using printf, but I don't need to print this value.  I need
to use it as a variable.

I've even tried stuff like this: 

$prevhour = &DateCalc("today","- 1hour","%H");
print "prev hour: $prevhour\n";

but it gives me the whole date.


It seems to me that to achieve something so simple wouldnt' be so
difficult.

Any ideas?

Thanks,
Kevin

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to