Time compare using milliseconds

Discussion in 'Perl Misc' started by Paulers, Jul 31, 2003.

  1. Paulers

    Paulers Guest

    Hello all,

    I have two timestamps that look like this:

    08:42:38:624
    08:42:39:437

    I need to find out the difference. I have never worked with
    milliseconds before so I was wondering if you could point me in the
    right direction.
    Paulers, Jul 31, 2003
    #1
    1. Advertising

  2. Paulers <> wrote:

    > I have two timestamps that look like this:
    >
    > 08:42:38:624
    > 08:42:39:437
    >
    > I need to find out the difference. I have never worked with
    > milliseconds before so I was wondering if you could point me in the
    > right direction.



    Huh? Your cause and effect are completely disjoint.

    Milliseconds are just like seconds, only there are 1000 of them.

    Either:

    instead of integer seconds, use floating point seconds: 38.624 seconds.

    or, better:

    convert everything to (integer) milliseconds, and then just subtract.


    --------------------------------
    #!/usr/bin/perl
    use strict;
    use warnings;

    foreach ( qw/ 08:42:38:624 08:42:39:437/ ) {
    my $millis = to_millis($_);
    print "$_ is $millis milliseconds\n";
    }

    sub to_millis {
    my( $hours, $minutes, $seconds, $millis) = split /:/, $_[0];

    $millis += 1000 * $seconds;
    $millis += 1000 * 60 * $minutes;
    $millis += 1000 * 60 * 60 * $hours;

    return $millis;
    }
    --------------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jul 31, 2003
    #2
    1. Advertising

  3. Paulers

    Paulers Guest

    Got any ideas how to account for one entry being 23:59:59:200 and
    another being 00:02:10:200?

    (Tad McClellan) wrote in message news:<>...
    > Paulers <> wrote:
    >
    > > I have two timestamps that look like this:
    > >
    > > 08:42:38:624
    > > 08:42:39:437
    > >
    > > I need to find out the difference. I have never worked with
    > > milliseconds before so I was wondering if you could point me in the
    > > right direction.

    >
    >
    > Huh? Your cause and effect are completely disjoint.
    >
    > Milliseconds are just like seconds, only there are 1000 of them.
    >
    > Either:
    >
    > instead of integer seconds, use floating point seconds: 38.624 seconds.
    >
    > or, better:
    >
    > convert everything to (integer) milliseconds, and then just subtract.
    >
    >
    > --------------------------------
    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    >
    > foreach ( qw/ 08:42:38:624 08:42:39:437/ ) {
    > my $millis = to_millis($_);
    > print "$_ is $millis milliseconds\n";
    > }
    >
    > sub to_millis {
    > my( $hours, $minutes, $seconds, $millis) = split /:/, $_[0];
    >
    > $millis += 1000 * $seconds;
    > $millis += 1000 * 60 * $minutes;
    > $millis += 1000 * 60 * 60 * $hours;
    >
    > return $millis;
    > }
    > --------------------------------
    Paulers, Aug 1, 2003
    #3
  4. Paulers

    Guest

    PLEASE don't top-post. It's not the accepted practice in this newsgroup.

    Paulers <> wrote:
    > I have two timestamps that look like this:
    > 08:42:38:624
    > 08:42:39:437
    > I need to find out the difference.


    (Tad McClellan) replied:
    > convert everything to (integer) milliseconds, and then just subtract.


    Paulers <> then asked
    > Got any ideas how to account for one entry being 23:59:59:200 and
    > another being 00:02:10:200?


    It depends on what you want the result to be. I see two possibilities
    based on your *stated* requirements, and one possibility with implicit
    assumptions.

    1. Tad's posted function returns the correct result (signed difference)

    2. Use abs() to strip the sign of Tad's posted function (unsigned
    difference)

    3. You want an implicit date to be in there too, in which case you need
    to extend the function so that if it calculates a negative result you
    add a day's worth of milliseconds to make the result positive again.

    Note that because there's no date information provided in the values
    it's impossible to handle any changes between summer and winter times,
    or the odd occasion when a leap second is added to the time.

    Chris
    --
    @s=split(//,"Je,\nhn ersloak rcet thuarP");$k=$l=@s;for(;$k;$k--){$i=($i+1)%$l
    until$s[$i];$c=$s[$i];print$c;undef$s[$i];$i=($i+(ord$c))%$l}
    , Aug 4, 2003
    #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. Michelle
    Replies:
    5
    Views:
    4,198
    Michelle
    Aug 17, 2004
  2. Al Koch
    Replies:
    5
    Views:
    11,213
    James Korman
    Jul 2, 2005
  3. Replies:
    5
    Views:
    1,523
    Ben Bacarisse
    Nov 19, 2006
  4. Replies:
    3
    Views:
    1,979
    Steven D'Aprano
    Jul 25, 2009
  5. Replies:
    9
    Views:
    294
    Michal Kleczek
    May 27, 2013
Loading...

Share This Page