0

In perl can we directly get the difference of time given in below format?

Requirement is I have to convert absolute time into relative time:

Input:

2013/06/19 05:16:51:209  INFO
2013/06/19 05:16:54:365  INFO
2013/06/19 05:16:54:365  INFO

Expected output :

000000.000000 INFO 
000003.156000 INFO
000003.156000 INFO

So here I have to take 05:16:51:209 as a reference time and make it 0 then need to subtract it with next time.

Please let me know if there is any function available for the same.

devnull
  • 118,548
  • 33
  • 236
  • 227
user381862
  • 189
  • 1
  • 4
  • 8

1 Answers1

0

Its a very complex problem (i like that :D ). But i will give you a solution (its not finished, but a huge of your problems should be cleared):

use DateTime::Format::Strptime;
my $parser = DateTime::Format::Strptime->new(
  pattern  => '%y/%m/%d %H:%M:%S:%N',
  on_error => 'croak',
);

my @dates = (
  '2013/06/19 05:16:51:209 INFO',
  '2013/06/19 05:16:54:365 INFO',
  '2013/06/19 05:16:54:365 INFO',
);

my %dates;
$dates{$_} = $parser->parse_datetime( $_ ) foreach @dates;

I use [DateTime::Format::Strptime] for parsing the Datetime and getting a DateTime Object back. Then you just need to call one of the delta-Methods of the DateTime Module to get what you need :)

Here are some links for you:

  • Here if i ignore date and take 05:16:51:209 as input, is it possible to get the difference without using any perl module. I am restricted to use the perl module here. – user381862 Aug 01 '13 at 06:16
  • If you cannot use DateTime perl module you should inform your boss that this is a very large and complex problem that will necessitate pretty much reimplementing it. See this SO post for the kind of complexities you're dealing with: http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result/6841479#6841479 – OmnipotentEntity Aug 01 '13 at 06:27
  • @user381862: If you can't use any Perl modules that aren't in the base Perl distribution, you can rewrite the code for the module. It ain't simple, but you could do it. However, laziness is a virtue...why reinvent the wheel (badly)? – Jonathan Leffler Aug 01 '13 at 06:27