Time compare using milliseconds

P

Paulers

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.
 
T

Tad McClellan

Paulers said:
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;
}
 
P

Paulers

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

Paulers said:
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;
}
--------------------------------
 
N

news

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

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

(e-mail address removed) (Tad McClellan) replied:
convert everything to (integer) milliseconds, and then just subtract.

Paulers said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top