LinuxQuestions.org
Register a domain and help support LQ
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 11-17-2007, 08:09 AM   #1
abdul_zu
Member
 
Registered: Feb 2005
Posts: 71

Rep: Reputation: 15
How to subtract two date in Perl?


Hi all,

I have two datetime as string in variable how i can subtract to get the different in seconds?


$startdat = "2007-11-17 12:50:22";
$stopdat = "2007-11-17 12:53:22";

$result = $stopdat - $startdat;

But it is not working, can you guys please help to get the differences in sec?

Thank You
Abdul
 
Old 11-17-2007, 11:17 AM   #2
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Rep: Reputation: 16
First you'll need to convert your dates to seconds, i've not used perl for a number of years, so here's a result from google:
http://www.unix.org.ua/orelly/perl/cookbook/ch03_03.htm
Once you've converted to seconds it'll be easy to subtract...
 
Old 11-17-2007, 01:19 PM   #3
abdul_zu
Member
 
Registered: Feb 2005
Posts: 71

Original Poster
Rep: Reputation: 15
HI,

I tried the following code but i did not work for me.


$startdat = "2007-11-17 12:51:22";
$date2 = ParseDate($startdat); #Convert string into date

use Time::Local;
$TIMEa = timegm($date2);
print $TIMEa;

Error:
perl /var/lib/asterisk/agi-bin/test.pl
Day '' out of range 1..31 at /var/lib/asterisk/agi-bin/test.pl line 21

Any Idea please?

Thank You
 
Old 11-17-2007, 01:58 PM   #4
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Rep: Reputation: 16
If I remember the article, you had to split your date string into it's constituent parts before passing it to the function!
 
Old 11-17-2007, 08:15 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,242

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
One way is to get the date diff using Delta_YMDHMS from http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod, then cvt to epoch secs eg
use Time::Local;
$TIME = timelocal($sec, $min, $hours, $mday, $mon, $year);
 
Old 11-18-2007, 01:46 AM   #6
abdul_zu
Member
 
Registered: Feb 2005
Posts: 71

Original Poster
Rep: Reputation: 15
Hi friends,

Thank you for your help i used the following code and it worked well for me.

$startdat = "2007-11-17 12:51:22";
$stopdate = "2007-11-17 12:52:22";

my ($years, $months, $days, $hours, $mins, $secs) = split /\W+/, $startdat;
my ($yeart, $montht, $dayt, $hourt, $mint, $sect) = split /\W+/, $stopdate;

my $times = timelocal($secs,$mins,$hours,$days,$months,$years);
my $timet = timelocal($sect,$mint,$hourt,$dayt,$montht,$yeart);

$time = $timet - $times;

print $time;
 
Old 12-08-2007, 09:35 AM   #7
abdul_zu
Member
 
Registered: Feb 2005
Posts: 71

Original Poster
Rep: Reputation: 15
H friends,

Again i start getting error once the month changed.

this is the error i am getting

Month '12' out of range 0..11 at /var/lib/asterisk/agi-bin/test2.pl line 44


this is my code:

PHP Code:

$startdat 
"2007-12-07 12:51:22";
$stopdate "2007-12-07 12:52:22";

my ($years$months$days$hours$mins$secs) = split /W+/, $startdat;
my ($yeart$montht$dayt$hourt$mint$sect) = split /W+/, $stopdate;

my $times timelocal($secs,$mins,$hours,$days,$months,$years);
my $timet timelocal($sect,$mint,$hourt,$dayt,$montht,$yeart);

$time $timet $times;

print 
$time
Please help me where is the fault?

Thank You
 
Old 12-08-2007, 09:50 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Code:
$ cat t.pl
#!/usr/bin/perl

use strict;
use warnings;

use Date::Parse;
use Date::Format;

my $start = "2007-11-17 12:50:22";
my $stop  = "2007-11-17 12:53:22";
my $diff  = str2time($stop) - str2time($start);

printf "diff between %s and %s is %d seconds\n", $start, $stop, $diff;

$ ./t.pl
diff between 2007-11-17 12:50:22 and 2007-11-17 12:53:22 is 180 seconds
 
Old 12-08-2007, 09:59 AM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Be careful when working with dates. If there is no time zone as part of the date string, and you are in a region where there is daylight savings in effect for some of the year there, the exact date and time is ambiguous.

If you format dates without a time zone, you will have problems like this time sequence where the time jumps "back". I've actually seen this sort of thing in production environment, and it can be a problem:
Code:
Sun Oct 28 01:58:10 2007
Sun Oct 28 01:59:10 2007
Sun Oct 28 01:00:10 2007
Sun Oct 28 01:01:10 2007
Where as the same time sequence printed with the time zone would look like this:
Code:
Sun Oct 28 01:58:10 BST 2007
Sun Oct 28 01:59:10 BST 2007
Sun Oct 28 01:00:10 GMT 2007
Sun Oct 28 01:01:10 GMT 2007
Consider the case where start time is Sun Oct 28 01:59:10 BST 2007 and stop time is Sun Oct 28 01:00:10 GMT 2007... with your date format you would end up with a negative result, which might go on to cause other problems in your software. By using timezones in your date strings can you avoid this. You can also avoid it by always using UTC for times in your application and converting them to local time only when displayed.
 
  


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 subtract time apaehc access_log noir911 Programming 2 04-26-2007 09:24 AM
Date Manipulation in Perl gjagadish Programming 2 09-26-2006 06:39 AM
Perl: get file date kenneho Programming 5 11-01-2005 11:16 AM
using bc to subtract hexdemical schurt Linux - Software 1 01-07-2005 01:18 PM
perl and date manipulation Syncrm Programming 5 10-28-2002 02:05 AM


All times are GMT -5. The time now is 04:17 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration