in reply to Re^3: date and time difference
in thread date and time difference

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.

Replies are listed 'Best First'.
Re^5: date and time difference
by haukex (Archbishop) on Jan 08, 2020 at 19:20 UTC
    And followings are code changed by your recommendation.

    Not really, you're still using timelocal instead of timegm_modern (and the month numbers are still off by one).

    As I said, for best results, use DateTime. For the other solutions, if your local system isn't set to the right time zone, you could try forcing it via local $ENV{TZ}='Asia/Seoul';, but that's not guaranteed to work and may not be portable.

    use warnings; use strict; my $date = "Nov 23 22:31:12 2019 GMT"; print "Input date: $date\n"; { use Time::Local qw/timegm_modern/; my %MONTH_NAMES = ( Jan=>0, Feb=>1, Mar=>2, Apr=>3, May=>4, Jun=>5, Jul=>6, Aug=>7, Sep=>8, Oct=>9, Nov=>10, Dec=>11 ); my ($mon,$mday,$hour,$minute,$sec,$year) = split /\s+|:/, $date; my $tm = timegm_modern($sec,$minute,$hour,$mday, $MONTH_NAMES{$mon},$year); print "timegm+gmtime: ", gmtime($tm)." GMT\n"; print "timegm+localtime: ", localtime($tm)." Local\n"; } # - OR - { use Time::Piece; my $tm = Time::Piece->strptime($date, '%b %d %H:%M:%S %Y %Z'); print "Time::Piece: ", $tm->strftime, "\n"; print "Time::Piece+localtime: ", localtime($tm->epoch)." Local\n"; } # - OR - { use DateTime; use DateTime::Format::Strptime; my $strp = DateTime::Format::Strptime->new( pattern => '%b %d %H:%M:%S %Y %Z', on_error => 'croak'); my $dt = $strp->parse_datetime($date); print "DateTime: ",$dt->strftime("%Y-%m-%dT%H:%M:%S%z (%Z)"),"\n"; $dt->set_time_zone('Asia/Seoul'); print "DateTime: ",$dt->strftime("%Y-%m-%dT%H:%M:%S%z (%Z)"),"\n"; }

      Hi, Haukex

      Thanks a lot for your very kindly answer

      I tried to execute the script but got the following error message.

      It looked there were not the module in my perl version.

      I am sorry to say whether there is another way to solve in my perl version?

      Thanks in advance,

      sd_loader# perl ./test1.pl

      "timegm_modern" is not exported by the Time::Local module

      Can't continue after import errors at ./test1.pl line 8

      BEGIN failed--compilation aborted at ./test1.pl line 8.

      sd_loader# perl ./test1.pl

      Can't locate Time/Piece.pm in @INC (@INC contains: /usr/libdata/perl5/i386-openbsd/5.8.8 /usr/local/libdata/perl5/i386-openbsd/5.8.8 /usr/libdata/perl5 /usr/local/libdata/perl5 /usr/local/libdata/perl5/site_perl/i386-openbsd /usr/libdata/perl5/site_perl/i386-openbsd /usr/local/libdata/perl5/site_perl /usr/libdata/perl5/site_perl /usr/local/lib/perl5/site_perl .) at ./test1.pl line 9.

      BEGIN failed--compilation aborted at ./test1.pl line 9.

      sd_loader# perl ./test1.pl

      Can't locate DateTime.pm in @INC (@INC contains: /usr/libdata/perl5/i386-openbsd/5.8.8 /usr/local/libdata/perl5/i386-

      openbsd/5.8.8 /usr/libdata/perl5 /usr/local/libdata/perl5 /usr/local/libdata/perl5/site_perl/i386-

      openbsd /usr/libdata/perl5/site_perl/i386-openbsd /usr/local/libdata/perl5/site_perl /usr/libdata/perl5/site_perl /usr/local/lib/perl5/site_perl .) at ./test1.pl line 9.

      BEGIN failed--compilation aborted at ./test1.pl line 9.

        "timegm_modern" is not exported by the Time::Local module

        That function wasn't added to Time::Local until version 1.27; Perl 5.8.7 shipped with version 1.11. In all three cases, the best option is to update/install the modules.

        And the bad "solution" is to use timegm instead of timegm_modern. In that case, at least prefix it with die "Bad year $year" unless $year>1000;.

        It looked there were not the module in my perl version.

        Hi,

        The solution is to install the module, its much simpler than reinventing the module.

        cpan Time::Piece