2015-01-27 63 views
0

我试图将格林威治标准时间提供的日期标记的字符串值转换为EST(或GMT -5)中的正确时间。我没有掌握如何通过GMT将值传递给函数并返回EST值。将GMT从GMT转换为EST

源值是这样的:2015年1月1日17时05分53秒

,我需要回到2015年1月1日12时05分53秒

赞赏任何帮助...

谢谢!

+0

POSS可重复的[我如何解析日期和在Perl中转换时区?](http://stackoverflow.com/questions/411740/how-can-i-parse-dates-and-convert-time-zones-in- perl的) – 2015-01-27 06:57:44

回答

-1

使用日期时间::格式:: 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

1
#!/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);