in reply to Subtracting and Comparing Two Dates

# Always use gmtime and timegm when # dealing with dates without times. use Time::Local qw( timegm ); # Replace with your input. my $then_date_str = 'YYYYMMDD'; my $then_date = do { my ($y, $m, $d) = $then_date_str =~ /^(.{4})(.{2})(.{2})\z/; $m--; timegm(0, 0, 0, $d, $m, $y) }; my $now_date = do { my ($y, $m, $d) = (localtime())[5,4,3]; timegm(0, 0, 0, $d, $m, $y) }; my $diff = $now_date - $then_date; my $diff_days = int($diff / (24*60*60)); if ($diff_days < -1) { printf("%d days ago\n", -$diff_days); } elsif ($diff_days == -1) { print("yesterday\n"); } elsif ($diff_days == 1) { print("tomorrow\n"); } elsif ($diff_days > 1) { printf("%d days from now\n", $diff_days); } else { print("today\n"); }

Notes:

Update: Oops, I was subtracting from $m twice. Fixed.

Replies are listed 'Best First'.
Re^2: Subtracting and Comparing Two Dates
by BaldPenguin (Friar) on Jan 08, 2007 at 23:03 UTC
    For my own clarification, what benefits are there to using Time::Local instead of POSIX. The functionality seems to be the same. Is implemetation different? Or is it just a preference thing?

    Don
    WHITEPAGES.COM | INC
    Everything I've learned in life can be summed up in a small perl script!
      I don't see anything in POSIX similar to Time::Local's timegm. To which POSIX function were you refering?
        timegm() in Time::Local == gmtime() in POSIX

        After looking it looks like Time::Local is a Pure Perl interface to the Perl localtime objects where POSIX is an XS interface to the system libraries.

        Don
        WHITEPAGES.COM | INC
        Everything I've learned in life can be summed up in a small perl script!
Re^2: Subtracting and Comparing Two Dates
by mindful07 (Initiate) on Jan 11, 2007 at 22:29 UTC
    Right now I do not have Date::Calc installed and I'm trying to figure out how I can get the value in days of two dates. Here is my code to give you an idea of what I'm trying to accomplish.

    If I somehow can get date::calc installed could I just then do $diff_in_days = Delta_days($sysdate, $installed_date)? Also in the event I can't install date::Calc can you please give a suggestion of how else to accomplish this?

    use POSIX qw(strftime); use Time::Local; #use Date::Calc qw(:all); #use time::Piece; #use Date::Calc qw(Delta_Days); sub give_warning { my($carrier) = @_; my($installed_date); my($max_days) = 15; $installed_date = get_value($carrier ."_DAYS", $trn_info, '') ; printf("Value of Carrier Days is $installed_date \n"); if ($installed_date ne "" ) { my $sysdate = decode_date ('SYSDATE','CCYYMMDD'); printf("Todays date is $sysdate \n"); my $diff_in_days = $sysdate - $installed_date ; print "Difference in days: $diff_in_days\n"; if $result > $max_days # give error and return { &print_error ("New Installation is required."); return 1; } elsif $result <= $max_days # give warning and return { &print_warning ("New Installation is required."); return 0; } } else { return 0; } }
      Why do you want me to look at this? The code in the post to which you replied does exactly what you want.
        I apologize. I've tried so many suggestions and I thought when I tried to use local:time(timegm) it had given me an error when i tried to include it. It works just fine. Thanks again for your help!