Perl Timestamps

Posted on 2003-02-25
Medium Priority
Last Modified: 2008-03-06
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!

Question by:Monica2002
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 48

Accepted Solution

Tintin earned 150 total points
ID: 8023137

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

LVL 11

Expert Comment

ID: 8027187
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
LVL 48

Expert Comment

ID: 8029382
Date::Manip is a very powerful, but slow module. For a simple operation like this, it is total overkill.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question