Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 将字符串从GMT转换为EST_Perl_Datetime - Fatal编程技术网

Perl 将字符串从GMT转换为EST

Perl 将字符串从GMT转换为EST,perl,datetime,Perl,Datetime,我试图将GMT中提供的日期戳的字符串值转换为EST中的正确时间(或GMT-5)。我没有掌握如何使用GMT将值传递给函数并返回EST值 源值如下所示:2015-01-01 17:05:53 我需要返回2015-01-01 12:05:53 感谢您的帮助 谢谢 使用DateTime::Format::strtime获取DateTime对象,然后将时区设置为“UTC”,后跟“-0500”以获取转换 use DateTime::Format::Strptime; my $parser = DateTi

我试图将GMT中提供的日期戳的字符串值转换为EST中的正确时间(或GMT-5)。我没有掌握如何使用GMT将值传递给函数并返回EST值

源值如下所示:2015-01-01 17:05:53

我需要返回2015-01-01 12:05:53

感谢您的帮助


谢谢

使用DateTime::Format::strtime获取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文档:

可能存在的副本
#!/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);