LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to subtract two date in Perl? (https://www.linuxquestions.org/questions/programming-9/how-to-subtract-two-date-in-perl-600366/)

abdul_zu 11-17-2007 08:09 AM

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

brazilnut 11-17-2007 11:17 AM

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...

abdul_zu 11-17-2007 01:19 PM

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

brazilnut 11-17-2007 01:58 PM

If I remember the article, you had to split your date string into it's constituent parts before passing it to the function!

chrism01 11-17-2007 08:15 PM

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

abdul_zu 11-18-2007 01:46 AM

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;

abdul_zu 12-08-2007 09:35 AM

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

matthewg42 12-08-2007 09:50 AM

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


matthewg42 12-08-2007 09:59 AM

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.


All times are GMT -5. The time now is 06:36 AM.