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

I have to run a script on the first of the month from cron. Problem is... the script needs to know the name of the month that ended the day before. Like 'date +%m' comes back w/ 06 but i need for the script to be able to figure out that last month was 'May'. I just don't know how to get to that point. Any help would be appreciated. -d00f

Replies are listed 'Best First'.
Re: Month info from 'date'
by particle (Vicar) on Jun 23, 2001 at 00:21 UTC
    well, if you use Date::Calc, you can use Month_to_Text
    Month_to_Text $string = Month_to_Text($month);

    ~Particle

Re: Month info from 'date'
by VSarkiss (Monsignor) on Jun 23, 2001 at 00:23 UTC
    You can get the current month from localtime. Then it's pretty easy to map the month number to a string with a simple array: @monthname = qw(Jan Feb Mar Apr May Jun Jul ...); Put the two together:
    my $thismonth = (localtime)[4]; my $lastmonth = ($thismonth == 0)? 11 : $thismonth-1; print $monthname[$lastmonth], "\n";
    Substitute for print as necessary.

    HTH

      Note that you don't even need the $thismonth check; an index of -1 will return the last element from the array (eg December).


      Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      You can skip the acrobatics altogether with:
      print qw(Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov)[(localtime)[ +4]]
        Ok... here is how I ended up doing it... tell me if this looks terrible:
        my $month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[(local +time)[4]-1]; print "$month\n";
        --Neb
Re: Month info from 'date'
by mischief (Hermit) on Jun 23, 2001 at 02:31 UTC
    Another way might be to use POSIX::strftime in a way similar to this:
    #!/usr/bin/perl -w use POSIX qw(strftime); use strict; my $thismonth = strftime "%m", localtime; # strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, + isdst = -1) # don't forget that the months start counting from 0 as January print strftime("%m", 0, 0, 0, 0, $thismonth-2, 0); #prints "05"
Re: Month info from 'date'
by Tuna (Friar) on Jun 23, 2001 at 07:18 UTC
    use Date::Manip; $yesterday = UnixDate(ParseDate("yesterday"), "%B %E, %Y"); print "$yesterday\n";
    will print "June 21st, 2001"

    so......
    use Date::Manip; $yesterday = UnixDate(ParseDate("yesterday"), "%B"); print "$yesterday\n";
    will print "June"
Re: Month info from 'date'
by davorg (Chancellor) on Jun 25, 2001 at 12:24 UTC

    Some incredibly complex answers here for something that is really very simple.

    use POSIX 'strftime'; # NOTE: 86_400 seconds in a day. $yesterdays_month = strftime('%b', time - 86_400);

    strftime and localtime work together really well in cases like this. Date::Manip is almost always far too big and slow for real work.

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

    Perl Training in the UK <http://www.iterative-software.com>