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

Dear Monks,
I know about Date::Manip and similar CPAN modules, which are an overkill for my purpose.
All I need is to insert two time stamps generated by localtime() and get the difference in seconds.
Could you recommend me something. If you still advocate using Date::Manip for my really small task, please let me know the reasons.
  • Comment on Compact script for time difference in seconds

Replies are listed 'Best First'.
Re: Compact script for time difference in seconds
by bichonfrise74 (Vicar) on Sep 16, 2009 at 23:36 UTC
    I wonder if you can do something like this?
    #!/usr/bin/perl use strict; my $time_1 = time(); sleep 2; my $time_2 = time(); my $diff = $time_2 - $time_1; print scalar localtime( $time_1 ) . "\n"; print "Epoch Time 1: $time_1\n"; print scalar localtime( $time_2 ) . "\n"; print "Epoch Time 2: $time_2\n"; print "Difference: $diff seconds.\n";
      Thank a lot!!
      I think this one is perfect for me purpose.
Re: Compact script for time difference in seconds
by McDarren (Abbot) on Sep 17, 2009 at 08:08 UTC
    Hi :)

    This is something I find I need quite often, and I usually do something like:

    use Time::HiRes qw( time ); # If required my @t; $t[0]= time(); # do some stuff $t[1] = time(); my $diff = formatted_time_diff(@t); print "Processing took $diff seconds\n"; sub formatted_time_diff { return sprintf("%.2f", $_[1]-$_[0]) }

    Hope this helps,
    Darren

Re: Compact script for time difference in seconds
by Anonymous Monk on Sep 16, 2009 at 22:06 UTC
    All I need is to insert two time stamps generated by localtime() and get the difference in seconds.

    You should use time

    perl -le "print q!10 seconds in the future !, scalar localtime(time + +10);" 10 seconds in the future Wed Sep 16 15:05:50 2009

    which are an overkill for my purpose.

    How did you determine that? I don't know how to do it without Date::Calc ergo Date::Calc is overkill? :)

    If you still advocate using Date::Manip for my really small task

    Why suffer for a really small task?

Re: Compact script for time difference in seconds
by graff (Chancellor) on Sep 17, 2009 at 04:11 UTC
    All I need is to insert two time stamps

    Insert into what?

    generated by localtime()

    Generated where? In the same script that is inserting? Some other process? A file? A database?

    and get the difference in seconds.

    If the two time stamps have been generated within the same script that is inserting them, the difference in seconds (as mentioned in a previous reply) is the difference in the values returned by time at each moment when those time stamps were generated.

    If it's a really small task, maybe you could show us the relevant code you have (or even just pseudo-code) so we know what you're really trying to do.