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

Hi,
I have a perl script that pulls out the records from Oracle DB for 15 days.
Timestamps are used in the DB tables. Shall I use Date::Calc? Please let me know if there is any way to do it.
use strict; use warnings; my $start = DateTime->new(year => 2009, month => 5, day => 16); my $end = DateTime->new(year => 2009, month => 6, day => 2); my $curr = $start;

A quick response is highly appreciated!
Thanks
Rocko19

Replies are listed 'Best First'.
Re: Want to use Date module
by Corion (Patriarch) on Jun 26, 2009 at 13:35 UTC

    Why aren't you doing the date calculation in the database? That way you would only need to calculate the start and end date in Perl, or, if your database has date calculation functions, not even that:

    $sth = $dbh->prepare(<<SQL); SELECT * FROM mytable WHERE mydate between ? and ? ORDER BY mydate ASC SQL $sth->execute($start_date, $end_date); my $res = $sth->fetchall_arrayref;
    or
    $sth = $dbh->prepare(<<SQL); SELECT * FROM mytable WHERE ? - mydate <= 15 -- this should really use the date/time fun +ctions instead ORDER BY mydate ASC SQL $sth->execute($end_date); my $res = $sth->fetchall_arrayref;
Re: Want to use Date module
by davorg (Chancellor) on Jun 26, 2009 at 13:18 UTC
    Am I using Date::Calc properly?

    You're not actually using Date::Calc at all. Well, I mean you're loading the module. But then you're completely ignoring it. You're just incrementing a string.

    I recommend DateTime for all date and time processing tasks.

    #!/usr/bin/perl use strict; use warnings; use DateTime; my $start = DateTime->new(year => 2009, month => 5, day => 16); my $end = DateTime->new(year => 2009, month => 6, day => 2); my $curr = $start; while ($curr <= $end) { print $curr->strftime('%Y%m%d'), "\n"; $curr->add(days => 1); }

    Update: Corion's point about doing this in the database is an excellent one.

    --

    See the Copyright notice on my home node.

    Perl training courses

Re: Want to use Date module
by toolic (Bishop) on Jun 26, 2009 at 13:23 UTC
    Am I using Date::Calc properly?
    In the code you have shown, you are not using any of the functions of the Date::Calc module. Despite your inclusion of the module with use, you are not actually using the module. If you comment out that line, the code should run the same way.

    Perl is interpreting your date as an integer value. So, '20090516' is really 20,090,516, and the for loop just iterates up to the next integer you specify, 20,090,602. Perl does not consider them dates.

    You should calculate the number of days between your dates using one of the Delta_Days unctions provided by Date::Calc.

Re: Want to use Date module
by moritz (Cardinal) on Jun 26, 2009 at 14:03 UTC
    I loke Date::Simple for these kind of calculations:
    use Date::Simple qw(date); for (my $d = date('2009-05-16'); $d <= date('20090602'); $d++) { print $d, "\n"; }
      Hi,
      Thanks for your reply. I was trying to use Date::Simple but got the following error.
      Can't Locate Date/Simple.pm in @INC <@INC contains: C:/Perl/site/lib.
      Please suggest how to include this module in my script.

      Thanks
      Rocko

        That is probably because you haven't got Date::Simple installed. From the error, it looks like you are using Activestate perl. In that case, fire up the package manager (ppm) and ask it to install Date::Simple.

Re: Want to use Date module
by Anonymous Monk on Jun 26, 2009 at 13:15 UTC
    You are not using Date::Calc at all
Re: Want to use Date module
by gulden (Monk) on Jun 26, 2009 at 15:59 UTC
    Another aproach
    use Date::Format; use Date::Parse; $ts = &str2time("2009-05-16"); $ts2= &str2time("2009-06-02"); for (my $i=$ts; $i <= $ts2; $i+=60*60*24){ print &time2str("%Y%m%d", $i)."\n" }