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:
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.
Thanks!
-CJ
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