Perl Timestamps

Hi all,

I have a list of session ids with timestamps (using the Perl time function). I want the sessions to expire if they have lasted longer than two hours. So, I have a cron job that runs every two hours: It gets the current timestamp (using the time function). Then for each session id, it calculates the difference between the session's timestamp and the current timestamp. If the hours difference > 2, then I kill the session. Calculating this time difference is my biggest problem. How do I do this? What is the format of the time function? Is there a Perl function that can convert the timestamp to all minutes or all hours? Please help!

Who is Participating?
TintinConnect With a Mentor Commented:

perldoc -f time


time    Returns the number of non-leap seconds since whatever time the system considers to be the epoch
that's 00:00:00, January 1,1904 for Mac OS, and 00:00:00 UTC, January 1, 1970 for most other systems).  Suitable for feeding to "gmtime" and "local-time".

It's pretty simple to see if something is older than 2 hours, by doing something like:

Assuming $origtime has the timestamp from over two hours ago.

my $now = time();

if (($now - $origtime) > (60 * 60 * 2)) {
   print "Greater than 2 hours old\n";

Monica there's a way easier way.

I know dates, and though I have not handled sessions this
way here's some sample code I worked up:

use Date::Manip;
my $file = "test.txt";
my $expires = UnixDate("-5 minutes","%M");
my $curtime = UnixDate("now","%M");
my $diff = $curtime-$expires;
my $timer = (-A $file);
my $session = $timer*24*60*60/100;
print "Expires: $expires\n";
print "Diff: $diff\n";
print "Timer: $timer\n";
print "Curtime: $curtime\n";
print "Session: $session\n";
if ($session > $expires) {
print "Your session has expired!\n";
} else {
print "Your session is in use!\n";


~Kelly W. Black
Date::Manip is a very powerful, but slow module. For a simple operation like this, it is total overkill.
All Courses

From novice to tech pro — start learning today.