Skip to content

Commit

Permalink
Fixes for #114
Browse files Browse the repository at this point in the history
  • Loading branch information
nilproject committed Apr 22, 2018
1 parent 51ee08a commit 6c64c3c
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 131 deletions.
5 changes: 3 additions & 2 deletions FunctionalTests/Generated/FileTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ protected void RunFile(string fileName)
{
pass = negative;
if (!pass)
Console.WriteLine(e.Message);
output.Append(e);
}
catch (Exception)
catch (Exception e)
{
System.Diagnostics.Debugger.Break();
output.Append(e);
pass = false;
}
finally
Expand Down
266 changes: 137 additions & 129 deletions NiL.JS/BaseLibrary/Date.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public sealed class Date
new[]{ 365 * _dayMilliseconds, 366 * _dayMilliseconds }
};

private static readonly string[] daysOfWeek = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", };
private static readonly string[] daysOfWeek = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
private readonly static string[] months = new[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

private long _time;
Expand Down Expand Up @@ -75,23 +75,23 @@ public Date(Arguments args)
case JSValueType.Integer:
case JSValueType.Boolean:
case JSValueType.Double:
{
var timeValue = Tools.JSObjectToDouble(arg);
if (double.IsNaN(timeValue) || double.IsInfinity(timeValue))
{
var timeValue = Tools.JSObjectToDouble(arg);
if (double.IsNaN(timeValue) || double.IsInfinity(timeValue))
{
_error = true;
break;
}

_time = (long)timeValue + _unixTimeBase;
_timeZoneOffset = getTimeZoneOffset(_time);
_error = true;
break;
}

_time = (long)timeValue + _unixTimeBase;
_timeZoneOffset = getTimeZoneOffset(_time);
break;
}
case JSValueType.String:
{
_error = !tryParse(arg.ToString(), out _time, out _timeZoneOffset);
break;
}
{
_error = !tryParse(arg.ToString(), out _time, out _timeZoneOffset);
break;
}
}
}
else
Expand Down Expand Up @@ -184,7 +184,9 @@ public Date(Arguments args)

private static long getTimeZoneOffset(long time)
{
return TimeZoneInfo.Local.GetUtcOffset(new DateTime(System.Math.Min(System.Math.Max(time * _timeAccuracy, DateTime.MinValue.Ticks), DateTime.MaxValue.Ticks))).Ticks / _timeAccuracy;
var dateTime = new DateTime(System.Math.Min(System.Math.Max(time * _timeAccuracy, DateTime.MinValue.Ticks), DateTime.MaxValue.Ticks), DateTimeKind.Utc);
var offset = TimeZoneInfo.Local.GetUtcOffset(dateTime).Ticks / _timeAccuracy;
return offset;
}

private void offsetTimeValue(JSValue value, long amort, long mul)
Expand Down Expand Up @@ -359,13 +361,19 @@ public JSValue getUTCDate()
[DoNotEnumerate]
public JSValue getDay()
{
return (int)((_time / _dayMilliseconds + 1) % 7);
return getDayImpl(true);
}

[DoNotEnumerate]
public JSValue getUTCDay()
{
return (int)((_time / _dayMilliseconds + 1) % 7);
return getDayImpl(false);
}

private int getDayImpl(bool withTzo)
{
var t = System.Math.Abs(_time + (withTzo ? _timeZoneOffset : 0));
return (int)((t / _dayMilliseconds + 1) % 7);
}

[DoNotEnumerate]
Expand Down Expand Up @@ -739,9 +747,9 @@ public JSValue toDateString()
{
var res =
daysOfWeek[(System.Math.Abs(_time) % _weekMilliseconds) / _dayMilliseconds] + " "
+ months[getMonthImpl(true)]
+ " " + getDateImpl(true).ToString("00") + " "
+ getYearImpl(true);
+ months[getMonthImpl(false)]
+ " " + getDateImpl(false).ToString("00") + " "
+ getYearImpl(false);
return res;
}

