Using DateTime, it's simply
$edt->subtract_datetime_absolute($sdt)->in_units('nanoseconds') / 1e9
All that's left is generating the DateTime objects. For that, DateTime::Format::Strptime is almost perfect.
use DateTime::Format::Strptime qw( );
my $sts = "2015/01/13 13:57:02.079-05:00";
my $ets = "2015/01/13 13:59:02.079-05:00";
my $format = DateTime::Format::Strptime->new(
pattern => '%Y/%m/%d %H:%M:%S.%3N%z',
on_error => 'croak',
);
my $sdt = $format->parse_datetime( $sts =~ s/:(?=ddz)//r );
my $edt = $format->parse_datetime( $ets =~ s/:(?=ddz)//r );
my $diff = $edt->subtract_datetime_absolute($sdt)->in_units('nanoseconds') / 1e9;
printf("%.3f
", $diff); # 120.000
If you want the code to also run on Perl versions older than 5.14, replace
my $sdt = $format->parse_datetime( $sts =~ s/:(?=ddz)//r );
my $edt = $format->parse_datetime( $ets =~ s/:(?=ddz)//r );
with
s/:(?=ddz)// for $sts, $ets;
my $sdt = $format->parse_datetime($sts);
my $edt = $format->parse_datetime($ets);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…