فهرست منبع

Fixing some Date tests

Sebastien Ros 11 سال پیش
والد
کامیت
99e29590e2
3فایلهای تغییر یافته به همراه42 افزوده شده و 13 حذف شده
  1. 22 6
      Jint/Native/Date/DateConstructor.cs
  2. 4 4
      Jint/Native/Date/DateInstance.cs
  3. 16 3
      Jint/Native/Date/DatePrototype.cs

+ 22 - 6
Jint/Native/Date/DateConstructor.cs

@@ -9,7 +9,7 @@ namespace Jint.Native.Date
 {
     public sealed class DateConstructor : FunctionInstance, IConstructor
     {
-        private static readonly DateTime Origin = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
+        internal static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
         private const long MsPerSecond = 1000;
         private const long MsPerMinute = MsPerSecond * 60;
         private const long MsPerHour = MsPerMinute * 60;
@@ -51,9 +51,25 @@ namespace Jint.Native.Date
             var date = TypeConverter.ToString(arguments.At(0));
 
 
-            if (!DateTime.TryParseExact(date, new[] { "yyyy-MM-ddTH:m:s.fffK", "yyyy-MM-dd", "yyyy-MM", "yyyy", "THH:m:s.fff", "TH:mm:sm", "THH:mm", "THH" }, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out result))
+            if (!DateTime.TryParseExact(date, new[]
             {
-                throw new JavaScriptException(Engine.SyntaxError, "Invalid date");
+                "yyyy/MM/ddTH:m:s.fffK", 
+                "yyyy/MM/dd", 
+                "yyyy/MM", 
+                "yyyy-MM-ddTH:m:s.fffK", 
+                "yyyy-MM-dd", 
+                "yyyy-MM", 
+                "yyyy", 
+                "THH:m:s.fff", 
+                "TH:mm:sm", 
+                "THH:mm", 
+                "THH"
+            }, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out result))
+            {
+                if (!DateTime.TryParse(date, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out result))
+                {
+                    throw new JavaScriptException(Engine.SyntaxError, "Invalid date");
+                }
             }
 
             return Construct(result);
@@ -67,7 +83,7 @@ namespace Jint.Native.Date
 
         private JsValue Now(JsValue thisObj, JsValue[] arguments)
         {
-            return (DateTime.UtcNow - Origin).TotalMilliseconds;
+            return (DateTime.UtcNow - Epoch).TotalMilliseconds;
         }
 
         public override JsValue Call(JsValue thisObject, JsValue[] arguments)
@@ -130,7 +146,7 @@ namespace Jint.Native.Date
                                          + seconds*MsPerSecond
                                          + minutes*MsPerMinute
                                          + hours*MsPerHour
-                                         + date*MsPerDay;
+                                         + (date-1)*MsPerDay;
 
                     return Construct(primitiveValue);
                 }
@@ -184,7 +200,7 @@ namespace Jint.Native.Date
 
         public static double FromDateTime(DateTime dt)
         {
-            return (dt.ToUniversalTime() - Origin).TotalMilliseconds;
+            return (dt.ToUniversalTime() - Epoch).TotalMilliseconds;
         }
     }
 }

+ 4 - 4
Jint/Native/Date/DateInstance.cs

@@ -7,10 +7,10 @@ namespace Jint.Native.Date
     public class DateInstance : ObjectInstance
     {
         // Maximum allowed value to prevent DateTime overflow
-        private static readonly double Max = (DateTime.MaxValue - new DateTime(170, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
+        internal static readonly double Max = (DateTime.MaxValue - new DateTime(170, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
 
         // Minimum allowed value to prevent DateTime overflow
-        private static readonly double Min = -(new DateTime(170, 1, 1, 0, 0, 0, DateTimeKind.Utc) - DateTime.MinValue).TotalMilliseconds;
+        internal static readonly double Min = -(new DateTime(170, 1, 1, 0, 0, 0, DateTimeKind.Utc) - DateTime.MinValue).TotalMilliseconds;
 
         public DateInstance(Engine engine)
             : base(engine)
@@ -29,11 +29,11 @@ namespace Jint.Native.Date
         {
             if (double.IsNaN(PrimitiveValue) || PrimitiveValue > Max || PrimitiveValue < Min)
             {
-                return DateTime.MinValue;
+                throw new JavaScriptException(Engine.RangeError);
             }
             else
             {
-                return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(PrimitiveValue);
+                return DateConstructor.Epoch.AddMilliseconds(PrimitiveValue);
             }
         }
 

+ 16 - 3
Jint/Native/Date/DatePrototype.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Globalization;
+using Jint.Parser.Ast;
 using Jint.Runtime;
 using Jint.Runtime.Interop;
 
@@ -411,17 +412,29 @@ namespace Jint.Native.Date
 
         private JsValue ToUTCString(JsValue thisObj, JsValue[] arguments)
         {
-            return thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
+            return thisObj.TryCast<DateInstance>(x =>
+            {
+                throw new JavaScriptException(Engine.TypeError);
+            } )
+            .ToDateTime().ToUniversalTime().ToString("r");
         }
 
         private JsValue ToISOString(JsValue thisObj, JsValue[] arguments)
         {
-            return thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
+            return thisObj.TryCast<DateInstance>(x =>
+            {
+                throw new JavaScriptException(Engine.TypeError);
+            })
+           .ToDateTime().ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
         }
 
         private JsValue ToJSON(JsValue thisObj, JsValue[] arguments)
         {
-            return thisObj.TryCast<DateInstance>().ToDateTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
+            return thisObj.TryCast<DateInstance>(x =>
+            {
+                throw new JavaScriptException(Engine.TypeError);
+            })
+           .ToDateTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
         }
     }
 }