Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

More Date Arithmetic/Manipulation: Question

by newbie01.perl (Sexton)
on Jan 22, 2010 at 09:34 UTC ( [id://818927]=perlquestion: print w/replies, xml ) Need Help??

newbie01.perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Need some advise please on how do date arithmetic/calculation.

Basically, I have a CSV delimited file that I need to do some date calculations on.

Am parsing the fields as $date1 and $date2 and I need to do the time difference as $date2-$date1

Example of $date1 and $date2 below:

$date1="31/12/2009 6:14:25 p.m." $date2="31/12/2009 6:29:26 p.m."

Am still Googling and posting this at the same time ... :-)

Any suggestion will be very much appreciated. Thanks in advance.

  • Comment on More Date Arithmetic/Manipulation: Question

Replies are listed 'Best First'.
Re: More Date Arithmetic/Manipulation: Question
by marto (Cardinal) on Jan 22, 2010 at 09:38 UTC
Re: More Date Arithmetic/Manipulation: Question
by Your Mother (Archbishop) on Jan 22, 2010 at 17:44 UTC

    Date::Manip is excellent for certain things like natural string parsing but otherwise it's not the best choice. DateTime is amazing and handles time zones, ranges, date math, and such really well but it has bit of a learning curve and it's a little slow. Date::Calc is very fast but its functions are generally low level so your code will be more verbose. There are some other date packages but they offer nothing over DateTime and Date::Calc.

    Prefer DateTime if you want a wealth of date operational power. Prefer Date::Calc if you have a need for speed.

    Update: been years but I want to amend it—Time::Piece has something to offer in comparison to the others: it’s core (since v5.9.5).

Re: More Date Arithmetic/Manipulation: Question
by Herkum (Parson) on Jan 22, 2010 at 14:40 UTC
    Another option would be to use DateTime for working with and comparing two dates.
Re: More Date Arithmetic/Manipulation: Question
by furry_marmot (Pilgrim) on Jan 22, 2010 at 19:12 UTC
    I need to do the time difference as $date2-$date1

    I'll assume you need the difference in seconds. If you need minutes or hours, divide by 60 or by 60/60.

    I've used Date::Calc for a long time with fine results. There are several functions (as well as an OOP interface for the same functions) available, depending on what you need. You'll have to do a little parsing, but then you turn the string into the seconds since the epoch (doesn't matter which one) and just subtract one from the other.

    use strict; use warnings; use Date::Calc qw(Mktime); # Turns a date string into seconds since ep +och my $dt_str2="31/12/2009 6:29:26 p.m."; my $dt_str1="31/12/2009 6:14:25 p.m."; my ($day, $mo, $yr, $hr, $min, $sec, $mer) = $dt_str1 =~ m{(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+) ([ap])}; $hr += 12 if $mer eq 'p'; die "Problem with date string $_" if $hr > 23; # Note element order for Date::Calc functions! my $dt1 = Mktime($yr, $mo, $day, $hr, $min, $sec); ($day, $mo, $yr, $hr, $min, $sec, $mer) = $dt_str2 =~ m{(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+) ([ap])}; $hr += 12 if $mer eq 'p'; die "Problem with date string $_" if $hr > 23; my $dt2 = Mktime($yr, $mo, $day, $hr, $min, $sec); print $dt2 - $dt1, "\n";

    Or you could make it into a loop if you had a lot of these to parse at once (and it's cleaner looking too).

    use strict; use warnings; use Date::Calc qw(Mktime); # Turns a date string into seconds since ep +och my $dt_str2="31/12/2009 6:29:26 p.m."; my $dt_str1="31/12/2009 6:14:25 p.m."; my @dates_to_compare; for ($dt_str1, $dt_str2) { my ($day, $mo, $yr, $hr, $min, $sec, $mer) = m{(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+) ([ap])}; $hr += 12 if $mer eq 'p'; die "Problem with date string $_" if $hr > 23; # Note element order for Date::Calc functi +ons! push @dates_to_compare, Mktime($yr, $mo, $day, $hr, $min, $sec); } # using pop clears out the array, which is helpful when looping print pop(@dates_to_compare) - pop(@dates_to_compare), "\n";

    I hope this helps

    marmot

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://818927]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2024-04-19 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found