Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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; } #--------------------------------------------------------------------- +--------

Comment on days between dates
Download Code
Re: days between dates
by SuicideJunkie (Priest) 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 Ratazong (Prior) on Mar 15, 2013 at 14:27 UTC

    Hi!

    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 choroba (Abbot) 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 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;
Re: days between dates
by vagabonding electron (Hermit) 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".

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023704]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-12-27 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls