DatePrototype.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  2. using System.Globalization;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. using Jint.Collections;
  6. using Jint.Native.Object;
  7. using Jint.Native.Symbol;
  8. using Jint.Runtime;
  9. using Jint.Runtime.Descriptors;
  10. using Jint.Runtime.Interop;
  11. namespace Jint.Native.Date
  12. {
  13. /// <summary>
  14. /// https://tc39.es/ecma262/#sec-properties-of-the-date-prototype-object
  15. /// </summary>
  16. internal sealed class DatePrototype : Prototype
  17. {
  18. // ES6 section 20.3.1.1 Time Values and Time Range
  19. private const double MinYear = -1000000.0;
  20. private const double MaxYear = -MinYear;
  21. private const double MinMonth = -10000000.0;
  22. private const double MaxMonth = -MinMonth;
  23. private readonly DateConstructor _constructor;
  24. private readonly ITimeSystem _timeSystem;
  25. internal DatePrototype(
  26. Engine engine,
  27. DateConstructor constructor,
  28. ObjectPrototype objectPrototype)
  29. : base(engine, engine.Realm)
  30. {
  31. _prototype = objectPrototype;
  32. _constructor = constructor;
  33. _timeSystem = engine.Options.TimeSystem;
  34. }
  35. protected override void Initialize()
  36. {
  37. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  38. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  39. var properties = new PropertyDictionary(50, checkExistingKeys: false)
  40. {
  41. ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  42. ["toString"] = new PropertyDescriptor(new ClrFunction(Engine, "toString", ToString, 0, lengthFlags), propertyFlags),
  43. ["toDateString"] = new PropertyDescriptor(new ClrFunction(Engine, "toDateString", ToDateString, 0, lengthFlags), propertyFlags),
  44. ["toTimeString"] = new PropertyDescriptor(new ClrFunction(Engine, "toTimeString", ToTimeString, 0, lengthFlags), propertyFlags),
  45. ["toLocaleString"] = new PropertyDescriptor(new ClrFunction(Engine, "toLocaleString", ToLocaleString, 0, lengthFlags), propertyFlags),
  46. ["toLocaleDateString"] = new PropertyDescriptor(new ClrFunction(Engine, "toLocaleDateString", ToLocaleDateString, 0, lengthFlags), propertyFlags),
  47. ["toLocaleTimeString"] = new PropertyDescriptor(new ClrFunction(Engine, "toLocaleTimeString", ToLocaleTimeString, 0, lengthFlags), propertyFlags),
  48. ["valueOf"] = new PropertyDescriptor(new ClrFunction(Engine, "valueOf", ValueOf, 0, lengthFlags), propertyFlags),
  49. ["getTime"] = new PropertyDescriptor(new ClrFunction(Engine, "getTime", GetTime, 0, lengthFlags), propertyFlags),
  50. ["getFullYear"] = new PropertyDescriptor(new ClrFunction(Engine, "getFullYear", GetFullYear, 0, lengthFlags), propertyFlags),
  51. ["getYear"] = new PropertyDescriptor(new ClrFunction(Engine, "getYear", GetYear, 0, lengthFlags), propertyFlags),
  52. ["getUTCFullYear"] = new PropertyDescriptor(new ClrFunction(Engine, "getUTCFullYear", GetUTCFullYear, 0, lengthFlags), propertyFlags),
  53. ["getMonth"] = new PropertyDescriptor(new ClrFunction(Engine, "getMonth", GetMonth, 0, lengthFlags), propertyFlags),
  54. ["getUTCMonth"] = new PropertyDescriptor(new ClrFunction(Engine, "getUTCMonth", GetUTCMonth, 0, lengthFlags), propertyFlags),
  55. ["getDate"] = new PropertyDescriptor(new ClrFunction(Engine, "getDate", GetDate, 0, lengthFlags), propertyFlags),
  56. ["getUTCDate"] = new PropertyDescriptor(new ClrFunction(Engine, "getUTCDate", GetUTCDate, 0, lengthFlags), propertyFlags),
  57. ["getDay"] = new PropertyDescriptor(new ClrFunction(Engine, "getDay", GetDay, 0, lengthFlags), propertyFlags),
  58. ["getUTCDay"] = new PropertyDescriptor(new ClrFunction(Engine, "getUTCDay", GetUTCDay, 0, lengthFlags), propertyFlags),
  59. ["getHours"] = new PropertyDescriptor(new ClrFunction(Engine, "getHours", GetHours, 0, lengthFlags), propertyFlags),
  60. ["getUTCHours"] = new PropertyDescriptor(new ClrFunction(Engine, "getUTCHours", GetUTCHours, 0, lengthFlags), propertyFlags),
  61. ["getMinutes"] = new PropertyDescriptor(new ClrFunction(Engine, "getMinutes", GetMinutes, 0, lengthFlags), propertyFlags),
  62. ["getUTCMinutes"] = new PropertyDescriptor(new ClrFunction(Engine, "getUTCMinutes", GetUTCMinutes, 0, lengthFlags), propertyFlags),
  63. ["getSeconds"] = new PropertyDescriptor(new ClrFunction(Engine, "getSeconds", GetSeconds, 0, lengthFlags), propertyFlags),
  64. ["getUTCSeconds"] = new PropertyDescriptor(new ClrFunction(Engine, "getUTCSeconds", GetUTCSeconds, 0, lengthFlags), propertyFlags),
  65. ["getMilliseconds"] = new PropertyDescriptor(new ClrFunction(Engine, "getMilliseconds", GetMilliseconds, 0, lengthFlags), propertyFlags),
  66. ["getUTCMilliseconds"] = new PropertyDescriptor(new ClrFunction(Engine, "getUTCMilliseconds", GetUTCMilliseconds, 0, lengthFlags), propertyFlags),
  67. ["getTimezoneOffset"] = new PropertyDescriptor(new ClrFunction(Engine, "getTimezoneOffset", GetTimezoneOffset, 0, lengthFlags), propertyFlags),
  68. ["setTime"] = new PropertyDescriptor(new ClrFunction(Engine, "setTime", SetTime, 1, lengthFlags), propertyFlags),
  69. ["setMilliseconds"] = new PropertyDescriptor(new ClrFunction(Engine, "setMilliseconds", SetMilliseconds, 1, lengthFlags), propertyFlags),
  70. ["setUTCMilliseconds"] = new PropertyDescriptor(new ClrFunction(Engine, "setUTCMilliseconds", SetUTCMilliseconds, 1, lengthFlags), propertyFlags),
  71. ["setSeconds"] = new PropertyDescriptor(new ClrFunction(Engine, "setSeconds", SetSeconds, 2, lengthFlags), propertyFlags),
  72. ["setUTCSeconds"] = new PropertyDescriptor(new ClrFunction(Engine, "setUTCSeconds", SetUTCSeconds, 2, lengthFlags), propertyFlags),
  73. ["setMinutes"] = new PropertyDescriptor(new ClrFunction(Engine, "setMinutes", SetMinutes, 3, lengthFlags), propertyFlags),
  74. ["setUTCMinutes"] = new PropertyDescriptor(new ClrFunction(Engine, "setUTCMinutes", SetUTCMinutes, 3, lengthFlags), propertyFlags),
  75. ["setHours"] = new PropertyDescriptor(new ClrFunction(Engine, "setHours", SetHours, 4, lengthFlags), propertyFlags),
  76. ["setUTCHours"] = new PropertyDescriptor(new ClrFunction(Engine, "setUTCHours", SetUTCHours, 4, lengthFlags), propertyFlags),
  77. ["setDate"] = new PropertyDescriptor(new ClrFunction(Engine, "setDate", SetDate, 1, lengthFlags), propertyFlags),
  78. ["setUTCDate"] = new PropertyDescriptor(new ClrFunction(Engine, "setUTCDate", SetUTCDate, 1, lengthFlags), propertyFlags),
  79. ["setMonth"] = new PropertyDescriptor(new ClrFunction(Engine, "setMonth", SetMonth, 2, lengthFlags), propertyFlags),
  80. ["setUTCMonth"] = new PropertyDescriptor(new ClrFunction(Engine, "setUTCMonth", SetUTCMonth, 2, lengthFlags), propertyFlags),
  81. ["setFullYear"] = new PropertyDescriptor(new ClrFunction(Engine, "setFullYear", SetFullYear, 3, lengthFlags), propertyFlags),
  82. ["setYear"] = new PropertyDescriptor(new ClrFunction(Engine, "setYear", SetYear, 1, lengthFlags), propertyFlags),
  83. ["setUTCFullYear"] = new PropertyDescriptor(new ClrFunction(Engine, "setUTCFullYear", SetUTCFullYear, 3, lengthFlags), propertyFlags),
  84. ["toUTCString"] = new PropertyDescriptor(new ClrFunction(Engine, "toUTCString", ToUtcString, 0, lengthFlags), propertyFlags),
  85. ["toISOString"] = new PropertyDescriptor(new ClrFunction(Engine, "toISOString", ToISOString, 0, lengthFlags), propertyFlags),
  86. ["toJSON"] = new PropertyDescriptor(new ClrFunction(Engine, "toJSON", ToJson, 1, lengthFlags), propertyFlags)
  87. };
  88. SetProperties(properties);
  89. var symbols = new SymbolDictionary(1)
  90. {
  91. [GlobalSymbolRegistry.ToPrimitive] = new PropertyDescriptor(new ClrFunction(Engine, "[Symbol.toPrimitive]", ToPrimitive, 1, PropertyFlag.Configurable), PropertyFlag.Configurable),
  92. };
  93. SetSymbols(symbols);
  94. }
  95. /// <summary>
  96. /// https://tc39.es/ecma262/#sec-date.prototype-@@toprimitive
  97. /// </summary>
  98. private JsValue ToPrimitive(JsValue thisObject, JsValue[] arguments)
  99. {
  100. var oi = thisObject as ObjectInstance;
  101. if (oi is null)
  102. {
  103. ExceptionHelper.ThrowTypeError(_realm);
  104. }
  105. var hint = arguments.At(0);
  106. if (!hint.IsString())
  107. {
  108. ExceptionHelper.ThrowTypeError(_realm);
  109. }
  110. var hintString = hint.ToString();
  111. var tryFirst = Types.Empty;
  112. if (string.Equals(hintString, "default", StringComparison.Ordinal) || string.Equals(hintString, "string", StringComparison.Ordinal))
  113. {
  114. tryFirst = Types.String;
  115. }
  116. else if (string.Equals(hintString, "number", StringComparison.Ordinal))
  117. {
  118. tryFirst = Types.Number;
  119. }
  120. else
  121. {
  122. ExceptionHelper.ThrowTypeError(_realm);
  123. }
  124. return TypeConverter.OrdinaryToPrimitive(oi, tryFirst);
  125. }
  126. private JsValue ValueOf(JsValue thisObject, JsValue[] arguments)
  127. {
  128. return ThisTimeValue(thisObject).ToJsValue();
  129. }
  130. /// <summary>
  131. /// https://tc39.es/ecma262/#thistimevalue
  132. /// </summary>
  133. private DatePresentation ThisTimeValue(JsValue thisObject)
  134. {
  135. if (thisObject is JsDate dateInstance)
  136. {
  137. return dateInstance._dateValue;
  138. }
  139. ExceptionHelper.ThrowTypeError(_realm, "this is not a Date object");
  140. return default;
  141. }
  142. /// <summary>
  143. /// https://tc39.es/ecma262/#sec-date.prototype.tostring
  144. /// </summary>
  145. internal JsValue ToString(JsValue thisObject, JsValue[] arg2)
  146. {
  147. var tv = ThisTimeValue(thisObject);
  148. return ToDateString(tv);
  149. }
  150. /// <summary>
  151. /// https://tc39.es/ecma262/#sec-date.prototype.todatestring
  152. /// </summary>
  153. private JsValue ToDateString(JsValue thisObject, JsValue[] arguments)
  154. {
  155. var tv = ThisTimeValue(thisObject);
  156. if (tv.IsNaN)
  157. {
  158. return "Invalid Date";
  159. }
  160. var t = LocalTime(tv);
  161. return DateString(t);
  162. }
  163. /// <summary>
  164. /// https://tc39.es/ecma262/#sec-todatestring
  165. /// </summary>
  166. private JsValue ToDateString(DatePresentation tv)
  167. {
  168. if (tv.IsNaN)
  169. {
  170. return "Invalid Date";
  171. }
  172. var t = LocalTime(tv);
  173. return DateString(t) + " " + TimeString(t) + TimeZoneString(tv);
  174. }
  175. /// <summary>
  176. /// https://tc39.es/ecma262/#sec-date.prototype.totimestring
  177. /// </summary>
  178. private JsValue ToTimeString(JsValue thisObject, JsValue[] arguments)
  179. {
  180. var tv = ThisTimeValue(thisObject);
  181. if (tv.IsNaN)
  182. {
  183. return "Invalid Date";
  184. }
  185. var t = LocalTime(tv);
  186. return TimeString(t) + TimeZoneString(tv);
  187. }
  188. /// <summary>
  189. /// https://tc39.es/ecma262/#sec-date.prototype.tolocalestring
  190. /// </summary>
  191. private JsValue ToLocaleString(JsValue thisObject, JsValue[] arguments)
  192. {
  193. var dateInstance = ThisTimeValue(thisObject);
  194. if (dateInstance.IsNaN)
  195. {
  196. return "Invalid Date";
  197. }
  198. return ToLocalTime(dateInstance).ToString("F", Engine.Options.Culture);
  199. }
  200. /// <summary>
  201. /// https://tc39.es/ecma262/#sec-date.prototype.tolocaledatestring
  202. /// </summary>
  203. private JsValue ToLocaleDateString(JsValue thisObject, JsValue[] arguments)
  204. {
  205. var dateInstance = ThisTimeValue(thisObject);
  206. if (dateInstance.IsNaN)
  207. {
  208. return "Invalid Date";
  209. }
  210. return ToLocalTime(dateInstance).ToString("D", Engine.Options.Culture);
  211. }
  212. /// <summary>
  213. /// https://tc39.es/ecma262/#sec-date.prototype.tolocaletimestring
  214. /// </summary>
  215. private JsValue ToLocaleTimeString(JsValue thisObject, JsValue[] arguments)
  216. {
  217. var dateInstance = ThisTimeValue(thisObject);
  218. if (dateInstance.IsNaN)
  219. {
  220. return "Invalid Date";
  221. }
  222. return ToLocalTime(dateInstance).ToString("T", Engine.Options.Culture);
  223. }
  224. private JsValue GetTime(JsValue thisObject, JsValue[] arguments)
  225. {
  226. var t = ThisTimeValue(thisObject);
  227. if (t.IsNaN)
  228. {
  229. return JsNumber.DoubleNaN;
  230. }
  231. return t.ToJsValue();
  232. }
  233. private JsValue GetFullYear(JsValue thisObject, JsValue[] arguments)
  234. {
  235. var t = ThisTimeValue(thisObject);
  236. if (t.IsNaN)
  237. {
  238. return JsNumber.DoubleNaN;
  239. }
  240. return YearFromTime(LocalTime(t));
  241. }
  242. private JsValue GetYear(JsValue thisObject, JsValue[] arguments)
  243. {
  244. var t = ThisTimeValue(thisObject);
  245. if (t.IsNaN)
  246. {
  247. return JsNumber.DoubleNaN;
  248. }
  249. return YearFromTime(LocalTime(t)) - 1900;
  250. }
  251. private JsValue GetUTCFullYear(JsValue thisObject, JsValue[] arguments)
  252. {
  253. var t = ThisTimeValue(thisObject);
  254. if (t.IsNaN)
  255. {
  256. return JsNumber.DoubleNaN;
  257. }
  258. return YearFromTime(t);
  259. }
  260. private JsValue GetMonth(JsValue thisObject, JsValue[] arguments)
  261. {
  262. var t = ThisTimeValue(thisObject);
  263. if (t.IsNaN)
  264. {
  265. return JsNumber.DoubleNaN;
  266. }
  267. return MonthFromTime(LocalTime(t));
  268. }
  269. /// <summary>
  270. /// https://tc39.es/ecma262/#sec-date.prototype.getutcmonth
  271. /// </summary>
  272. private JsValue GetUTCMonth(JsValue thisObject, JsValue[] arguments)
  273. {
  274. var t = ThisTimeValue(thisObject);
  275. if (t.IsNaN)
  276. {
  277. return JsNumber.DoubleNaN;
  278. }
  279. return MonthFromTime(t);
  280. }
  281. /// <summary>
  282. /// https://tc39.es/ecma262/#sec-date.prototype.getdate
  283. /// </summary>
  284. private JsValue GetDate(JsValue thisObject, JsValue[] arguments)
  285. {
  286. var t = ThisTimeValue(thisObject);
  287. if (t.IsNaN)
  288. {
  289. return JsNumber.DoubleNaN;
  290. }
  291. return DateFromTime(LocalTime(t));
  292. }
  293. private JsValue GetUTCDate(JsValue thisObject, JsValue[] arguments)
  294. {
  295. var t = ThisTimeValue(thisObject);
  296. if (t.IsNaN)
  297. {
  298. return JsNumber.DoubleNaN;
  299. }
  300. return DateFromTime(t);
  301. }
  302. private JsValue GetDay(JsValue thisObject, JsValue[] arguments)
  303. {
  304. var t = ThisTimeValue(thisObject);
  305. if (t.IsNaN)
  306. {
  307. return JsNumber.DoubleNaN;
  308. }
  309. return WeekDay(LocalTime(t));
  310. }
  311. private JsValue GetUTCDay(JsValue thisObject, JsValue[] arguments)
  312. {
  313. var t = ThisTimeValue(thisObject);
  314. if (t.IsNaN)
  315. {
  316. return JsNumber.DoubleNaN;
  317. }
  318. return WeekDay(t);
  319. }
  320. private JsValue GetHours(JsValue thisObject, JsValue[] arguments)
  321. {
  322. var t = ThisTimeValue(thisObject);
  323. if (t.IsNaN)
  324. {
  325. return JsNumber.DoubleNaN;
  326. }
  327. return HourFromTime(LocalTime(t));
  328. }
  329. private JsValue GetUTCHours(JsValue thisObject, JsValue[] arguments)
  330. {
  331. var t = ThisTimeValue(thisObject);
  332. if (t.IsNaN)
  333. {
  334. return JsNumber.DoubleNaN;
  335. }
  336. return HourFromTime(t);
  337. }
  338. private JsValue GetMinutes(JsValue thisObject, JsValue[] arguments)
  339. {
  340. var t = ThisTimeValue(thisObject);
  341. if (t.IsNaN)
  342. {
  343. return JsNumber.DoubleNaN;
  344. }
  345. return MinFromTime(LocalTime(t));
  346. }
  347. private JsValue GetUTCMinutes(JsValue thisObject, JsValue[] arguments)
  348. {
  349. var t = ThisTimeValue(thisObject);
  350. if (t.IsNaN)
  351. {
  352. return JsNumber.DoubleNaN;
  353. }
  354. return MinFromTime(t);
  355. }
  356. private JsValue GetSeconds(JsValue thisObject, JsValue[] arguments)
  357. {
  358. var t = ThisTimeValue(thisObject);
  359. if (t.IsNaN)
  360. {
  361. return JsNumber.DoubleNaN;
  362. }
  363. return SecFromTime(LocalTime(t));
  364. }
  365. private JsValue GetUTCSeconds(JsValue thisObject, JsValue[] arguments)
  366. {
  367. var t = ThisTimeValue(thisObject);
  368. if (t.IsNaN)
  369. {
  370. return JsNumber.DoubleNaN;
  371. }
  372. return SecFromTime(t);
  373. }
  374. private JsValue GetMilliseconds(JsValue thisObject, JsValue[] arguments)
  375. {
  376. var t = ThisTimeValue(thisObject);
  377. if (t.IsNaN)
  378. {
  379. return JsNumber.DoubleNaN;
  380. }
  381. return MsFromTime(LocalTime(t));
  382. }
  383. private JsValue GetUTCMilliseconds(JsValue thisObject, JsValue[] arguments)
  384. {
  385. var t = ThisTimeValue(thisObject);
  386. if (t.IsNaN)
  387. {
  388. return JsNumber.DoubleNaN;
  389. }
  390. return MsFromTime(t);
  391. }
  392. private JsValue GetTimezoneOffset(JsValue thisObject, JsValue[] arguments)
  393. {
  394. var t = ThisTimeValue(thisObject);
  395. if (t.IsNaN)
  396. {
  397. return JsNumber.DoubleNaN;
  398. }
  399. return (int) ((double) t.Value - LocalTime(t).Value)/MsPerMinute;
  400. }
  401. /// <summary>
  402. /// https://tc39.es/ecma262/#sec-date.prototype.settime
  403. /// </summary>
  404. private JsValue SetTime(JsValue thisObject, JsValue[] arguments)
  405. {
  406. ThisTimeValue(thisObject);
  407. var t = TypeConverter.ToNumber(arguments.At(0));
  408. var v = t.TimeClip();
  409. ((JsDate) thisObject)._dateValue = t;
  410. return v.ToJsValue();
  411. }
  412. /// <summary>
  413. /// https://tc39.es/ecma262/#sec-date.prototype.setmilliseconds
  414. /// </summary>
  415. private JsValue SetMilliseconds(JsValue thisObject, JsValue[] arguments)
  416. {
  417. var t = LocalTime(ThisTimeValue(thisObject));
  418. var ms = TypeConverter.ToNumber(arguments.At(0));
  419. if (t.IsNaN)
  420. {
  421. return JsNumber.DoubleNaN;
  422. }
  423. var time = MakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), ms);
  424. var u = Utc(MakeDate(Day(t), time)).TimeClip();
  425. ((JsDate) thisObject)._dateValue = u;
  426. return u.ToJsValue();
  427. }
  428. /// <summary>
  429. /// https://tc39.es/ecma262/#sec-date.prototype.setutcmilliseconds
  430. /// </summary>
  431. private JsValue SetUTCMilliseconds(JsValue thisObject, JsValue[] arguments)
  432. {
  433. var t = ThisTimeValue(thisObject);
  434. var milli = TypeConverter.ToNumber(arguments.At(0));
  435. if (t.IsNaN)
  436. {
  437. return double.NaN;
  438. }
  439. var time = MakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), milli);
  440. var u = MakeDate(Day(t), time).TimeClip();
  441. ((JsDate) thisObject)._dateValue = u;
  442. return u.ToJsValue();
  443. }
  444. /// <summary>
  445. /// https://tc39.es/ecma262/#sec-date.prototype.setseconds
  446. /// </summary>
  447. private JsValue SetSeconds(JsValue thisObject, JsValue[] arguments)
  448. {
  449. var t = LocalTime(ThisTimeValue(thisObject));
  450. var s = TypeConverter.ToNumber(arguments.At(0));
  451. var milli = arguments.Length <= 1 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  452. if (t.IsNaN)
  453. {
  454. return JsNumber.DoubleNaN;
  455. }
  456. var date = MakeDate(Day(t), MakeTime(HourFromTime(t), MinFromTime(t), s, milli));
  457. var u = Utc(date).TimeClip();
  458. ((JsDate) thisObject)._dateValue = u;
  459. return u.ToJsValue();
  460. }
  461. /// <summary>
  462. /// https://tc39.es/ecma262/#sec-date.prototype.setutcseconds
  463. /// </summary>
  464. private JsValue SetUTCSeconds(JsValue thisObject, JsValue[] arguments)
  465. {
  466. var t = ThisTimeValue(thisObject);
  467. var s = TypeConverter.ToNumber(arguments.At(0));
  468. var milli = arguments.Length <= 1 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  469. if (t.IsNaN)
  470. {
  471. return JsNumber.DoubleNaN;
  472. }
  473. var date = MakeDate(Day(t), MakeTime(HourFromTime(t), MinFromTime(t), s, milli));
  474. var u = date.TimeClip();
  475. ((JsDate) thisObject)._dateValue = u;
  476. return u.ToJsValue();
  477. }
  478. /// <summary>
  479. /// https://tc39.es/ecma262/#sec-date.prototype.setminutes
  480. /// </summary>
  481. private JsValue SetMinutes(JsValue thisObject, JsValue[] arguments)
  482. {
  483. var t = LocalTime(ThisTimeValue(thisObject));
  484. var m = TypeConverter.ToNumber(arguments.At(0));
  485. var s = arguments.Length <= 1 ? SecFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  486. var milli = arguments.Length <= 2 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
  487. if (t.IsNaN)
  488. {
  489. return JsNumber.DoubleNaN;
  490. }
  491. var date = MakeDate(Day(t), MakeTime(HourFromTime(t), m, s, milli));
  492. var u = Utc(date).TimeClip();
  493. ((JsDate) thisObject)._dateValue = u;
  494. return u.ToJsValue();
  495. }
  496. /// <summary>
  497. /// https://tc39.es/ecma262/#sec-date.prototype.setutcminutes
  498. /// </summary>
  499. private JsValue SetUTCMinutes(JsValue thisObject, JsValue[] arguments)
  500. {
  501. var t = ThisTimeValue(thisObject);
  502. var m = TypeConverter.ToNumber(arguments.At(0));
  503. var s = arguments.Length <= 1 ? SecFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  504. var milli = arguments.Length <= 2 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
  505. if (t.IsNaN)
  506. {
  507. return JsNumber.DoubleNaN;
  508. }
  509. var date = MakeDate(Day(t), MakeTime(HourFromTime(t), m, s, milli));
  510. var u = date.TimeClip();
  511. ((JsDate) thisObject)._dateValue = u;
  512. return u.ToJsValue();
  513. }
  514. /// <summary>
  515. /// https://tc39.es/ecma262/#sec-date.prototype.sethours
  516. /// </summary>
  517. private JsValue SetHours(JsValue thisObject, JsValue[] arguments)
  518. {
  519. var t = LocalTime(ThisTimeValue(thisObject));
  520. var h = TypeConverter.ToNumber(arguments.At(0));
  521. var m = arguments.Length <= 1 ? MinFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  522. var s = arguments.Length <= 2 ? SecFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
  523. var milli = arguments.Length <= 3 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(3));
  524. if (t.IsNaN)
  525. {
  526. return JsNumber.DoubleNaN;
  527. }
  528. var date = MakeDate(Day(t), MakeTime(h, m, s, milli));
  529. var u = Utc(date).TimeClip();
  530. ((JsDate) thisObject)._dateValue = u;
  531. return u.ToJsValue();
  532. }
  533. /// <summary>
  534. /// https://tc39.es/ecma262/#sec-date.prototype.setutchours
  535. /// </summary>
  536. private JsValue SetUTCHours(JsValue thisObject, JsValue[] arguments)
  537. {
  538. var t = ThisTimeValue(thisObject);
  539. var h = TypeConverter.ToNumber(arguments.At(0));
  540. var m = arguments.Length <= 1 ? MinFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  541. var s = arguments.Length <= 2 ? SecFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
  542. var milli = arguments.Length <= 3 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(3));
  543. if (t.IsNaN)
  544. {
  545. return JsNumber.DoubleNaN;
  546. }
  547. var newDate = MakeDate(Day(t), MakeTime(h, m, s, milli));
  548. var v = newDate.TimeClip();
  549. ((JsDate) thisObject)._dateValue = v;
  550. return v.ToJsValue();
  551. }
  552. /// <summary>
  553. /// https://tc39.es/ecma262/#sec-date.prototype.setdate
  554. /// </summary>
  555. private JsValue SetDate(JsValue thisObject, JsValue[] arguments)
  556. {
  557. var t = LocalTime(ThisTimeValue(thisObject));
  558. var dt = TypeConverter.ToNumber(arguments.At(0));
  559. if (t.IsNaN)
  560. {
  561. return JsNumber.DoubleNaN;
  562. }
  563. var (year, month, __) = YearMonthDayFromTime(t);
  564. var newDate = MakeDate(MakeDay(year, month, dt), TimeWithinDay(t));
  565. var u = Utc(newDate).TimeClip();
  566. ((JsDate) thisObject)._dateValue = u;
  567. return u.ToJsValue();
  568. }
  569. /// <summary>
  570. /// https://tc39.es/ecma262/#sec-date.prototype.setutcdate
  571. /// </summary>
  572. private JsValue SetUTCDate(JsValue thisObject, JsValue[] arguments)
  573. {
  574. var t = ThisTimeValue(thisObject);
  575. var dt = TypeConverter.ToNumber(arguments.At(0));
  576. if (t.IsNaN)
  577. {
  578. return JsNumber.DoubleNaN;
  579. }
  580. var newDate = MakeDate(MakeDay(YearFromTime(t), MonthFromTime(t), dt), TimeWithinDay(t));
  581. var u = newDate.TimeClip();
  582. ((JsDate) thisObject)._dateValue = u;
  583. return u.ToJsValue();
  584. }
  585. /// <summary>
  586. /// https://tc39.es/ecma262/#sec-date.prototype.setmonth
  587. /// </summary>
  588. private JsValue SetMonth(JsValue thisObject, JsValue[] arguments)
  589. {
  590. var t = LocalTime(ThisTimeValue(thisObject));
  591. var m = TypeConverter.ToNumber(arguments.At(0));
  592. var dt = arguments.Length <= 1 ? DateFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  593. if (t.IsNaN)
  594. {
  595. return JsNumber.DoubleNaN;
  596. }
  597. var newDate = MakeDate(MakeDay(YearFromTime(t), m, dt), TimeWithinDay(t));
  598. var u = Utc(newDate).TimeClip();
  599. ((JsDate) thisObject)._dateValue = u;
  600. return u.ToJsValue();
  601. }
  602. /// <summary>
  603. /// https://tc39.es/ecma262/#sec-date.prototype.setutcmonth
  604. /// </summary>
  605. private JsValue SetUTCMonth(JsValue thisObject, JsValue[] arguments)
  606. {
  607. var t = ThisTimeValue(thisObject);
  608. var m = TypeConverter.ToNumber(arguments.At(0));
  609. var dt = arguments.Length <= 1 ? DateFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  610. if (t.IsNaN)
  611. {
  612. return JsNumber.DoubleNaN;
  613. }
  614. var newDate = MakeDate(MakeDay(YearFromTime(t), m, dt), TimeWithinDay(t));
  615. var u = newDate.TimeClip();
  616. ((JsDate) thisObject)._dateValue = u;
  617. return u.ToJsValue();
  618. }
  619. /// <summary>
  620. /// https://tc39.es/ecma262/#sec-date.prototype.setfullyear
  621. /// </summary>
  622. private JsValue SetFullYear(JsValue thisObject, JsValue[] arguments)
  623. {
  624. var thisTime = ThisTimeValue(thisObject);
  625. var t = thisTime.IsNaN ? 0 : LocalTime(thisTime);
  626. var y = TypeConverter.ToNumber(arguments.At(0));
  627. var m = arguments.Length <= 1 ? MonthFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  628. var dt = arguments.Length <= 2 ? DateFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
  629. var newDate = MakeDate(MakeDay(y, m, dt), TimeWithinDay(t));
  630. var u = Utc(newDate).TimeClip();
  631. ((JsDate) thisObject)._dateValue = u;
  632. return u.ToJsValue();
  633. }
  634. /// <summary>
  635. /// https://tc39.es/ecma262/#sec-date.prototype.setyear
  636. /// </summary>
  637. private JsValue SetYear(JsValue thisObject, JsValue[] arguments)
  638. {
  639. var thisTime = ThisTimeValue(thisObject);
  640. var t = thisTime.IsNaN ? 0 : LocalTime(thisTime);
  641. var y = TypeConverter.ToNumber(arguments.At(0));
  642. if (double.IsNaN(y))
  643. {
  644. ((JsDate) thisObject)._dateValue = double.NaN;
  645. return JsNumber.DoubleNaN;
  646. }
  647. var fy = TypeConverter.ToInteger(y);
  648. if (y >= 0 && y <= 99)
  649. {
  650. fy += 1900;
  651. }
  652. var newDate = MakeDay(fy, MonthFromTime(t), DateFromTime(t));
  653. var u = Utc(MakeDate(newDate, TimeWithinDay(t)));
  654. ((JsDate) thisObject)._dateValue = u.TimeClip();
  655. return u.ToJsValue();
  656. }
  657. /// <summary>
  658. /// https://tc39.es/ecma262/#sec-date.prototype.setutcfullyear
  659. /// </summary>
  660. private JsValue SetUTCFullYear(JsValue thisObject, JsValue[] arguments)
  661. {
  662. var thisTime = ThisTimeValue(thisObject);
  663. var t = thisTime.IsNaN ? 0 : thisTime;
  664. var y = TypeConverter.ToNumber(arguments.At(0));
  665. var m = arguments.Length <= 1 ? MonthFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
  666. var dt = arguments.Length <= 2 ? DateFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
  667. var newDate = MakeDate(MakeDay(y, m, dt), TimeWithinDay(t));
  668. var u = newDate.TimeClip();
  669. ((JsDate) thisObject)._dateValue = u;
  670. return u.ToJsValue();
  671. }
  672. /// <summary>
  673. /// https://tc39.es/ecma262/#sec-date.prototype.toutcstring
  674. /// </summary>
  675. private JsValue ToUtcString(JsValue thisObject, JsValue[] arguments)
  676. {
  677. var tv = ThisTimeValue(thisObject);
  678. if (!IsFinite(tv))
  679. {
  680. return "Invalid Date";
  681. }
  682. var weekday = _dayNames[WeekDay(tv)];
  683. var month = _monthNames[MonthFromTime(tv)];
  684. var day = DateFromTime(tv).ToString("00", CultureInfo.InvariantCulture);
  685. var yv = YearFromTime(tv);
  686. var paddedYear = yv.ToString("0000", CultureInfo.InvariantCulture);
  687. return $"{weekday}, {day} {month} {paddedYear} {TimeString(tv)}";
  688. }
  689. /// <summary>
  690. /// https://tc39.es/ecma262/#sec-date.prototype.toisostring
  691. /// </summary>
  692. private JsValue ToISOString(JsValue thisObject, JsValue[] arguments)
  693. {
  694. var thisTime = ThisTimeValue(thisObject);
  695. var t = thisTime;
  696. if (t.IsNaN)
  697. {
  698. ExceptionHelper.ThrowRangeError(_realm);
  699. }
  700. if (((JsDate) thisObject).DateTimeRangeValid)
  701. {
  702. // shortcut
  703. var dt = thisTime.ToDateTime();
  704. return $"{dt.Year:0000}-{dt.Month:00}-{dt.Day:00}T{dt.Hour:00}:{dt.Minute:00}:{dt.Second:00}.{dt.Millisecond:000}Z";
  705. }
  706. var h = HourFromTime(t);
  707. var m = MinFromTime(t);
  708. var s = SecFromTime(t);
  709. var ms = MsFromTime(t);
  710. if (h < 0) { h += HoursPerDay; }
  711. if (m < 0) { m += MinutesPerHour; }
  712. if (s < 0) { s += SecondsPerMinute; }
  713. if (ms < 0) { ms += MsPerSecond; }
  714. var (year, month, day) = YearMonthDayFromTime(t);
  715. month++;
  716. var formatted = $"{year:0000}-{month:00}-{day:00}T{h:00}:{m:00}:{s:00}.{ms:000}Z";
  717. if (year > 9999)
  718. {
  719. formatted = "+" + formatted;
  720. }
  721. return formatted;
  722. }
  723. private JsValue ToJson(JsValue thisObject, JsValue[] arguments)
  724. {
  725. var o = TypeConverter.ToObject(_realm, thisObject);
  726. var tv = TypeConverter.ToPrimitive(o, Types.Number);
  727. if (tv.IsNumber() && !IsFinite(((JsNumber) tv)._value))
  728. {
  729. return Null;
  730. }
  731. return Invoke(o, "toISOString", Arguments.Empty);
  732. }
  733. private const int HoursPerDay = 24;
  734. private const int MinutesPerHour = 60;
  735. private const int SecondsPerMinute = 60;
  736. private const int MsPerSecond = 1000;
  737. private const int MsPerMinute = 60000;
  738. private const int MsPerHour = 3600000;
  739. private const long MsPerDay = 86400000;
  740. /// <summary>
  741. /// https://tc39.es/ecma262/#eqn-Day
  742. /// </summary>
  743. private static int Day(DatePresentation t)
  744. {
  745. return (int) System.Math.Floor((double) t.Value / MsPerDay);
  746. }
  747. /// <summary>
  748. /// https://tc39.es/ecma262/#eqn-Day
  749. /// </summary>
  750. private static long TimeWithinDay(DatePresentation t)
  751. {
  752. var result = t.Value % MsPerDay;
  753. if (result < 0)
  754. {
  755. result += MsPerDay;
  756. }
  757. return result;
  758. }
  759. /// <summary>
  760. /// The number of days in a year
  761. /// </summary>
  762. private static int DaysInYear(double y)
  763. {
  764. if (y%4 != 0)
  765. {
  766. return 365;
  767. }
  768. if (y%4 == 0 && y%100 != 0)
  769. {
  770. return 366;
  771. }
  772. if (y%100 == 0 && y%400 != 0)
  773. {
  774. return 365;
  775. }
  776. if (y%400 == 0)
  777. {
  778. return 366;
  779. }
  780. return 365;
  781. }
  782. /// <summary>
  783. /// The day number of the first day of the year.
  784. /// </summary>
  785. private static int DayFromYear(DatePresentation y)
  786. {
  787. return (int) (365*(y.Value - 1970)
  788. + System.Math.Floor((y.Value - 1969)/4d)
  789. - System.Math.Floor((y.Value - 1901)/100d)
  790. + System.Math.Floor((y.Value - 1601)/400d));
  791. }
  792. /// <summary>
  793. /// The time value of the start of the year
  794. /// </summary>
  795. private static long TimeFromYear(DatePresentation y)
  796. {
  797. return MsPerDay*DayFromYear(y);
  798. }
  799. /// <summary>
  800. /// The year of a time value.
  801. /// </summary>
  802. private static int YearFromTime(DatePresentation t)
  803. {
  804. var (year, _, _) = YearMonthDayFromTime(t);
  805. return year;
  806. }
  807. /// <summary>
  808. /// <value>true</value> if the time is within a leap year, <value>false</value> otherwise
  809. /// </summary>
  810. private static int InLeapYear(DatePresentation t)
  811. {
  812. var daysInYear = DaysInYear(YearFromTime(t));
  813. if (daysInYear == 365)
  814. {
  815. return 0;
  816. }
  817. if (daysInYear == 366)
  818. {
  819. return 1;
  820. }
  821. ExceptionHelper.ThrowArgumentException();
  822. return 0;
  823. }
  824. /// <summary>
  825. /// The month number of a time value.
  826. /// </summary>
  827. private static int MonthFromTime(DatePresentation t)
  828. {
  829. var dayWithinYear = DayWithinYear(t);
  830. var inLeapYear = InLeapYear(t);
  831. if (dayWithinYear < 31)
  832. {
  833. return 0;
  834. }
  835. if (dayWithinYear < 59 + inLeapYear)
  836. {
  837. return 1;
  838. }
  839. if (dayWithinYear < 90 + inLeapYear)
  840. {
  841. return 2;
  842. }
  843. if (dayWithinYear < 120 + inLeapYear)
  844. {
  845. return 3;
  846. }
  847. if (dayWithinYear < 151 + inLeapYear)
  848. {
  849. return 4;
  850. }
  851. if (dayWithinYear < 181 + inLeapYear)
  852. {
  853. return 5;
  854. }
  855. if (dayWithinYear < 212 + inLeapYear)
  856. {
  857. return 6;
  858. }
  859. if (dayWithinYear < 243 + inLeapYear)
  860. {
  861. return 7;
  862. }
  863. if (dayWithinYear < 273 + inLeapYear)
  864. {
  865. return 8;
  866. }
  867. if (dayWithinYear < 304 + inLeapYear)
  868. {
  869. return 9;
  870. }
  871. if (dayWithinYear < 334 + inLeapYear)
  872. {
  873. return 10;
  874. }
  875. if (dayWithinYear < 365 + inLeapYear)
  876. {
  877. return 11;
  878. }
  879. ExceptionHelper.ThrowInvalidOperationException();
  880. return 0;
  881. }
  882. private static int DayWithinYear(DatePresentation t)
  883. {
  884. return Day(t) - DayFromYear(YearFromTime(t));
  885. }
  886. private static int DateFromTime(DatePresentation t)
  887. {
  888. var monthFromTime = MonthFromTime(t);
  889. var dayWithinYear = DayWithinYear(t);
  890. if (monthFromTime == 0)
  891. {
  892. return dayWithinYear + 1;
  893. }
  894. if (monthFromTime== 1)
  895. {
  896. return dayWithinYear - 30;
  897. }
  898. if (monthFromTime == 2)
  899. {
  900. return dayWithinYear - 58 - InLeapYear(t);
  901. }
  902. if (monthFromTime == 3)
  903. {
  904. return dayWithinYear - 89 - InLeapYear(t);
  905. }
  906. if (monthFromTime == 4)
  907. {
  908. return dayWithinYear - 119 - InLeapYear(t);
  909. }
  910. if (monthFromTime == 5)
  911. {
  912. return dayWithinYear - 150 - InLeapYear(t);
  913. }
  914. if (monthFromTime == 6)
  915. {
  916. return dayWithinYear - 180 - InLeapYear(t);
  917. }
  918. if (monthFromTime == 7)
  919. {
  920. return dayWithinYear - 211 - InLeapYear(t);
  921. }
  922. if (monthFromTime == 8)
  923. {
  924. return dayWithinYear - 242 - InLeapYear(t);
  925. }
  926. if (monthFromTime == 9)
  927. {
  928. return dayWithinYear - 272 - InLeapYear(t);
  929. }
  930. if (monthFromTime == 10)
  931. {
  932. return dayWithinYear - 303 - InLeapYear(t);
  933. }
  934. if (monthFromTime == 11)
  935. {
  936. return dayWithinYear - 333 - InLeapYear(t);
  937. }
  938. ExceptionHelper.ThrowInvalidOperationException();
  939. return 0;
  940. }
  941. /// <summary>
  942. /// https://tc39.es/ecma262/#sec-week-day
  943. /// </summary>
  944. private static int WeekDay(DatePresentation t)
  945. {
  946. var result = (Day(t) + 4) % 7;
  947. return result >= 0 ? result : result + 7;
  948. }
  949. private DateTime ToLocalTime(DatePresentation t)
  950. {
  951. var utcOffset = _timeSystem.GetUtcOffset(t.Value).TotalMilliseconds;
  952. return (t + utcOffset).ToDateTime();
  953. }
  954. /// <summary>
  955. /// https://tc39.es/ecma262/#sec-localtime
  956. /// </summary>
  957. private DatePresentation LocalTime(DatePresentation t)
  958. {
  959. if (t.IsNaN)
  960. {
  961. return DatePresentation.NaN;
  962. }
  963. var offset = _timeSystem.GetUtcOffset(t.Value).TotalMilliseconds;
  964. return t + offset;
  965. }
  966. internal DatePresentation Utc(DatePresentation t)
  967. {
  968. var offset = _timeSystem.GetUtcOffset(t.Value).TotalMilliseconds;
  969. return t - offset;
  970. }
  971. private static int HourFromTime(DatePresentation t)
  972. {
  973. var hours = System.Math.Floor((double) t.Value / MsPerHour) % HoursPerDay;
  974. if (hours < 0)
  975. {
  976. hours += HoursPerDay;
  977. }
  978. return (int) hours;
  979. }
  980. private static int MinFromTime(DatePresentation t)
  981. {
  982. var minutes = System.Math.Floor((double) t.Value / MsPerMinute) % MinutesPerHour;
  983. if (minutes < 0)
  984. {
  985. minutes += MinutesPerHour;
  986. }
  987. return (int) minutes;
  988. }
  989. private static int SecFromTime(DatePresentation t)
  990. {
  991. var seconds = System.Math.Floor((double) t.Value / MsPerSecond) % SecondsPerMinute;
  992. if (seconds < 0)
  993. {
  994. seconds += SecondsPerMinute;
  995. }
  996. return (int) seconds;
  997. }
  998. private static int MsFromTime(DatePresentation t)
  999. {
  1000. var milli = t.Value % MsPerSecond;
  1001. if (milli < 0)
  1002. {
  1003. milli += MsPerSecond;
  1004. }
  1005. return (int) milli;
  1006. }
  1007. internal static double MakeTime(double hour, double min, double sec, double ms)
  1008. {
  1009. if (!AreFinite(hour, min, sec, ms))
  1010. {
  1011. return double.NaN;
  1012. }
  1013. var h = TypeConverter.ToInteger(hour);
  1014. var m = TypeConverter.ToInteger(min);
  1015. var s = TypeConverter.ToInteger(sec);
  1016. var milli = TypeConverter.ToInteger(ms);
  1017. var t = h*MsPerHour + m*MsPerMinute + s*MsPerSecond + milli;
  1018. return t;
  1019. }
  1020. private static readonly int[] _dayFromMonth = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  1021. private static readonly int[] _dayFromMonthLeapYear = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
  1022. internal static double MakeDay(double year, double month, double date)
  1023. {
  1024. if (year < MinYear || year > MaxYear || month < MinMonth || month > MaxMonth || !AreFinite(year, month, date))
  1025. {
  1026. return double.NaN;
  1027. }
  1028. var y = (long) TypeConverter.ToInteger(year);
  1029. var m = (long) TypeConverter.ToInteger(month);
  1030. var dt = (long) TypeConverter.ToInteger(date);
  1031. y += m / 12;
  1032. m %= 12;
  1033. if (m < 0)
  1034. {
  1035. m += 12;
  1036. y -= 1;
  1037. }
  1038. // kYearDelta is an arbitrary number such that:
  1039. // a) kYearDelta = -1 (mod 400)
  1040. // b) year + kYearDelta > 0 for years in the range defined by
  1041. // ECMA 262 - 15.9.1.1, i.e. upto 100,000,000 days on either side of
  1042. // Jan 1 1970. This is required so that we don't run into integer
  1043. // division of negative numbers.
  1044. // c) there shouldn't be an overflow for 32-bit integers in the following
  1045. // operations.
  1046. const int kYearDelta = 399999;
  1047. const long kBaseDay =
  1048. 365 * (1970 + kYearDelta) + (1970 + kYearDelta) / 4 -
  1049. (1970 + kYearDelta) / 100 + (1970 + kYearDelta) / 400;
  1050. var dayFromYear = 365 * (y + kYearDelta) + (y + kYearDelta) / 4 -
  1051. (y + kYearDelta) / 100 + (y + kYearDelta) / 400 - kBaseDay;
  1052. if (y % 4 != 0 || (y % 100 == 0 && y % 400 != 0))
  1053. {
  1054. dayFromYear += _dayFromMonth[m];
  1055. }
  1056. else
  1057. {
  1058. dayFromYear += _dayFromMonthLeapYear[m];
  1059. }
  1060. return dayFromYear + dt - 1;
  1061. }
  1062. internal static DatePresentation MakeDate(double day, double time)
  1063. {
  1064. if (!AreFinite(day, time))
  1065. {
  1066. return DatePresentation.NaN;
  1067. }
  1068. return new DatePresentation((long) (day * MsPerDay + time), DateFlags.None);
  1069. }
  1070. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1071. private static bool IsFinite(double value) => !double.IsNaN(value) && !double.IsInfinity(value);
  1072. private static bool AreFinite(double value1, double value2) => IsFinite(value1) && IsFinite(value2);
  1073. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1074. private static bool IsFinite(DatePresentation value) => value.IsFinite;
  1075. private static bool AreFinite(double value1, double value2, double value3)
  1076. => IsFinite(value1) && IsFinite(value2) && IsFinite(value3);
  1077. private static bool AreFinite(double value1, double value2, double value3, double value4)
  1078. => IsFinite(value1) && IsFinite(value2) && IsFinite(value3) && IsFinite(value4);
  1079. [StructLayout(LayoutKind.Auto)]
  1080. private readonly record struct Date(int Year, int Month, int Day);
  1081. private static readonly int[] kDaysInMonths = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  1082. private static Date YearMonthDayFromTime(DatePresentation t) => YearMonthDayFromDays((long) System.Math.Floor(t.Value / 1000 / 60 / 60 / 24d));
  1083. private static Date YearMonthDayFromDays(long days)
  1084. {
  1085. const int kDaysIn4Years = 4 * 365 + 1;
  1086. const int kDaysIn100Years = 25 * kDaysIn4Years - 1;
  1087. const int kDaysIn400Years = 4 * kDaysIn100Years + 1;
  1088. const int kDays1970to2000 = 30 * 365 + 7;
  1089. const int kDaysOffset = 1000 * kDaysIn400Years + 5 * kDaysIn400Years - kDays1970to2000;
  1090. const int kYearsOffset = 400000;
  1091. days += kDaysOffset;
  1092. var year = 400 * (days / kDaysIn400Years) - kYearsOffset;
  1093. days %= kDaysIn400Years;
  1094. days--;
  1095. var yd1 = days / kDaysIn100Years;
  1096. days %= kDaysIn100Years;
  1097. year += 100 * yd1;
  1098. days++;
  1099. var yd2 = days / kDaysIn4Years;
  1100. days %= kDaysIn4Years;
  1101. year += 4 * yd2;
  1102. days--;
  1103. var yd3 = days / 365;
  1104. days %= 365;
  1105. year += yd3;
  1106. var is_leap = (yd1 == 0 || yd2 != 0) && yd3 == 0;
  1107. days += is_leap ? 1 : 0;
  1108. var month = 0;
  1109. var day = 0;
  1110. // Check if the date is after February.
  1111. if (days >= 31 + 28 + (is_leap ? 1 : 0))
  1112. {
  1113. days -= 31 + 28 + (is_leap ? 1 : 0);
  1114. // Find the date starting from March.
  1115. for (int i = 2; i < 12; i++)
  1116. {
  1117. if (days < kDaysInMonths[i])
  1118. {
  1119. month = i;
  1120. day = (int) (days + 1);
  1121. break;
  1122. }
  1123. days -= kDaysInMonths[i];
  1124. }
  1125. }
  1126. else
  1127. {
  1128. // Check January and February.
  1129. if (days < 31)
  1130. {
  1131. month = 0;
  1132. day = (int) (days + 1);
  1133. }
  1134. else
  1135. {
  1136. month = 1;
  1137. day = (int) (days - 31 + 1);
  1138. }
  1139. }
  1140. return new Date((int) year, month, day);
  1141. }
  1142. private static readonly string[] _dayNames =
  1143. {
  1144. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  1145. };
  1146. private static readonly string[] _monthNames =
  1147. {
  1148. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  1149. };
  1150. /// <summary>
  1151. /// https://tc39.es/ecma262/#sec-datestring
  1152. /// </summary>
  1153. private static string DateString(DatePresentation tv)
  1154. {
  1155. var weekday = _dayNames[WeekDay(tv)];
  1156. var month = _monthNames[MonthFromTime(tv)];
  1157. var dateFromTime = DateFromTime(tv);
  1158. var day = System.Math.Max(1, dateFromTime).ToString("00", CultureInfo.InvariantCulture);
  1159. var yv = YearFromTime(tv);
  1160. var yearSign = yv < 0 ? "-" : "";
  1161. var year = System.Math.Abs(yv);
  1162. var paddedYear = year.ToString("0000", CultureInfo.InvariantCulture);
  1163. return weekday + " " + month + " " + day + " " + yearSign + paddedYear;
  1164. }
  1165. /// <summary>
  1166. /// https://tc39.es/ecma262/#sec-timestring
  1167. /// </summary>
  1168. private static string TimeString(DatePresentation t)
  1169. {
  1170. var hour = HourFromTime(t).ToString("00", CultureInfo.InvariantCulture);
  1171. var minute = MinFromTime(t).ToString("00", CultureInfo.InvariantCulture);
  1172. var second = SecFromTime(t).ToString("00", CultureInfo.InvariantCulture);
  1173. return hour + ":" + minute + ":" + second + " GMT";
  1174. }
  1175. /// <summary>
  1176. /// https://tc39.es/ecma262/#sec-timezoneestring
  1177. /// </summary>
  1178. private string TimeZoneString(DatePresentation tv)
  1179. {
  1180. var offset = _timeSystem.GetUtcOffset(tv.Value).TotalMilliseconds;
  1181. string offsetSign;
  1182. double absOffset;
  1183. if (offset >= 0)
  1184. {
  1185. offsetSign = "+";
  1186. absOffset = offset;
  1187. }
  1188. else
  1189. {
  1190. offsetSign = "-";
  1191. absOffset = -1 * offset;
  1192. }
  1193. var offsetMin = MinFromTime(absOffset).ToString("00", CultureInfo.InvariantCulture);
  1194. var offsetHour = HourFromTime(absOffset).ToString("00", CultureInfo.InvariantCulture);
  1195. var tzName = " (" + _timeSystem.DefaultTimeZone.StandardName + ")";
  1196. return offsetSign + offsetHour + offsetMin + tzName;
  1197. }
  1198. public override string ToString()
  1199. {
  1200. return "Date.prototype";
  1201. }
  1202. }
  1203. }