Perl将UTC MySQL日期时间格式化为GMT / BST伦敦/欧洲字符串

时间:2019-02-06 12:46:46

标签: perl datetime

我从MySQL数据库返回了DateTime,格式为2019-01-01 01:02:03。我想将其转换为Europe/London并以以下格式输出:

2019-01-01 01:02:03 GMT (UTC +00:00)

这也应该能够处理BST日期时间,例如:

2019-07-01 01:02:03 UTC将输出2019-07-01 02:02:03 BST (UTC +01:00)

我尝试查看此question and answer,但不知道如何在提供的DateTime字符串而不是now上应用该语言环境转换?

这是我所需的伪代码,原谅我可怕的Perl ...

use DateTime;
my $my_utc_date_time = "2019-01-01 01:02:03";

# this needs to use $my_utc_date_time not now
my $start_date = DateTime->now->set_time_zone("Europe/London")->iso8601;

my $start_date_formatted = strftime("%Y-%m-%d %H:%M:%S %Z (UTC +00:00)", $start_date);

3 个答案:

答案 0 :(得分:2)

use strict;
use warnings;

use DateTime;
use DateTime::Format::Strptime;

my $my_utc_date_time = "2019-07-01 01:02:03";

my $datetime_parser = DateTime::Format::Strptime->new(pattern => '%F %T',
                                                      locale => 'en_GB',
                                                      time_zone => 'UTC');

my $datetime = $datetime_parser->parse_datetime($my_utc_date_time);
print $datetime->strftime('%FT%T %Z (UTC %z)'), "\n";

my $local_datetime = $datetime->set_time_zone('Europe/London');
print $local_datetime->strftime('%FT%T %Z (UTC %z)'), "\n";

输出:

2019-07-01T01:02:03 UTC (UTC +0000)
2019-07-01T02:02:03 BST (UTC +0100)

答案 1 :(得分:1)

有多个选项,example是 (和format

use strict;
use warnings;

use Date::Language;

my $dt = Date::Language->new('English');
print $dt->time2str("%Y-%m-%d %H:%M:%S %Z (UTC %z)", time, 'Europe/London');

结果

2019-02-06 14:08:05 EUROPE/LONDON (UTC +0000)

答案 2 :(得分:1)

出于完整性考虑,我也为Date::Manip添加了答案:

f

输出:

mark_­count()