You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Perl的Time::Piece计算日志时间差遇问题及跨夜场景咨询

Perl Time::Piece计算时间差返回0及跨夜时间处理方案

问题原因分析

  1. 时间格式不匹配:你的正则捕获的时间是一位小时格式(如8:11:03),但strptime使用的%H格式要求两位小时(如08),导致解析失败,两个Time::Piece对象实际为默认值,相减结果为0。
  2. 缺失依赖模块:代码中使用了GetOptions但未声明use Getopt::Long;,可能导致参数解析异常。
  3. 未处理跨夜场景:直接相减会得到负数结果,无法正确计算跨夜耗时。

修正后的完整代码

use Time::Piece;
use Getopt::Long;

my ($S, $E);

GetOptions (
    "S=s" => \$S,
    "E=s" => \$E,
);

my $start_time;
my $end_time;

# 提取日志中的时间戳
if( ($start_time) = $S =~ /\[gg\s+(\d+\:\d+\:\d+)\s+I\]/ ){
    print "Build_Time: start at $start_time\n";
}
if( ($end_time) = $E =~ /\[gg\s+(\d+\:\d+\:\d+)\s+I\]/ ){
    print "Build_Time: end at $end_time\n";
}

# 对一位小时格式补零,适配%H的两位要求
$start_time =~ s/^(\d):/0$1:/;
$end_time =~ s/^(\d):/0$1:/;

# 解析时间字符串为Time::Piece对象
my $start_t = Time::Piece->strptime($start_time, '%H:%M:%S');
my $end_t = Time::Piece->strptime($end_time, '%H:%M:%S');

# 计算时间差(单位:秒)
my $diff_sec = $end_t - $start_t;

# 处理跨夜情况:若差值为负,加上一天的秒数(86400秒)
if ($diff_sec < 0) {
    $diff_sec += 86400;
}

# 转换为小时:分钟:秒的可读格式
my $hours = int($diff_sec / 3600);
my $minutes = int(($diff_sec % 3600) / 60);
my $seconds = $diff_sec % 60;

print "Difference: $hours:$minutes:$seconds ($diff_sec seconds)\n";

关键修正点说明

  • 补全依赖模块:添加use Getopt::Long;确保参数解析正常工作。
  • 时间格式兼容:通过正则对一位小时补零,确保strptime能正确解析时间。
  • 跨夜逻辑处理:判断时间差是否为负,若为负则加上一天的秒数,得到真实耗时。
  • 可读输出:将秒数转换为小时:分钟:秒格式,便于查看结果。

测试验证

正常时段测试

执行命令:

perl time.pl -S "[gg 8:11:03 I]: Copyright" -E "[gg 8:19:03 I]:  BT_DEPTH=99 "

输出结果:

Build_Time: start at 8:11:03
Build_Time: end at 8:19:03
Difference: 0:8:0 (480 seconds)

跨夜时段测试

执行命令:

perl time.pl -S "[gg 23:30:00 I]: Start" -E "[gg 01:15:00 I]: End"

输出结果:

Build_Time: start at 23:30:00
Build_Time: end at 01:15:00
Difference: 1:45:0 (6300 seconds)

内容的提问来源于stack exchange,提问作者Public Email

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 19:31:02