in reply to perl basic count days between two dates

Even if you do not want to use a module, pick one that does the job and compare your code against the module's. There are so many things one can do wrong with date math that it is not worth writing one's own code.

  • Comment on Re: perl basic count days between two dates

Replies are listed 'Best First'.
Re^2: perl basic count days between two dates
by scripter87 (Novice) on Oct 02, 2013 at 20:46 UTC

    Thanks guys I am aware of the leap year but I did not want to further confuse myself so for now I am just trying to ease myself in to understanding. I added this sub routine but it only keeps 365 so even if put a date of 2 years e.g. 1-jan-2000 to 1-jan-2002 it still only gives 365 as the number of days.

    sub isFullYear{ my$startDay=shift; my$endDay=shift; my$sMon=shift; my$eMon=shift; my$fullYear; if ( NumberOfDaysBetween($startDay,$endDay ,$endDay,$eMon) == 365){ $fullYear=365; } return $fullYear; }

    this is the call for the sub in the main

    my$years= ($cy-$y) * isFullYear($d,$cd ,$m,$cm); $days= $days + NumberOfDaysBetween($d,31,$m,12) + NumberOfDaysBetween( +1,$cd,1,$cm) + $years; print "$days";

      Ok, this code:

      my$fullYear; if ( NumberOfDaysBetween($startDay,$endDay ,$endDay,$eMon) == 365){ $fullYear=365; } return $fullYear; }

      What happens when NumberOfDaysBetween() is not 365? $fullYear is undefined. Since you have warnings enabled you should see a corresponding warning message. It will still evaluate to 0, but initializing the variable "my $fullYear=0;" is better.

      Also, why call NumberOfDaysBetween() with $endDay twice as parameter? Shouldn't there be a $sMon instead?

      But if you want to program yourself you surely would prefer finding the bugs yourself instead of us just telling you what to do. So here is how:

      It is very fortunate that you are already using subroutines. You first want to make sure that your subroutines are correct. Before you can check what is wrong with the subroutine FullYear you have to be sure that NumberOfDaysBetween really does what you expect it to do. So lets do that:

      After the line "use warnings;" at the beginning of your program add the following lines:

      print "1,1,1,1 == ",NumberOfDaysBetween(1,1,1,1),"\n"; print "1,1,5,1 == ",NumberOfDaysBetween(1,1,5,1),"\n"; print "1,1,1,2 == ",NumberOfDaysBetween(1,1,1,2),"\n"; print "8,3,3,8 == ",NumberOfDaysBetween(8,3,3,8),"\n"; exit(0);

      Now call your script. Is the output as you expect it? If yes, lets be sure and put in some other values and check these too. If you are finally convinced that NumberOfDaysBetween is correct, then do the same with isFullYear(). And so on, until there is nothing left to test

      But what happens when you find unexpected results? Then you should add print-statements in that subroutine where you print out the values of variables. Or simply check which path the program takes in an if-clause. For example:

      print "daysBetween=",$daysBetween,"\n";

      directly after that variable was set would tell you if the program executed the line before and what the result was. Start the program and check if the output you are getting is as you expected. Put in as many print statements as you need, the only downside is that you have to delete them again after you found the bugs

      That is everything you need to know at the moment. Should you later have more complex data structures like arrays and hashes I recommend you take a look at the module Data::Dumper to print out those. Also you should know that there is a debugger built into perl. Instead of putting in all those print statements you can step through the program line by line and print out variables while the programm is running without having to change one line of code. But for the moment print-statements will do.

        ok using the debug method you have shown my numberofdays sub seem to be out by 1. e.g print "1,1,1,1 == " = O- which is ok. print "1,1,5,1 == " = 0 because the format of the sub is d,cd,m,cm: so when I changed it to 1,1,1,5 it printed 121- which is ok. Now here is where it got weird. "1,1,1,2 == 32 (should be 31) again print "8,3,3,8 == 149 (should be 148). How is it possible to be half correct??

        thank you very much for this debugging help, I will apply this and see how I get on, and yes that was a silly error in that sub.