in reply to date and time difference

timelocal requires the month as a number, you're giving it a string, which is being interpreted as 0 (January). You can see this with a print gmtime($time_1)."\n";. You'd need to convert it from string to number first. Also, since your times are in GMT, you should use timegm instead.

Note that the Time::Local docs strongly recommend to use timegm_modern instead. And personally I'd strongly recommend you switch to a better module, such as Time::Piece, which has a strptime method to parse strings directly, or DateTime with DateTime::Format::Strptime. Also, your version of Perl if about 15 years old, I would strongly recommend looking into upgrading.

Replies are listed 'Best First'.
Re^2: date and time difference
by invisiblehand (Initiate) on Jan 08, 2020 at 15:19 UTC

    Oh, thanks a lot, it worked well after change the Nov to 11 as you recommended.

    And thanks for your concern for using old version, but I can't not upgrade. It is not my authority.

    Please I have another question more.

    1. My time zone is KST(South Korea republic), but when I executed following command in my system(openbsd),I got GMT time.

    In this case, Can I change the output GMT to KST?

      My time zone is KST(South Korea republic), but when I executed following command in my system(openbsd),I got GMT time.

      Which command do you mean? If you mean Perl's output, gmtime will give you GMT, and localtime will give you whatever the current time zone setting on that machine is. If you want more advanced functionality than that, then you need to switch to DateTime.

      And thanks for your concern for using old version, but I can't not upgrade. It is not my authority.

      Similar advice as in Yes, even you can use CPAN applies.

        Thanks a lot for my missing comment.

        On openbsd system, typed the command:

        SD_OCC1# openssl x509 -in /etc/isakmpd/certs/local.crt -noout -dates (enter!)

        notBefore=Nov 23 22:31:12 2019 GMT

        notAfter=Mar 19 06:52:23 2020 GMT

        At thie point, I hope to change the GMT to current my contry time zone(KST).

        Thanks in advance,

        And followings are code changed by your recommendation.

        my %MONTH_NAMES = ( "Jan" => '01', "Feb"=> '02', "Mar" => '03', "Apr +"=> '04',"May" => '05', "Jun" => '06', "Jul" => '07', "Aug" => '08', + "Sep" => '09', "Oct" => '10', "Nov" => '11', "Dec" => '12' );
        if(m/notBefore/) { chomp; ($notB1, $notB2) = split('=', $_); $notB2 =~ tr/:/ /; my($mon,$mday,$hour,$minute,$sec,$year)=split(/ /,$notB2); #print $mon, $mday, $hour, $minute, $sec, $year; $time_1 = timelocal($sec,$minute,$hour,$mday,$MONTH_NAMES{$mon},$ye +ar); print $MONTH_NAMES{$mon}; }

        2020-01-09 Athanasius fixed long code line.

      Nov is 10, actually. The month number is 0-based (for consistency with localtime).