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

Has anyone else out in the cloisters had trouble with the Date::Day module? All I want is a three character representation of the day in the week for a corresponding date, Seems like a job for Date::day doesn't it?! So why do I NEVER get the correct day back? Here's a wee noddy script that I knocked up to test this module when the results from the big script containing this module reported rubbish!
#!/usr/bin/perl -w ############################################################### # Extra Modules should be declared here. # # All these modules MUST be found on the server attempting to # # call this script. # ############################################################### use strict ; # Enforce private + variables use Date::Day ; # Returns the act +ual day of a date ############################################################### # Set up variables # ############################################################### # Date and time variables for headings in Emails/files etc. # ############################################################### my ($day, $mon, $year) = (localtime) [3,4,5] ; my $date = sprintf("%02d/%02d/%04d", $day, $mon +=1, $year += 1900); my $today = undef ; ##################### print "\n\t\t*** test_day starts ***\n" ; $day = '9' ; $mon = '8' ; $year = '2005' ; print "\n\t\tDay :: $day" ; print "\n\t\tMon :: $mon" ; print "\n\t\tYear :: $year\n" ; $today = &day($day,$mon,$year) ; print "\n\t\tToday is a $today\n" ; print "\n\t\t*** test_day ends ***\n" ;
According to the Date::Day the 9th of August 2005 is a THU! Have I done something mind blowingly stupid? I've looked and looked and can see nothing wrong with my code, any ideas? Cheers, Ronnie
PS Using the current date from localtime - the result is that today is a TUE???????????

Replies are listed 'Best First'.
Re: Date::Day problem?!
by muntfish (Chaplain) on Aug 11, 2005 at 09:53 UTC

    According to Date::Day the parameters are in month, day, year order. So when you pass in (9,8,2005) it's telling you (correctly) that the 8th of September is a Thursday.

    That said, the documentation doesn't look too great and there hasn't been an update for nearly 3 years. There are probably better modules out there.


    s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&
      Thanks I misread the parameter sequence!!
Re: Date::Day problem?!
by gellyfish (Monsignor) on Aug 11, 2005 at 09:47 UTC

    I would recommend that you use POSIX::strftime which has the added advantage of using the appropriate locale:

    use POSIX qw(locale_h); + foreach my $locale (qw(en_GB de_DE fr_FR es_ES)) { POSIX::setlocale(LC_TIME,$locale); print POSIX::strftime("%a\n", localtime); }

    /J\

Re: Date::Day problem?!
by salva (Canon) on Aug 11, 2005 at 09:30 UTC
Re: Date::Day problem?!
by ikegami (Patriarch) on Aug 11, 2005 at 15:56 UTC
    You should be using
    $today = &day($mon,$day,$year) ;
    instead of
    $today = &day($day,$mon,$year) ;
    The following does the same, and it uses only core modules:
    use POSIX qw( strftime ); use Time::Local qw( timegm ); print strftime("%a", gmtime(timegm(0, 0, 0, $day, $mon-1, $year)));
    or for current local time:
    use POSIX qw( strftime ); use Time::Local qw( timelocal ); print strftime("%a", localtime);
    Use %A instead of %a if you are interested in the full name of the day instead of the three letter abbrieviation.