Date::Manip week-of-year oddness

C

CJ Kucera

Hello, everyone. I'm having an issue with Date::Manip which has
been driving me bonkers.

Essentially, what I want to do is, given a date, generate a
datestamp representing the Sunday of the week the given date is in.
For instance, if I pass in "2004-06-17" it should give me 2004-06-13
back. It does this, actually, without problems, until I try
it with a week that goes into any previous year or any forthcoming
year, at which time it consistently gives me values that are exactly
a week past what I'm looking for. Here's some simplified code:
#!/usr/bin/perl -w

use strict;
use Date::Manip;

my $startdate;
my $enddate;
my $tempdate;
$tempdate = ParseDate("2004-01-01");
print "Interpreted date as " . UnixDate($tempdate, "%c") . "\n";
print "Going to try \"" . UnixDate($tempdate, "sunday week %U %L") . "\"\n";
$startdate = ParseDate(UnixDate($tempdate, "sunday week %U %L"));
$enddate = DateCalc($startdate, "+1 week");

print "Start: " . UnixDate($startdate, "%c") . "\n";
print "End: " . UnixDate($enddate, "%c") . "\n";

The output from that is:
Interpreted date as Thu Jan 1 00:00:00 2004
Going to try "sunday week 53 2003"
Start: Sun Jan 4 00:00:00 2004
End: Sun Jan 11 00:00:00 2004

The problem seems to be with the %U in the UnixDate. It's telling me
that I need to look at the 53rd week of 2003, when I actually want
to be looking at the 52nd week of 2003. I've read through the perldoc
for Date::Manip a number of times in the sections that deal with this
kind of week calculation, but I'm pretty sure I'm using the correct
format options for UnixDate. I'm using DateManip 5.42a, btw.

Thanks!

-CJ
 
S

Steven Kuo

Hello, everyone. I'm having an issue with Date::Manip which has
been driving me bonkers.

Essentially, what I want to do is, given a date, generate a
datestamp representing the Sunday of the week the given date is in.
For instance, if I pass in "2004-06-17" it should give me 2004-06-13
back. It does this, actually, without problems, until I try
it with a week that goes into any previous year or any forthcoming
year, at which time it consistently gives me values that are exactly
a week past what I'm looking for. Here's some simplified code:


The output from that is:


The problem seems to be with the %U in the UnixDate. It's telling me
that I need to look at the 53rd week of 2003, when I actually want
to be looking at the 52nd week of 2003. I've read through the perldoc
for Date::Manip a number of times in the sections that deal with this
kind of week calculation, but I'm pretty sure I'm using the correct
format options for UnixDate. I'm using DateManip 5.42a, btw.




The Date::Calc module does fine. I've not tried Date::Manip.

use Date::Calc qw(
Week_of_Year
Monday_of_Week
Add_Delta_Days
Date_to_Text
) ;

my ($year, $month, $day, $week);

( $week, $year)
= Week_of_Year (2004, 01, 01);

( $year, $month, $day)
= Add_Delta_Days (
Monday_of_Week ($week, $year),
-1
);

print Date_to_Text($year, $month, $day) . "\n";

__END__
Sun 28-Dec-2003
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top