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

Found out how to add or subtract days. But I want the last day of the month. It's a script that runs once on the 10th of every month and needs to print the last day of that month.

Any suggestions? I tried some oracle solutions :) But does anyone has a perl solution, because it's stupid to open a oracle connection for this...

--
My opinions may have changed,
but not the fact that I am right

Replies are listed 'Best First'.
Re: Date::Manip and Date::Calc
by davorg (Chancellor) on Oct 22, 2001 at 19:22 UTC

    How about a solution that doesn't use any non-standard modules?

    #!/usr/bin/perl -w use strict; use Time::Local; use POSIX 'strftime'; my @now = localtime; $now[4]++; if ($now[4] == 12) { $now[4] = 0; $now[5]++; } $now[3] = 1; my @then = localtime(timelocal(0, 0, 12, @now[3 .. 5]) - 86_400); print strftime '%Y-%m-%d', @then;
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

      Is Time::Local a standard module? I had to install it manually for 5.005_03, though it seems to have come standard with my 5.6.0 installation.

      I once asked a similiar question, but since you used both Time::Local and POSIX, I'll ask it again. Is there a particular reason you chose Time::Local::timelocal() over POSIX::mktime()?

      -Blake

        Time::Local has been a part of standard Perl for at least six years. If you had to install it manually for 5.005_03, then I think you had a broken installation.

        The reason for using Time::Local::timelocal is that it's a function that I know and use. I didn't know that POSIX::mktime existed until you mentioned it the other day. In this case, POSIX::mktime might make more sense, as it would mean loading one less module.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you don't talk about Perl club."

Re: Date::Manip and Date::Calc
by tomhukins (Curate) on Oct 22, 2001 at 19:22 UTC
    Have you tried using Days_in_Month from Date::Calc? It will tell you how many days are in the current month, so if there are 30 days in the month the last day is the 30th.
      Well not quite the current month, as you have to provide a year and month:
      print "The last day of the month is ", Date_to_Text_Long((Today())[0..1],Days_in_Month((Today())[0..1]));

      --
      I'd like to be able to assign to an luser

Re: Date::Manip and Date::Calc
by stefan k (Curate) on Oct 22, 2001 at 19:24 UTC
    Uhm,
    I'm not really sure if I got your question right, but would it be Days_in_Month? A quick glance at the manpage of Date::Calc and a search for 'last' also gave an example of 'How can I calculate the last business day (payday!) of a month'. That should be a good start ...

    Regards... Stefan
    you begin bashing the string with a +42 regexp of confusion

Re: Date::Manip and Date::Calc
by thinker (Parson) on Oct 22, 2001 at 20:10 UTC
    Hi toadi,

    ,

    Here ia a one liner you could adapt for a cron job.


    perl -MDate::Calc=Days_in_Month -e'@l=localtime;printf("Final Day %i\n +",Days_in_Month($l[5]+1900,($l[4]+1)%12))'


    cheers

    thinker