Pārlūkot izejas kodu

Fixed date setters.

Dmitry Panov 8 gadi atpakaļ
vecāks
revīzija
da19abe48a
2 mainītis faili ar 50 papildinājumiem un 16 dzēšanām
  1. 16 16
      builtin_date.go
  2. 34 0
      date_test.go

+ 16 - 16
builtin_date.go

@@ -518,7 +518,7 @@ func (r *Runtime) dateproto_setMilliseconds(call FunctionCall) Value {
 		if d.isSet {
 			msec := int(call.Argument(0).ToInteger())
 			d.time = time.Date(d.time.Year(), d.time.Month(), d.time.Day(), d.time.Hour(), d.time.Minute(), d.time.Second(), msec*1e6, time.Local)
-			return intToValue(d.time.Unix() / 1e6)
+			return intToValue(d.time.UnixNano() / 1e6)
 		} else {
 			return _NaN
 		}
@@ -534,7 +534,7 @@ func (r *Runtime) dateproto_setUTCMilliseconds(call FunctionCall) Value {
 			msec := int(call.Argument(0).ToInteger())
 			t := d.time.In(time.UTC)
 			d.time = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), msec*1e6, time.UTC).In(time.Local)
-			return intToValue(d.time.Unix() / 1e6)
+			return intToValue(d.time.UnixNano() / 1e6)
 		} else {
 			return _NaN
 		}
@@ -555,7 +555,7 @@ func (r *Runtime) dateproto_setSeconds(call FunctionCall) Value {
 				nsec = d.time.Nanosecond()
 			}
 			d.time = time.Date(d.time.Year(), d.time.Month(), d.time.Day(), d.time.Hour(), d.time.Minute(), sec, nsec, time.Local)
-			return intToValue(d.time.Unix() / 1e6)
+			return intToValue(d.time.UnixNano() / 1e6)
 		} else {
 			return _NaN
 		}
@@ -577,7 +577,7 @@ func (r *Runtime) dateproto_setUTCSeconds(call FunctionCall) Value {
 				nsec = t.Nanosecond()
 			}
 			d.time = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), sec, nsec, time.UTC).In(time.Local)
-			return intToValue(d.time.Unix() / 1e6)
+			return intToValue(d.time.UnixNano() / 1e6)
 		} else {
 			return _NaN
 		}
@@ -603,7 +603,7 @@ func (r *Runtime) dateproto_setMinutes(call FunctionCall) Value {
 				nsec = d.time.Nanosecond()
 			}
 			d.time = time.Date(d.time.Year(), d.time.Month(), d.time.Day(), d.time.Hour(), min, sec, nsec, time.Local)
-			return intToValue(d.time.Unix() / 1e6)
+			return intToValue(d.time.UnixNano() / 1e6)
 		} else {
 			return _NaN
 		}
@@ -630,7 +630,7 @@ func (r *Runtime) dateproto_setUTCMinutes(call FunctionCall) Value {
 				nsec = t.Nanosecond()
 			}
 			d.time = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), min, sec, nsec, time.UTC).In(time.Local)
-			return intToValue(d.time.Unix() / 1e6)
+			return intToValue(d.time.UnixNano() / 1e6)
 		} else {
 			return _NaN
 		}
@@ -661,7 +661,7 @@ func (r *Runtime) dateproto_setHours(call FunctionCall) Value {
 				nsec = d.time.Nanosecond()
 			}
 			d.time = time.Date(d.time.Year(), d.time.Month(), d.time.Day(), hour, min, sec, nsec, time.Local)
-			return intToValue(d.time.Unix() / 1e6)
+			return intToValue(d.time.UnixNano() / 1e6)
 		} else {
 			return _NaN
 		}
@@ -693,7 +693,7 @@ func (r *Runtime) dateproto_setUTCHours(call FunctionCall) Value {
 				nsec = t.Nanosecond()
 			}
 			d.time = time.Date(d.time.Year(), d.time.Month(), d.time.Day(), hour, min, sec, nsec, time.UTC).In(time.Local)
-			return intToValue(d.time.Unix() / 1e6)
+			return intToValue(d.time.UnixNano() / 1e6)
 		} else {
 			return _NaN
 		}
