LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-26-2007, 08:11 AM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
Perl subtract time apaehc access_log


I need to subtract the date from the first line to the date from the last line in access log to get the total days but I'm not quite sure how. I understand that I need to use localtime() to convert the date in seconds before subtracting. Can anyone give some example? Thanks.
 
Old 04-26-2007, 09:05 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Try time::local
Code:
$epoch_seconds = timelocal($sec,$min,$hour,$mday,$mon,$year);
 
Old 04-26-2007, 09:24 AM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
You can use the Date::Calc module for this, like so:
Code:
#!/usr/bin/env perl

use warnings;
use strict;

use Date::Calc qw(:all);

my $line1 = '192.168.2.101 - - [14/Feb/2007:09:54:20 -0500] "GET / HTTP/1.1" 200 592 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.1) Gecko/20060601 Firefox/2.0.0.1 (Ubuntu-edgy)"';
my $line2 = '192.168.2.101 - - [30/Mar/2007:20:40:30 -0400] "GET /favicon.ico HTTP/1.1" 404 268 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.7) Gecko/20061022 Iceweasel/1.5.0.7-g2"';

my %month_name = ( 'Jan' => 1,  'Feb' => 2,  'Mar' => 3,
                   'Apr' => 4,  'May' => 5,  'Jun' => 6,
                   'Jul' => 7,  'Aug' => 8,  'Sep' => 9,
                   'Oct' => 10, 'Nov' => 11, 'Dec' => 12  );

$line1 =~ m!\[(\d\d)/(\w\w\w)/(\d\d\d\d):(\d\d):(\d\d):(\d\d) [-+]\d\d\d\d\]!;
my %first = ( 'year'    => $3, 'month'   => $month_name{$2}, 'day'     => $1,
              'hours'   => $4, 'minutes' => $5, 'seconds' => $6);

$line2 =~ m!\[(\d\d)/(\w\w\w)/(\d\d\d\d):(\d\d):(\d\d):(\d\d) [-+]\d\d\d\d\]!;
my %second = ( 'year'    => $3, 'month'   => $month_name{$2}, 'day'     => $1,
               'hours'   => $4, 'minutes' => $5, 'seconds' => $6);

my ($years, $months, $days, $hours, $minutes, $seconds) =
    Date::Calc::Delta_YMDHMS($first{'year'}, $first{'month'}, $first{'day'},
                             $first{'hours'}, $first{'minutes'}, $first{'seconds'},
                             $second{'year'}, $second{'month'}, $second{'day'},
                             $second{'hours'}, $second{'minutes'}, $second{'seconds'});

print "The difference is: ", $years, " years | ", $months, " months | ", $days, " days | ", $hours, " hours | ", $minutes, " minutes | ", $seconds, " seconds\n";
Assuming you haven't changed Apache's logging format for the date.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl - Reading Real Time streams vitoal18t Programming 10 05-18-2005 06:59 AM
mysql query time into perl cgi tommasopiff Programming 0 03-25-2005 04:25 PM
using bc to subtract hexdemical schurt Linux - Software 1 01-07-2005 01:18 PM
reading idle time with perl/shell script daryl314 Linux - General 1 12-27-2004 01:11 PM
Perl - Time Travel? SiLiCoN Programming 4 12-17-2004 05:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration