Skip to content

Commit 8c8fcbe

Browse files
author
Erik Schilling
committed
Fixed date conversions
JavaScript usually does not care a lot about timezones, but since the conversion happens via milliseconds since epoch (which is UTC based) one needs to be a bit careful about which conversions to do where. Added/moved tests to a central place so there is an overview over all date related tests.
1 parent 3adb5d4 commit 8c8fcbe

File tree

6 files changed

+82
-23
lines changed

6 files changed

+82
-23
lines changed

Source/Noesis.Javascript/JavascriptInterop.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ JavascriptInterop::ConvertObjectFromV8(Handle<Object> iObject, ConvertedObjects
354354
System::DateTime^
355355
JavascriptInterop::ConvertDateFromV8(Handle<Value> iValue)
356356
{
357-
System::DateTime^ startDate = gcnew System::DateTime(1970, 1, 1);
357+
System::DateTime^ startDate = gcnew System::DateTime(1970, 1, 1, 0, 0, 0, 0, System::DateTimeKind::Utc);
358358
double milliseconds = iValue->NumberValue();
359359
System::TimeSpan^ timespan = System::TimeSpan::FromMilliseconds(milliseconds);
360-
return System::DateTime(timespan->Ticks + startDate->Ticks).ToLocalTime();
360+
return System::DateTime(timespan->Ticks + startDate->Ticks).ToLocalTime();
361361
}
362362

363363
////////////////////////////////////////////////////////////////////////////////////////////////////

Source/Noesis.Javascript/SystemInterop.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ SystemInterop::ConvertToSystemString(std::string iString)
337337
double
338338
SystemInterop::ConvertFromSystemDateTime(System::DateTime^ iDateTime)
339339
{
340-
System::DateTime^ startDate = gcnew System::DateTime(1970, 1, 1);
341-
System::TimeSpan^ timespan = *iDateTime - *startDate;
340+
System::DateTime^ startDate = gcnew System::DateTime(1970, 1, 1, 0, 0, 0, 0, System::DateTimeKind::Utc);
341+
System::TimeSpan^ timespan = iDateTime->ToUniversalTime() - *startDate;
342342

343343
return timespan->TotalMilliseconds;
344344
}

Tests/Noesis.Javascript.Tests/ConvertFromJavascriptTests.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,6 @@ public void ReadBooleanTrue()
108108
_context.GetParameter("myBool").Should().BeOfType<bool>().Which.Should().BeTrue();
109109
}
110110

111-
[TestMethod]
112-
public void ReadDate()
113-
{
114-
_context.Run("var myDate = new Date(2010,9,10)");
115-
116-
_context.GetParameter("myDate").Should().BeOfType<DateTime>().Which.Should().Be(new DateTime(2010, 10, 10));
117-
}
118-
119111
[TestMethod]
120112
public void ReadObject()
121113
{

Tests/Noesis.Javascript.Tests/ConvertToJavascriptTests.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,6 @@ public void SetBoolean()
134134
_context.Run("val === true").Should().BeOfType<bool>().Which.Should().BeTrue();
135135
}
136136

137-
[TestMethod]
138-
public void SetDateTime()
139-
{
140-
_context.SetParameter("val", new DateTime(2010, 10, 10, 0, 0, 0, DateTimeKind.Utc));
141-
142-
_context.Run("val.getUTCFullYear()").Should().BeOfType<int>().Which.Should().Be(2010);
143-
_context.Run("val.getUTCMonth()").Should().BeOfType<int>().Which.Should().Be(9);
144-
_context.Run("val.getUTCDate()").Should().BeOfType<int>().Which.Should().Be(10);
145-
}
146-
147137
[TestMethod]
148138
public void SetObject()
149139
{
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using FluentAssertions;
4+
5+
namespace Noesis.Javascript.Tests
6+
{
7+
[TestClass]
8+
public class DateTest
9+
{
10+
private JavascriptContext _context;
11+
12+
[TestInitialize]
13+
public void SetUp()
14+
{
15+
_context = new JavascriptContext();
16+
}
17+
18+
[TestCleanup]
19+
public void TearDown()
20+
{
21+
_context.Dispose();
22+
}
23+
24+
[TestMethod]
25+
public void SetDateTime()
26+
{
27+
_context.SetParameter("val", new DateTime(2010, 10, 10, 0, 0, 0, DateTimeKind.Utc));
28+
29+
_context.Run("val.getUTCFullYear()").Should().BeOfType<int>().Which.Should().Be(2010);
30+
_context.Run("val.getUTCMonth()").Should().BeOfType<int>().Which.Should().Be(9);
31+
_context.Run("val.getUTCDate()").Should().BeOfType<int>().Which.Should().Be(10);
32+
}
33+
34+
[TestMethod]
35+
public void SetAndReadDateTimeUtc()
36+
{
37+
var dateTime = new DateTime(2010, 10, 10, 0, 0, 0, DateTimeKind.Utc);
38+
_context.SetParameter("val", dateTime);
39+
40+
var dateFromV8 = (DateTime) _context.Run("val");
41+
dateFromV8.ToUniversalTime().Should().Be(dateTime);
42+
}
43+
44+
[TestMethod]
45+
public void SetAndReadDateTimeLocal()
46+
{
47+
var dateTime = new DateTime(2010, 10, 10, 0, 0, 0, DateTimeKind.Local);
48+
_context.SetParameter("val", dateTime);
49+
50+
_context.Run("val").Should().Be(dateTime);
51+
}
52+
53+
[TestMethod]
54+
public void SetAndReadDateTimeUnspecified()
55+
{
56+
var dateTime = new DateTime(2010, 10, 10);
57+
_context.SetParameter("val", dateTime);
58+
59+
_context.Run("val").Should().Be(dateTime);
60+
}
61+
62+
[TestMethod]
63+
public void CreateCurrentDateInJavaScript()
64+
{
65+
DateTime currentTimeAsReportedByV8 = (DateTime)_context.Run("new Date()");
66+
(currentTimeAsReportedByV8 - DateTime.Now).TotalSeconds.Should().BeLessThan(1, "Dates should be mostly equal");
67+
}
68+
69+
[TestMethod]
70+
public void CreateFixedDateInJavaScript()
71+
{
72+
DateTime dateAsReportedByV8 = (DateTime)_context.Run("new Date(2010, 9, 10)");
73+
dateAsReportedByV8.Should().Be(new DateTime(2010, 10, 10));
74+
}
75+
}
76+
}

Tests/Noesis.Javascript.Tests/Noesis.Javascript.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<Compile Include="MemoryLeakTests.cs" />
9292
<Compile Include="Properties\AssemblyInfo.cs" />
9393
<Compile Include="MultipleAppDomainsTest.cs" />
94+
<Compile Include="DateTest.cs" />
9495
<Compile Include="VersionStringTests.cs" />
9596
</ItemGroup>
9697
<ItemGroup>
@@ -120,4 +121,4 @@ copy $(ProjectDir)..\..\$(V8Platform)\$(Configuration)\icu*.* $(ProjectDir)$(Out
120121
copy $(ProjectDir)..\..\$(V8Platform)\$(Configuration)\*.bin $(ProjectDir)$(OutDir)
121122
</PostBuildEvent>
122123
</PropertyGroup>
123-
</Project>
124+
</Project>

0 commit comments

Comments
 (0)