Solved

Perl Time Stamp Calculation

Posted on 2009-05-05
4
1,326 Views
Last Modified: 2012-05-06
I have a file that contains start and end times for processes:
     Start BatPrcsDrvr.pl CAEXPENS 1    2009-05-05 01:05:52
     End BatPrcsDrvr.pl CAEXPENS 1 Successfully    2009-05-05 01:05:53
     Start BatPrtnDrop.pl CAEXPENS 1    2009-05-05 01:06:12
     End BatPrtnDrop.pl CAEXPENS 1 Successfully    2009-05-05 01:15:29
     Start BatBackup.pl CAEXPENS 1    2009-05-05 01:21:11
     End BatBackup.pl CAEXPENS 1 Successfully    2009-05-05 01:25:57
etc.
I want to take the time stamp on the End line and compare it to the time stamp on the start line to get the elapsed time.  I am working on this now but not getting very far so any tips would be appreciated.  I will repost if I figure it out first.
0
Comment
Question by:Angela_Wilcox
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 24307147
use  Time::Local;
$_='Start BatPrcsDrvr.pl CAEXPENS 1    2009-05-05 01:05:52';
@d=/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/;
$timestamp = timelocal(@d[5,4,3,2],$d[1]-1,$d[0]);

0
 

Author Comment

by:Angela_Wilcox
ID: 24307395
Epoch time conversion  - Exactly what was needed...
0
 
LVL 5

Assisted Solution

by:vikaskhoria
vikaskhoria earned 100 total points
ID: 24312471
You can also use the CPAN, Date::Manip modules to manipulate date. This also handles text having dates.

See:
http://search.cpan.org/~sbeck/Date-Manip-5.54/lib/Date/Manip.pod
0
 

Author Comment

by:Angela_Wilcox
ID: 24313640
Although the Date:: Manip could probably also do what I needed - I took the first authors comments and created a function then imbedded this into my output as follows - this is in a while loop that does other stuff as well but thought I would show how I used the solution - thanks to both of you for your help.

print $fh_output_time_log ("$_", " ", &epoch_time($_) ," \n");

sub epoch_time
{
    local @_;
    @d=/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/;
    return(sprintf(timelocal(@d[5,4,3,2],$d[1]-1,$d[0])));
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Script to read file contents, make changes, and build new file 7 90
perl: Cleaning meta tags using RegEX 12 60
Perl File::Find alternative 1 51
Perl Untar File 1 26
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now