Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Finding the difference between two dates including milliseconds

by Anonymous Monk
on Jan 09, 2017 at 05:52 UTC ( [id://1179200]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all - I am a bit new to PERL. I have the data in below format.
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
I like to take the difference of the two (seperated by comma). I looked at some example in monks but nothing uses the example with milliseconds. Could you help what modules I can explore?

Replies are listed 'Best First'.
Re: Finding the difference between two dates including milliseconds
by haukex (Archbishop) on Jan 09, 2017 at 06:19 UTC

    Hi Anonymous,

    What code have you tried? Have a look at perlintro, chomp, and split, that should give you what you need to know about reading the input line-by-line and splitting the string into two. Try writing some code and posting it here.

    As for the date/time handling, one way to do it (and my favorite) is this. Use DateTime::Format::Strptime to parse each date/time string into a DateTime object - the fractional part of your values can be parsed with the pattern "%6N". Note that if any time zone differences occur in your data at all, then every DateTime object needs to have its time zone set, such as by specifying the time_zone parameter to DateTime::Format::Strptime->new(...). Then, use this code to find the difference in milliseconds (note that the step of converting to nanoseconds first is required):

    my $diff_ms = $dt1->subtract_datetime_absolute($dt2) ->in_units('nanoseconds')/1e6;

    Hope this helps,
    -- Hauke D

      Looks like the DateTime object accepts the input as the below format and not the data I have
      "Mon Mar 27 05:54:08 CDT 2009";

        Hi Anonymous,

        AFAIK DateTime doesn't accept strings at all; I'm not sure where you're getting that specific format from? Again, please show the code you are trying, along with some sample input, the expected output for that sample input, and the actual output of the code including exact error messages, if any - see How do I post a question effectively?

        DateTime::Format::Strptime will accept the data in almost any format including the one in the OP, but you'll have to build an appropriate pattern according to its documentation. So something like "%Y-%m-%d ..." - scroll down in the documentation to see all the different tokens and pick the ones appropriate for your format.

        Update: Hint: Super search is your friend, among the first search results you'll find example code posted by myself and others.

        Update 2: Hm, I'm guessing you may have gotten the string "Mon Mar 27 05:54:08 CDT 2009" from this StackOverflow post. Note that that code uses Time::ParseDate, and not DateTime. Follow the links I've given you and you'll find the appropriate documentation including example code.

        Regards,
        -- Hauke D

      Modify: 2017-03-11 15:06:21.724171690 +0530 Wed Nov 9 15:14:20 2017 how to find the 'time' difference between that two files??

        Hi annonymous1,

        Modify: 2017-03-11 15:06:21.724171690 +0530 Wed Nov 9 15:14:20 2017 how to find the 'time' difference between that two files??

        You create two different DateTime::Format::Strptime objects, one for each format, and then apply all the same principles as described in the other posts in this thread.

        Note that the first value already has a time zone specified in the string, while for the second one you should specify a time_zone=>'...' value in the DateTime::Format::Strptime constructor, since it's best to have either none or all DateTime values involved in a calculation to have a time zone specified, not just a few.

        Hope this helps,
        -- Hauke D

Re: Finding the difference between two dates including milliseconds
by hippo (Bishop) on Jan 09, 2017 at 09:09 UTC

    Time::Moment has the delta_milliseconds() method which will do what your title asks and also the delta_microseconds() method which might be more appropriate for your supplied dataset.

Re: Finding the difference between two dates including milliseconds
by 1nickt (Canon) on Jan 09, 2017 at 13:51 UTC

    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.
Re: Finding the difference between two dates including milliseconds (Time::Local)
by tye (Sage) on Jan 10, 2017 at 03:42 UTC

    So simple to do with minimal prerequisites (one core module, Time::Local):

    use Time::Local qw< timegm >; while( <> ) { my @when = split /,\s*/, $_; for( @when ) { my @parts = /([0-9]+)/g; $_ = timegm( reverse( $parts[0]-1900, $parts[1]-1, @parts[2..5 +] ) ); $_ += "0.$parts[6]"; } print $when[0] - $when[1], $/; } __END__ 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 __END__ 0.256465911865234 0.253427028656006 0.256719827651978 0.253830909729004 0.255806922912598 0.253437995910645 0.255546092987061 0.253252029418945

    - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1179200]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-04-18 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found