Re: Numeric Date Validation
Dr John Stockton wrote:[color=blue]
> Richard Cornford posted:[color=green]
>>I make it 6-10% faster than Lasse's original (with !!ML). The version
>>I was using for testing was:-
>>
>>var DateOK2b = (function(){
>> var __months = [NaN,31,28,31,30 ,31,30,31,31,30 ,31,30,31]
>> return (function(Y, M, D){
>> return (D <= __months[M])? (D > 0) :
>> (D==29) &&
>> (M==2) &&
>> !( ((Y&3)||((Y&15) &&(!(Y%25))) ) );
>> });
>>})();[/color]
>
> OK. But && M==2 seems superfluous.[/color]
It is necessary as M <= 0, or M >= 13 will refer to undefined (or NaN at
index zero) values and - D <= __months[M]) - will type convert those
undefined values into NaN for the comparison. All comparisons with NaN
return false, entering the branch where M == 2 is tested in order to
eliminate those out-of-range possibilities (which may still coincide
with D == 29, tested first as that would literally be a coincidence).
But I thought that testing M was better than using a local variable -
var ML = __months[M] - and applying a type-converting test to ML instead
(as it produces a boolean result in one operation and avoids the
assignment and additional work in variable instantiation).
[color=blue]
> Should NaN (instead of
> blank) be faster?[/color]
I assume you mean starting the array - [NaN,31,28 - instead of -
[,31,28, -. It should be very fractionally faster when M == 0 as it
avoids a type-conversion to NaN from undefined, but I used it because I
don't trust javascript implementations to handle sparse array literals
consistently, so I avoid them when I can.
[color=blue]
> It's not obvious how best to speed-test - one should cover all 1461
> good dates in 4 years, or 146097 in 400, but how many & which bad
> ones?[/color]
I thought it only fair to use some month values outside of the
acceptable range along with some dates of zero or less and greater than
31 while comparing the various functions. But optimisation should be the
fast verification of correct date combinations as users should get those
right more often than not.
[color=blue]
> I wonder why the using Date Object is so slow; possibly in order to
> accommodate out-of-range fields.[/color]
If you think IE's Date object is slow you haven't seen Opera 7.11
struggling through my first test script. :)
[color=blue]
> I've been re-looking at Zeller's Concordance (and have been in
> communication with one of his great-grandsons). In converting Y M D
> to day-of-week, Zeller separated out the century, which seems simpler
> for manual arithmetic; but, at least on a computer, ISTM better not
> to :- <URL:http://www.merlyn.demo n.co.uk/zeller-c.htm#DDCC>
> boxes 1 & 3
> Box 3 gives a much faster way of converting Y M D to a day count than
> using a Date Object (at least, in my system).
>
> That function is repeated at
> <URL:http://www.merlyn.demo n.co.uk/dayscale.htm#CD D> Box 1
> and Box 2 gives an inverse. ISTM unlikely that one can do much better
> for the forward function; but the inverse might be improvable ....[/color]
I am going to be very pressed for time over the next couple of days but
I will have a look at that when I get the chance.
Richard.
Dr John Stockton wrote:[color=blue]
> Richard Cornford posted:[color=green]
>>I make it 6-10% faster than Lasse's original (with !!ML). The version
>>I was using for testing was:-
>>
>>var DateOK2b = (function(){
>> var __months = [NaN,31,28,31,30 ,31,30,31,31,30 ,31,30,31]
>> return (function(Y, M, D){
>> return (D <= __months[M])? (D > 0) :
>> (D==29) &&
>> (M==2) &&
>> !( ((Y&3)||((Y&15) &&(!(Y%25))) ) );
>> });
>>})();[/color]
>
> OK. But && M==2 seems superfluous.[/color]
It is necessary as M <= 0, or M >= 13 will refer to undefined (or NaN at
index zero) values and - D <= __months[M]) - will type convert those
undefined values into NaN for the comparison. All comparisons with NaN
return false, entering the branch where M == 2 is tested in order to
eliminate those out-of-range possibilities (which may still coincide
with D == 29, tested first as that would literally be a coincidence).
But I thought that testing M was better than using a local variable -
var ML = __months[M] - and applying a type-converting test to ML instead
(as it produces a boolean result in one operation and avoids the
assignment and additional work in variable instantiation).
[color=blue]
> Should NaN (instead of
> blank) be faster?[/color]
I assume you mean starting the array - [NaN,31,28 - instead of -
[,31,28, -. It should be very fractionally faster when M == 0 as it
avoids a type-conversion to NaN from undefined, but I used it because I
don't trust javascript implementations to handle sparse array literals
consistently, so I avoid them when I can.
[color=blue]
> It's not obvious how best to speed-test - one should cover all 1461
> good dates in 4 years, or 146097 in 400, but how many & which bad
> ones?[/color]
I thought it only fair to use some month values outside of the
acceptable range along with some dates of zero or less and greater than
31 while comparing the various functions. But optimisation should be the
fast verification of correct date combinations as users should get those
right more often than not.
[color=blue]
> I wonder why the using Date Object is so slow; possibly in order to
> accommodate out-of-range fields.[/color]
If you think IE's Date object is slow you haven't seen Opera 7.11
struggling through my first test script. :)
[color=blue]
> I've been re-looking at Zeller's Concordance (and have been in
> communication with one of his great-grandsons). In converting Y M D
> to day-of-week, Zeller separated out the century, which seems simpler
> for manual arithmetic; but, at least on a computer, ISTM better not
> to :- <URL:http://www.merlyn.demo n.co.uk/zeller-c.htm#DDCC>
> boxes 1 & 3
> Box 3 gives a much faster way of converting Y M D to a day count than
> using a Date Object (at least, in my system).
>
> That function is repeated at
> <URL:http://www.merlyn.demo n.co.uk/dayscale.htm#CD D> Box 1
> and Box 2 gives an inverse. ISTM unlikely that one can do much better
> for the forward function; but the inverse might be improvable ....[/color]
I am going to be very pressed for time over the next couple of days but
I will have a look at that when I get the chance.
Richard.
Comment