Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Date::Manip week-of-year oddness

Reply
Thread Tools

Date::Manip week-of-year oddness

 
 
CJ Kucera
Guest
Posts: n/a
 
      07-14-2004
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

--
WOW: Kakistocracy | "The ships hung in the sky in much the same
apocalyptech.com/wow | way that bricks don't." - Douglas Adams,
http://www.velocityreviews.com/forums/(E-Mail Removed) | _The Hitchhiker's Guide To The Galaxy_
 
Reply With Quote
 
 
 
 
Steven Kuo
Guest
Posts: n/a
 
      07-15-2004
On Wed, 14 Jul 2004, CJ Kucera wrote:

> 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.





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

--
Hope this helps,
Steven

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TRY/CATCH ODDNESS Obi Wan Shinobi Java 6 04-15-2006 03:48 AM
float and clear oddness requires border? Uncle Pirate HTML 2 08-12-2004 01:56 AM
XP User Profile Oddness XP Newb Computer Support 2 11-24-2003 06:38 AM
ASP.NET DateTime.Parse oddness Kevin Kenny ASP .Net 3 08-10-2003 11:02 PM
"High Fidelity" oddness Berndt Jobst DVD Video 1 07-05-2003 01:37 PM



Advertisments