From 0857f815af6d6a0c1d9400effa115f52347587df Mon Sep 17 00:00:00 2001 From: Pisotskyi Pavel Date: Thu, 18 Jan 2024 14:14:09 +0200 Subject: [PATCH] The getDate() method returns 0 for December 31st before 1970. --- Jint.Tests/Runtime/DateTests.cs | 21 +++++++++++++++++++++ Jint/Native/Date/DatePrototype.cs | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Jint.Tests/Runtime/DateTests.cs b/Jint.Tests/Runtime/DateTests.cs index 3ac6b9e16d..1698b2871f 100644 --- a/Jint.Tests/Runtime/DateTests.cs +++ b/Jint.Tests/Runtime/DateTests.cs @@ -104,6 +104,27 @@ public void CanParseLocaleString(string input, long expected) Assert.Equal(expected, _engine.Evaluate($"new Date('{input}') * 1").AsNumber()); } + [Theory] + [InlineData("December 31 1900 12:00:00 +0300", 31)] + [InlineData("January 1 1969 12:00:00 +0300", 1)] + [InlineData("December 31 1969 12:00:00 +0300", 31)] + [InlineData("January 1 1970 12:00:00 +0300", 1)] + [InlineData("December 31 1970 12:00:00 +0300", 31)] + public void CanParseDate(string input, int expectedDate) + { + TimeZoneInfo timeZoneInfo; + try + { + timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Europe/Kiev"); + } + catch + { + timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("FLE Standard Time"); + } + var engine = new Engine(options => options.LocalTimeZone(timeZoneInfo)); + Assert.Equal(expectedDate, _engine.Evaluate($"new Date('{input}').getDate()").AsNumber()); + } + [Fact] public void CanUseMoment() { diff --git a/Jint/Native/Date/DatePrototype.cs b/Jint/Native/Date/DatePrototype.cs index 5427d34794..e2fbf53bfb 100644 --- a/Jint/Native/Date/DatePrototype.cs +++ b/Jint/Native/Date/DatePrototype.cs @@ -1270,7 +1270,7 @@ private static bool AreFinite(double value1, double value2, double value3, doubl private static readonly int[] kDaysInMonths = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - private static Date YearMonthDayFromTime(DatePresentation t) => YearMonthDayFromDays(t.Value / 1000 / 60 / 60 / 24); + private static Date YearMonthDayFromTime(DatePresentation t) => YearMonthDayFromDays((long) System.Math.Floor(t.Value / 1000 / 60 / 60 / 24d)); private static Date YearMonthDayFromDays(long days) {