Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed manual conversion to local time in date conversions #55

Merged
merged 2 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = System::TimeSpan::FromTicks(iDateTime->Ticks - startDate->Ticks);
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>