我試圖將格林威治標準時間提供的日期標記的字符串值轉換爲EST(或GMT -5)中的正確時間。我沒有掌握如何通過GMT將值傳遞給函數並返回EST值。將GMT從GMT轉換爲EST
源值是這樣的:2015年1月1日17時05分53秒
,我需要回到2015年1月1日12時05分53秒
讚賞任何幫助...
謝謝!
我試圖將格林威治標準時間提供的日期標記的字符串值轉換爲EST(或GMT -5)中的正確時間。我沒有掌握如何通過GMT將值傳遞給函數並返回EST值。將GMT從GMT轉換爲EST
源值是這樣的:2015年1月1日17時05分53秒
,我需要回到2015年1月1日12時05分53秒
讚賞任何幫助...
謝謝!
使用日期時間::格式:: Strptime獲得DateTime對象,然後設置遵循了時區爲「UTC」是「-0500」,讓您的轉換
use DateTime::Format::Strptime;
my $parser = DateTime::Format::Strptime->new(pattern => "%Y-%m-%d %H:%M:%S");
$datetime=$parse->parse_datetime("2015-01-01 17:05:53");
$datetime->set_time_zone("UTC");
$datetime->set_time_zone("-0500");
print $datetime->strftime("%Y-%m-%d %H:%M:%S");
欲瞭解更多信息,請參閱CPAN文檔:
http://search.cpan.org/~drolsky/DateTime-Format-Strptime-1.56/lib/DateTime/Format/Strptime.pm
http://search.cpan.org/~drolsky/DateTime-1.18/lib/DateTime.pm
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
# Use DateTime::Format::Strptime to parse your date string
use DateTime::Format::Strptime;
my $format = '%F %T'; # This is the format of your date/time strings
my $from_tz = 'UTC';
my $to_tz = '-0500';
# Create a parser object that knows the strings it is given are in UTC
my $parser = DateTime::Format::Strptime->new(
pattern => $format,
time_zone => $from_tz,
);
my $in_date = '2015-01-01 17:05:53';
# Use the parser to convert your string to a DateTime object
my $dt = $parser->parse_datetime($in_date);
# Use DateTime's set_time_zone() method to change the time zone
$dt->set_time_zone($to_tz);
# Print the (shifted) date/time string in the same format
say $dt->strftime($format);
POSS可重複的[我如何解析日期和在Perl中轉換時區?](http://stackoverflow.com/questions/411740/how-can-i-parse-dates-and-convert-time-zones-in- perl的) – 2015-01-27 06:57:44