http://www.perlmonks.org?node_id=1019521

luca76 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I think I've found a bug in Date::Calc with add_delta_days function. I get a week number and a year in my function and I want to calculate the date starting and ending that week. To calculate it, I use supplied Monday_of_week (first day), then I add 6 days to it to get the Sunday. Trying this function with:
print weeksToDays ( 1, 2013) ; (1st week of 2013)
it returns:
31/12/2012 to 8/1/2012 (it should be ...to 6/1/2013)
while:
print weeksToDays ( 2, 2013) ; (2nd week of 2013)
it returns:
7/1/2013 to 13/1/2013
which is correct.
use Date::Calc qw ( Monday_of_Week Add_Delta_Days ); sub weekToDays { $week = shift; $year = shift; ($year,$month,$day) = Monday_of_Week($week,$year); ($y, $m, $d) = Add_Delta_Days( Monday_of_Week( $week, $year ), 6); return "$day/$month/$year to $d/$m/$y"; } print "First week of 2013: ".weekToDays ( 1, 2013) ."\n" ; print "Second week of 2013: ".weekToDays ( 2, 2013) ."\n" ;

Update: Thank you to all. One word: I'm a dumb idiot :D

Replies are listed 'Best First'.
Re: big bug in Date::Calc?
by choroba (Cardinal) on Feb 19, 2013 at 11:30 UTC
    Use strict and warnings. Do not use global variables.
    #!/usr/bin/perl use warnings; use strict; use Date::Calc qw ( Monday_of_Week Add_Delta_Days ); sub weekToDays { my $week = shift; my $year = shift; my ($year2, $month, $day) = Monday_of_Week($week, $year); my ($y, $m, $d) = Add_Delta_Days( Monday_of_Week($week, $year), 6) +; # Not $year2 here! return "$day/$month/$year2 to $d/$m/$y"; } print "First week of 2013: ".weekToDays ( 1, 2013) ."\n" ; print "Second week of 2013: ".weekToDays ( 2, 2013) ."\n" ;

    Output:

    First week of 2013: 31/12/2012 to 6/1/2013 Second week of 2013: 7/1/2013 to 13/1/2013
    Update: You can even avoid calling the function again when you already know the result:
    my ($y, $m, $d) = Add_Delta_Days($year2, $month, $day, 6);
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: big bug in Date::Calc?
by tangent (Parson) on Feb 19, 2013 at 11:30 UTC
    I think the problem is that you are calling Monday_of_Week() twice:
    ($y, $m, $d) = Add_Delta_Days( Monday_of_Week( $week, $year ), 6); # Change line above to: ($y, $m, $d) = Add_Delta_Days( $year, $month, $day, 6); First week of 2013: 31/12/2012 to 6/1/2013 Second week of 2013: 7/1/2013 to 13/1/2013
Re: big bug in Date::Calc?
by ww (Archbishop) on Feb 19, 2013 at 11:36 UTC
    Looks to me like the bug is in your logic. You're working with the LAST day of 2012 (2012 Dec 31, Mon) but expecting that to be treated as the first day of the first week of 2013.

    Yes, it's part of a week that's MOSTLY in 2013, but Date::Calc isn't on board for that very human interpretation. Like most programs, it's quite literal minded. Day 1 of 2013 is 2013 Jan 1, Tues, and 2013 Jan 8 is one week later.


    If you didn't program your executable by toggling in binary, it wasn't really programming!

Re: big bug in Date::Calc?
by ww (Archbishop) on Feb 19, 2013 at 15:07 UTC

    Whoops! It's another bug (or something):   "dumb idiot" look a lot like TWO (2) words where I come from.   :-)

    And please, don't delete text of a question. It leaves the answers without context... which just might be helpful to some future worshipper.


    If you didn't program your executable by toggling in binary, it wasn't really programming!

Re: big bug in Date::Calc?
by sundialsvc4 (Abbot) on Feb 19, 2013 at 13:55 UTC

    Please don’t remove the content of a post once you have solved the problem.   Someone else will eventually have the same problem.   We are all “dumb idiots,” merely at differing times.