Hi Anonymous Monk,

You can use DateTime and friends to do this, comparing with subtract_datetime_absolute(), but you'll have to start with a delta in nanoseconds. The code below shows the difference in milliseconds as specified, including fractions.

use strict; use warnings; use feature 'say'; use DateTime::Format::Strptime; my $parser = DateTime::Format::Strptime->new( pattern => '%F %T.%6N', on_error => 'croak', ); while ( <DATA> ) { chomp; my ( $t2, $t1 ) = split /, /; my $dt2 = $parser->parse_datetime( $t2 ); my $dt1 = $parser->parse_datetime( $t1 ); my $delta = $dt2->subtract_datetime_absolute( $dt1 ); my $delta_ms = sprintf( '%.3f', $delta->in_units('nanoseconds') / 1_000_000, ); say for ( $t2, $t1, "Differs by $delta_ms ms", '' ); } __DATA__ 2017-01-09 05:08:04.575804, 2017-01-09 05:08:04.319338 2017-01-09 05:08:17.093719, 2017-01-09 05:08:16.840292 2017-01-09 05:12:48.629407, 2017-01-09 05:12:48.372687 2017-01-09 05:13:25.315665, 2017-01-09 05:13:25.061834 2017-01-09 05:14:08.171994, 2017-01-09 05:14:07.916187 2017-01-09 05:17:03.182747, 2017-01-09 05:17:02.929309 2017-01-09 05:18:46.561289, 2017-01-09 05:18:46.305743 2017-01-09 05:19:09.772227, 2017-01-09 05:19:09.518975
Output:
perl 1179200.pl 2017-01-09 05:08:04.575804 2017-01-09 05:08:04.319338 Differs by 256.466 ms 2017-01-09 05:08:17.093719 2017-01-09 05:08:16.840292 Differs by 253.427 ms 2017-01-09 05:12:48.629407 2017-01-09 05:12:48.372687 Differs by 256.720 ms 2017-01-09 05:13:25.315665 2017-01-09 05:13:25.061834 Differs by 253.831 ms 2017-01-09 05:14:08.171994 2017-01-09 05:14:07.916187 Differs by 255.807 ms 2017-01-09 05:17:03.182747 2017-01-09 05:17:02.929309 Differs by 253.438 ms 2017-01-09 05:18:46.561289 2017-01-09 05:18:46.305743 Differs by 255.546 ms 2017-01-09 05:19:09.772227 2017-01-09 05:19:09.518975 Differs by 253.252 ms

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re: Finding the difference between two dates including milliseconds by 1nickt
in thread Finding the difference between two dates including milliseconds by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.