Velocity Reviews > Perl > Date Manipulation: Week_Number back to Date

# Date Manipulation: Week_Number back to Date

wls
Guest
Posts: n/a

 02-02-2005
I've found a number of functions and expressions for taking dates and
converting them to a week number.

Interestingly enough, I need to go in the reverse direction.

( \$day, \$mon, \$year ) = ReverseTheProcess( 2007, 7 ); # Week 7 of 2007
is 4-Feb-2007

Is there such a beast, or does someone have a clever way to calculate
it?

Thanks,
http://www.velocityreviews.com/forums/(E-Mail Removed)

Martien Verbruggen
Guest
Posts: n/a

 02-02-2005
On 1 Feb 2005 18:20:51 -0800,
wls <(E-Mail Removed)> wrote:
> I've found a number of functions and expressions for taking dates and
> converting them to a week number.
>
> Interestingly enough, I need to go in the reverse direction.
>
> ( \$day, \$mon, \$year ) = ReverseTheProcess( 2007, 7 ); # Week 7 of 2007
> is 4-Feb-2007

Week 7 of 2007 is a range of seven days. 4 Feb 2007 is a Sunday, not a
week.

> Is there such a beast, or does someone have a clever way to calculate
> it?

The Date::Calc module has a method

Monday_of_Week
(\$year,\$month,\$day) = Monday_of_Week(\$week,\$year);

which probably is what you want. If you need the Sunday instead of the
Monday, also use Add_Delta_Days() or one of those.

http://search.cpan.org/

Martien
--
|
Martien Verbruggen | Failure is not an option. It comes bundled
|

Guest
Posts: n/a

 02-02-2005
Martien Verbruggen wrote:
> wls wrote:
>> I've found a number of functions and expressions for taking dates
>> and converting them to a week number.
>>
>> Interestingly enough, I need to go in the reverse direction.

<snip>

> The Date::Calc module has a method
>
> Monday_of_Week
> (\$year,\$month,\$day) = Monday_of_Week(\$week,\$year);

That's probably the best way. Still, out of curiosity I gave it a try
using my favorite module Time::Local.

sub MondayOfWeek {
my (\$year, \$week) = @_;
require Time::Local;
import Time::Local 'timegm';
my \$firstday = ( gmtime timegm(0,0,0,1,0,\$year-1900) )[6];
my (\$d, \$m, \$y) = ( gmtime( (\$week-1)*604800 +
timegm(0,0,0,(1+(8-\$firstday)%7),0,\$year-1900) ) )[3..5];
sprintf '%d-%02d-%02d', \$y+1900, \$m+1, \$d
}

print MondayOfWeek( 2007, 7 ), "\n";

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl

brian d foy
Guest
Posts: n/a

 02-02-2005
In article <(E-Mail Removed)>, Gunnar Hjalmarsson
<(E-Mail Removed)> wrote:

> Martien Verbruggen wrote:

> > The Date::Calc module has a method
> >
> > Monday_of_Week
> > (\$year,\$month,\$day) = Monday_of_Week(\$week,\$year);

>
> That's probably the best way. Still, out of curiosity I gave it a try
> using my favorite module Time::Local.
>
> sub MondayOfWeek {

> }

This sort of thing makes me think there should be a big
book of Date Recipes in <Foo>, just like we have Numerical
Recipes in <Foo>.

--
brian d foy, (E-Mail Removed)
Subscribe to The Perl Review: http://www.theperlreview.com