@@ -707,7 +707,7 @@ func (r *Runtime) dateproto_setDate(call FunctionCall) Value {
 	if d, ok := obj.self.(*dateObject); ok {
 		if d.isSet {
 			d.time = time.Date(d.time.Year(), d.time.Month(), int(call.Argument(0).ToInteger()), d.time.Hour(), d.time.Minute(), d.time.Second(), d.time.Nanosecond(), time.Local)
-			return intToValue(d.time.Unix() / 1e6)
+			return intToValue(d.time.UnixNano() / 1e6)
 		} else {
 			return _NaN
 		}
@@ -721,8 +721,8 @@ func (r *Runtime) dateproto_setUTCDate(call FunctionCall) Value {
 	if d, ok := obj.self.(*dateObject); ok {
 		if d.isSet {
 			t := d.time.In(time.UTC)
-			d.time = time.Date(t.Year(), t.Month(), int(call.Argument(0).ToInteger()), d.time.Hour(), d.time.Minute(), d.time.Second(), d.time.Nanosecond(), time.UTC).In(time.Local)
-			return intToValue(d.time.Unix() / 1e6)
+			d.time = time.Date(t.Year(), t.Month(), int(call.Argument(0).ToInteger()), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.UTC).In(time.Local)
+			return intToValue(d.time.UnixNano() / 1e6)
 		} else {
 			return _NaN
 		}
@@ -743,7 +743,7 @@ func (r *Runtime) dateproto_setMonth(call FunctionCall) Value {
 				day = d.time.Day()
 			}
 			d.time = time.Date(d.time.Year(), month, day, d.time.Hour(), d.time.Minute(), d.time.Second(), d.time.Nanosecond(), time.Local)
-			return intToValue(d.time.Unix() / 1e6)
+			return intToValue(d.time.UnixNano() / 1e6)
 		} else {
 			return _NaN
 		}
@@ -765,7 +765,7 @@ func (r *Runtime) dateproto_setUTCMonth(call FunctionCall) Value {
 				day = t.Day()
 			}
 			d.time = time.Date(t.Year(), month, day, t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.UTC).In(time.Local)
-			return intToValue(d.time.Unix() / 1e6)
+			return intToValue(d.time.UnixNano() / 1e6)
 		} else {
 			return _NaN
 		}
@@ -794,7 +794,7 @@ func (r *Runtime) dateproto_setFullYear(call FunctionCall) Value {
 			day = d.time.Day()
 		}
 		d.time = time.Date(year, month, day, d.time.Hour(), d.time.Minute(), d.time.Second(), d.time.Nanosecond(), time.Local)
-		return intToValue(d.time.Unix() / 1e6)
+		return intToValue(d.time.UnixNano() / 1e6)
 	}
 	r.typeErrorResult(true, "Method Date.prototype.setFullYear is called on incompatible receiver")
 	panic("Unreachable")
@@ -820,8 +820,8 @@ func (r *Runtime) dateproto_setUTCFullYear(call FunctionCall) Value {
 		} else {
 			day = t.Day()
 		}
-		d.time = time.Date(year, month, day, d.time.Hour(), d.time.Minute(), d.time.Second(), d.time.Nanosecond(), time.UTC).In(time.Local)
-		return intToValue(d.time.Unix() / 1e6)
+		d.time = time.Date(year, month, day, t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.UTC).In(time.Local)
+		return intToValue(d.time.UnixNano() / 1e6)
 	}
 	r.typeErrorResult(true, "Method Date.prototype.setUTCFullYear is called on incompatible receiver")
 	panic("Unreachable")

+ 34 - 0
date_test.go

@@ -183,3 +183,37 @@ func TestDateValueOf(t *testing.T) {
 
 	testScript1(SCRIPT, intToValue(1.23e15), t)
 }
+
+func TestDateSetters(t *testing.T) {
+	const SCRIPT = `
+	assert.sameValue((new Date(0)).setMilliseconds(2345), 2345, "setMilliseconds()");
+	assert.sameValue((new Date(0)).setUTCMilliseconds(2345), 2345, "setUTCMilliseconds()");
+	assert.sameValue((new Date(0)).setSeconds(12), 12000, "setSeconds()");
+	assert.sameValue((new Date(0)).setUTCSeconds(12), 12000, "setUTCSeconds()");
+	assert.sameValue((new Date(0)).setMinutes(12), 12 * 60 * 1000, "setMinutes()");
+	assert.sameValue((new Date(0)).setUTCMinutes(12), 12 * 60 * 1000, "setUTCMinutes()");
+	assert.sameValue((new Date("2016-06-01")).setHours(1), 1464739200000, "setHours()");
+	assert.sameValue((new Date("2016-06-01")).setUTCHours(1), 1464742800000, "setUTCHours()");
+	assert.sameValue((new Date(0)).setDate(2), 86400000, "setDate()");
+	assert.sameValue((new Date(0)).setUTCDate(2), 86400000, "setUTCDate()");
+	assert.sameValue((new Date(0)).setMonth(2), 5097600000, "setMonth()");
+	assert.sameValue((new Date(0)).setUTCMonth(2), 5097600000, "setUTCMonth()");
+	assert.sameValue((new Date(0)).setFullYear(1971), 31536000000, "setFullYear()");
+	assert.sameValue((new Date(0)).setFullYear(1971, 2, 3), 36806400000, "setFullYear(Y,M,D)");
+	assert.sameValue((new Date(0)).setUTCFullYear(1971), 31536000000, "setUTCFullYear()");
+	assert.sameValue((new Date(0)).setUTCFullYear(1971, 2, 3), 36806400000, "setUTCFullYear(Y,M,D)");
+
+	`
+
+	l := time.Local
+	defer func() {
+		time.Local = l
+	}()
+	var err error
+	time.Local, err = time.LoadLocation("Europe/London")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	testScript1(TESTLIB+SCRIPT, _undefined, t)
+}