Luke, Thanks!!! Works exactly as needed! -c
On Wednesday 07 July 2004 09:41 am, Bakken, Luke wrote: > > Hi Guys, > > > > I'm having a real hard time trying to figure this out.. > > > > There are tons of modules on dates, etc, but I can't seem to > > find one to do > > what I need. > > > > I have one date, for example: 2004-07-07. > > > > I need to take that date, get Monday's date and Sunday's date > > where 2004-07-07 > > is between. > > > > Any suggestions? > > > > Thanks!! > > -c > > This was a fun little problem! > > No special modules are needed. I believe POSIX and Time::Local should be > in every distribution. I tested this with ActivePerl 5.8.3 on Win32. > > use strict; > use warnings; > use Time::Local qw(timelocal); > use POSIX qw(strftime); > > unless ($ARGV[0] =~ /^\d{4}-\d{2}-\d{2}$/) { > die "Date given must be in YYYY-MM-DD form.\n"; > } > my @given_date = split /-/, $ARGV[0]; > my $given_year = shift @given_date; > # Months start at zero > my $given_month = shift @given_date; > --$given_month; > my $given_day = shift @given_date; > > # We want these dates > # Monday ... Given Date ... Sunday > # ^^^^^^ ^^^^^^ > # 1 X 0(7) > my $given_time = timelocal(0, 0, 0, $given_day, $given_month, > $given_year); > > # Get the day of week of given date. > # 0 is Sunday, 1 is Monday, 6 is Saturday > my $wday = (localtime($given_time))[6]; > > my $days_to_prev_monday; > my $days_to_next_sunday; > if ($wday == 0) { > # Given date is a Sunday > # Next Sunday is 7 days from now > # Previous Monday is six days ago > $days_to_prev_monday = 6; > $days_to_next_sunday = 7; > } elsif ($wday == 1) { > # Given date is a Monday > # Previous Monday is 7 days ago > # Next Sunday is six days from now > $days_to_prev_monday = 7; > $days_to_next_sunday = 6; > } else { > # Date given is between a Monday and a Sunday > $days_to_prev_monday = $wday - 1; > $days_to_next_sunday = 7 - $wday; > } > # Time returned from timelocal is in seconds, time given to > # localtime is in seconds as well. 86400 seconds in a day. > my @prev_mon_date = localtime($given_time - ($days_to_prev_monday * > 86400)); > my @next_sun_date = localtime($given_time + ($days_to_next_sunday * > 86400)); > my $previous_monday = strftime '%Y-%m-%d', @prev_mon_date; > my $next_sunday = strftime '%Y-%m-%d', @next_sun_date; > > print "Previous Monday: $previous_monday\n"; > print "Given Date: $ARGV[0]\n"; > print "Next Sunday: $next_sunday\n"; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>