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

Hello everyone, I'm fairly new here and have a hopefully quick question for you. I'm currently writing a program where the user is able to enter in a date that can be anytime within the last 5 years and anytime in the future. What I need to do with this user-supplied date is confirm whether it is a friday or not. So if the user gives me 10/15/2008, I need to be able to tell if October 15th of 2008 is a friday. So far I've determined that localtime can provide me this information with the wday array index, however I'm not sure how to have localtime work with a date other then todays date. Any help would be appreciated.

Replies are listed 'Best First'.
Re: is today a friday
by philcrow (Priest) on Mar 10, 2006 at 18:56 UTC
    Take a look at Date::Calc or one of the other date modules on CPAN. In particular, see the Day_of_Week function in Date::Calc.

    Phil

Re: is today a friday
by izut (Chaplain) on Mar 10, 2006 at 19:07 UTC

    localtime can receive an argument, which is the number of seconds since epoch. You can get today's time with time function, and then add the time needed in seconds (i.e. tomorrow will be time() + 24 * 60 * 60 * $days, where $days will be 1, and so).

    I think it's enough for starting. You can also check Date::Manip, Date::Calc and others.

    Good luck!

    Igor 'izut' Sutton
    your code, your rules.

      Thanks for the start izut. Is there any way to pass in a date without having to count the number of days that have passed since epoch? is there any way to just pass in "03102006" (today's date).
        Some or all of the modules already listed do this. Core module Time::Local also does.
        use Time::Local qw( timelocal ); my $date = '10/15/2008'; my ($m, $d, $y) = split(m{/}, $date); my $time = timelocal(0, 0, 0, $d, $m-1, $y); my $dow = (localtime($time))[6]; if ($dow == 5) { print("It's Friiiiiiiiday!\n"); } elsif ($dow == 0 || $dow == 6) { print("Sweet heavenly weekend!\n"); } else { print("Get back to work, you bum!\n"); }
        Well, you could use Mktime in Date::Calc to convert your string to unix time, then pass that to another Date::Calc function... but what's the point? Just use one of Date::Calc's Day_of_Week methods and be done with it.
        ---
        It's all fine and dandy until someone has to look at the code.
Re: is today a friday
by Herkum (Parson) on Mar 10, 2006 at 20:19 UTC

    All those comments and noone has even mentioned DateTime?

    my $dt = DateTime( day => $day,
                       month => $month,
                       year  => $year );
    
    if ($dt->day_name eq 'Friday') {
        print "Thank god it's Friday\n";
    }
    
Re: is today a friday
by prasadbabu (Prior) on Mar 11, 2006 at 01:12 UTC

    Hi baysbenj, TIMTOWTDI

    use Date::Day; my $date = '10/15/2008'; #Today not Friday #my $date = '03/10/2006'; # Today Friday my ($month, $day, $year) = split(m{/}, $date); my $result = &day($month,$day,$year); #find the day of the given date ($result =~ /fri/i) ? print "Today Friday" : print "Sorry, Today not F +riday";

    Prasad

Re: is today a friday
by PreferredUserName (Pilgrim) on Mar 10, 2006 at 18:59 UTC
    Since this is probably homework, I assume Date::Calc won't be helpful to you.

    I suggest you write out in English how to do it, and then translate that English to Perl.

      Nah, not homework. I'm doing an internship where I'm working to create a time management system. The program is only to accept dates that are a friday. If the day is not a friday, I need to automatically adjust to the friday of that week. I could brute force a solution, but before I did I was just wondering if there was a way to have localtime or gmtime do the work for me. Date::Calc is also not an option, because they do not have the module installed and I'm unable to install it (I ran into a similar problem when i wanted them to install a module for threading).
Re: is today a friday
by ryantate (Friar) on Mar 10, 2006 at 23:32 UTC
    Date::Parse is almost certainly installed. Use its str2time function to get a time value (str2time("15-Oct-2008")), which you can then feed to localtime (localtime($time)), from which you can extract the wday. If it is not friday, keep doing localtime($time += 60 * 60 * 24) until you get a Friday in wday.