我想使用DateTime::Format::Duration
添加两个持续时间。
这是我的代码
my $format = DateTime::Format::Duration->new(
pattern => '%H:%M:%S,%3N'
);
my $dur1 = $format->format_duration('01:59:58,923');
my $dur2 = $format->format_duration('05:01:02,456');
my $total = $dur1->add_duration( $dur2 );
print "\nDuration : " . $total;
当我执行时,我收到错误
Can't call method "deltas" without a package or object reference at D:/Strawberry/perl/site/lib/DateTime/Format/Duration.pm line 131.
我期望的结果应采用以下格式:hh:mm:ss,sss
。 (n.b.两个持续时间的总和不超过24小时)
编辑1
my $dur1 = $format->parse_duration('01:59:58,923');
my $dur2 = $format->parse_duration('05:01:02,456');
my $total1 = $dur1->add_duration( $dur2 );
my $total2 = $dur1 + $dur2;
print "\nTotal Duration 1: " . $format->parse_duration($total1);
print "\nTotal Duration 2: " . $format->parse_duration($total2);
这是我得到的输出:
总持续时间1:DateTime :: Duration = HASH(0x2be8564)
总持续时间2:DateTime :: Duration = HASH(0x2be6d74)
编辑2:
这是我的新代码:
my $dur1 = $format->parse_duration('01:59:51,923');
my $dur2 = $format->parse_duration('05:21:02,456');
my $total = $dur1->add_duration( $dur2 );
my ($h, $m, $s, $ns) = $total->in_units(qw( hours minutes seconds ));
my $ns = ( $total )->nanoseconds; # delta_ms loses these.
if ( $ns >= 1000 ) {
$ns = $ns - 1000;
$s = $s + 1;
}
# print $ns ."\n\n";
my $final = sprintf '%d:%02d:%02d,%03.0f', $h, $m, $s, $ns;
print $final;
显然,它完成了这项工作。我想知道你对它的看法。
编辑3
执行此代码后执行:
my $dur1 = $format->parse_duration('00:00:50,339');
my $dur2 = $format->parse_duration('00:00:58,686');
my $total = $dur1->add_duration( $dur2 );
my ($h, $m, $s) = $total->in_units(qw( hours minutes seconds ));
my $ns = ( $total )->nanoseconds;
if ( $ns >= 1000 ) {
$ns = $ns - 1000;
$s = $s + 1;
}
# 00:00:02,339 00:00:00,686
my $duration = sprintf '%d:%02d:%02d,%03.0f', $h, $m, $s, $ns;
print "\nDuration : " . $duration;
这是输出:Duration : 0:00:109,025
。这似乎是显而易见的,因为DateTime::Duration->add_duration
没有进行“内部”转换。
答案 0 :(得分:2)
您有 两个 不同的对象:
您需要做的是创建一个具有两个持续时间的DateTime::Duration
,然后使用DateTime::Format::Duration
对象创建一个格式供他们使用。
#! /usr/bin/env perl
#
use strict; # Lets you know when you misspell variable names
use warnings; # Warns of issues (using undefined variables
use feature qw(say);
use DateTime::Duration;
use DateTime::Format::Duration;
#
# First Create the Durations
#
my $dur1 = DateTime::Duration->new(
hours => 1,
minutes => 59,
seconds => 58,
nanoseconds => 923,
);
my $dur2 = DateTime::Duration->new(
hours => 5,
minutes => 59,
seconds => 02,
nanoseconds => 923,
);
#
# Add the two durations together.
# This creates a new DateTime::Duration object
#
my $total = $dur1->add($dur2);
#
# Great. It all works. Let's create a format, so we can print it out
#
my $format = DateTime::Format::Duration->new(
pattern => '%H:%M:%S,%3N',
normalize => 1, # Normalize (i.e. minutes can't be more than 59)
);
#
# Use 'format_duration` to create a string representation of the duration
#
say "Duration: " . $format->format_duration($total);
跟踪所使用的所有各种对象类型有时会让人感到困惑,但了解方法使用和返回的内容非常重要。我发现阅读和理解概要示例非常重要,以便真正了解正在发生的事情。这来自DateTime::Format::Duration概要:
use DateTime::Format::Duration;
$d = DateTime::Format::Duration->new(
pattern => '%Y years, %m months, %e days, '.
'%H hours, %M minutes, %S seconds'
);
print $d->format_duration(
DateTime::Duration->new(
years => 3,
months => 5,
days => 1,
hours => 6,
minutes => 15,
seconds => 45,
nanoseconds => 12000
)
);
我正在创建格式对象类型,然后使用format_duration
方法将其打印出来,但format_duration
方法需要DateTime::Duration
个对象!在format_duration
方法下查看POD,它说:
format_duration($ datetime_duration_object)
是的,这很有帮助。 $datetime_duration_object
假设是什么?文档可以使用一些工作。幸运的是,概要很清楚。