Monday, January 12, 2009

Flex Date class

I found a bug in Date class of AS 3.0.

Its assumed that always date object is an idle solution for few timer based events.
But, the date object after continuous access over a period of 2 seconds will not show any
difference in the output.

Try this...

var d0:Number = (new Date() ).getTime();
var d1:Number = (new Date() ).getTime();
// iterate this.. you will get same values for both d0 and d1
trace(d0 +" - " + d1);

1 comment:

Unknown said...

Not true - they are not always equal. Check the following snippet:



var iterations:Number = 0;

var different:Number = 0;

while(true) {

iterations++;

var d0:Number = (new Date()).getTime();

var d1:Number = (new Date()).getTime();

if (d0 != d1) {

different++;

trace(d0 + " " + d1 + " " + 100 * different / iterations + "%");

}

}




The result is platform and hardware dependant - my test showed that they are different in around 0.01% of the cases (once in 10000).



Also, this is not a bug - they are almost always equal because timer resolution appears to be around 15 milliseconds (on Windows at least).