From 43d8f8165c4040eb200223323b403cd58045af2e Mon Sep 17 00:00:00 2001 From: nilproject Date: Thu, 10 May 2018 14:05:38 +0300 Subject: [PATCH] Fix setTime() #133 --- IntegrationTests/BaseLibrary/DateTests.cs | 13 +++++++++++++ NiL.JS/BaseLibrary/Date.cs | 14 +++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/IntegrationTests/BaseLibrary/DateTests.cs b/IntegrationTests/BaseLibrary/DateTests.cs index a33117cc2..0304113b8 100644 --- a/IntegrationTests/BaseLibrary/DateTests.cs +++ b/IntegrationTests/BaseLibrary/DateTests.cs @@ -68,5 +68,18 @@ public void ShouldCorrectHandleSwitchFromDstToStandardBySetDate_MoscowTime() d.setDate(28); Assert.AreEqual(-240, d.getTimezoneOffset().As()); } + + [TestMethod] + public void ShouldCorrectHandleSwitchFromDstToStandardBySetTime_MoscowTime() + { + var timezones = TimeZoneInfo.GetSystemTimeZones().Where(x => x.BaseUtcOffset.Ticks == 3 * 3600 * 10000000L).ToArray(); + var timezone = timezones.First(x => x.Id.IndexOf("MSK", StringComparison.OrdinalIgnoreCase) != -1 || x.Id.IndexOf("Moscow", StringComparison.OrdinalIgnoreCase) != -1); + Date.CurrentTimeZone = timezone; + + var d = new Date(new Arguments { "3/27/2010 08:00" }); + Assert.AreEqual(-180, d.getTimezoneOffset().As()); + d.setTime((long)d.getTime() + 86400 * 1000); + Assert.AreEqual(-240, d.getTimezoneOffset().As()); + } } } diff --git a/NiL.JS/BaseLibrary/Date.cs b/NiL.JS/BaseLibrary/Date.cs index ba41a1dbb..22e33c28c 100644 --- a/NiL.JS/BaseLibrary/Date.cs +++ b/NiL.JS/BaseLibrary/Date.cs @@ -210,8 +210,7 @@ private void offsetTimeValue(JSValue value, long amort, long mul) else { _time = _time + (-amort + Tools.JSObjectToInt64(value)) * mul; - if (_time < 5992660800000) - _error = true; + _error = isIncorrectTimeRange(_time); var oldTzo = _timeZoneOffset; _timeZoneOffset = getTimeZoneOffset(_time); @@ -496,9 +495,9 @@ public JSValue setTime(JSValue time) } else { - this._time = Tools.JSObjectToInt64(time) + _unixTimeBase + _timeZoneOffset; - _error = this._time < 5992660800000; + this.offsetTimeValue(time, _time - _unixTimeBase, 1); } + return getTime(); } @@ -706,7 +705,7 @@ public JSValue toISOString() private JSValue toIsoString() { - if ((_time + _timeZoneOffset) > 8702135604000000 || (_time + _timeZoneOffset) <= -8577864403199999 || _error) + if (_error || isIncorrectTimeRange(_time)) ExceptionHelper.Throw(new RangeError("Invalid time value")); return getYearImpl(false) + @@ -719,6 +718,11 @@ private JSValue toIsoString() "Z"; } + private static bool isIncorrectTimeRange(long time) + { + return time > 8702135604000000 || time <= -8577864403199999; + } + private string stringify(bool withTzo, bool rfc1123) { if (_error)