DatePrototype.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. using System;
  2. using System.Globalization;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Interop;
  5. namespace Jint.Native.Date
  6. {
  7. /// <summary>
  8. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5
  9. /// </summary>
  10. public sealed class DatePrototype : DateInstance
  11. {
  12. private DatePrototype(Engine engine)
  13. : base(engine)
  14. {
  15. }
  16. public static DatePrototype CreatePrototypeObject(Engine engine, DateConstructor dateConstructor)
  17. {
  18. var obj = new DatePrototype(engine)
  19. {
  20. Prototype = engine.Object.PrototypeObject,
  21. Extensible = true,
  22. PrimitiveValue = double.NaN
  23. };
  24. obj.FastAddProperty("constructor", dateConstructor, true, false, true);
  25. return obj;
  26. }
  27. public void Configure()
  28. {
  29. FastAddProperty("toString", new ClrFunctionInstance(Engine, ToString, 0), true, false, true);
  30. FastAddProperty("toDateString", new ClrFunctionInstance(Engine, ToDateString, 0), true, false, true);
  31. FastAddProperty("toTimeString", new ClrFunctionInstance(Engine, ToTimeString, 0), true, false, true);
  32. FastAddProperty("toLocaleString", new ClrFunctionInstance(Engine, ToLocaleString, 0), true, false, true);
  33. FastAddProperty("toLocaleDateString", new ClrFunctionInstance(Engine, ToLocaleDateString, 0), true, false, true);
  34. FastAddProperty("toLocaleTimeString", new ClrFunctionInstance(Engine, ToLocaleTimeString, 0), true, false, true);
  35. FastAddProperty("valueOf", new ClrFunctionInstance(Engine, ValueOf, 0), true, false, true);
  36. FastAddProperty("getTime", new ClrFunctionInstance(Engine, GetTime, 0), true, false, true);
  37. FastAddProperty("getFullYear", new ClrFunctionInstance(Engine, GetFullYear, 0), true, false, true);
  38. FastAddProperty("getYear", new ClrFunctionInstance(Engine, GetYear, 0), true, false, true);
  39. FastAddProperty("getUTCFullYear", new ClrFunctionInstance(Engine, GetUTCFullYear, 0), true, false, true);
  40. FastAddProperty("getMonth", new ClrFunctionInstance(Engine, GetMonth, 0), true, false, true);
  41. FastAddProperty("getUTCMonth", new ClrFunctionInstance(Engine, GetUTCMonth, 0), true, false, true);
  42. FastAddProperty("getDate", new ClrFunctionInstance(Engine, GetDate, 0), true, false, true);
  43. FastAddProperty("getUTCDate", new ClrFunctionInstance(Engine, GetUTCDate, 0), true, false, true);
  44. FastAddProperty("getDay", new ClrFunctionInstance(Engine, GetDay, 0), true, false, true);
  45. FastAddProperty("getUTCDay", new ClrFunctionInstance(Engine, GetUTCDay, 0), true, false, true);
  46. FastAddProperty("getHours", new ClrFunctionInstance(Engine, GetHours, 0), true, false, true);
  47. FastAddProperty("getUTCHours", new ClrFunctionInstance(Engine, GetUTCHours, 0), true, false, true);
  48. FastAddProperty("getMinutes", new ClrFunctionInstance(Engine, GetMinutes, 0), true, false, true);
  49. FastAddProperty("getUTCMinutes", new ClrFunctionInstance(Engine, GetUTCMinutes, 0), true, false, true);
  50. FastAddProperty("getSeconds", new ClrFunctionInstance(Engine, GetSeconds, 0), true, false, true);
  51. FastAddProperty("getUTCSeconds", new ClrFunctionInstance(Engine, GetUTCSeconds, 0), true, false, true);
  52. FastAddProperty("getMilliseconds", new ClrFunctionInstance(Engine, GetMilliseconds, 0), true, false, true);
  53. FastAddProperty("getUTCMilliseconds", new ClrFunctionInstance(Engine, GetUTCMilliseconds, 0), true, false, true);
  54. FastAddProperty("getTimezoneOffset", new ClrFunctionInstance(Engine, GetTimezoneOffset, 0), true, false, true);
  55. FastAddProperty("setTime", new ClrFunctionInstance(Engine, SetTime, 1), true, false, true);
  56. FastAddProperty("setMilliseconds", new ClrFunctionInstance(Engine, SetMilliseconds, 1), true, false, true);
  57. FastAddProperty("setUTCMilliseconds", new ClrFunctionInstance(Engine, SetUTCMilliseconds, 1), true, false, true);
  58. FastAddProperty("setSeconds", new ClrFunctionInstance(Engine, SetSeconds, 2), true, false, true);
  59. FastAddProperty("setUTCSeconds", new ClrFunctionInstance(Engine, SetUTCSeconds, 2), true, false, true);
  60. FastAddProperty("setMinutes", new ClrFunctionInstance(Engine, SetMinutes, 3), true, false, true);
  61. FastAddProperty("setUTCMinutes", new ClrFunctionInstance(Engine, SetUTCMinutes, 3), true, false, true);
  62. FastAddProperty("setHours", new ClrFunctionInstance(Engine, SetHours, 4), true, false, true);
  63. FastAddProperty("setUTCHours", new ClrFunctionInstance(Engine, SetUTCHours, 4), true, false, true);
  64. FastAddProperty("setDate", new ClrFunctionInstance(Engine, SetDate, 1), true, false, true);
  65. FastAddProperty("setUTCDate", new ClrFunctionInstance(Engine, SetUTCDate, 1), true, false, true);
  66. FastAddProperty("setMonth", new ClrFunctionInstance(Engine, SetMonth, 2), true, false, true);
  67. FastAddProperty("setUTCMonth", new ClrFunctionInstance(Engine, SetUTCMonth, 2), true, false, true);
  68. FastAddProperty("setFullYear", new ClrFunctionInstance(Engine, SetFullYear, 3), true, false, true);
  69. FastAddProperty("setYear", new ClrFunctionInstance(Engine, SetYear, 1), true, false, true);
  70. FastAddProperty("setUTCFullYear", new ClrFunctionInstance(Engine, SetUTCFullYear, 3), true, false, true);
  71. FastAddProperty("toUTCString", new ClrFunctionInstance(Engine, ToUtcString, 0), true, false, true);
  72. FastAddProperty("toISOString", new ClrFunctionInstance(Engine, ToISOString, 0), true, false, true);
  73. FastAddProperty("toJSON", new ClrFunctionInstance(Engine, ToJSON, 1), true, false, true);
  74. }
  75. private static JsValue ValueOf(JsValue thisObj, JsValue[] arguments)
  76. {
  77. return thisObj.TryCast<DateInstance>().PrimitiveValue;
  78. }
  79. public JsValue ToString(JsValue thisObj, JsValue[] arg2)
  80. {
  81. return thisObj.TryCast<DateInstance>().ToDateTime().ToLocalTime().ToString("ddd MMM dd yyyy HH:mm:ss 'GMT'K", CultureInfo.InvariantCulture);
  82. }
  83. private static JsValue ToDateString(JsValue thisObj, JsValue[] arguments)
  84. {
  85. return thisObj.TryCast<DateInstance>().ToDateTime().ToString("D", CultureInfo.InvariantCulture);
  86. }
  87. private static JsValue ToTimeString(JsValue thisObj, JsValue[] arguments)
  88. {
  89. return thisObj.TryCast<DateInstance>().ToDateTime().ToString("T", CultureInfo.InvariantCulture);
  90. }
  91. private static JsValue ToLocaleString(JsValue thisObj, JsValue[] arguments)
  92. {
  93. return thisObj.TryCast<DateInstance>().ToDateTime().ToString("F");
  94. }
  95. private static JsValue ToLocaleDateString(JsValue thisObj, JsValue[] arguments)
  96. {
  97. return thisObj.TryCast<DateInstance>().ToDateTime().ToString("D");
  98. }
  99. private JsValue ToLocaleTimeString(JsValue thisObj, JsValue[] arguments)
  100. {
  101. return thisObj.TryCast<DateInstance>().ToDateTime().ToString("T");
  102. }
  103. private static JsValue GetTime(JsValue thisObj, JsValue[] arguments)
  104. {
  105. if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
  106. {
  107. return double.NaN;
  108. }
  109. return thisObj.TryCast<DateInstance>().PrimitiveValue;
  110. }
  111. private static JsValue GetFullYear(JsValue thisObj, JsValue[] arguments)
  112. {
  113. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  114. if (double.IsNaN(t))
  115. {
  116. return double.NaN;
  117. }
  118. return YearFromTime(LocalTime(t));
  119. }
  120. private static JsValue GetYear(JsValue thisObj, JsValue[] arguments)
  121. {
  122. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  123. if (double.IsNaN(t))
  124. {
  125. return double.NaN;
  126. }
  127. return YearFromTime(LocalTime(t)) - 1900;
  128. }
  129. private static JsValue GetUTCFullYear(JsValue thisObj, JsValue[] arguments)
  130. {
  131. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  132. if (double.IsNaN(t))
  133. {
  134. return double.NaN;
  135. }
  136. return YearFromTime(t);
  137. }
  138. private static JsValue GetMonth(JsValue thisObj, JsValue[] arguments)
  139. {
  140. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  141. if (double.IsNaN(t))
  142. {
  143. return double.NaN;
  144. }
  145. return MonthFromTime(LocalTime(t));
  146. }
  147. private static JsValue GetUTCMonth(JsValue thisObj, JsValue[] arguments)
  148. {
  149. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  150. if (double.IsNaN(t))
  151. {
  152. return double.NaN;
  153. }
  154. return MonthFromTime(t);
  155. }
  156. private static JsValue GetDate(JsValue thisObj, JsValue[] arguments)
  157. {
  158. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  159. if (double.IsNaN(t))
  160. {
  161. return double.NaN;
  162. }
  163. return DateFromTime(LocalTime(t));
  164. }
  165. private static JsValue GetUTCDate(JsValue thisObj, JsValue[] arguments)
  166. {
  167. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  168. if (double.IsNaN(t))
  169. {
  170. return double.NaN;
  171. }
  172. return DateFromTime(t);
  173. }
  174. private static JsValue GetDay(JsValue thisObj, JsValue[] arguments)
  175. {
  176. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  177. if (double.IsNaN(t))
  178. {
  179. return double.NaN;
  180. }
  181. return WeekDay(LocalTime(t));
  182. }
  183. private static JsValue GetUTCDay(JsValue thisObj, JsValue[] arguments)
  184. {
  185. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  186. if (double.IsNaN(t))
  187. {
  188. return double.NaN;
  189. }
  190. return WeekDay(t);
  191. }
  192. private static JsValue GetHours(JsValue thisObj, JsValue[] arguments)
  193. {
  194. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  195. if (double.IsNaN(t))
  196. {
  197. return double.NaN;
  198. }
  199. return HourFromTime(LocalTime(t));
  200. }
  201. private static JsValue GetUTCHours(JsValue thisObj, JsValue[] arguments)
  202. {
  203. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  204. if (double.IsNaN(t))
  205. {
  206. return double.NaN;
  207. }
  208. return HourFromTime(t);
  209. }
  210. private static JsValue GetMinutes(JsValue thisObj, JsValue[] arguments)
  211. {
  212. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  213. if (double.IsNaN(t))
  214. {
  215. return double.NaN;
  216. }
  217. return MinFromTime(LocalTime(t));
  218. }
  219. private static JsValue GetUTCMinutes(JsValue thisObj, JsValue[] arguments)
  220. {
  221. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  222. if (double.IsNaN(t))
  223. {
  224. return double.NaN;
  225. }
  226. return MinFromTime(t);
  227. }
  228. private static JsValue GetSeconds(JsValue thisObj, JsValue[] arguments)
  229. {
  230. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  231. if (double.IsNaN(t))
  232. {
  233. return double.NaN;
  234. }
  235. return SecFromTime(LocalTime(t));
  236. }
  237. private static JsValue GetUTCSeconds(JsValue thisObj, JsValue[] arguments)
  238. {
  239. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  240. if (double.IsNaN(t))
  241. {
  242. return double.NaN;
  243. }
  244. return SecFromTime(t);
  245. }
  246. private static JsValue GetMilliseconds(JsValue thisObj, JsValue[] arguments)
  247. {
  248. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  249. if (double.IsNaN(t))
  250. {
  251. return double.NaN;
  252. }
  253. return MsFromTime(LocalTime(t));
  254. }
  255. private static JsValue GetUTCMilliseconds(JsValue thisObj, JsValue[] arguments)
  256. {
  257. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  258. if (double.IsNaN(t))
  259. {
  260. return double.NaN;
  261. }
  262. return MsFromTime(t);
  263. }
  264. private static JsValue GetTimezoneOffset(JsValue thisObj, JsValue[] arguments)
  265. {
  266. var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
  267. if (double.IsNaN(t))
  268. {
  269. return double.NaN;
  270. }
  271. return (t - LocalTime(t))/MsPerMinute;
  272. }
  273. private static JsValue SetTime(JsValue thisObj, JsValue[] arguments)
  274. {
  275. return thisObj.As<DateInstance>().PrimitiveValue = TimeClip(TypeConverter.ToNumber(arguments.At(0)));
  276. }
  277. private static JsValue SetMilliseconds(JsValue thisObj, JsValue[] arguments)
  278. {
  279. var t = LocalTime(thisObj.As<DateInstance>().PrimitiveValue);
  280. var time = MakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), TypeConverter.ToNumber(arguments.At(0)));
  281. var u = TimeClip(Utc(MakeDate(Day(t), time)));
  282. thisObj.As<DateInstance>().PrimitiveValue = u;
  283. return u;
  284. }
  285. private static JsValue SetUTCMilliseconds(JsValue thisObj, JsValue[] arguments)
  286. {
  287. var t = thisObj.As<DateInstance>().PrimitiveValue;
  288. var time = MakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), TypeConverter.ToNumber(arguments.At(0)));
  289. var u = TimeClip(MakeDate(Day(t), time));
  290. thisObj.As<DateInstance>().PrimitiveValue = u;
  291. return u;
  292. }
  293. private static JsValue SetSeconds(JsValue thisObj, JsValue[] arguments)
  294. {
  295. var t = LocalTime(thisObj.As<DateInstance>().PrimitiveValue);
  296. var s = TypeConverter.ToNumber(arguments.At(0));
  297. var milli = arguments.Length <= 1 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  298. var date = MakeDate(Day(t), MakeTime(HourFromTime(t), MinFromTime(t), s, milli));
  299. var u = TimeClip(Utc(date));
  300. thisObj.As<DateInstance>().PrimitiveValue = u;
  301. return u;
  302. }
  303. private static JsValue SetUTCSeconds(JsValue thisObj, JsValue[] arguments)
  304. {
  305. var t = thisObj.As<DateInstance>().PrimitiveValue;
  306. var s = TypeConverter.ToNumber(arguments.At(0));
  307. var milli = arguments.Length <= 1 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  308. var date = MakeDate(Day(t), MakeTime(HourFromTime(t), MinFromTime(t), s, milli));
  309. var u = TimeClip(date);
  310. thisObj.As<DateInstance>().PrimitiveValue = u;
  311. return u;
  312. }
  313. private static JsValue SetMinutes(JsValue thisObj, JsValue[] arguments)
  314. {
  315. var t = LocalTime(thisObj.As<DateInstance>().PrimitiveValue);
  316. var m = TypeConverter.ToNumber(arguments.At(0));
  317. var s = arguments.Length <= 1 ? SecFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  318. var milli = arguments.Length <= 2 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
  319. var date = MakeDate(Day(t), MakeTime(HourFromTime(t), m, s, milli));
  320. var u = TimeClip(Utc(date));
  321. thisObj.As<DateInstance>().PrimitiveValue = u;
  322. return u;
  323. }
  324. private static JsValue SetUTCMinutes(JsValue thisObj, JsValue[] arguments)
  325. {
  326. var t = thisObj.As<DateInstance>().PrimitiveValue;
  327. var m = TypeConverter.ToNumber(arguments.At(0));
  328. var s = arguments.Length <= 1 ? SecFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  329. var milli = arguments.Length <= 2 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
  330. var date = MakeDate(Day(t), MakeTime(HourFromTime(t), m, s, milli));
  331. var u = TimeClip(date);
  332. thisObj.As<DateInstance>().PrimitiveValue = u;
  333. return u;
  334. }
  335. private static JsValue SetHours(JsValue thisObj, JsValue[] arguments)
  336. {
  337. var t = LocalTime(thisObj.As<DateInstance>().PrimitiveValue);
  338. var h = TypeConverter.ToNumber(arguments.At(0));
  339. var m = arguments.Length <= 1 ? MinFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  340. var s = arguments.Length <= 2 ? SecFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
  341. var milli = arguments.Length <= 3 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(3));
  342. var date = MakeDate(Day(t), MakeTime(h, m, s, milli));
  343. var u = TimeClip(Utc(date));
  344. thisObj.As<DateInstance>().PrimitiveValue = u;
  345. return u;
  346. }
  347. private static JsValue SetUTCHours(JsValue thisObj, JsValue[] arguments)
  348. {
  349. var t = thisObj.As<DateInstance>().PrimitiveValue;
  350. var h = TypeConverter.ToNumber(arguments.At(0));
  351. var m = arguments.Length <= 1 ? MinFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  352. var s = arguments.Length <= 2 ? SecFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
  353. var milli = arguments.Length <= 3 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(3));
  354. var date = MakeDate(Day(t), MakeTime(h, m, s, milli));
  355. var u = TimeClip(date);
  356. thisObj.As<DateInstance>().PrimitiveValue = u;
  357. return u;
  358. }
  359. private static JsValue SetDate(JsValue thisObj, JsValue[] arguments)
  360. {
  361. var t = LocalTime(thisObj.As<DateInstance>().PrimitiveValue);
  362. var dt = TypeConverter.ToNumber(arguments.At(0));
  363. var newDate = MakeDate(MakeDay(YearFromTime(t), MonthFromTime(t), dt), TimeWithinDay(t));
  364. var u = TimeClip(Utc(newDate));
  365. thisObj.As<DateInstance>().PrimitiveValue = u;
  366. return u;
  367. }
  368. private static JsValue SetUTCDate(JsValue thisObj, JsValue[] arguments)
  369. {
  370. var t = thisObj.As<DateInstance>().PrimitiveValue;
  371. var dt = TypeConverter.ToNumber(arguments.At(0));
  372. var newDate = MakeDate(MakeDay(YearFromTime(t), MonthFromTime(t), dt), TimeWithinDay(t));
  373. var u = TimeClip(newDate);
  374. thisObj.As<DateInstance>().PrimitiveValue = u;
  375. return u;
  376. }
  377. private static JsValue SetMonth(JsValue thisObj, JsValue[] arguments)
  378. {
  379. var t = LocalTime(thisObj.As<DateInstance>().PrimitiveValue);
  380. var m = TypeConverter.ToNumber(arguments.At(0));
  381. var dt = arguments.Length <= 1 ? DateFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  382. var newDate = MakeDate(MakeDay(YearFromTime(t), m, dt), TimeWithinDay(t));
  383. var u = TimeClip(Utc(newDate));
  384. thisObj.As<DateInstance>().PrimitiveValue = u;
  385. return u;
  386. }
  387. private static JsValue SetUTCMonth(JsValue thisObj, JsValue[] arguments)
  388. {
  389. var t = thisObj.As<DateInstance>().PrimitiveValue;
  390. var m = TypeConverter.ToNumber(arguments.At(0));
  391. var dt = arguments.Length <= 1 ? DateFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  392. var newDate = MakeDate(MakeDay(YearFromTime(t), m, dt), TimeWithinDay(t));
  393. var u = TimeClip(newDate);
  394. thisObj.As<DateInstance>().PrimitiveValue = u;
  395. return u;
  396. }
  397. private static JsValue SetFullYear(JsValue thisObj, JsValue[] arguments)
  398. {
  399. var thisTime = thisObj.As<DateInstance>().PrimitiveValue;
  400. var t = double.IsNaN(thisTime) ? +0 : LocalTime(thisTime);
  401. var y = TypeConverter.ToNumber(arguments.At(0));
  402. var m = arguments.Length <= 1 ? MonthFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  403. var dt = arguments.Length <= 2 ? DateFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
  404. var newDate = MakeDate(MakeDay(y, m, dt), TimeWithinDay(t));
  405. var u = TimeClip(Utc(newDate));
  406. thisObj.As<DateInstance>().PrimitiveValue = u;
  407. return u;
  408. }
  409. private static JsValue SetYear(JsValue thisObj, JsValue[] arguments)
  410. {
  411. var thisTime = thisObj.As<DateInstance>().PrimitiveValue;
  412. var t = double.IsNaN(thisTime) ? +0 : LocalTime(thisTime);
  413. var y = TypeConverter.ToNumber(arguments.At(0));
  414. if (double.IsNaN(y))
  415. {
  416. thisObj.As<DateInstance>().PrimitiveValue = double.NaN;
  417. return double.NaN;
  418. }
  419. var fy = TypeConverter.ToInteger(y);
  420. if (y >= 0 && y <= 99)
  421. {
  422. fy = fy + 1900;
  423. }
  424. var newDate = MakeDay(fy, MonthFromTime(t), DateFromTime(t));
  425. var u = Utc(MakeDate(newDate, TimeWithinDay(t)));
  426. thisObj.As<DateInstance>().PrimitiveValue = TimeClip(u);
  427. return u;
  428. }
  429. private static JsValue SetUTCFullYear(JsValue thisObj, JsValue[] arguments)
  430. {
  431. var thisTime = thisObj.As<DateInstance>().PrimitiveValue;
  432. var t = double.IsNaN(thisTime) ? +0 : thisTime;
  433. var y = TypeConverter.ToNumber(arguments.At(0));
  434. var m = arguments.Length <= 1 ? MonthFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  435. var dt = arguments.Length <= 2 ? DateFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
  436. var newDate = MakeDate(MakeDay(y, m, dt), TimeWithinDay(t));
  437. var u = TimeClip(newDate);
  438. thisObj.As<DateInstance>().PrimitiveValue = u;
  439. return u;
  440. }
  441. private JsValue ToUtcString(JsValue thisObj, JsValue[] arguments)
  442. {
  443. return thisObj.TryCast<DateInstance>(x =>
  444. {
  445. throw new JavaScriptException(Engine.TypeError);
  446. } )
  447. .ToDateTime().ToUniversalTime().ToString("r");
  448. }
  449. private JsValue ToISOString(JsValue thisObj, JsValue[] arguments)
  450. {
  451. var t = thisObj.TryCast<DateInstance>(x =>
  452. {
  453. throw new JavaScriptException(Engine.TypeError);
  454. }).PrimitiveValue;
  455. if (double.IsInfinity(t) || double.IsNaN(t))
  456. {
  457. throw new JavaScriptException(Engine.RangeError);
  458. }
  459. return string.Format("{0:0000}-{1:00}-{2:00}T{3:00}:{4:00}:{5:00}.{6:000}Z",
  460. YearFromTime(t),
  461. MonthFromTime(t)+1,
  462. DateFromTime(t),
  463. HourFromTime(t),
  464. MinFromTime(t),
  465. SecFromTime(t),
  466. MsFromTime(t));
  467. }
  468. private JsValue ToJSON(JsValue thisObj, JsValue[] arguments)
  469. {
  470. var o = TypeConverter.ToObject(Engine, thisObj);
  471. var tv = TypeConverter.ToPrimitive(o, Types.Number);
  472. if (tv.IsNumber() && double.IsInfinity(tv.AsNumber()))
  473. {
  474. return JsValue.Null;
  475. }
  476. var toIso = o.Get("toISOString");
  477. if (!toIso.Is<ICallable>())
  478. {
  479. throw new JavaScriptException(Engine.TypeError);
  480. }
  481. return toIso.TryCast<ICallable>().Call(o, Arguments.Empty);
  482. }
  483. public static double HoursPerDay = 24;
  484. public static double MinutesPerHour = 60;
  485. public static double SecondsPerMinute = 60;
  486. public static double MsPerSecond = 1000;
  487. public static double MsPerMinute = 60000;
  488. public static double MsPerHour = 3600000;
  489. public static double MsPerDay = 86400000;
  490. /// <summary>
  491. /// 15.9.1.2
  492. /// </summary>
  493. public static double Day(double t)
  494. {
  495. return System.Math.Floor(t / MsPerDay);
  496. }
  497. /// <summary>
  498. /// 15.9.1.2
  499. /// </summary>
  500. public static double TimeWithinDay(double t)
  501. {
  502. return t % MsPerDay;
  503. }
  504. /// <summary>
  505. /// The number of days in a year
  506. /// </summary>
  507. public static double DaysInYear(double y)
  508. {
  509. if (!(y%4).Equals(0))
  510. {
  511. return 365;
  512. }
  513. if ((y%4).Equals(0) && !(y%100).Equals(0))
  514. {
  515. return 366;
  516. }
  517. if ((y%100).Equals(0) && !(y%400).Equals(0))
  518. {
  519. return 365;
  520. }
  521. if ((y%400).Equals(0))
  522. {
  523. return 366;
  524. }
  525. return 365;
  526. }
  527. /// <summary>
  528. /// The day number of the first day of the year.
  529. /// </summary>
  530. public static double DayFromYear(double y)
  531. {
  532. return 365*(y - 1970)
  533. + System.Math.Floor((y - 1969)/4)
  534. - System.Math.Floor((y - 1901)/100)
  535. + System.Math.Floor((y - 1601)/400);
  536. }
  537. /// <summary>
  538. /// The time value of the start of the year
  539. /// </summary>
  540. public static double TimeFromYear(double y)
  541. {
  542. return MsPerDay*DayFromYear(y);
  543. }
  544. /// <summary>
  545. /// The year of a time value.
  546. /// </summary>
  547. public static double YearFromTime(double t)
  548. {
  549. double upper = double.MaxValue;
  550. double lower = double.MinValue;
  551. while (upper > lower + 1)
  552. {
  553. var current = System.Math.Floor((upper + lower) / 2);
  554. var tfy = TimeFromYear(current);
  555. if (tfy <= t)
  556. {
  557. lower = current;
  558. }
  559. else
  560. {
  561. upper = current;
  562. }
  563. }
  564. return lower;
  565. }
  566. /// <summary>
  567. /// <value>true</value> if the time is within a leap year, <value>false</value> otherwise
  568. /// </summary>
  569. public static double InLeapYear(double t)
  570. {
  571. var daysInYear = DaysInYear(YearFromTime(t));
  572. if (daysInYear.Equals(365))
  573. {
  574. return 0;
  575. }
  576. if (daysInYear.Equals(366))
  577. {
  578. return 1;
  579. }
  580. throw new ArgumentException();
  581. }
  582. /// <summary>
  583. /// The month number of a time value.
  584. /// </summary>
  585. public static double MonthFromTime(double t)
  586. {
  587. var dayWithinYear = DayWithinYear(t);
  588. var inLeapYear = InLeapYear(t);
  589. if (dayWithinYear < 31)
  590. {
  591. return 0;
  592. }
  593. if (dayWithinYear < 59 + inLeapYear)
  594. {
  595. return 1;
  596. }
  597. if (dayWithinYear < 90 + inLeapYear)
  598. {
  599. return 2;
  600. }
  601. if (dayWithinYear < 120 + inLeapYear)
  602. {
  603. return 3;
  604. }
  605. if (dayWithinYear < 151 + inLeapYear)
  606. {
  607. return 4;
  608. }
  609. if (dayWithinYear < 181 + inLeapYear)
  610. {
  611. return 5;
  612. }
  613. if (dayWithinYear < 212 + inLeapYear)
  614. {
  615. return 6;
  616. }
  617. if (dayWithinYear < 243 + inLeapYear)
  618. {
  619. return 7;
  620. }
  621. if (dayWithinYear < 273 + inLeapYear)
  622. {
  623. return 8;
  624. }
  625. if (dayWithinYear < 304 + inLeapYear)
  626. {
  627. return 9;
  628. }
  629. if (dayWithinYear < 334 + inLeapYear)
  630. {
  631. return 10;
  632. }
  633. if (dayWithinYear < 365 + inLeapYear)
  634. {
  635. return 11;
  636. }
  637. throw new InvalidOperationException();
  638. }
  639. public static double DayWithinYear(double t)
  640. {
  641. return Day(t) - DayFromYear(YearFromTime(t));
  642. }
  643. public static double DateFromTime(double t)
  644. {
  645. var monthFromTime = MonthFromTime(t);
  646. var dayWithinYear = DayWithinYear(t);
  647. if (monthFromTime.Equals(0))
  648. {
  649. return dayWithinYear + 1;
  650. }
  651. if (monthFromTime.Equals(1))
  652. {
  653. return dayWithinYear - 30;
  654. }
  655. if (monthFromTime.Equals(2))
  656. {
  657. return dayWithinYear - 58 - InLeapYear(t);
  658. }
  659. if (monthFromTime.Equals(3))
  660. {
  661. return dayWithinYear - 89 - InLeapYear(t);
  662. }
  663. if (monthFromTime.Equals(4))
  664. {
  665. return dayWithinYear - 119 - InLeapYear(t);
  666. }
  667. if (monthFromTime.Equals(5))
  668. {
  669. return dayWithinYear - 150 - InLeapYear(t);
  670. }
  671. if (monthFromTime.Equals(6))
  672. {
  673. return dayWithinYear - 180 - InLeapYear(t);
  674. }
  675. if (monthFromTime.Equals(7))
  676. {
  677. return dayWithinYear - 211 - InLeapYear(t);
  678. }
  679. if (monthFromTime.Equals(8))
  680. {
  681. return dayWithinYear - 242 - InLeapYear(t);
  682. }
  683. if (monthFromTime.Equals(9))
  684. {
  685. return dayWithinYear - 272 - InLeapYear(t);
  686. }
  687. if (monthFromTime.Equals(10))
  688. {
  689. return dayWithinYear - 303 - InLeapYear(t);
  690. }
  691. if (monthFromTime.Equals(11))
  692. {
  693. return dayWithinYear - 333 - InLeapYear(t);
  694. }
  695. throw new InvalidOperationException();
  696. }
  697. /// <summary>
  698. /// The weekday for a particular time value.
  699. /// </summary>
  700. public static double WeekDay(double t)
  701. {
  702. return (Day(t) + 4)%7;
  703. }
  704. public static double LocalTza
  705. {
  706. get
  707. {
  708. return TimeZoneInfo.Local.BaseUtcOffset.TotalMilliseconds;
  709. }
  710. }
  711. public static double DaylightSavingTa(double t)
  712. {
  713. var timeInYear = t - TimeFromYear(YearFromTime(t));
  714. if (double.IsInfinity(timeInYear) || double.IsNaN(timeInYear))
  715. {
  716. return 0;
  717. }
  718. var year = YearFromTime(t);
  719. if (year < 9999 && year > -9999)
  720. {
  721. // in DateTimeOffset range so we can use it
  722. }
  723. else
  724. {
  725. // use similar leap-ed year
  726. var isLeapYear = InLeapYear(t).Equals(1);
  727. year = isLeapYear ? 2000 : 1999;
  728. }
  729. var dateTime = new DateTime((int)year, 1, 1).AddMilliseconds(timeInYear);
  730. return TimeZoneInfo.Local.IsDaylightSavingTime(dateTime) ? MsPerHour : 0;
  731. }
  732. public static double LocalTime(double t)
  733. {
  734. return t + LocalTza + DaylightSavingTa(t);
  735. }
  736. public static double Utc(double t)
  737. {
  738. return t - LocalTza - DaylightSavingTa(t - LocalTza);
  739. }
  740. public static double HourFromTime(double t)
  741. {
  742. return System.Math.Floor(t / MsPerHour) % HoursPerDay;
  743. }
  744. public static double MinFromTime(double t)
  745. {
  746. return System.Math.Floor(t / MsPerMinute) % MinutesPerHour;
  747. }
  748. public static double SecFromTime(double t)
  749. {
  750. return System.Math.Floor(t / MsPerSecond) % SecondsPerMinute;
  751. }
  752. public static double MsFromTime(double t)
  753. {
  754. return t % MsPerSecond;
  755. }
  756. public static double DayFromMonth(double year, double month)
  757. {
  758. double day = month * 30;
  759. if (month >= 7)
  760. {
  761. day += month/2 - 1;
  762. }
  763. else if (month >= 2)
  764. {
  765. day += (month - 1)/2 - 1;
  766. }
  767. else
  768. {
  769. day += month;
  770. }
  771. if (month >= 2 && InLeapYear(year).Equals(1))
  772. {
  773. day++;
  774. }
  775. return day;
  776. }
  777. public static double DaysInMonth(double month, double leap)
  778. {
  779. month = month%12;
  780. switch ((long) month)
  781. {
  782. case 0:
  783. case 2:
  784. case 4:
  785. case 6:
  786. case 7:
  787. case 9:
  788. case 11:
  789. return 31;
  790. case 3:
  791. case 5:
  792. case 8:
  793. case 10:
  794. return 30;
  795. case 1:
  796. return 28 + leap;
  797. default:
  798. throw new ArgumentOutOfRangeException("month");
  799. }
  800. }
  801. public static double MakeTime(double hour, double min, double sec, double ms)
  802. {
  803. if (double.IsInfinity(hour) || double.IsInfinity(min) || double.IsInfinity(sec) || double.IsInfinity(ms))
  804. {
  805. return double.NaN;
  806. }
  807. var h = (long) hour;
  808. var m = (long) min;
  809. var s = (long) sec;
  810. var milli = (long) ms;
  811. var t = h*MsPerHour + m*MsPerMinute + s*MsPerSecond + milli;
  812. return t;
  813. }
  814. public static double MakeDay(double year, double month, double date)
  815. {
  816. if (double.IsInfinity(year) || double.IsInfinity(month) || double.IsInfinity(date))
  817. {
  818. return double.NaN;
  819. }
  820. year = TypeConverter.ToInteger(year);
  821. month = TypeConverter.ToInteger(month);
  822. date = TypeConverter.ToInteger(date);
  823. var sign = (year < 1970) ? -1 : 1;
  824. double t = (year < 1970) ? 1 : 0;
  825. int y;
  826. if (sign == -1)
  827. {
  828. for (y = 1969; y >= year; y += sign)
  829. {
  830. t += sign * DaysInYear(y) * MsPerDay;
  831. }
  832. }
  833. else
  834. {
  835. for (y = 1970; y < year; y += sign)
  836. {
  837. t += sign * DaysInYear(y) * MsPerDay;
  838. }
  839. }
  840. for (var m = 0; m < month; m++)
  841. {
  842. t += DaysInMonth(m, InLeapYear(t)) * MsPerDay;
  843. }
  844. return Day(t) + date - 1;
  845. }
  846. public static double MakeDate(double day, double time)
  847. {
  848. if (double.IsInfinity(day) || double.IsInfinity(time))
  849. {
  850. return double.NaN;
  851. }
  852. return day * MsPerDay + time;
  853. }
  854. public static double TimeClip(double time)
  855. {
  856. if (double.IsInfinity(time))
  857. {
  858. return double.NaN;
  859. }
  860. if (System.Math.Abs(time) > 8640000000000000)
  861. {
  862. return double.NaN;
  863. }
  864. return (long) time + 0;
  865. }
  866. }
  867. }