Browse Source

Fixing TimeWithinDay for negative values (#549)

Fixes #383
Sébastien Ros 6 years ago
parent
commit
ce69f30a81
2 changed files with 17 additions and 6 deletions
  1. 11 0
      Jint.Tests/Runtime/EngineTests.cs
  2. 6 6
      Jint/Native/Date/DatePrototype.cs

+ 11 - 0
Jint.Tests/Runtime/EngineTests.cs

@@ -1106,6 +1106,17 @@ namespace Jint.Tests.Runtime
             Assert.Equal(0, result);
         }
 
+        [Fact]
+        public void TimeWithinDayShouldHandleNegativeValues()
+        {
+            RunTest(@"
+                // using a date < 1970 so that the primitive value is negative
+                var d = new Date(1958, 0, 1);
+                d.setMonth(-1);
+                assert(d.getDate() == 1);
+            ");
+        }
+
         [Fact]
         public void LocalDateTimeShouldNotLoseTimezone()
         {

+ 6 - 6
Jint/Native/Date/DatePrototype.cs

@@ -590,14 +590,14 @@ namespace Jint.Native.Date
         /// </summary>
         public static double TimeWithinDay(double t)
         {
-            if (t < 0)
-            {
-                return ((t % MsPerDay) + MsPerDay);
-            }
-            else
+            var result = t % MsPerDay;
+
+            if (result < 0)
             {
-                return (t % MsPerDay);
+                result += MsPerDay;
             }
+
+            return result;
         }
 
         /// <summary>