in reply to Re^4: Is there an easy way to get the start date of the current week?
in thread Is there an easy way to get the start date of the current week?

Time::Seconds and Time::Piece are core modules, they do not need to be installed, and Date::Calc, already mentioned in this thread, is used to calculate the arbitrary date. But sure, how about this, then?

Update: changed hour to noon, instead of midnight, thanks daylight saving time (now works for the week of March 14 2010, thanks ikegami)
use strict; use warnings; sub get_bofw { my $time = shift; my @t = localtime($time); my $bow = $time - ($t[6] * 86400); localtime($bow); } # Tue Mar 16 2010 12pm my $bofw = get_bofw(1268766000); print "$bofw\n";

Update: Adding Time::Local, to make the call a little more clear:
use strict; use warnings; sub get_bofw { my $time = shift; my @t = localtime($time); my $bow = $time - ($t[6] * 86400); localtime($bow); } # Tue Mar 16 2010 12pm my $time = timelocal(0,0,12,21,4,74); my $bofw = get_bofw($time); print "$bofw\n";

Replies are listed 'Best First'.
Re^6: Is there an easy way to get the start date of the current week?
by BrowserUk (Patriarch) on Aug 21, 2010 at 16:01 UTC

    The numbers are for Date::Calc on its own. You didn't appear to be using Time Piece or Time::Seconds, so I didn't count them.

      ONE_DAY is from Time::Seconds and the object interface to the return value of localtime (instead of accessing array elements by index) is from Time::Piece. Date::Calc was used since its Mktime method accepts date and time fields more obviously than Time::Local's timelocal does, and it had been mentioned in the thread already. It was either that or DateTime which is heavier, but is the Date/Time module I usually use. I guess Time::Local is a fine, if not completely intuitive, choice.