Closed Bug 361325 Opened 18 years ago Closed 13 years ago

Incorrect behavior for date.setMonth(13) (out-of-bounds)

Categories

(Core :: JavaScript Engine, defect)

x86
Linux
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: Waldo, Unassigned)

Details

Discovered while verifying bug 361214:

If the argument to Date.prototype.setMonth() is out-of-bounds, e.g. isn't in the range 0-11 (ignoring ToInteger calls as on, say, 11.3), the date should be invalidated and its [[Value]] should be set to NaN.  This currently doesn't happen:

js> var q = new Date(2006, 0, 31);
js> q.setMonth(13)
1172898000000
js> q.toString()
Sat Mar 03 2007 00:00:00 GMT-0500 (EST)

From the spec, this specifically should occur in MakeDay in step 7, which for the example above will be called with arguments (2006, 13, 31); that step returns NaN, which propagates through the rest of the call.

This also applies to setUTCMonth, setFullYear, and setUTCFullYear; I believe these are the only functions with the flaw demonstrated here, although I didn't check exhaustively or particularly thoroughly.  (It also applies to out-of-range year values passed to the Year mutators, but I didn't test whether that broke or not because I'm not sure what the year range boundaries are.)
Step 7 of the spec performs range checks on the results of step 5 and step 6, not on the year and month that are initially provided.

Thus for your example, range checks would be performed on 

  ToInteger(2006) + floor(ToInteger(13)/12) = 2006 + 1 = 2007

and

  ToInteger(13) modulo 12 = 1

So I think that the example you provide is actually showing correct behaviour.

(Also note that this interpretation matches with the end-user documentation for setMonth: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Date:setMonth)
EcmaScript 5 
15.9.1.12 MakeDay (year, month, date)
Step 5. Let ym be y + floor(m /12).
Status: NEW → RESOLVED
Closed: 13 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.