Browse Source

Supporting DateTimeOffset conversion

#19
Sebastien Ros 11 years ago
parent
commit
15de63e633
3 changed files with 34 additions and 17 deletions
  1. 15 0
      Jint.Tests/Runtime/InteropTests.cs
  2. 5 7
      Jint/Native/Date/DateConstructor.cs
  3. 14 10
      Jint/Native/JsValue.cs

+ 15 - 0
Jint.Tests/Runtime/InteropTests.cs

@@ -184,6 +184,21 @@ namespace Jint.Tests.Runtime
             ");
         }
 
+        [Fact]
+        public void DateTimeOffsetIsConvertedToDate()
+        {
+            var o = new
+            {
+                z = new DateTimeOffset(1970, 1, 1, 0, 0, 0, new TimeSpan())
+            };
+
+            _engine.SetValue("o", o);
+
+            RunTest(@"
+                assert(o.z.valueOf() === 0);
+            ");
+        }
+
         [Fact]
         public void EcmaValuesAreAutomaticallyConvertedWhenSetInPoco()
         {

+ 5 - 7
Jint/Native/Date/DateConstructor.cs

@@ -1,6 +1,5 @@
 using System;
 using System.Globalization;
-using System.Text.RegularExpressions;
 using Jint.Native.Function;
 using Jint.Native.Object;
 using Jint.Runtime;
@@ -11,12 +10,6 @@ namespace Jint.Native.Date
     public sealed class DateConstructor : FunctionInstance, IConstructor
     {
         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;
-        private const long MsPerDay = MsPerHour * 24;
-
-
 
         public DateConstructor(Engine engine) : base(engine, null, null, false)
         {
@@ -160,6 +153,11 @@ namespace Jint.Native.Date
 
         public DatePrototype PrototypeObject { get; private set; }
 
+        public DateInstance Construct(DateTimeOffset value)
+        {
+            return Construct(value.UtcDateTime);
+        }
+
         public DateInstance Construct(DateTime value)
         {
             var instance = new DateInstance(Engine)

+ 14 - 10
Jint/Native/JsValue.cs

@@ -4,7 +4,6 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics.Contracts;
 using System.Dynamic;
-using System.Linq;
 using Jint.Native.Array;
 using Jint.Native.Boolean;
 using Jint.Native.Date;
@@ -287,7 +286,7 @@ namespace Jint.Native
                 case TypeCode.Boolean:
                     return new JsValue((bool)value);
                 case TypeCode.Byte:
-                    return new JsValue((double)(byte)value);
+                    return new JsValue((byte)value);
                 case TypeCode.Char:
                     return new JsValue(value.ToString());
                 case TypeCode.DateTime:
@@ -297,23 +296,23 @@ namespace Jint.Native
                 case TypeCode.Double:
                     return new JsValue((double)value);
                 case TypeCode.Int16:
-                    return new JsValue((double)(Int16)value);
+                    return new JsValue((Int16)value);
                 case TypeCode.Int32:
-                    return new JsValue((double)(Int32)value);
+                    return new JsValue((Int32)value);
                 case TypeCode.Int64:
-                    return new JsValue((double)(Int64)value);
+                    return new JsValue((Int64)value);
                 case TypeCode.SByte:
-                    return new JsValue((double)(SByte)value);
+                    return new JsValue((SByte)value);
                 case TypeCode.Single:
-                    return new JsValue((double)(Single)value);
+                    return new JsValue((Single)value);
                 case TypeCode.String:
                     return new JsValue((string)value);
                 case TypeCode.UInt16:
-                    return new JsValue((double)(UInt16)value);
+                    return new JsValue((UInt16)value);
                 case TypeCode.UInt32:
-                    return new JsValue((double)(UInt32)value);
+                    return new JsValue((UInt32)value);
                 case TypeCode.UInt64:
-                    return new JsValue((double)(UInt64)value);
+                    return new JsValue((UInt64)value);
                 case TypeCode.Object:
                     break;
                 case TypeCode.Empty:
@@ -322,6 +321,11 @@ namespace Jint.Native
                     throw new ArgumentOutOfRangeException();
             }
 
+            if (value is DateTimeOffset)
+            {
+                    return engine.Date.Construct((DateTimeOffset)value);
+            }
+
             // if an ObjectInstance is passed directly, use it as is
             var instance = value as ObjectInstance;
             if (instance != null)