paragkalra has asked for the wisdom of the Perl Monks concerning the following question:

Hey All,

Could someone please suggest some good date/time modules to compare dates or increment/decrement dates.

Ideally I would to achieve this through some standard function without using any additional module.

  • Comment on Comparing and increamenting dates in Perl

Replies are listed 'Best First'.
Re: Comparing and increamenting dates in Perl
by Corion (Patriarch) on May 13, 2010 at 10:02 UTC
Re: Comparing and increamenting dates in Perl
by cdarke (Prior) on May 13, 2010 at 11:43 UTC
    without using any additional module

    Many of the useful functions come from the POSIX module, and you should have that already installed. Here is an example script which adds a number of days to current date:
    #!/usr/bin/perl use warnings; use strict; use POSIX qw(strftime); my $calc_days; if ( ! @ARGV ) { print "Please enter number of days (+/-): "; $calc_days = <STDIN>; chomp $calc_days; exit 1 if $calc_days eq ''; } else { $calc_days = $ARGV[0]; } # Convert days to seconds; my $secs = $calc_days * 24 * 60 * 60; # Convert seconds to a readable format, adding them to the current tim +e my $final_date = strftime ('%d/%m/%Y %T', localtime(time() + $secs)); print "$final_date\n";
Re: Comparing and increamenting dates in Perl
by JavaFan (Canon) on May 13, 2010 at 14:39 UTC
    Could someone please suggest some good date/time modules to compare dates or increment/decrement dates.
    There are a gazillion CPAN modules for that. For simple things, I like Date::Simple.
    Ideally I would to achieve this through some standard function
    I think most, if not all, CPAN date modules store dates as integers, or a set of integers. Incrementing/decrementing dates doesn't use anything less standard than addition, subtraction, and numeric compare. Perhaps the modulus function if there's a need to find out whether a year is a leap year.
    without using any additional module.
    Whatever. If you want to reimplement one of the many existing wheels out there, be my guest. I'm not going to do that for you. You could always take the source code of the mentioned modules and use that as an inspiration.
Re: Comparing and increamenting dates in Perl
by doug (Pilgrim) on May 13, 2010 at 16:32 UTC

    Everyone has their one favorite module for doing this. I'm a fan of Date::Manip because there almost nothing that it cannot handle. Others prefer slimmer, more specialized tools for doing the same thing. You'll just have to download a few from CPAN and try them out for yourself.

    - doug

Re: Comparing and increamenting dates in Perl
by ashokpj (Hermit) on May 13, 2010 at 11:13 UTC
    Time::Piece
Re: Comparing and increamenting dates in Perl
by Marshall (Canon) on May 13, 2010 at 17:32 UTC
    Corion has listed the "usual suspects". Basically if you want to do date math efficiently with dates that are within the past ~century, this requires a trip to "epoch" time. Unix has a time for this and Macintosh O/S has another time. What this amounts to is generating an integer number of seconds that is plus/minus from the epoch date/time. You convert date/time strings into this integer value and then add/subtract a number of seconds from this "epoch based" value and then convert that integer back to a string.

    If you just want to compare or sort date/times without going through this conversion hassle, I would advise using a format like: 2010-05-12 (YYYY-MM-DD). Leading zero'es are important! A string sort of dates like that will yield the correct order. So that is how to do compares and sorts of dates. For calculations using dates, see above.