Expand All @@ -754,7 +762,7 @@ public JSValue toLocaleDateString()
while (y < 0)
y += 2800;
var dt = new DateTime(0);
dt = dt.AddDays((System.Math.Abs(_time) % _weekMilliseconds) / _dayMilliseconds);
dt = dt.AddDays((System.Math.Abs(_time + _timeZoneOffset) % _weekMilliseconds) / _dayMilliseconds);
dt = dt.AddMonths(getMonthImpl(true));
dt = dt.AddYears(y);
#if (PORTABLE || NETCORE)
Expand All @@ -773,7 +781,7 @@ public override string ToString()
var offset = new TimeSpan(_timeZoneOffset * _timeAccuracy);
var timeName = TimeZoneInfo.Local.IsDaylightSavingTime(new DateTimeOffset(_time * _timeAccuracy, offset)) ? TimeZoneInfo.Local.DaylightName : TimeZoneInfo.Local.StandardName;
var res =
daysOfWeek[System.Math.Abs(_time) / _dayMilliseconds % 7] + " "
daysOfWeek[System.Math.Abs(_time + _timeZoneOffset) / _dayMilliseconds % 7] + " "
+ months[getMonthImpl(true)]
+ " " + getDateImpl(true).ToString("00") + " "
+ getYearImpl(true) + " "
Expand Down Expand Up @@ -1112,141 +1120,141 @@ private static bool parseIso8601(string timeStr, out long time, out long timeZon
switch (char.ToLowerInvariant(format[i]))
{
case 'y':
{
if (part != 0)
return false;
if (!Tools.IsDigit(timeStr[j]))
return false;
year = year * 10 + timeStr[j] - '0';
break;
}
{
if (part != 0)
return false;
if (!Tools.IsDigit(timeStr[j]))
return false;
year = year * 10 + timeStr[j] - '0';
break;
}
case 'm':
{
if (!Tools.IsDigit(timeStr[j]))
return false;
switch (part)
{
if (!Tools.IsDigit(timeStr[j]))
return false;
switch (part)
case 0:
{
case 0:
{
if (month == int.MinValue)
month = 0;
month = month * 10 + timeStr[j] - '0';
break;
}
case 1:
{
minutes = minutes * 10 + timeStr[j] - '0';
break;
}
default:
return false;
if (month == int.MinValue)
month = 0;
month = month * 10 + timeStr[j] - '0';
break;
}
break;
case 1:
{
minutes = minutes * 10 + timeStr[j] - '0';
break;
}
default:
return false;
}
break;
}
case 'd':
{
if (part != 0)
return false;
{
if (part != 0)
return false;

if (!Tools.IsDigit(timeStr[j]))
return false;
if (!Tools.IsDigit(timeStr[j]))
return false;

if (day == int.MinValue)
day = 0;
if (day == int.MinValue)
day = 0;

day = day * 10 + timeStr[j] - '0';
break;
}
day = day * 10 + timeStr[j] - '0';
break;
}
case 'h':
{
if (part != 1)
return false;
if (!Tools.IsDigit(timeStr[j]))
return false;
hour = hour * 10 + timeStr[j] - '0';
break;
}
{
if (part != 1)
return false;
if (!Tools.IsDigit(timeStr[j]))
return false;
hour = hour * 10 + timeStr[j] - '0';
break;
}
case 's':
{
if (part < 1)
return false;
if (!Tools.IsDigit(timeStr[j]))
return false;
if (part == 1)
seconds = seconds * 10 + timeStr[j] - '0';
else
milliseconds = milliseconds * 10 + timeStr[j] - '0';
break;
}
{
if (part < 1)
return false;
if (!Tools.IsDigit(timeStr[j]))
return false;
if (part == 1)
seconds = seconds * 10 + timeStr[j] - '0';
else
milliseconds = milliseconds * 10 + timeStr[j] - '0';
break;
}
case ':':
{
if (part != 1)
return false;
if (format[i] != timeStr[j])
return false;
break;
}
{
if (part != 1)
return false;
if (format[i] != timeStr[j])
return false;
break;
}
case '/':
{
if (part != 0)
return false;
if (format[i] != timeStr[j])
return false;
break;
}
{
if (part != 0)
return false;
if (format[i] != timeStr[j])
return false;
break;
}
case ' ':
{
if (format[i] != timeStr[j])
return false;
while (j < timeStr.Length && Tools.IsWhiteSpace(timeStr[j]))
j++;
j--;
break;
}
{
if (format[i] != timeStr[j])
return false;
while (j < timeStr.Length && Tools.IsWhiteSpace(timeStr[j]))
j++;
j--;
break;
}
case '-':
{
if (format[i] != timeStr[j])
return false;
break;
}
{
if (format[i] != timeStr[j])
return false;
break;
}
case 't':
{
if (part != 0)
return false;
{
if (part != 0)
return false;

if (char.ToLowerInvariant(timeStr[j]) != 't' && !char.IsWhiteSpace(timeStr[j]))
return false;
if (char.ToLowerInvariant(timeStr[j]) != 't' && !char.IsWhiteSpace(timeStr[j]))
return false;

computeTzo = char.ToLowerInvariant(timeStr[j]) != 't';
computeTzo = char.ToLowerInvariant(timeStr[j]) != 't';

part++;
break;
}
part++;
break;
}
case '.':
{
if ('.' != timeStr[j])
{
if ('.' != timeStr[j])
{
if (char.ToLowerInvariant(timeStr[j]) == 'z')
{
j = timeStr.Length;
i = format.Length;
break;
}
return false;
}
if (part != 1)
return false;
else
if (char.ToLowerInvariant(timeStr[j]) == 'z')
{
part++;
j = timeStr.Length;
i = format.Length;
break;
}
return false;
}
case '|':
if (part != 1)
return false;
else
{
j--;
part++;
break;
}
}
case '|':
{
j--;
break;
}
default:
return false;
}
Expand Down

0 comments on commit 6c64c3c

Please sign in to comment.