sub get_time {
# --------------------------------------------------------
# Returns the time in the format "hh-mm-ss".
#
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime(time()+10800);
($sec < 10) and ($sec = "0$sec");
($min < 10) and ($min = "0$min");
return "$hour:$min:$sec";
}
sub get_date {
# --------------------------------------------------------
# Returns the date in the format "dd-mmm-yy".
# Warning: If you change the default format, you must also modify the &date_to_unix
# subroutine below which converts your date format into a unix time in seconds for sorting
# purposes.
my ($time) = @_[0];
$time = time()+10800;
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time);
my (@months) = qw!January February March April May June July August September October November December!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;
($sec < 10) and ($sec = "0$sec");
($min < 10) and ($min = "0$min");
if ($hour == 12) { $ampm = "pm"}
elsif ($hour == 0) {$hour = 12; $ampm = "am"}
elsif ($hour < 12) {$ampm = "am"}
else {$hour -= 12; $ampm = "pm"}
return "$months[$mon] $day, $year $hour:$min:$sec $ampm";
}
sub date_to_unix {
# --------------------------------------------------------
# This routine must take your date format and return the time a la UNIX time().
# Some things to be careful about..
# int your values just in case to remove spaces, etc.
# catch the fatal error timelocal will generate if you have a bad date..
# don't forget that the month is indexed from 0!
#
my ($date_and_time) = $_[0];
my (%months) = ("January" => 0, "February" => 1, "March" => 2, "April" => 3, "May" => 4, "June" => 5,
"July" => 6, "August" => 7, "September" => 8, "October" => 9, "November" => 10,"December" => 11);
my ($mon, $day, $year, $time, $ampm) = split(/ /, $date);
$day =~ s/,//;
my ($hour, $min, $sec) = split(/:/, $time);
if (($hour == 12) && ($ampm eq "am")) {$hour == 0; }
elsif (($hour<12) && ($ampm eq "pm")) {$hour +=12; }
unless ($mon and $day and $year) { return undef; }
unless (defined($months{$mon})) { return undef; }
use Time::Local;
eval {
$sec = int($sec);
$min = int($min);
$hour = int($hour);
$day = int($day);
$year = int($year) - 1900;
$time = timelocal($sec,$min,$hour,$day, $months{$mon}, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}