TimeSpan calculation error

  • Thread starter Thread starter Hilton
  • Start date Start date
H

Hilton

Hi,

Assuming dt1 and dt0 (DateTime structs) represent times about or less than
one second apart, then using:

(dt1 - dt0).TotalSeconds (or TotalMilliSeconds, or dt1.Subtract (dt0))

returns 0.0 *or* 1.0 whereas on the desktop, .NET can return any value
between 0.0 and 1.0.

Is this a known issue?

Hilton
 
Turns out dt.MilliSeconds is always zero too - that sucks. I was hoping to
use it to calculate the timespan milliseconds.

Any simple way to get times less than a second? Any reason why this is so
crippled?

Hilton
 
Hilton skrev:
Turns out dt.MilliSeconds is always zero too - that sucks. I was hoping to
use it to calculate the timespan milliseconds.

Any simple way to get times less than a second? Any reason why this is so
crippled?
It depends on what you need this for.

If you just want some timing you can use DateTime.Now.Ticks
If you divide the number from Ticks by 10000 you get milliseconds.

long startTime = DateTime.Now.Ticks;
OperationSpendingTime();
long duration = DateTime.Now.Ticks - startTime;

MessageLog(String.Format("Used: {0}ms", (double)duration / 10000.0));
 
Hilton,

Environment.TickCount

Ruediger

Hilton said:
Turns out dt.MilliSeconds is always zero too - that sucks. I was hoping
to use it to calculate the timespan milliseconds.

Any simple way to get times less than a second? Any reason why this is so
crippled?

Hilton
 
You da man!

Thanks!

Hilton
P.S. If Environment.Ticks has less than 1seccond resolution, why not
DateTime?
 
Because ticks and the real-time clock are *NOT* the same thing. Ticks are
counted based on a 1ms interrupt. Real-time is maintained by a completely
different circuit which is in no way connected with the ticks (or
synchronized with them). The limitation on the resolution of 'time' is in
the hardware.

Paul T.
 
Back
Top