Ashes.cfg has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Comparing two dates and finding out number of days

Replies are listed 'Best First'.
Re: Comparing two dates and finding out number of days
by erroneousBollock (Curate) on Nov 15, 2007 at 15:29 UTC
    I am not allowed to use any new modules as this script has to be run on multiple computers.
    That's just silly. If your admins don't allow new modules to be installed, use the Perl Archive Toolkit to bundle up requisite modules into archives to be distributed with your script.

    Use Date::Calc like this:

    use Date::Calc qw/Delta_Days/; use strict; my @date1 = (2007, 12, 24); # or split '-', '2006-12-24'; my @date2 = (2009, 4, 16); # or split '-', '2009-4-16'; my $days = Delta_Days(@date1, @date2); print "There are $days days between ". sprintf('%04d-%02d-%02d',@date1)." and ". sprintf('%04d-%02d-%02d',@date1).".\n";

    -David

Re: Comparing two dates and finding out number of days
by eric256 (Parson) on Nov 15, 2007 at 16:59 UTC

    POSIX includes a mktime function wich takes (seconds, minutes, hours, day (0 based), month (o based), year (1900 based)) and returns the epoch seconds. So if you use it on each date to get there seconds, then subtract and divide by the number of seconds in a day you get the number of days seperating two days.

    use strict; use warnings; my $first = "11/15/2007"; my $second = "12/25/2007"; use POSIX qw/mktime/; sub days_between { my ($start, $end) = @_; my ($m1, $d1, $y1) = split ("/", $start); my ($m2, $d2, $y2) = split ("/", $end); my $diff = mktime(0,0,0, $d2-1, $m2-1, $y2 - 1900) - mktime(0 +,0,0, $d1-1, $m1-1, $y1 - 1900); return $diff / (60*60*24); } print "Between $first and $second there are ", days_between($first, $s +econd), " days\n";

    I have no idea if this handles leap anything but I feel pretty safe in assuming that it does ;)


    ___________
    Eric Hodges
Re: Comparing two dates and finding out number of days
by thezip (Vicar) on Nov 15, 2007 at 17:11 UTC

    Here's a link to something that might be helpful:

    The time strings might be in a little different format, but you'll get the idea. (BTW, you'll also need to change from seconds elapsed to days elapsed.)


    Where do you want *them* to go today?
Re: Comparing two dates and finding out number of days
by moritz (Cardinal) on Nov 15, 2007 at 16:16 UTC
    From the conversation in the CB I gathered that you tried to install Date::Simple, which is the right tool for the job.

    Why don't you ask how to install it? In the CB it didn't sound like you weren't allowed to install it.

Re: Comparing two dates and finding out number of days
by Sixtease (Friar) on Nov 15, 2007 at 15:34 UTC

    I don't understand what you mean by "any new modules". Does it mean you can only use the core modules?

    I would do this with the help of DateTime and DateTime::Duration.

    Coding this by hand could prove quite tedious. In what format do you have the dates in the variables?

Re: Comparing two dates and finding out number of days
by RaduH (Scribe) on Nov 15, 2007 at 16:29 UTC
    I may be wrong here, but this sounds like a homework question, where the school's sysadmin won't allow you to install new modules ... The fact that you have not solved it at all (not even an approximate solution...) in about 1 week supports my suspicion. This is a classic homework problem in C/C++ programming courses, when they teach you about enums...

    How about transforming both dates in days and finding the difference? Yes, you will need to account for leap years. How about finding the number of days till the end of the month of the first date, then the rest of the year, then add years until the second date, then add (days in the) months until the month number in the second date, then the few days left? Yes, you will also need to account for leap years too (multiple of 4, not of 100, but also multiple of 400). Sounds like an array there index i holds the number of days in that month (then you do the trick with the leap year verification).

    Again, since I suspect this is homework, I will stop here and not post any code. This should help plenty I think.

      homework.Well I am well past homework stage. here is the deal. I am a telecommunications engineer and trying to learn how to script in perl as my manager wants me to write some scripts for him :) . I would never even ask fellow monks for code for my homework. i would love to just install Date::Simple and simplify my problem. I have tried even installing it but my PPM doesnt even detect it when i refresh. I dont know what the problem is. i used time::local and tried finding out diff between two dates in seconds but i dont know i am getting really weird answers. but i get what you said. I shall try tht..if not then God Help :)

        When all else fails install manually:

        1. download the tarball
        2. decompress it,
        3. cd to the directory created, something like Date-Simple-3.02,
        4. perl Makefile.PL will create a make file for you,
        5. make test will ensure that everything is working nicely,
        6. make all will install the module locally in the directory you are working in,
        7. The module will now be in the blib/lib directory; copy it to your development lib directory, use lib on that directory, and you can use Date::Simple.
        perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'
        I have tried even installing it but my PPM doesnt even detect it when i refresh.

        I personally believe you may want to add some repositories to your PPM's configuration.