On Jun 1, 5:36 am, [EMAIL PROTECTED] (Mathew Snyder) wrote:
> A while ago I had posted requesting help with a long block of code that would 
> do
> all kinds of stuff dealing with the date.  It turned out to not work despite
> being technically, correct.  Instead of getting help with it, Mr. Phoenix
> provided me with a block of code that did what I needed but much more 
> concisely.
>  And, more importantly, correctly.
>
> for (1 .. 7) {
>   $time -= 24*60*60;
>   my @date = (localtime($time))[3 .. 5];
>   push @days, (sprintf '%02d', $date[0]);
>   push @months,(sprintf '%02d',$date[1] + 1);
>   push @years, $date[2] + 1900;
>   push @searchDate, join "-", ($date[2] + 1900), (sprintf '%02d',$date[1] + 
> 1),
> (sprintf '%02d', $date[0]);

You code would be much more readable (but a little slower) with
strftime() rather than rolling your own manipulations of the return
value of localtime().

  use POSIX 'strftime';
  my @date = localtime($time);
  push @days, strftime '%d', @date;
  push @months, strftime'%m',@date;
  push @years, strftime '%Y',@date;
  push @searchDate, strftime '%Y-%m-%d',@date

Be aware strftime() is in the POSIX module so there's a theoretical
risk that it may not be available on all platforms. However AFAIK
there are no platforms on which Perl currently runs where
POSIX::strftime is not implemented.

> The thing I see being a problem
> though is that some months have 30 days, some months have 31 days and
> February has 28/29 days.

That's not a problem. localtime() knows all about leap years.

>  How can I populate an array of an entire month's worth
> of dates without worrying about how many days the month has?

Prozac?

But don't simply pop a pill because what you _should_ worry about is
daylight saving time which could cause a date to be skipped or
repeated.

You should make sure that $time is not within the first or last hour
of the day. I would make sure $time starts out at a 12:00.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to