Skip to content

Commit

Permalink
Fix setTime() #133
Browse files Browse the repository at this point in the history
  • Loading branch information
nilproject committed May 10, 2018
1 parent 9498e6b commit 43d8f81
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
13 changes: 13 additions & 0 deletions IntegrationTests/BaseLibrary/DateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,18 @@ public void ShouldCorrectHandleSwitchFromDstToStandardBySetDate_MoscowTime()
d.setDate(28);
Assert.AreEqual(-240, d.getTimezoneOffset().As<int>());
}

[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<int>());
d.setTime((long)d.getTime() + 86400 * 1000);
Assert.AreEqual(-240, d.getTimezoneOffset().As<int>());
}
}
}
14 changes: 9 additions & 5 deletions NiL.JS/BaseLibrary/Date.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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) +
Expand All @@ -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)
Expand Down

0 comments on commit 43d8f81

Please sign in to comment.