Skip to content

Commit

Permalink
Removed manual conversion to local time in date conversions
Browse files Browse the repository at this point in the history
Calling `ToLocalTime()` leads to changes of the hour component. Since in
JavaScript Date is basically simply a number starting from
1970-01-01 UTC we should not do any conversions here. This also means
that the test which simply checked that `new Date(2010, 9, 10)` equals
`new DateTime(2010, 10, 10)` was wrong. C# does not do any conversions
by default, so `new DateTime(2010, 10, 10)` does not specificy any
timezone. The JavaScript code should match the UTC Date of
2010-10-10 (00:00:00) converted to the local timezone, for example,
2010-10-09 (22:00:00) for MESZ (GMT+2).
  • Loading branch information
Erik Schilling committed Sep 21, 2018
1 parent 3adb5d4 commit 96a0ecd
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Source/Noesis.Javascript/JavascriptInterop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,10 @@ JavascriptInterop::ConvertObjectFromV8(Handle<Object> iObject, ConvertedObjects
System::DateTime^
JavascriptInterop::ConvertDateFromV8(Handle<Value> iValue)
{
System::DateTime^ startDate = gcnew System::DateTime(1970, 1, 1);
System::DateTime^ startDate = gcnew System::DateTime(1970, 1, 1, 0, 0, 0, 0, System::DateTimeKind::Utc);
double milliseconds = iValue->NumberValue();
System::TimeSpan^ timespan = System::TimeSpan::FromMilliseconds(milliseconds);
return System::DateTime(timespan->Ticks + startDate->Ticks).ToLocalTime();
return System::DateTime(timespan->Ticks + startDate->Ticks).ToLocalTime();
}

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions Source/Noesis.Javascript/SystemInterop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ SystemInterop::ConvertToSystemString(std::string iString)
double
SystemInterop::ConvertFromSystemDateTime(System::DateTime^ iDateTime)
{
System::DateTime^ startDate = gcnew System::DateTime(1970, 1, 1);
System::TimeSpan^ timespan = *iDateTime - *startDate;
System::DateTime^ startDate = gcnew System::DateTime(1970, 1, 1, 0, 0, 0, 0, System::DateTimeKind::Utc);
System::TimeSpan^ timespan = iDateTime->ToUniversalTime() - *startDate;

return timespan->TotalMilliseconds;
}
Expand Down
8 changes: 0 additions & 8 deletions Tests/Noesis.Javascript.Tests/ConvertFromJavascriptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,6 @@ public void ReadBooleanTrue()
_context.GetParameter("myBool").Should().BeOfType<bool>().Which.Should().BeTrue();
}

[TestMethod]
public void ReadDate()
{
_context.Run("var myDate = new Date(2010,9,10)");

_context.GetParameter("myDate").Should().BeOfType<DateTime>().Which.Should().Be(new DateTime(2010, 10, 10));
}

[TestMethod]
public void ReadObject()
{
Expand Down
10 changes: 0 additions & 10 deletions Tests/Noesis.Javascript.Tests/ConvertToJavascriptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,6 @@ public void SetBoolean()
_context.Run("val === true").Should().BeOfType<bool>().Which.Should().BeTrue();
}

[TestMethod]
public void SetDateTime()
{
_context.SetParameter("val", new DateTime(2010, 10, 10, 0, 0, 0, DateTimeKind.Utc));

_context.Run("val.getUTCFullYear()").Should().BeOfType<int>().Which.Should().Be(2010);
_context.Run("val.getUTCMonth()").Should().BeOfType<int>().Which.Should().Be(9);
_context.Run("val.getUTCDate()").Should().BeOfType<int>().Which.Should().Be(10);
}

[TestMethod]
public void SetObject()
{
Expand Down
76 changes: 76 additions & 0 deletions Tests/Noesis.Javascript.Tests/DateTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FluentAssertions;

namespace Noesis.Javascript.Tests
{
[TestClass]
public class DateTest
{
private JavascriptContext _context;

[TestInitialize]
public void SetUp()
{
_context = new JavascriptContext();
}

[TestCleanup]
public void TearDown()
{
_context.Dispose();
}

[TestMethod]
public void SetDateTime()
{
_context.SetParameter("val", new DateTime(2010, 10, 10, 0, 0, 0, DateTimeKind.Utc));

_context.Run("val.getUTCFullYear()").Should().BeOfType<int>().Which.Should().Be(2010);
_context.Run("val.getUTCMonth()").Should().BeOfType<int>().Which.Should().Be(9);
_context.Run("val.getUTCDate()").Should().BeOfType<int>().Which.Should().Be(10);
}

[TestMethod]
public void SetAndReadDateTimeUtc()
{
var dateTime = new DateTime(2010, 10, 10, 0, 0, 0, DateTimeKind.Utc);
_context.SetParameter("val", dateTime);

var dateFromV8 = (DateTime) _context.Run("val");
dateFromV8.ToUniversalTime().Should().Be(dateTime);
}

[TestMethod]
public void SetAndReadDateTimeLocal()
{
var dateTime = new DateTime(2010, 10, 10, 0, 0, 0, DateTimeKind.Local);
_context.SetParameter("val", dateTime);

_context.Run("val").Should().Be(dateTime);
}

[TestMethod]
public void SetAndReadDateTimeUnspecified()
{
var dateTime = new DateTime(2010, 10, 10);
_context.SetParameter("val", dateTime);

_context.Run("val").Should().Be(dateTime);
}

[TestMethod]
public void CreateCurrentDateInJavaScript()
{
DateTime currentTimeAsReportedByV8 = (DateTime)_context.Run("new Date()");
(currentTimeAsReportedByV8 - DateTime.Now).TotalSeconds.Should().BeLessThan(1, "Dates should be mostly equal");
}

[TestMethod]
public void CreateFixedDateInJavaScript()
{
DateTime dateAsReportedByV8 = (DateTime)_context.Run("new Date(2010, 9, 10)");
dateAsReportedByV8.Should().Be(new DateTime(2010, 10, 10));
}
}
}
3 changes: 2 additions & 1 deletion Tests/Noesis.Javascript.Tests/Noesis.Javascript.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<Compile Include="MemoryLeakTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="MultipleAppDomainsTest.cs" />
<Compile Include="DateTest.cs" />
<Compile Include="VersionStringTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -120,4 +121,4 @@ copy $(ProjectDir)..\..\$(V8Platform)\$(Configuration)\icu*.* $(ProjectDir)$(Out
copy $(ProjectDir)..\..\$(V8Platform)\$(Configuration)\*.bin $(ProjectDir)$(OutDir)
</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>

0 comments on commit 96a0ecd

Please sign in to comment.