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!

Thanks,
Monica
Monica2002Asked:
Who is Participating?
 
TintinConnect With a Mentor Commented:
Doing

perldoc -f time

reveals:

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";
}


0
 
kblack05Commented:
Monica there's a way easier way.
http://search.cpan.org/search?query=Date::Manip&mode=all

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

#!/usr/local/bin/perl
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";
}

Regards,

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

From novice to tech pro — start learning today.