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"; }

In reply to Re^5: date and time difference by haukex
in thread date and time difference by invisiblehand

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.