Browse Source

Fixing Date.getHours (#432)

Fixes #420
Sébastien Ros 7 years ago
parent
commit
561d8c3101

+ 5 - 4
Jint.Tests/Jint.Tests.csproj

@@ -21,10 +21,11 @@
     <ProjectReference Include="..\Jint\Jint.csproj" />
   </ItemGroup>
   <ItemGroup>
-    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
-    <PackageReference Include="xunit" Version="2.2.0" />
-    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
-    <PackageReference Include="xunit.analyzers" Version="0.3.0" />
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
+    <PackageReference Include="xunit" Version="2.3.1" />
+    <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
+    <PackageReference Include="xunit.analyzers" Version="0.7.0" />
+    <PackageReference Include="xunit.runner.console" Version="2.3.1" />
   </ItemGroup>
   <ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
     <Reference Include="System" />

+ 6 - 6
Jint.Tests/Parser/JavascriptParserTests.cs

@@ -56,7 +56,7 @@ namespace Jint.Tests.Parser
             var body = program.Body;
 
             Assert.NotNull(body);
-            Assert.Equal(1, body.Count());
+            Assert.Single(body);
             Assert.Equal(SyntaxNodes.ThisExpression, body.First().As<ExpressionStatement>().Expression.Type);
         }
 
@@ -67,7 +67,7 @@ namespace Jint.Tests.Parser
             var body = program.Body;
 
             Assert.NotNull(body);
-            Assert.Equal(1, body.Count());
+            Assert.Single(body);
             Assert.Equal(SyntaxNodes.Literal, body.First().As<ExpressionStatement>().Expression.Type);
             Assert.Equal(null, body.First().As<ExpressionStatement>().Expression.As<Literal>().Value);
             Assert.Equal("null", body.First().As<ExpressionStatement>().Expression.As<Literal>().Raw);
@@ -83,7 +83,7 @@ namespace Jint.Tests.Parser
             var body = program.Body;
 
             Assert.NotNull(body);
-            Assert.Equal(1, body.Count());
+            Assert.Single(body);
             Assert.Equal(SyntaxNodes.Literal, body.First().As<ExpressionStatement>().Expression.Type);
             Assert.Equal(42d, body.First().As<ExpressionStatement>().Expression.As<Literal>().Value);
             Assert.Equal("42", body.First().As<ExpressionStatement>().Expression.As<Literal>().Raw);
@@ -98,7 +98,7 @@ namespace Jint.Tests.Parser
             var body = program.Body;
 
             Assert.NotNull(body);
-            Assert.Equal(1, body.Count());
+            Assert.Single(body);
             Assert.NotNull(binary = body.First().As<ExpressionStatement>().Expression.As<BinaryExpression>());
             Assert.Equal(3d, binary.Right.As<Literal>().Value);
             Assert.Equal(BinaryOperator.Times, binary.Operator);
@@ -133,7 +133,7 @@ namespace Jint.Tests.Parser
             var body = program.Body;
 
             Assert.NotNull(body);
-            Assert.Equal(1, body.Count());
+            Assert.Single(body);
             Assert.NotNull(literal = body.First().As<ExpressionStatement>().Expression.As<Literal>());
             Assert.Equal(Convert.ToDouble(expected), Convert.ToDouble(literal.Value));
         }
@@ -153,7 +153,7 @@ namespace Jint.Tests.Parser
             var body = program.Body;
 
             Assert.NotNull(body);
-            Assert.Equal(1, body.Count());
+            Assert.Single(body);
             Assert.NotNull(literal = body.First().As<ExpressionStatement>().Expression.As<Literal>());
             Assert.Equal(expected, literal.Value);
         }

+ 22 - 2
Jint.Tests/Runtime/EngineTests.cs

@@ -1479,7 +1479,7 @@ namespace Jint.Tests.Runtime
             Assert.NotNull(debugInfo.CurrentStatement);
             Assert.NotNull(debugInfo.Locals);
 
-            Assert.Equal(1, debugInfo.CallStack.Count);
+            Assert.Single(debugInfo.CallStack);
             Assert.Equal("func1()", debugInfo.CallStack.Peek());
             Assert.Contains(debugInfo.Globals, kvp => kvp.Key.Equals("global", StringComparison.Ordinal) && kvp.Value.AsBoolean() == true);
             Assert.Contains(debugInfo.Globals, kvp => kvp.Key.Equals("local", StringComparison.Ordinal) && kvp.Value.AsBoolean() == false);
