DatePrototype.cs 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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 ToLocalTime(thisObj.TryCast<DateInstance>().ToDateTime()).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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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. if (!AreFinite(t))
  550. {
  551. return Double.NaN;
  552. }
  553. double upper = double.MaxValue;
  554. double lower = double.MinValue;
  555. while (upper > lower + 1)
  556. {
  557. var current = System.Math.Floor((upper + lower) / 2);
  558. var tfy = TimeFromYear(current);
  559. if (tfy <= t)
  560. {
  561. lower = current;
  562. }
  563. else
  564. {
  565. upper = current;
  566. }
  567. }
  568. return lower;
  569. }
  570. /// <summary>
  571. /// <value>true</value> if the time is within a leap year, <value>false</value> otherwise
  572. /// </summary>
  573. public static double InLeapYear(double t)
  574. {
  575. var daysInYear = DaysInYear(YearFromTime(t));
  576. if (daysInYear.Equals(365))
  577. {
  578. return 0;
  579. }
  580. if (daysInYear.Equals(366))
  581. {
  582. return 1;
  583. }
  584. throw new ArgumentException();
  585. }
  586. /// <summary>
  587. /// The month number of a time value.
  588. /// </summary>
  589. public static double MonthFromTime(double t)
  590. {
  591. var dayWithinYear = DayWithinYear(t);
  592. var inLeapYear = InLeapYear(t);
  593. if (dayWithinYear < 31)
  594. {
  595. return 0;
  596. }
  597. if (dayWithinYear < 59 + inLeapYear)
  598. {
  599. return 1;
  600. }
  601. if (dayWithinYear < 90 + inLeapYear)
  602. {
  603. return 2;
  604. }
  605. if (dayWithinYear < 120 + inLeapYear)
  606. {
  607. return 3;
  608. }
  609. if (dayWithinYear < 151 + inLeapYear)
  610. {
  611. return 4;
  612. }
  613. if (dayWithinYear < 181 + inLeapYear)
  614. {
  615. return 5;
  616. }
  617. if (dayWithinYear < 212 + inLeapYear)
  618. {
  619. return 6;
  620. }
  621. if (dayWithinYear < 243 + inLeapYear)
  622. {
  623. return 7;
  624. }
  625. if (dayWithinYear < 273 + inLeapYear)
  626. {
  627. return 8;
  628. }
  629. if (dayWithinYear < 304 + inLeapYear)
  630. {
  631. return 9;
  632. }
  633. if (dayWithinYear < 334 + inLeapYear)
  634. {
  635. return 10;
  636. }
  637. if (dayWithinYear < 365 + inLeapYear)
  638. {
  639. return 11;
  640. }
  641. throw new InvalidOperationException();
  642. }
  643. public static double DayWithinYear(double t)
  644. {
  645. return Day(t) - DayFromYear(YearFromTime(t));
  646. }
  647. public static double DateFromTime(double t)
  648. {
  649. var monthFromTime = MonthFromTime(t);
  650. var dayWithinYear = DayWithinYear(t);
  651. if (monthFromTime.Equals(0))
  652. {
  653. return dayWithinYear + 1;
  654. }
  655. if (monthFromTime.Equals(1))
  656. {
  657. return dayWithinYear - 30;
  658. }
  659. if (monthFromTime.Equals(2))
  660. {
  661. return dayWithinYear - 58 - InLeapYear(t);
  662. }
  663. if (monthFromTime.Equals(3))
  664. {
  665. return dayWithinYear - 89 - InLeapYear(t);
  666. }
  667. if (monthFromTime.Equals(4))
  668. {
  669. return dayWithinYear - 119 - InLeapYear(t);
  670. }
  671. if (monthFromTime.Equals(5))
  672. {
  673. return dayWithinYear - 150 - InLeapYear(t);
  674. }
  675. if (monthFromTime.Equals(6))
  676. {
  677. return dayWithinYear - 180 - InLeapYear(t);
  678. }
  679. if (monthFromTime.Equals(7))
  680. {
  681. return dayWithinYear - 211 - InLeapYear(t);
  682. }
  683. if (monthFromTime.Equals(8))
  684. {
  685. return dayWithinYear - 242 - InLeapYear(t);
  686. }
  687. if (monthFromTime.Equals(9))
  688. {
  689. return dayWithinYear - 272 - InLeapYear(t);
  690. }
  691. if (monthFromTime.Equals(10))
  692. {
  693. return dayWithinYear - 303 - InLeapYear(t);
  694. }
  695. if (monthFromTime.Equals(11))
  696. {
  697. return dayWithinYear - 333 - InLeapYear(t);
  698. }
  699. throw new InvalidOperationException();
  700. }
  701. /// <summary>
  702. /// The weekday for a particular time value.
  703. /// </summary>
  704. public static double WeekDay(double t)
  705. {
  706. return (Day(t) + 4)%7;
  707. }
  708. public double LocalTza
  709. {
  710. get { return Engine.Options.GetLocalTimeZone().BaseUtcOffset.TotalMilliseconds; }
  711. }
  712. public double DaylightSavingTa(double t)
  713. {
  714. var timeInYear = t - TimeFromYear(YearFromTime(t));
  715. if (double.IsInfinity(timeInYear) || double.IsNaN(timeInYear))
  716. {
  717. return 0;
  718. }
  719. var year = YearFromTime(t);
  720. if (year < 9999 && year > -9999)
  721. {
  722. // in DateTimeOffset range so we can use it
  723. }
  724. else
  725. {
  726. // use similar leap-ed year
  727. var isLeapYear = InLeapYear(t).Equals(1);
  728. year = isLeapYear ? 2000 : 1999;
  729. }
  730. var dateTime = new DateTime((int)year, 1, 1).AddMilliseconds(timeInYear);
  731. return Engine.Options.GetLocalTimeZone().IsDaylightSavingTime(dateTime) ? MsPerHour : 0;
  732. }
  733. public DateTime ToLocalTime(DateTime t)
  734. {
  735. if (t.Kind == DateTimeKind.Local)
  736. {
  737. return t;
  738. }
  739. if (t.Kind == DateTimeKind.Unspecified)
  740. {
  741. t = DateTime.SpecifyKind(t, DateTimeKind.Utc);
  742. }
  743. return TimeZoneInfo.ConvertTime(t, Engine.Options.GetLocalTimeZone());
  744. }
  745. public double LocalTime(double t)
  746. {
  747. return t + LocalTza + DaylightSavingTa(t);
  748. }
  749. public double Utc(double t)
  750. {
  751. return t - LocalTza - DaylightSavingTa(t - LocalTza);
  752. }
  753. public static double HourFromTime(double t)
  754. {
  755. return System.Math.Floor(t / MsPerHour) % HoursPerDay;
  756. }
  757. public static double MinFromTime(double t)
  758. {
  759. return System.Math.Floor(t / MsPerMinute) % MinutesPerHour;
  760. }
  761. public static double SecFromTime(double t)
  762. {
  763. return System.Math.Floor(t / MsPerSecond) % SecondsPerMinute;
  764. }
  765. public static double MsFromTime(double t)
  766. {
  767. return t % MsPerSecond;
  768. }
  769. public static double DayFromMonth(double year, double month)
  770. {
  771. double day = month * 30;
  772. if (month >= 7)
  773. {
  774. day += month/2 - 1;
  775. }
  776. else if (month >= 2)
  777. {
  778. day += (month - 1)/2 - 1;
  779. }
  780. else
  781. {
  782. day += month;
  783. }
  784. if (month >= 2 && InLeapYear(year).Equals(1))
  785. {
  786. day++;
  787. }
  788. return day;
  789. }
  790. public static double DaysInMonth(double month, double leap)
  791. {
  792. month = month%12;
  793. switch ((long) month)
  794. {
  795. case 0:
  796. case 2:
  797. case 4:
  798. case 6:
  799. case 7:
  800. case 9:
  801. case 11:
  802. return 31;
  803. case 3:
  804. case 5:
  805. case 8:
  806. case 10:
  807. return 30;
  808. case 1:
  809. return 28 + leap;
  810. default:
  811. throw new ArgumentOutOfRangeException("month");
  812. }
  813. }
  814. public static double MakeTime(double hour, double min, double sec, double ms)
  815. {
  816. if (!AreFinite(hour, min, sec, ms))
  817. {
  818. return double.NaN;
  819. }
  820. var h = (long) hour;
  821. var m = (long) min;
  822. var s = (long) sec;
  823. var milli = (long) ms;
  824. var t = h*MsPerHour + m*MsPerMinute + s*MsPerSecond + milli;
  825. return t;
  826. }
  827. public static double MakeDay(double year, double month, double date)
  828. {
  829. if (!AreFinite(year, month, date))
  830. {
  831. return double.NaN;
  832. }
  833. year = TypeConverter.ToInteger(year);
  834. month = TypeConverter.ToInteger(month);
  835. date = TypeConverter.ToInteger(date);
  836. var sign = (year < 1970) ? -1 : 1;
  837. double t = (year < 1970) ? 1 : 0;
  838. int y;
  839. if (sign == -1)
  840. {
  841. for (y = 1969; y >= year; y += sign)
  842. {
  843. t += sign * DaysInYear(y) * MsPerDay;
  844. }
  845. }
  846. else
  847. {
  848. for (y = 1970; y < year; y += sign)
  849. {
  850. t += sign * DaysInYear(y) * MsPerDay;
  851. }
  852. }
  853. for (var m = 0; m < month; m++)
  854. {
  855. t += DaysInMonth(m, InLeapYear(t)) * MsPerDay;
  856. }
  857. return Day(t) + date - 1;
  858. }
  859. public static double MakeDate(double day, double time)
  860. {
  861. if (!AreFinite(day, time))
  862. {
  863. return double.NaN;
  864. }
  865. return day * MsPerDay + time;
  866. }
  867. public static double TimeClip(double time)
  868. {
  869. if (!AreFinite(time))
  870. {
  871. return double.NaN;
  872. }
  873. if (System.Math.Abs(time) > 8640000000000000)
  874. {
  875. return double.NaN;
  876. }
  877. return (long) time + 0;
  878. }
  879. private static bool AreFinite(params double[] values)
  880. {
  881. for (int index = 0; index < values.Length; index++)
  882. {
  883. var value = values[index];
  884. if (double.IsNaN(value) || double.IsInfinity(value))
  885. {
  886. return false;
  887. }
  888. }
  889. return true;
  890. }
  891. }
  892. }