Jeff,

One quick question...since I want to run this every week, will that part
here you hard-coded today's date need to be changed to be dynamic?

thanks,

rory

On Wed, 2002-03-20 at 22:30, Jeff 'japhy' Pinyan wrote:
> On Mar 20, rory oconnor said:
> 
> >Can anyone think of a good way for me to find out what the date of last
> >sunday is with perl?  I'm writing a script that will need to do some
> >basic reporting starting from the previous sunday.
> >
> >I'm usign the date format YYYY-MM-DD
> 
> I suggest the standard Time::Local module.  It's timelocal() function,
> along with the builtin localtime() function, allow you do to virtually
> anything.
> 
>   use Time::Local;
>   use constant SECONDS_PER_DAY => 86400;
> 
>   my ($year, $mon, $day) = (2002, 3, 20);  # today's date
>   my $today_at_noon = timelocal(0,0,12, $day, $mon-1, $year-1900);
>   my $days_since_sunday = (localtime $today_at_noon)[6] || 7;
>   my $sunday = $today_at_noon - $days_since_sunday * 86400;
> 
> Now we have $sunday, the number of seconds that represents last Sunday; in
> the event that this program is run ON a Sunday, we reach back to last
> week's Sunday.
> 
> With this value, you can then do:
> 
>   my ($d, $m, $y) = (localtime $sunday)[3,4,5];
> 
> and concoct a date in the form of YYYY-MM-DD like so:
> 
>   sprintf "%4d-%02d-%02d", $y+1900, $m+1, $d;
> 
> -- 
> Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
> RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
> ** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
> <stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
> [  I'm looking for programming work.  If you like my work, let me know.  ]
> 
> 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to