-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed manual conversion to local time in date conversions
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
Showing
6 changed files
with
82 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters