Permalink
Browse files

Refactoring Date to reflect specifications

The goal was to remove any unnecessary reference
to DateTime as the bounds are limited compared to
those in JavaScript (-9999 to 9999 in .NET)
  • Loading branch information...
sebastienros committed Feb 6, 2014
1 parent d162eda commit 478662033e519904cef6791a028a9d92c37f127c
@@ -5,21 +5,21 @@ namespace Jint.Tests.Ecma
[Trait("Category", "Pass")]
public class Test_15_9_5_43 : EcmaTest
{
[Fact(Skip = "DateTime is out of range")]
[Fact]
[Trait("Category", "15.9.5.43")]
public void DatePrototypeToisostringRangeerrorIsNotThrownWhenValueOfDateIsDate19700999999990001TheTimeZoneIsUtc0()
{
RunTest(@"TestCases/ch15/15.9/15.9.5/15.9.5.43/15.9.5.43-0-10.js", false);
}
[Fact(Skip = "DateTime is out of range")]
[Fact]
[Trait("Category", "15.9.5.43")]
public void DatePrototypeToisostringRangeerrorIsNotThrownWhenValueOfDateIsDate197001000000010001TheTimeZoneIsUtc0()
{
RunTest(@"TestCases/ch15/15.9/15.9.5/15.9.5.43/15.9.5.43-0-11.js", false);
}
[Fact(Skip = "DateTime is out of range")]
[Fact]
[Trait("Category", "15.9.5.43")]
public void DatePrototypeToisostringRangeerrorIsNotThrownWhenValueOfDateIsDate197001000000010000TheTimeZoneIsUtc0()
{
@@ -33,14 +33,14 @@ public void DatePrototypeToisostringRangeerrorIsThrownWhenValueOfDateIsDate19700
RunTest(@"TestCases/ch15/15.9/15.9.5/15.9.5.43/15.9.5.43-0-13.js", false);
}
[Fact(Skip = "DateTime is out of range")]
[Fact]
[Trait("Category", "15.9.5.43")]
public void DatePrototypeToisostringWhenValueOfYearIsInfinityDatePrototypeToisostringThrowTheRangeerror()
{
RunTest(@"TestCases/ch15/15.9/15.9.5/15.9.5.43/15.9.5.43-0-14.js", false);
}
[Fact(Skip = "DateTime is out of range")]
[Fact]
[Trait("Category", "15.9.5.43")]
public void DatePrototypeToisostringValueOfYearIsInfinityDatePrototypeToisostringThrowTheRangeerror()
{
@@ -103,7 +103,7 @@ public void DatePrototypeToisostringRangeerrorIsThrownWhenValueOfDateIsDate19700
RunTest(@"TestCases/ch15/15.9/15.9.5/15.9.5.43/15.9.5.43-0-8.js", false);
}
[Fact(Skip = "DateTime is out of range")]
[Fact]
[Trait("Category", "15.9.5.43")]
public void DatePrototypeToisostringRangeerrorIsNotThrownWhenValueOfDateIsDate19700999999990000TheTimeZoneIsUtc0()
{
View
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ISO/@EntryIndexedValue">ISO</s:String></wpf:ResourceDictionary>
@@ -1,5 +1,6 @@
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using Jint.Native.Function;
using Jint.Native.Object;
using Jint.Runtime;
@@ -52,21 +53,35 @@ private JsValue Parse(JsValue thisObj, JsValue[] arguments)
if (!DateTime.TryParseExact(date, new[]
{
"yyyy/MM/ddTH:m:s.fffK",
"yyyy/MM/dd",
"yyyy/MM",
"yyyy-MM-ddTH:m:s.fffK",
"yyyy-MM-dd",
"yyyy-MM",
"THH:m:s.fff",
"TH:mm:sm",
"THH:mm",
"yyyy/MM/ddTH:m:s.fff",
"yyyy/MM/dd",
"yyyy/MM",
"yyyy-MM-ddTH:m:s.fff",
"yyyy-MM-dd",
"yyyy-MM",
"yyyy",
"THH:m:s.fff",
"TH:mm:sm",
"THH:mm",
"THH"
}, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out result))
}, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out result))
{
if (!DateTime.TryParseExact(date, "yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out result))
if (!DateTime.TryParseExact(date, new[]
{
if (!DateTime.TryParse(date, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out result))
"yyyy/MM/ddTH:m:s.fffK",
"yyyy/MM/ddK",
"yyyy/MMK",
"yyyy-MM-ddTH:m:s.fffK",
"yyyy-MM-ddK",
"yyyy-MMK",
"yyyyK",
"THH:m:s.fffK",
"TH:mm:smK",
"THH:mmK",
"THHK"
}, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out result))
{
if (!DateTime.TryParse(date, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal,out result))
{
throw new JavaScriptException(Engine.SyntaxError, "Invalid date");
}
@@ -111,17 +126,17 @@ public ObjectInstance Construct(JsValue[] arguments)
return Parse(Undefined.Instance, Arguments.From(v)).AsObject();
}
return Construct(TypeConverter.ToNumber(v));
return Construct(DatePrototype.TimeClip(TypeConverter.ToNumber(v)));
}
else
{
var y = TypeConverter.ToNumber(arguments[0]);
var m = (int)TypeConverter.ToInteger(arguments[1]);
var date = arguments.Length > 2 ? (int)TypeConverter.ToInteger(arguments[2]) : 1;
var hours = arguments.Length > 3 ? (int)TypeConverter.ToInteger(arguments[3]) : 0;
var minutes = arguments.Length > 4 ? (int)TypeConverter.ToInteger(arguments[4]) : 0;
var seconds = arguments.Length > 5 ? (int)TypeConverter.ToInteger(arguments[5]) : 0;
var ms = arguments.Length > 6 ? (int)TypeConverter.ToInteger(arguments[6]) : 0;
var dt = arguments.Length > 2 ? (int)TypeConverter.ToInteger(arguments[2]) : 1;
var h = arguments.Length > 3 ? (int)TypeConverter.ToInteger(arguments[3]) : 0;
var min = arguments.Length > 4 ? (int)TypeConverter.ToInteger(arguments[4]) : 0;
var s = arguments.Length > 5 ? (int)TypeConverter.ToInteger(arguments[5]) : 0;
var milli = arguments.Length > 6 ? (int)TypeConverter.ToInteger(arguments[6]) : 0;
for (int i = 2; i < arguments.Length; i++)
{
@@ -136,25 +151,10 @@ public ObjectInstance Construct(JsValue[] arguments)
y += 1900;
}
var ym = y + System.Math.Floor(m / 12);
var mn = m % 12;
var finalDate = DatePrototype.MakeDate(DatePrototype.MakeDay(y, m, dt),
DatePrototype.MakeTime(h, min, s, milli));
try
{
var dt = new DateTime((int) ym, mn + 1, 1, 0, 0, 0, 0, DateTimeKind.Local);
var primitiveValue = FromDateTime(dt)
+ ms
+ seconds*MsPerSecond
+ minutes*MsPerMinute
+ hours*MsPerHour
+ (date-1)*MsPerDay;
return Construct(primitiveValue);
}
catch(ArgumentOutOfRangeException)
{
throw new JavaScriptException(Engine.RangeError);
}
return Construct(DatePrototype.TimeClip(DatePrototype.Utc(finalDate)));
}
}
@@ -201,7 +201,7 @@ public static double TimeClip(double time)
public static double FromDateTime(DateTime dt)
{
return (dt.ToUniversalTime() - Epoch).TotalMilliseconds;
return System.Math.Floor((dt.ToUniversalTime() - Epoch).TotalMilliseconds);
}
}
}
Oops, something went wrong.

0 comments on commit 4786620

Please sign in to comment.