in reply to Code to calculate Delta b/w two dates in seconds
There is no 23:59:55 with PM. Either you got 24hour-format or AM/PM :-)my $t1 = "2009-04-12 23:59:55 PM"; my $t2 = "2009-03-30 00:00:02 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.
Just parse your timestamp and feed it into timelocal from Time::Local.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);
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 |