Velocity Reviews > Perl > Time compare using milliseconds

# Time compare using milliseconds

Paulers
Guest
Posts: n/a

 07-31-2003
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.

Tad McClellan
Guest
Posts: n/a

 07-31-2003
Paulers <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas

Paulers
Guest
Posts: n/a

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

(E-Mail Removed) (Tad McClellan) wrote in message news:<(E-Mail Removed)>.. .
> Paulers <(E-Mail Removed)> 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;
> }
> --------------------------------

news@roaima.freeserve.co.uk
Guest
Posts: n/a

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

Paulers <(E-Mail Removed)> wrote:
> 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 Removed) (Tad McClellan) replied:
> convert everything to (integer) milliseconds, and then just subtract.

Paulers <(E-Mail Removed)> 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}

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post scriptlearner@gmail.com Python 3 07-25-2009 06:18 AM pedro.ballester@gmail.com C Programming 5 11-19-2006 11:45 AM Al Koch Java 5 07-02-2005 01:26 AM Michelle Java 5 08-17-2004 05:44 AM Thomas Baier C++ 6 09-22-2003 01:51 AM

Advertisments