Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

days between dates

by StayTheCourse (Initiate)
on Mar 15, 2013 at 14:14 UTC ( #1023704=perlquestion: print w/replies, xml ) Need Help??
StayTheCourse has asked for the wisdom of the Perl Monks concerning the following question:

Unix and my pc give different answers when running this code. pc returns 39843 and unix returns 41338 and unix gives the correct answer.

#!/bin/perl58 use Time::Local; $date='3/5/2013'; $dbt=&daysbetween('1/1/1904',$date); $dbt += 1462;# for 1900-1903 plus 2 days warn "$dbt $date and 1/1/1900\n"; #--------------------------------------------------------------------- +-------- sub daysbetween { my($dte,$dte1)= @_; my ( $mo, $dy, $yr) = split /\//, $dte; my ( $mo1, $dy1, $yr1) = split /\//, $dte1; $mo = $mo - 1; $mo1 = $mo1 - 1; if($dy > 31 || $dy < 1){ warn "$transid $dte $dte1\n"; return 1000000; } if($dy1 > 31 || $dy1 < 1){ warn "$transid $dte $dte1\n"; return 1000000; } if($mo > 11 || $mo < 0){ warn "$transid $dte $dte1\n"; return 1000000; } if($mo1 > 11 || $mo1 < 0){ warn "$transid $dte $dte1\n"; return 1000000; } if($yr > 2037){ warn "$transid $dte $dte1\n"; return 1000000; } if($yr1 > 2037){ warn "$transid $dte $dte1\n"; return 1000000; } my $ltime = timelocal ( 0, 0, 1, $dy, $mo, $yr ); my $ltime1 = timelocal ( 0, 0, 1, $dy1, $mo1, $yr1 ); my $days=sprintf("%.0f",($ltime1-$ltime)/86400); return $days; } #--------------------------------------------------------------------- +--------

Replies are listed 'Best First'.
Re: days between dates
by Ratazong (Monsignor) on Mar 15, 2013 at 14:27 UTC


    If you are doing date-calculations, I strongly recommend not to re-invent the wheel, but to use a standard module. I have very good experiences with Date::Calc, which provides a function called Delta_Days. And which is consistent on any platform.

    use Date::Calc qw (Delta_Days); print Delta_Days (1904,1,1, 2013,3,5) + 1462 , "\n";

    HTH, Rata

Re: days between dates
by SuicideJunkie (Vicar) on Mar 15, 2013 at 14:22 UTC

    Your first step should be to print out the contents of your variables at each step and see where your assumptions become false.

    The zeroth step should be to use a CPAN module to do the date math for you since it will handle all the edge cases you missed.

Re: days between dates
by choroba (Chancellor) on Mar 15, 2013 at 14:45 UTC
    Another CPAN module to give the answer: Date::Manip.
    #!/usr/bin/perl use strict; use warnings; use Date::Manip; my $delta = DateCalc(ParseDate('1/1/1904'), ParseDate('3/5/2013')); print 1462 + Delta_Format($delta, 0,'%dt'), "\n";
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: days between dates
by vagabonding electron (Chaplain) on Mar 16, 2013 at 15:35 UTC
    Yet another option could be with Time::Piece (a core module now):
    #!/usr/bin/perl use strict; use warnings; use Time::Piece; my $datf = Time::Piece->strptime("1/1/1904", "%d/%m/%Y"); my $datt = Time::Piece->strptime("5/3/2013", "%d/%m/%Y"); my $diff = $datt - $datf; my $result = $diff->days + 1462; print "$result days\n";
    This prints "41338 days".
Re: days between dates
by pvaldes (Chaplain) on Mar 15, 2013 at 15:15 UTC

    Maybe $dte1 is not what you think. Check:

        my($dte,$dte1)= @_;

    try with:

    my $dte = $_[0]; my $dte1 = $_[1]; print "dte is :",$dte," and dte1 is: ",$dte1;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023704]
Approved by marto
[Corion]: The bots bow in response
[Discipulus]: so karlgoethebier, what this idiom is and where i can read about it? i tried to deparse and gives error..;=(
[karlgoethebier]: Eily: non-people == persone non grata?
[Your Mother]: Corion: I have lost the link to the pmdev guide(s). Do you have that handy?
[karlgoethebier]: Discipulus: see per lop please how to avoid the classic <c>perl 'rm -rfv *|'</>
[Eily]: karlgoethebier: more like grata non persona :P

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (12)
As of 2017-07-21 08:10 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (319 votes). Check out past polls.