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 | [reply] |
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.
| [reply] [d/l] [select] |
|
|
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).
| [reply] |
|
|
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");
}
| [reply] [d/l] |
|
|
| [reply] |
Re: is today a friday
by Herkum (Parson) on Mar 10, 2006 at 20:19 UTC
|
my $dt = DateTime( day => $day,
month => $month,
year => $year );
if ($dt->day_name eq 'Friday') {
print "Thank god it's Friday\n";
}
| [reply] |
Re: is today a friday
by prasadbabu (Prior) on Mar 11, 2006 at 01:12 UTC
|
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";
| [reply] [d/l] |
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. | [reply] |
|
|
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).
| [reply] |
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. | [reply] |