Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime precision?

Tags:

c#

.net

datetime

(ignoring the optimized compiler flag )

can it be that this code will enter the block on some systems ?

if (Datetime.Now!=Datetime.Now)
{
 ...
}

I mean , how does it evaluate the values here ? (is it by order) ?

Is there any situations where the condition might be true ?

again , ignore the optimized flag.

like image 595
Royi Namir Avatar asked Nov 06 '12 11:11

Royi Namir


People also ask

What is datetime precision?

The datetime precision specifies number of digits after the decimal dot and can be any integer number from 0 to 6. If no precision is specified it is assumed to be 0, for backward compatibility reasons. A datetime precision can be specified wherever a type name is used.

How precise is datetime now?

First, lets take a look at precision: The DateTime type is basically just a 64 bit integer that counts “ticks”. One tick is 100 nanoseconds (or 0.0001 milliseconds) long (MSDN). So DateTime 's precision can be up to 0.0001 milliseconds.

What is precision of datetime in MySQL?

MySQL permits fractional seconds for TIME , DATETIME , and TIMESTAMP values, with up to microseconds (6 digits) precision.


1 Answers

DateTime has a precision of 100ns. But on typical implementations, DateTime.Now only changes every few milliseconds.

Datetime.Now != Datetime.Now can be true, but it's extremely unlikely to happen. This is a typical race conditions of the kind you often see in multi-threaded code. i.e. you should not rely on DateTime.Now not changing, but rather store a copy in a local variable.

like image 145
CodesInChaos Avatar answered Oct 19 '22 02:10

CodesInChaos