in reply to Calculate the nearest day
In the spirit of TMTOWTDI, here's a version that only uses core Perl (ie. no Date::* modules):
#!/perl/bin/perl -w use strict; use Time::Local; my $next_target_day = 'Mon'; my $one_day = 86400; my @DOWname = qw/ Sun Mon Tues Wed Thurs Fri Sat /; my %DOWhash = map { $DOWname[$_] => $_ } 0 .. $#DOWname; my @T = localtime(time); my $days_til_target = $DOWhash{$next_target_day} - $T[6]; $days_til_target += 7 if $days_til_target < 0; my $beginning_of_today = timelocal(0,0,0,$T[3],$T[4],$T[5]); print scalar localtime($beginning_of_today), "\n"; print scalar localtime($beginning_of_today + $days_til_target*$one_day + $one_day/2 ), "\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Calculate the nearest day
by ikegami (Patriarch) on Apr 13, 2007 at 21:43 UTC |