I want to convert GMT time string to my system time zone. Ex. Tue Nov 04 22:03:03 2014 GMT
My machine time zone is PST, so output should be : 2014-11-04 14:03:03 PST
I can do this in bash but could not find any solution for perl.
Bash solution=> timestamp_local= date "+%Y-%m-%d %H:%M:%S %Z" -d "$timestamp_GMT"
Anyone have solution in perl?
PS: I have to process a huge file ( around 100-200MB of text file ). So, I want a optimized solution.
Simple enough with DateTime and friends.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use DateTime::Format::Strptime;
use DateTime;
my $format = '%a %b %d %H:%M:%S %Y %Z';
my $time_string = 'Tue Nov 04 22:03:03 2014 GMT';
my $dt_p = DateTime::Format::Strptime->new(
pattern => $format,
time_zone => 'UTC',
);
my $time = $dt_p->parse_datetime($time_string);
say $time->strftime('%a %b %d %H:%M:%S %Y %Z');
$time->set_time_zone('America/Los_Angeles');
say $time->strftime('%a %b %d %H:%M:%S %Y %Z');
Update: And this old answer shows how to do something very similar with the core module Time::Piece.
POSIX library should be enough to do this;
use strict;
use feature qw/say/;
use POSIX qw(strftime tzset);
say strftime("%Y %d %m %H:%M:%S GMT", gmtime(time)); # GMT
say strftime("%Y %d %m %H:%M:%S %Z", localtime(time)); # Local Time
# Set to custom timezone
$ENV{TZ} = 'America/Los_Angeles';
tzset;
say strftime("%Y %d %m %H:%M:%S %Z", localtime(time)); # Custom Zone
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.