Probably the best way to do this is to deal with the time in seconds -- the UNIX standard of seconds since the UNIX Epoch. If you can get away with storing your time that way, it's probably a lot easier to deal with.
Using functions like time and localtime, you can get the current time in seconds and change it over to a useful format. Using the POSIX module, you can easily format the time. I would suggest running SuperSearch for additional information - the time issue comes up a lot.
An example...
#!/usr/bin/perl -w
use strict;
use POSIX qw(strftime);
my $now = time(); #Gets current time
# Subtract 3 hours, 15 minutes
#(seconds/per minute * minutes/per hour * 3) = 3 hours
# plus
# (seconds/per minute * 15)
my $earlier = $now - ((60 * 60 * 3) + (60 * 15));
# Format the times using POSIX module
$now = strftime "%H:%M", localtime($now);
$earlier = strftime "%H:%M", localtime($earlier);
print "The current time is $now\nThe earlier time is $earlier\n";
=Output
The current time is 15:40
The earlier time is 12:25
If you have to store your time in the HH:MM format, just follow Masem's excellent advice...
Rich36
There's more than one way to screw it up...
|