Skip to content

Commit

Permalink
New fixes for #114
Browse files Browse the repository at this point in the history
  • Loading branch information
nilproject committed Apr 23, 2018
1 parent 3133a17 commit 068fc42
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions NiL.JS/BaseLibrary/Date.cs
Original file line number Diff line number Diff line change
Expand Up @@ -706,41 +706,45 @@ private JSValue toIsoString()
if ((_time + _timeZoneOffset) > 8702135600400000 || (_time + _timeZoneOffset) < -8577864403200000 || _error)
ExceptionHelper.Throw(new RangeError("Invalid time value"));

var y = getYearImpl(true);

return y +
"-" + (this.getMonthImpl(false) + 1).ToString("00") +
"-" + this.getDateImpl(false).ToString("00") +
"T" + this.getHoursImpl(false).ToString("00") +
":" + this.getMinutesImpl(false).ToString("00") +
":" + this.getSecondsImpl().ToString("00") +
"." + (this.getMillisecondsImpl() / 1000.0).ToString(".000", System.Globalization.CultureInfo.InvariantCulture).Substring(1) +
"Z";
return getYearImpl(false) +
"-" + (this.getMonthImpl(false) + 1).ToString("00") +
"-" + this.getDateImpl(false).ToString("00") +
"T" + this.getHoursImpl(false).ToString("00") +
":" + this.getMinutesImpl(false).ToString("00") +
":" + this.getSecondsImpl().ToString("00") +
"." + (this.getMillisecondsImpl() / 1000.0).ToString(".000", System.Globalization.CultureInfo.InvariantCulture).Substring(1) +
"Z";
}

private string stringify(bool withTzo)
private string stringify(bool withTzo, bool rfc1123)
{
if (_error)
return "Invalid date";

return stringifyDate(withTzo) + " " + stringifyTime(withTzo);
return stringifyDate(withTzo, rfc1123) + " " + stringifyTime(withTzo, rfc1123);
}

private string stringifyDate(bool withTzo)
private string stringifyDate(bool withTzo, bool rfc1123)
{
if (withTzo && rfc1123)
throw new ArgumentException();

if (_error)
return "Invalid date";

var res =
daysOfWeek[(getDayImpl(withTzo) + 6) % 7] + " "
daysOfWeek[(getDayImpl(withTzo) + 6) % 7] + (rfc1123 ? ", " : " ")
+ months[getMonthImpl(withTzo)]
+ " " + getDateImpl(withTzo).ToString("00") + " "
+ getYearImpl(withTzo);
return res;
}

private string stringifyTime(bool withTzo)
private string stringifyTime(bool withTzo, bool rfc1123)
{
if (withTzo && rfc1123)
throw new ArgumentException();

if (_error)
return "Invalid date";

Expand All @@ -750,14 +754,14 @@ private string stringifyTime(bool withTzo)
getHoursImpl(withTzo).ToString("00:")
+ getMinutesImpl(withTzo).ToString("00:")
+ getSecondsImpl().ToString("00")
+ " GMT" + (offset.Ticks > 0 ? "+" : "") + (offset.Hours * 100 + offset.Minutes).ToString("0000") + " (" + timeName + ")";
+ " GMT" + (withTzo ? (offset.Ticks > 0 ? "+" : "") + (offset.Hours * 100 + offset.Minutes).ToString("0000") + " (" + timeName + ")" : "");
return res;
}

[Hidden]
public override string ToString()
{
return stringify(true);
return stringify(true, false);
}

[DoNotEnumerate]
Expand All @@ -769,25 +773,25 @@ public JSValue toJSON(JSValue obj)
[DoNotEnumerate]
public JSValue toUTCString()
{
return stringify(false);
return stringify(false, true);
}

[DoNotEnumerate]
public JSValue toGMTString()
{
return stringify(false);
return stringify(false, true);
}

[DoNotEnumerate]
public JSValue toTimeString()
{
return stringifyTime(true);
return stringifyTime(true, false);
}

[DoNotEnumerate]
public JSValue toDateString()
{
return stringifyDate(true);
return stringifyDate(true, false);
}

[DoNotEnumerate]
Expand Down Expand Up @@ -815,7 +819,7 @@ public static JSValue parse(string dateTime)
var time = 0L;
var tzo = 0L;
if (tryParse(dateTime, out time, out tzo))
return time - tzo - _unixTimeBase;
return time;
return double.NaN;
}

Expand Down Expand Up @@ -1023,7 +1027,8 @@ private static bool parseSelf(string timeStr, out long time, out long timeZoneOf
}

if (token.StartsWith("gmt", StringComparison.OrdinalIgnoreCase)
|| (token.StartsWith("ut", StringComparison.OrdinalIgnoreCase) && (token.Length == 2 || token[2] == 'c' || token[2] == 'C'))
|| token.StartsWith("ut", StringComparison.OrdinalIgnoreCase)
|| token.StartsWith("utc", StringComparison.OrdinalIgnoreCase)
|| token.StartsWith("pst", StringComparison.OrdinalIgnoreCase)
|| token.StartsWith("pdt", StringComparison.OrdinalIgnoreCase))
{
Expand Down Expand Up @@ -1095,8 +1100,6 @@ private static bool parseSelf(string timeStr, out long time, out long timeZoneOf
time += _hourMilliseconds * 12;

timeZoneOffset = CurrentTimeZone.GetUtcOffset(new DateTime(time * _timeAccuracy)).Ticks / 10000;
if (wasTZ)
time += timeZoneOffset;
}
catch
{
Expand Down

0 comments on commit 068fc42

Please sign in to comment.