gpt4 book ai didi

perl - 如何转换 yyyy-mm-dd hh :mm:ss into UTC in Perl?

转载 作者:行者123 更新时间:2023-12-01 10:14:48 26 4
gpt4 key购买 nike

将日期时间格式 yyyy-mm-dd hh:mm:ss(可能是字符串)转换为 UTC,查看 DateTime,但我看不到如何解析字符串?

更新:

这工作正常吗?

require 5.002;

use strict;
use warnings;

use DateTime::Format::DateManip;

my $string = '2010-02-28 00:00:00';

my @dates = (
$string
);

for my $date ( @dates ) {
my $dt = DateTime::Format::DateManip->parse_datetime( $date );
die "Cannot parse date $date, Please use a valid date in this format 'yyyy-mm-dd mm:hh:ss'" unless defined $dt;
print $dt."\n";
$dt->set_time_zone( 'UTC' );
print $dt."Z\n"; # Is this correct???
}

最佳答案

use DateTime::Format::ISO8601;
my $dt = DateTime::Format::ISO8601->parse_datetime('yyyy-mm-ddThh:mm:ss');

使用 DateTime::Format::ISO8601模块来解析该格式并返回一个 DateTime对象。

关于perl - 如何转换 yyyy-mm-dd hh :mm:ss into UTC in Perl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2104491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com