Difference between log timestamps

Discussion in 'Perl Misc' started by R C V, Mar 18, 2008.

  1. R C V

    R C V Guest

    Hi,
    Can someone tell if they know of any perl modules which can
    find the difference between 2 timestamps in a log file..

    The timestamp format is as follows:
    20080317 130146543
    20080317 130156581

    So the difference here is 38 msec.

    Thanks !
    R C
     
    R C V, Mar 18, 2008
    #1
    1. Advertising

  2. R C V

    Guest

    On Mar 17, 6:08 pm, R C V <> wrote:
    > Hi,
    > Can someone tell if they know of any perl modules which can
    > find the difference between 2 timestamps in a log file..
    >
    > The timestamp format is as follows:
    > 20080317 130146543
    > 20080317 130156581
    >
    > So the difference here is 38 msec.


    Just construct DateTime objects and subtract:

    my $dt1 = DateTime->new(
    year => 2008,
    month => 03,
    day => 17,
    hour => 13,
    minute => 01,
    second => 46,
    nanosecond => 543 * 1000000,
    );

    my $dt2 = DateTime->new(
    year => 2008,
    month => 03,
    day => 17,
    hour => 13,
    minute => 01,
    second => 56,
    nanosecond => 581 * 1000000,
    );

    my $duration = $dt2 - $dt1;

    print $duration->in_units('days', 'hours', 'seconds', 'nanoseconds'),
    "\n";

    /sandy
    http://myperlquiz.com/
     
    , Mar 18, 2008
    #2
    1. Advertising

  3. R C V

    Guest

    On Mar 17, 6:36 pm, wrote:
    > On Mar 17, 6:08 pm, R C V <> wrote:


    > nanosecond => 543 * 1000000,


    Sorry, too many zeros, but you got an idea

    /sandy
    http://myperlquiz.com/
     
    , Mar 18, 2008
    #3
  4. R C V wrote:
    > Can someone tell if they know of any perl modules which can
    > find the difference between 2 timestamps in a log file..


    Most Perl programmers can tell you about a whole bunch of modules that
    can help you do that.

    > The timestamp format is as follows:
    > 20080317 130146543
    > 20080317 130156581


    I for one would make use of the most(?) useful date/time module Time::Local.

    use Time::Local;
    sub timediff {
    my @t;
    foreach ( @_ ) {
    my ($y, $mo, $d, $h, $min, $s, $ms) =
    /^(\d{4})(\d{2})(\d{2})\s+(\d{2})(\d{2})(\d{2})(\d{3})$/
    or die "Failed to parse timestamp '$_'";
    push @t, timelocal($s, $min, $h, $d, $mo-1, $y) + $ms/1000;
    }
    sprintf '%.3f', $t[1]-$t[0];
    }

    my $ts1 = '20080317 130146543';
    my $ts2 = '20080317 130156581';

    print "ts1: $ts1\n",
    "ts2: $ts2\n",
    'time diff: ', timediff($ts1, $ts2), " seconds\n";

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 18, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chumma Dede
    Replies:
    2
    Views:
    413
    Chumma Dede
    Feb 11, 2005
  2. Craig G

    Using SQL TimeStamps in datagrid

    Craig G, Feb 28, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    456
    bruce barker
    Mar 1, 2005
  3. jakk
    Replies:
    4
    Views:
    12,510
  4. sconeek

    Comparint timestamps

    sconeek, Nov 29, 2005, in forum: Java
    Replies:
    4
    Views:
    439
    Roedy Green
    Nov 29, 2005
  5. nukeymusic
    Replies:
    10
    Views:
    559
    nukeymusic
    Dec 18, 2011
Loading...

Share This Page