gpt4 book ai didi

perl - 如何在 Perl 中解析 JSON 日期格式?

转载 作者:行者123 更新时间:2023-12-01 09:40:13 25 4
gpt4 key购买 nike

如何解析我的 Web 服务在 Perl 中以 JSON 格式接收的日期格式?我想将其转换为 DateTime 对象:

Date(1216647000000-0400)

我认为它是从纪元以来的毫秒数以及时区偏移,但日期已经过时了。

最佳答案

时间从纪元开始以毫秒为单位列出。除以 1000 得到纪元秒。

确保这适用于您遇到的其他情况:

use DateTime;

my $json_date = 'Date(1216647000000-0400)';
if ($json_date =~ m{ \b (\d+) \b ([+-]\d\d\d\d\b)? }x ) {
my ( $epoch_milliseconds, $time_zone ) = ( $1, $2 );
my $dt = DateTime->from_epoch( epoch => $epoch_milliseconds / 1000 );
if ($time_zone) {
$dt->set_time_zone($time_zone);
}
print $dt->datetime;
}

关于perl - 如何在 Perl 中解析 JSON 日期格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/604442/

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