123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740 |
- using System;
- using System.Globalization;
- using Jint.Parser.Ast;
- using Jint.Runtime;
- using Jint.Runtime.Interop;
- namespace Jint.Native.Date
- {
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5
- /// </summary>
- public sealed class DatePrototype : DateInstance
- {
- private DatePrototype(Engine engine)
- : base(engine)
- {
- }
- public static DatePrototype CreatePrototypeObject(Engine engine, DateConstructor dateConstructor)
- {
- var obj = new DatePrototype(engine);
- obj.Prototype = engine.Object.PrototypeObject;
- obj.Extensible = true;
- obj.FastAddProperty("constructor", dateConstructor, true, false, true);
- return obj;
- }
- public void Configure()
- {
- FastAddProperty("toString", new ClrFunctionInstance(Engine, ToString, 0), true, false, true);
- FastAddProperty("toDateString", new ClrFunctionInstance(Engine, ToDateString, 0), true, false, true);
- FastAddProperty("toTimeString", new ClrFunctionInstance(Engine, ToTimeString, 0), true, false, true);
- FastAddProperty("toLocaleString", new ClrFunctionInstance(Engine, ToLocaleString, 0), true, false, true);
- FastAddProperty("toLocaleDateString", new ClrFunctionInstance(Engine, ToLocaleDateString, 0), true, false, true);
- FastAddProperty("toLocaleTimeString", new ClrFunctionInstance(Engine, ToLocaleTimeString, 0), true, false, true);
- FastAddProperty("valueOf", new ClrFunctionInstance(Engine, ValueOf, 0), true, false, true);
- FastAddProperty("getTime", new ClrFunctionInstance(Engine, GetTime, 0), true, false, true);
- FastAddProperty("getFullYear", new ClrFunctionInstance(Engine, GetFullYear, 0), true, false, true);
- FastAddProperty("getUTCFullYear", new ClrFunctionInstance(Engine, GetUTCFullYear, 0), true, false, true);
- FastAddProperty("getMonth", new ClrFunctionInstance(Engine, GetMonth, 0), true, false, true);
- FastAddProperty("getUTCMonth", new ClrFunctionInstance(Engine, GetUTCMonth, 0), true, false, true);
- FastAddProperty("getDate", new ClrFunctionInstance(Engine, GetDate, 0), true, false, true);
- FastAddProperty("getUTCDate", new ClrFunctionInstance(Engine, GetUTCDate, 0), true, false, true);
- FastAddProperty("getDay", new ClrFunctionInstance(Engine, GetDay, 0), true, false, true);
- FastAddProperty("getUTCDay", new ClrFunctionInstance(Engine, GetUTCDay, 0), true, false, true);
- FastAddProperty("getHours", new ClrFunctionInstance(Engine, GetHours, 0), true, false, true);
- FastAddProperty("getUTCHours", new ClrFunctionInstance(Engine, GetUTCHours, 0), true, false, true);
- FastAddProperty("getMinutes", new ClrFunctionInstance(Engine, GetMinutes, 0), true, false, true);
- FastAddProperty("getUTCMinutes", new ClrFunctionInstance(Engine, GetUTCMinutes, 0), true, false, true);
- FastAddProperty("getSeconds", new ClrFunctionInstance(Engine, GetSeconds, 0), true, false, true);
- FastAddProperty("getUTCSeconds", new ClrFunctionInstance(Engine, GetUTCSeconds, 0), true, false, true);
- FastAddProperty("getMilliseconds", new ClrFunctionInstance(Engine, GetMilliseconds, 0), true, false, true);
- FastAddProperty("getUTCMilliseconds", new ClrFunctionInstance(Engine, GetUTCMilliseconds, 0), true, false, true);
- FastAddProperty("getTimezoneOffset", new ClrFunctionInstance(Engine, GetTimezoneOffset, 0), true, false, true);
- FastAddProperty("setTime", new ClrFunctionInstance(Engine, SetTime, 1), true, false, true);
- FastAddProperty("setMilliseconds", new ClrFunctionInstance(Engine, SetMilliseconds, 1), true, false, true);
- FastAddProperty("setUTCMilliseconds", new ClrFunctionInstance(Engine, SetUTCMilliseconds, 1), true, false, true);
- FastAddProperty("setSeconds", new ClrFunctionInstance(Engine, SetSeconds, 2), true, false, true);
- FastAddProperty("setUTCSeconds", new ClrFunctionInstance(Engine, SetUTCSeconds, 2), true, false, true);
- FastAddProperty("setMinutes", new ClrFunctionInstance(Engine, SetMinutes, 3), true, false, true);
- FastAddProperty("setUTCMinutes", new ClrFunctionInstance(Engine, SetUTCMinutes, 3), true, false, true);
- FastAddProperty("setHours", new ClrFunctionInstance(Engine, SetHours, 4), true, false, true);
- FastAddProperty("setUTCHours", new ClrFunctionInstance(Engine, SetUTCHours, 4), true, false, true);
- FastAddProperty("setDate", new ClrFunctionInstance(Engine, SetDate, 1), true, false, true);
- FastAddProperty("setUTCDate", new ClrFunctionInstance(Engine, SetUTCDate, 1), true, false, true);
- FastAddProperty("setMonth", new ClrFunctionInstance(Engine, SetMonth, 2), true, false, true);
- FastAddProperty("setUTCMonth", new ClrFunctionInstance(Engine, SetUTCMonth, 2), true, false, true);
- FastAddProperty("setFullYear", new ClrFunctionInstance(Engine, SetFullYear, 3), true, false, true);
- FastAddProperty("setUTCFullYear", new ClrFunctionInstance(Engine, SetUTCFullYear, 3), true, false, true);
- FastAddProperty("toUTCString", new ClrFunctionInstance(Engine, ToUTCString, 0), true, false, true);
- FastAddProperty("toISOString", new ClrFunctionInstance(Engine, ToISOString, 0), true, false, true);
- FastAddProperty("toJSON", new ClrFunctionInstance(Engine, ToJSON, 1), true, false, true);
- }
- private JsValue ValueOf(JsValue thisObj, JsValue[] arguments)
- {
- return thisObj.TryCast<DateInstance>().PrimitiveValue;
- }
- public JsValue ToString(JsValue thisObj, JsValue[] arg2)
- {
- return thisObj.TryCast<DateInstance>().ToDateTime().ToString("F", CultureInfo.InvariantCulture);
- }
- private JsValue ToDateString(JsValue thisObj, JsValue[] arguments)
- {
- return thisObj.TryCast<DateInstance>().ToDateTime().ToString("D", CultureInfo.InvariantCulture);
- }
- private JsValue ToTimeString(JsValue thisObj, JsValue[] arguments)
- {
- return thisObj.TryCast<DateInstance>().ToDateTime().ToString("T", CultureInfo.InvariantCulture);
- }
- private JsValue ToLocaleString(JsValue thisObj, JsValue[] arguments)
- {
- return thisObj.TryCast<DateInstance>().ToDateTime().ToString("F");
- }
- private JsValue ToLocaleDateString(JsValue thisObj, JsValue[] arguments)
- {
- return thisObj.TryCast<DateInstance>().ToDateTime().ToString("D");
- }
- private JsValue ToLocaleTimeString(JsValue thisObj, JsValue[] arguments)
- {
- return thisObj.TryCast<DateInstance>().ToDateTime().ToString("T");
- }
- private JsValue GetTime(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().PrimitiveValue;
- }
- private JsValue GetFullYear(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime().Year;
- }
- private JsValue GetUTCFullYear(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime().Year;
- }
- private JsValue GetMonth(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime().Month - 1;
- }
- private JsValue GetUTCMonth(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime().Month;
- }
- private JsValue GetDate(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime().Day;
- }
- private JsValue GetUTCDate(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime().Day;
- }
- private JsValue GetDay(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return (int)thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime().DayOfWeek;
- }
- private JsValue GetUTCDay(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return (int)thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime().DayOfWeek;
- }
- private JsValue GetHours(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime().Hour;
- }
- private JsValue GetUTCHours(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime().Hour;
- }
- private JsValue GetMinutes(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime().Minute;
- }
- private JsValue GetUTCMinutes(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime().Minute;
- }
- private JsValue GetSeconds(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime().Second;
- }
- private JsValue GetUTCSeconds(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime().Second;
- }
- private JsValue GetMilliseconds(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime().Millisecond;
- }
- private JsValue GetUTCMilliseconds(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime().Second;
- }
- private JsValue GetTimezoneOffset(JsValue thisObj, JsValue[] arguments)
- {
- if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
- {
- return double.NaN;
- }
- return - TimeZoneInfo.Local.GetUtcOffset(thisObj.TryCast<DateInstance>().ToDateTime()).Hours*60;
- }
- private JsValue SetTime(JsValue thisObj, JsValue[] arguments)
- {
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.TimeClip(TypeConverter.ToNumber(arguments.At(0)));
- }
- private JsValue SetMilliseconds(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime();
- dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, (int)TypeConverter.ToNumber(arguments.At(0)), DateTimeKind.Local);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue SetUTCMilliseconds(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime();
- dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, (int)TypeConverter.ToNumber(arguments.At(0)), DateTimeKind.Utc);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue SetSeconds(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime();
- var ms = arguments.At(1) == Undefined.Instance ? dt.Millisecond : TypeConverter.ToNumber(arguments.At(1));
- dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, (int)TypeConverter.ToNumber(arguments.At(0)), (int)ms, DateTimeKind.Local);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue SetUTCSeconds(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime();
- var ms = arguments.At(1) == Undefined.Instance ? dt.Millisecond : TypeConverter.ToNumber(arguments.At(1));
- dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, (int)TypeConverter.ToNumber(arguments.At(0)), (int)ms, DateTimeKind.Utc);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue SetMinutes(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime();
- var s = arguments.At(1) == Undefined.Instance ? dt.Second : TypeConverter.ToNumber(arguments.At(1));
- var ms = arguments.At(2) == Undefined.Instance ? dt.Millisecond : TypeConverter.ToNumber(arguments.At(2));
- dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, (int)TypeConverter.ToNumber(arguments.At(0)), (int)s, (int)ms, DateTimeKind.Local);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue SetUTCMinutes(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime();
- var s = arguments.At(1) == Undefined.Instance ? dt.Second : TypeConverter.ToNumber(arguments.At(1));
- var ms = arguments.At(2) == Undefined.Instance ? dt.Millisecond : TypeConverter.ToNumber(arguments.At(2));
- dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, (int)TypeConverter.ToNumber(arguments.At(0)), (int)s, (int)ms, DateTimeKind.Utc);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue SetHours(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime();
- var min = arguments.At(1) == Undefined.Instance ? dt.Minute : TypeConverter.ToNumber(arguments.At(1));
- var s = arguments.At(2) == Undefined.Instance ? dt.Second : TypeConverter.ToNumber(arguments.At(2));
- var ms = arguments.At(3) == Undefined.Instance ? dt.Millisecond : TypeConverter.ToNumber(arguments.At(3));
- dt = new DateTime(dt.Year, dt.Month, dt.Day, (int)TypeConverter.ToNumber(arguments.At(0)), (int)min, (int)s, (int)ms, DateTimeKind.Local);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue SetUTCHours(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime();
- var min = arguments.At(1) == Undefined.Instance ? dt.Minute : TypeConverter.ToNumber(arguments.At(1));
- var s = arguments.At(2) == Undefined.Instance ? dt.Second : TypeConverter.ToNumber(arguments.At(2));
- var ms = arguments.At(3) == Undefined.Instance ? dt.Millisecond : TypeConverter.ToNumber(arguments.At(3));
- dt = new DateTime(dt.Year, dt.Month, dt.Day, (int)TypeConverter.ToNumber(arguments.At(0)), (int)min, (int)s, (int)ms, DateTimeKind.Utc);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue SetDate(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime();
- dt = new DateTime(dt.Year, dt.Month, (int)TypeConverter.ToNumber(arguments.At(0)), dt.Hour, dt.Minute, dt.Second, dt.Second, DateTimeKind.Local);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue SetUTCDate(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime();
- dt = new DateTime(dt.Year, dt.Month, (int)TypeConverter.ToNumber(arguments.At(0)), dt.Hour, dt.Minute, dt.Second, dt.Second, DateTimeKind.Utc);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue SetMonth(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime();
- var date = arguments.At(1) == Undefined.Instance ? dt.Day : TypeConverter.ToNumber(arguments.At(1));
- dt = new DateTime(dt.Year, (int)TypeConverter.ToNumber(arguments.At(0)), (int)date, dt.Hour, dt.Minute, dt.Second, dt.Second, DateTimeKind.Local);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue SetUTCMonth(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime();
- var date = arguments.At(1) == Undefined.Instance ? dt.Day : TypeConverter.ToNumber(arguments.At(1));
- dt = new DateTime(dt.Year, (int)TypeConverter.ToNumber(arguments.At(0)), (int)date, dt.Hour, dt.Minute, dt.Second, dt.Second, DateTimeKind.Utc);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue SetFullYear(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime();
- var month = arguments.At(1) == Undefined.Instance ? dt.Month : TypeConverter.ToNumber(arguments.At(1));
- var date = arguments.At(2) == Undefined.Instance ? dt.Day : TypeConverter.ToNumber(arguments.At(2));
- dt = new DateTime((int)TypeConverter.ToNumber(arguments.At(0)), (int)month, (int)date, dt.Hour, dt.Minute, dt.Second, dt.Second, DateTimeKind.Local);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue SetUTCFullYear(JsValue thisObj, JsValue[] arguments)
- {
- var dt = thisObj.TryCast<DateInstance>().ToDateTime().ToUniversalTime();
- var month = arguments.At(1) == Undefined.Instance ? dt.Month : TypeConverter.ToNumber(arguments.At(1));
- var date = arguments.At(2) == Undefined.Instance ? dt.Day : TypeConverter.ToNumber(arguments.At(2));
- dt = new DateTime((int)TypeConverter.ToNumber(arguments.At(0)), (int)month, (int)date, dt.Hour, dt.Minute, dt.Second, dt.Second, DateTimeKind.Utc);
- return thisObj.TryCast<DateInstance>().PrimitiveValue = DateConstructor.FromDateTime(dt);
- }
- private JsValue ToUTCString(JsValue thisObj, JsValue[] arguments)
- {
- return thisObj.TryCast<DateInstance>(x =>
- {
- throw new JavaScriptException(Engine.TypeError);
- } )
- .ToDateTime().ToUniversalTime().ToString("r");
- }
- private JsValue ToISOString(JsValue thisObj, JsValue[] arguments)
- {
- return thisObj.TryCast<DateInstance>(x =>
- {
- throw new JavaScriptException(Engine.TypeError);
- })
- .ToDateTime().ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
- }
- private JsValue ToJSON(JsValue thisObj, JsValue[] arguments)
- {
- return thisObj.TryCast<DateInstance>(x =>
- {
- throw new JavaScriptException(Engine.TypeError);
- })
- .ToDateTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
- }
- public static double MsPerDay = 86400000;
- public static double MsPerHour = 3600000;
- /// <summary>
- /// 15.9.1.2
- /// </summary>
- public static double Day(double t)
- {
- return System.Math.Floor(t / MsPerDay);
- }
- /// <summary>
- /// 15.9.1.2
- /// </summary>
- public static double TimeWithinDay(double t)
- {
- return t % MsPerDay;
- }
- /// <summary>
- /// The number of days in a year
- /// </summary>
- public static double DaysInYear(double y)
- {
- if (!(y%4).Equals(0))
- {
- return 365;
- }
- if ((y%4).Equals(0) && !(y%100).Equals(100))
- {
- return 365;
- }
- if ((y%100).Equals(0) && !(y%400).Equals(100))
- {
- return 365;
- }
- if ((y%400).Equals(0))
- {
- return 366;
- }
- return 365;
- }
- /// <summary>
- /// The day number of the first day of the year.
- /// </summary>
- public static double DayFromYear(double y)
- {
- return 365*(y - 1970) + System.Math.Floor((y - 1969)/4) - System.Math.Floor((y - 1901)/100) +
- System.Math.Floor((y - 1601)/400);
- }
- /// <summary>
- /// The time value of the start of the year
- /// </summary>
- public static double TimeFromYear(double y)
- {
- return MsPerDay*DayFromYear(y);
- }
- /// <summary>
- /// The year of a time value.
- /// </summary>
- public static double YearFromTime(double t)
- {
- double upper = double.PositiveInfinity;
- double lower = double.NegativeInfinity;
- while (upper > lower)
- {
- var current = System.Math.Floor(upper + lower / 2);
- if (TimeFromYear(current) <= t)
- {
- lower = current;
- }
- else
- {
- upper = current;
- }
- }
- return lower;
- }
- /// <summary>
- /// <value>true</value> if the time is within a leap year, <value>false</value> otherwise
- /// </summary>
- public static double InLeapYear(double t)
- {
- var daysInYear = DaysInYear(YearFromTime(t));
- if (daysInYear.Equals(365))
- {
- return 0;
- }
- if (daysInYear.Equals(366))
- {
- return 1;
- }
- throw new ArgumentException();
- }
- /// <summary>
- /// The month number of a time value.
- /// </summary>
- public static double MonthFromTime(double t)
- {
- var dayWithinYear = DayWithinYear(t);
- var inLeapYear = InLeapYear(t);
- if (dayWithinYear < 31)
- {
- return 0;
- }
- if (dayWithinYear < 59 + inLeapYear)
- {
- return 1;
- }
- if (dayWithinYear < 90 + inLeapYear)
- {
- return 2;
- }
- if (dayWithinYear < 120 + inLeapYear)
- {
- return 3;
- }
- if (dayWithinYear < 151 + inLeapYear)
- {
- return 4;
- }
- if (dayWithinYear < 181 + inLeapYear)
- {
- return 5;
- }
- if (dayWithinYear < 212 + inLeapYear)
- {
- return 6;
- }
- if (dayWithinYear < 243 + inLeapYear)
- {
- return 7;
- }
- if (dayWithinYear < 273 + inLeapYear)
- {
- return 8;
- }
- if (dayWithinYear < 304 + inLeapYear)
- {
- return 9;
- }
- if (dayWithinYear < 334 + inLeapYear)
- {
- return 10;
- }
- if (dayWithinYear < 365 + inLeapYear)
- {
- return 11;
- }
- throw new InvalidOperationException();
- }
- public static double DayWithinYear(double t)
- {
- return Day(t) - DayFromYear(YearFromTime(t));
- }
- public static double DateFromTime(double t)
- {
- var monthFromTime = MonthFromTime(t);
- var dayWithinYear = DayWithinYear(t);
- if (monthFromTime.Equals(0))
- {
- return dayWithinYear + 1;
- }
- if (monthFromTime.Equals(1))
- {
- return dayWithinYear - 30;
- }
- if (monthFromTime.Equals(2))
- {
- return dayWithinYear - 58;
- }
- if (monthFromTime.Equals(3))
- {
- return dayWithinYear - 89;
- }
- if (monthFromTime.Equals(4))
- {
- return dayWithinYear - 119;
- }
- if (monthFromTime.Equals(5))
- {
- return dayWithinYear - 150;
- }
- if (monthFromTime.Equals(6))
- {
- return dayWithinYear - 180;
- }
- if (monthFromTime.Equals(7))
- {
- return dayWithinYear - 211;
- }
- if (monthFromTime.Equals(8))
- {
- return dayWithinYear - 242;
- }
- if (monthFromTime.Equals(9))
- {
- return dayWithinYear - 272;
- }
- if (monthFromTime.Equals(10))
- {
- return dayWithinYear - 303;
- }
- if (monthFromTime.Equals(11))
- {
- return dayWithinYear - 333;
- }
- throw new InvalidOperationException();
- }
- /// <summary>
- /// The weekday for a particular time value.
- /// </summary>
- public static double WeekDay(double t)
- {
- return (Day(t) + 4)%7;
- }
- public static double LocalTza
- {
- get
- {
- return TimeZoneInfo.Local.BaseUtcOffset.TotalMilliseconds;
- }
- }
- public static double DaylightSavingTa(double t)
- {
- var timeInYear = t - TimeFromYear(YearFromTime(t));
- var isLeapYear = InLeapYear(t).Equals(1);
- var weekDay = WeekDay(TimeFromYear(YearFromTime(t)));
-
- var year = YearFromTime(t);
- if (year < 9999 && year > -9999)
- {
- // in DateTimeOffset range so we can use it
- }
- else
- {
- // use similar leap-ed year
- year = isLeapYear ? 2000 : 1999;
- }
- var dateTime = new DateTime((int)year, 1, 1).AddMilliseconds(timeInYear);
- return TimeZoneInfo.Local.IsDaylightSavingTime(dateTime) ? MsPerHour : 0;
- }
- public static double UtcToLocalTime(double t)
- {
- return t + LocalTza + DaylightSavingTa(t);
- }
- public static double LocalTimeToUtc(double t)
- {
- return t - LocalTza - DaylightSavingTa(t - LocalTza);
- }
- }
- }
|