Date::Manip week-of-year oddness

Discussion in 'Perl Misc' started by CJ Kucera, Jul 14, 2004.

  1. CJ Kucera

    CJ Kucera Guest

    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,
    | _The Hitchhiker's Guide To The Galaxy_
     
    CJ Kucera, Jul 14, 2004
    #1
    1. Advertising

  2. CJ Kucera

    Steven Kuo Guest

    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
     
    Steven Kuo, Jul 15, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    605
    Matthew Speed
    Nov 8, 2003
  2. Otuatail

    Week of year to full Year

    Otuatail, Dec 8, 2003, in forum: HTML
    Replies:
    4
    Views:
    968
    Toby A Inkster
    Dec 8, 2003
  3. Matt
    Replies:
    2
    Views:
    519
    Pete Becker
    Nov 8, 2003
  4. Replies:
    4
    Views:
    351
    Jorgen Grahn
    Sep 17, 2005
  5. J. Muenchbourg
    Replies:
    0
    Views:
    147
    J. Muenchbourg
    Aug 9, 2003
Loading...

Share This Page