Skip to content

Commit

Permalink
Fixing Date.getHours (#432)
Browse files Browse the repository at this point in the history
Fixes #420
  • Loading branch information
sebastienros committed Nov 3, 2017
1 parent 7564dee commit 561d8c3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 18 deletions.
9 changes: 5 additions & 4 deletions Jint.Tests/Jint.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
12 changes: 6 additions & 6 deletions Jint.Tests/Parser/JavascriptParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void ShouldParseThis()
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);
}

Expand All @@ -67,7 +67,7 @@ public void ShouldParseNull()
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);
Expand All @@ -83,7 +83,7 @@ public void ShouldParseNumeric()
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);
Expand All @@ -98,7 +98,7 @@ public void ShouldParseBinaryExpression()
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);
Expand Down Expand Up @@ -133,7 +133,7 @@ public void ShouldParseNumericLiterals(object expected, string source)
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));
}
Expand All @@ -153,7 +153,7 @@ public void ShouldParseStringLiterals(string expected, string source)
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);
}
Expand Down
24 changes: 22 additions & 2 deletions Jint.Tests/Runtime/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,7 @@ private StepMode EngineStepVerifyDebugInfo(object sender, DebugInformation debug
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);
Expand Down Expand Up @@ -1954,7 +1954,6 @@ public void ShouldEvaluateEscape(object expected, string source)
[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')")]
Expand Down Expand Up @@ -2133,5 +2132,26 @@ public void ShouldEvaluateUnescape(object expected, string source)

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);
}
}
}
5 changes: 3 additions & 2 deletions Jint/Native/Date/DateConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,11 @@ private double ConstructTimeValue(JsValue[] arguments, bool useUtc)
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; }
Expand Down
36 changes: 32 additions & 4 deletions Jint/Native/Date/DatePrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -930,22 +930,50 @@ public double Utc(double t)

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)
Expand Down

0 comments on commit 561d8c3

Please sign in to comment.