in reply to Code to calculate Delta b/w two dates in seconds

my $t1 = "2009-04-12 23:59:55 PM"; my $t2 = "2009-03-30 00:00:02 PM";
There is no 23:59:55 with PM. Either you got 24hour-format or AM/PM :-)

Your solution assums that each day has 24 hours. It's a common mistake, but it is simply not true. There is daylight saving, once they even put some days difference in a second and February hasn't always 28 days.

use Time::Local; sub String2Time { $_[0] =~ /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})\:(\d{2})\:(\d{2})/ or di +e "Invalid format: $_[0]"; return timelocal($6,$5,$4,$3,($2 - 1),($1 - 1900)); } my $t1 = "2009-04-12 23:59:55 PM"; my $t2 = "2009-03-30 00:00:02 PM"; print &String2Time($t1) - &String2Time($t2);
Just parse your timestamp and feed it into timelocal from Time::Local.

Please let us know if this is faster or slower than your solution.

Replies are listed 'Best First'.
Re^2: Code to calculate Delta b/w two dates in seconds
by snra_perl (Acolyte) on Sep 22, 2009 at 22:26 UTC
    Thanks for the inputs. It works well and speed of execution seems to have no much difference.
    Can we use the same approach of Time::Local to add a some seconds to the existing time.

    I need something to work like the behaviour mentioned below
    my $t1 = "2009-04-12 23:59:55"; my $delta = 600; # seconds $updated_time = &String2Time($t1) + $delta;

    Thanks again