http://www.perlmonks.org?node_id=1179201


in reply to Finding the difference between two dates including milliseconds

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

Replies are listed 'Best First'.
Re^2: Finding the difference between two dates including milliseconds
by Anonymous Monk on Jan 09, 2017 at 06:21 UTC
    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

Re^2: Finding the difference between two dates including milliseconds
by annonymous1 (Initiate) on Jan 11, 2017 at 09:48 UTC
    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