in reply to Is there an easy way to get the start date of the current week?

Thanks for your replies! roboticus I had already looked at the DateTime module on CPAN but nothing stood out other than determining the week of the year and then getting the Sunday date from that as being an easy way.

BrowserUk thanks that works fine! Just would be interested if you could breakdown the line

$now -= 43200 while scalar localtime($now) !~ m[Sun];

Into what is actually being done here which will help me understand exactly how the answer is acheived.

I did think after I had posted the easy way would be to take date now and just changed the day to Sunday. Could this be what you are doing?
  • Comment on Re: Is there an easy way to get the start date of the current week?
  • Download Code

Replies are listed 'Best First'.
Re^2: Is there an easy way to get the start date of the current week?
by james2vegas (Chaplain) on Aug 21, 2010 at 13:33 UTC
    Basically, yes, it subtracts half a day (43200 seconds) from $now until the stringification of localtime($now) ($now represented as a string like 'Sat Aug 21 06:33:51 2010') contains 'Sun' for Sunday.
Re^2: Is there an easy way to get the start date of the current week?
by ikegami (Patriarch) on Aug 21, 2010 at 15:48 UTC
    Keep in mind there are two hours every year where that code won't work (due to DST). Not every day has 24 hours. There is a solution below that doesn't suffer from this problem.

    Update: Ah, he's only subbing 12 hours!

      Care to point out which hours that would be? Unless there's a timezone that skips 12 hours or more on DST, I don't see there's a problem. Had ramjamman actually suggested to subtract 86400 in each iteration, there would be 6 hours/year where this would be a problem - the hour between midnight and 1 AM on the Monday - Saturday following the Sunday when DST is enabled. (Assuming DST skips an hour, and starts on a Sunday. And assuming the local timezone actually has DST - many parts of the world don't).

        Had ramjamman actually suggested to subtract 86400 in each iteration

        I stand corrected. (Magic numbers == bad)