@@ -1954,7 +1954,6 @@ namespace Jint.Tests.Runtime
         [InlineData("%uE", "unescape('%uE')")]
         [InlineData("%uf", "unescape('%uf')")]
         [InlineData("%uF", "unescape('%uF')")]
-        [InlineData("%u00", "unescape('%u00')")]
         [InlineData("%u01", "unescape('%u01')")]
         [InlineData("%u02", "unescape('%u02')")]
         [InlineData("%u03", "unescape('%u03')")]
@@ -2133,5 +2132,26 @@ namespace Jint.Tests.Runtime
 
             Assert.Equal(expected, result);
         }
+
+        [Theory]
+        [InlineData("new Date(1969,0,1,19,45,30,500).getHours()", 19)]
+        [InlineData("new Date(1970,0,1,19,45,30,500).getHours()", 19)]
+        [InlineData("new Date(1971,0,1,19,45,30,500).getHours()", 19)]
+        [InlineData("new Date(1969,0,1,19,45,30,500).getMinutes()", 45)]
+        [InlineData("new Date(1970,0,1,19,45,30,500).getMinutes()", 45)]
+        [InlineData("new Date(1971,0,1,19,45,30,500).getMinutes()", 45)]
+        [InlineData("new Date(1969,0,1,19,45,30,500).getSeconds()", 30)]
+        [InlineData("new Date(1970,0,1,19,45,30,500).getSeconds()", 30)]
+        [InlineData("new Date(1971,0,1,19,45,30,500).getSeconds()", 30)]
+        //[InlineData("new Date(1969,0,1,19,45,30,500).getMilliseconds()", 500)]
+        //[InlineData("new Date(1970,0,1,19,45,30,500).getMilliseconds()", 500)]
+        //[InlineData("new Date(1971,0,1,19,45,30,500).getMilliseconds()", 500)]
+        public void ShouldExtractDateParts(string source, double expected)
+        {
+            var engine = new Engine();
+            var result = engine.Execute(source).GetCompletionValue().ToObject();
+
+            Assert.Equal(expected, result);
+        }
     }
 }

+ 3 - 2
Jint/Native/Date/DateConstructor.cs

@@ -169,10 +169,11 @@ namespace Jint.Native.Date
                 y += 1900;
             }
 
-            var finalDate = DatePrototype.MakeDate(DatePrototype.MakeDay(y, m, dt),
+            var finalDate = DatePrototype.MakeDate(
+                DatePrototype.MakeDay(y, m, dt),
                 DatePrototype.MakeTime(h, min, s, milli));
 
-            return useUtc ? finalDate : PrototypeObject.Utc(finalDate);
+            return TimeClip(useUtc ? finalDate : PrototypeObject.Utc(finalDate));
         }
 
         public DatePrototype PrototypeObject { get; private set; }

+ 32 - 4
Jint/Native/Date/DatePrototype.cs

@@ -930,22 +930,50 @@ namespace Jint.Native.Date
 
         public static double HourFromTime(double t)
         {
-            return System.Math.Floor(t / MsPerHour) % HoursPerDay;
+            var hours = System.Math.Floor(t / MsPerHour) % HoursPerDay;
+
+            if (hours < 0)
+            {
+                hours += HoursPerDay;
+            }
+
+            return hours;
         }
 
         public static double MinFromTime(double t)
         {
-            return System.Math.Floor(t / MsPerMinute) % MinutesPerHour;
+            var minutes = System.Math.Floor(t / MsPerMinute) % MinutesPerHour;
+
+            if (minutes < 0)
+            {
+                minutes += MinutesPerHour;
+            }
+
+            return minutes;
         }
 
         public static double SecFromTime(double t)
         {
-            return System.Math.Floor(t / MsPerSecond) % SecondsPerMinute;
+            var seconds = System.Math.Floor(t / MsPerSecond) % SecondsPerMinute;
+
+            if (seconds < 0)
+            {
+                seconds += SecondsPerMinute;
+            }
+
+            return seconds;
         }
 
         public static double MsFromTime(double t)
         {
-            return t % MsPerSecond;
+            var milli = t % MsPerSecond;
+
+            if (milli < 0)
+            {
+                milli += MsPerSecond;
+            }
+
+            return milli;
         }
 
         public static double DayFromMonth(double year, double month)