Date.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <AK/Time.h>
  8. #include <LibCore/DateTime.h>
  9. #include <LibJS/Runtime/AbstractOperations.h>
  10. #include <LibJS/Runtime/Date.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <time.h>
  13. namespace JS {
  14. Date* Date::create(GlobalObject& global_object, Core::DateTime datetime, i16 milliseconds, bool is_invalid)
  15. {
  16. return global_object.heap().allocate<Date>(global_object, datetime, milliseconds, is_invalid, *global_object.date_prototype());
  17. }
  18. Date::Date(Core::DateTime datetime, i16 milliseconds, bool is_invalid, Object& prototype)
  19. : Object(prototype)
  20. , m_datetime(datetime)
  21. , m_milliseconds(milliseconds)
  22. , m_is_invalid(is_invalid)
  23. {
  24. }
  25. Date::~Date()
  26. {
  27. }
  28. tm Date::to_utc_tm() const
  29. {
  30. time_t timestamp = m_datetime.timestamp();
  31. struct tm tm;
  32. gmtime_r(&timestamp, &tm);
  33. return tm;
  34. }
  35. int Date::utc_date() const
  36. {
  37. return to_utc_tm().tm_mday;
  38. }
  39. int Date::utc_day() const
  40. {
  41. return to_utc_tm().tm_wday;
  42. }
  43. int Date::utc_full_year() const
  44. {
  45. return to_utc_tm().tm_year + 1900;
  46. }
  47. int Date::utc_hours() const
  48. {
  49. return to_utc_tm().tm_hour;
  50. }
  51. int Date::utc_minutes() const
  52. {
  53. return to_utc_tm().tm_min;
  54. }
  55. int Date::utc_month() const
  56. {
  57. return to_utc_tm().tm_mon;
  58. }
  59. int Date::utc_seconds() const
  60. {
  61. return to_utc_tm().tm_sec;
  62. }
  63. String Date::gmt_date_string() const
  64. {
  65. // Mon, 18 Dec 1995 17:28:35 GMT
  66. // FIXME: Note that we're totally cheating with the timezone part here..
  67. return datetime().to_string("%a, %e %b %Y %T GMT");
  68. }
  69. String Date::iso_date_string() const
  70. {
  71. auto tm = to_utc_tm();
  72. int year = tm.tm_year + 1900;
  73. int month = tm.tm_mon + 1;
  74. StringBuilder builder;
  75. if (year < 0)
  76. builder.appendff("-{:06}", -year);
  77. else if (year > 9999)
  78. builder.appendff("+{:06}", year);
  79. else
  80. builder.appendff("{:04}", year);
  81. builder.append('-');
  82. builder.appendff("{:02}", month);
  83. builder.append('-');
  84. builder.appendff("{:02}", tm.tm_mday);
  85. builder.append('T');
  86. builder.appendff("{:02}", tm.tm_hour);
  87. builder.append(':');
  88. builder.appendff("{:02}", tm.tm_min);
  89. builder.append(':');
  90. builder.appendff("{:02}", tm.tm_sec);
  91. builder.append('.');
  92. builder.appendff("{:03}", m_milliseconds);
  93. builder.append('Z');
  94. return builder.build();
  95. }
  96. // https://tc39.es/ecma262/#eqn-HoursPerDay
  97. static constexpr double HOURS_PER_DAY = 24;
  98. // https://tc39.es/ecma262/#eqn-MinutesPerHour
  99. static constexpr double MINUTES_PER_HOUR = 60;
  100. // https://tc39.es/ecma262/#eqn-SecondsPerMinute
  101. static constexpr double SECONDS_PER_MINUTE = 60;
  102. // https://tc39.es/ecma262/#eqn-msPerSecond
  103. static constexpr double MS_PER_SECOND = 1000;
  104. // https://tc39.es/ecma262/#eqn-msPerMinute
  105. static constexpr double MS_PER_MINUTE = 60000;
  106. // https://tc39.es/ecma262/#eqn-msPerHour
  107. static constexpr double MS_PER_HOUR = 3600000;
  108. // https://tc39.es/ecma262/#eqn-msPerDay
  109. static constexpr double MS_PER_DAY = 86400000;
  110. // DayWithinYear(t), https://tc39.es/ecma262/#eqn-DayWithinYear
  111. u16 day_within_year(double t)
  112. {
  113. // Day(t) - DayFromYear(YearFromTime(t))
  114. return static_cast<u16>(day(t) - day_from_year(year_from_time(t)));
  115. }
  116. // DateFromTime(t), https://tc39.es/ecma262/#sec-date-number
  117. u8 date_from_time(double t)
  118. {
  119. switch (month_from_time(t)) {
  120. // DayWithinYear(t) + 1𝔽 if MonthFromTime(t) = +0𝔽
  121. case 0:
  122. return day_within_year(t) + 1;
  123. // DayWithinYear(t) - 30𝔽 if MonthFromTime(t) = 1𝔽
  124. case 1:
  125. return day_within_year(t) - 30;
  126. // DayWithinYear(t) - 58𝔽 - InLeapYear(t) if MonthFromTime(t) = 2𝔽
  127. case 2:
  128. return day_within_year(t) - 58 - in_leap_year(t);
  129. // DayWithinYear(t) - 89𝔽 - InLeapYear(t) if MonthFromTime(t) = 3𝔽
  130. case 3:
  131. return day_within_year(t) - 89 - in_leap_year(t);
  132. // DayWithinYear(t) - 119𝔽 - InLeapYear(t) if MonthFromTime(t) = 4𝔽
  133. case 4:
  134. return day_within_year(t) - 119 - in_leap_year(t);
  135. // DayWithinYear(t) - 150𝔽 - InLeapYear(t) if MonthFromTime(t) = 5𝔽
  136. case 5:
  137. return day_within_year(t) - 150 - in_leap_year(t);
  138. // DayWithinYear(t) - 180𝔽 - InLeapYear(t) if MonthFromTime(t) = 6𝔽
  139. case 6:
  140. return day_within_year(t) - 180 - in_leap_year(t);
  141. // DayWithinYear(t) - 211𝔽 - InLeapYear(t) if MonthFromTime(t) = 7𝔽
  142. case 7:
  143. return day_within_year(t) - 211 - in_leap_year(t);
  144. // DayWithinYear(t) - 242𝔽 - InLeapYear(t) if MonthFromTime(t) = 8𝔽
  145. case 8:
  146. return day_within_year(t) - 242 - in_leap_year(t);
  147. // DayWithinYear(t) - 272𝔽 - InLeapYear(t) if MonthFromTime(t) = 9𝔽
  148. case 9:
  149. return day_within_year(t) - 272 - in_leap_year(t);
  150. // DayWithinYear(t) - 303𝔽 - InLeapYear(t) if MonthFromTime(t) = 10𝔽
  151. case 10:
  152. return day_within_year(t) - 303 - in_leap_year(t);
  153. // DayWithinYear(t) - 333𝔽 - InLeapYear(t) if MonthFromTime(t) = 11𝔽
  154. case 11:
  155. return day_within_year(t) - 333 - in_leap_year(t);
  156. default:
  157. VERIFY_NOT_REACHED();
  158. }
  159. }
  160. // DaysInYear(y), https://tc39.es/ecma262/#eqn-DaysInYear
  161. u16 days_in_year(i32 y)
  162. {
  163. // 365𝔽 if (ℝ(y) modulo 4) ≠ 0
  164. if (y % 4 != 0)
  165. return 365;
  166. // 366𝔽 if (ℝ(y) modulo 4) = 0 and (ℝ(y) modulo 100) ≠ 0
  167. if (y % 4 == 0 && y % 100 != 0)
  168. return 366;
  169. // 365𝔽 if (ℝ(y) modulo 100) = 0 and (ℝ(y) modulo 400) ≠ 0
  170. if (y % 100 == 0 && y % 400 != 0)
  171. return 365;
  172. // 366𝔽 if (ℝ(y) modulo 400) = 0
  173. if (y % 400 == 0)
  174. return 366;
  175. VERIFY_NOT_REACHED();
  176. }
  177. // DayFromYear(y), https://tc39.es/ecma262/#eqn-DaysFromYear
  178. double day_from_year(i32 y)
  179. {
  180. // 𝔽(365 × (ℝ(y) - 1970) + floor((ℝ(y) - 1969) / 4) - floor((ℝ(y) - 1901) / 100) + floor((ℝ(y) - 1601) / 400))
  181. return 365 * (y - 1970) + floor((y - 1969) / 4.0) - floor((y - 1901) / 100.0) + floor((y - 1601) / 400.0);
  182. }
  183. // TimeFromYear(y), https://tc39.es/ecma262/#eqn-TimeFromYear
  184. double time_from_year(i32 y)
  185. {
  186. // msPerDay × DayFromYear(y)
  187. return MS_PER_DAY * day_from_year(y);
  188. }
  189. // YearFromTime(t), https://tc39.es/ecma262/#eqn-YearFromTime
  190. i32 year_from_time(double t)
  191. {
  192. // the largest integral Number y (closest to +∞) such that TimeFromYear(y) ≤ t
  193. // Approximation using average number of milliseconds per year. We might have to adjust this guess afterwards.
  194. auto year = static_cast<i32>(t / (365.2425 * MS_PER_DAY) + 1970);
  195. auto year_t = time_from_year(year);
  196. if (year_t > t)
  197. year--;
  198. else if (year_t + days_in_year(year) * MS_PER_DAY <= t)
  199. year++;
  200. return year;
  201. }
  202. // InLeapYear(t), https://tc39.es/ecma262/#eqn-InLeapYear
  203. bool in_leap_year(double t)
  204. {
  205. // +0𝔽 if DaysInYear(YearFromTime(t)) = 365𝔽
  206. // 1𝔽 if DaysInYear(YearFromTime(t)) = 366𝔽
  207. return days_in_year(year_from_time(t)) == 366;
  208. }
  209. // MonthFromTime(t), https://tc39.es/ecma262/#eqn-MonthFromTime
  210. u8 month_from_time(double t)
  211. {
  212. auto in_leap_year = JS::in_leap_year(t);
  213. auto day_within_year = JS::day_within_year(t);
  214. // +0𝔽 if +0𝔽 ≤ DayWithinYear(t) < 31𝔽
  215. if (day_within_year < 31)
  216. return 0;
  217. // 1𝔽 if 31𝔽 ≤ DayWithinYear(t) < 59𝔽 + InLeapYear(t)
  218. if (31 <= day_within_year && day_within_year < 59 + in_leap_year)
  219. return 1;
  220. // 2𝔽 if 59𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 90𝔽 + InLeapYear(t)
  221. if (59 + in_leap_year <= day_within_year && day_within_year < 90 + in_leap_year)
  222. return 2;
  223. // 3𝔽 if 90𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 120𝔽 + InLeapYear(t)
  224. if (90 + in_leap_year <= day_within_year && day_within_year < 120 + in_leap_year)
  225. return 3;
  226. // 4𝔽 if 120𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 151𝔽 + InLeapYear(t)
  227. if (120 + in_leap_year <= day_within_year && day_within_year < 151 + in_leap_year)
  228. return 4;
  229. // 5𝔽 if 151𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 181𝔽 + InLeapYear(t)
  230. if (151 + in_leap_year <= day_within_year && day_within_year < 181 + in_leap_year)
  231. return 5;
  232. // 6𝔽 if 181𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 212𝔽 + InLeapYear(t)
  233. if (181 + in_leap_year <= day_within_year && day_within_year < 212 + in_leap_year)
  234. return 6;
  235. // 7𝔽 if 212𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 243𝔽 + InLeapYear(t)
  236. if (212 + in_leap_year <= day_within_year && day_within_year < 243 + in_leap_year)
  237. return 7;
  238. // 8𝔽 if 243𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 273𝔽 + InLeapYear(t)
  239. if (243 + in_leap_year <= day_within_year && day_within_year < 273 + in_leap_year)
  240. return 8;
  241. // 9𝔽 if 273𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 304𝔽 + InLeapYear(t)
  242. if (273 + in_leap_year <= day_within_year && day_within_year < 304 + in_leap_year)
  243. return 9;
  244. // 10𝔽 if 304𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 334𝔽 + InLeapYear(t)
  245. if (304 + in_leap_year <= day_within_year && day_within_year < 334 + in_leap_year)
  246. return 10;
  247. // 11𝔽 if 334𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 365𝔽 + InLeapYear(t)
  248. if (334 + in_leap_year <= day_within_year && day_within_year < 365 + in_leap_year)
  249. return 11;
  250. VERIFY_NOT_REACHED();
  251. }
  252. // HourFromTime(t), https://tc39.es/ecma262/#eqn-HourFromTime
  253. u8 hour_from_time(double t)
  254. {
  255. // 𝔽(floor(ℝ(t / msPerHour)) modulo HoursPerDay)
  256. return static_cast<u8>(modulo(floor(t / MS_PER_HOUR), HOURS_PER_DAY));
  257. }
  258. // MinFromTime(t), https://tc39.es/ecma262/#eqn-MinFromTime
  259. u8 min_from_time(double t)
  260. {
  261. // 𝔽(floor(ℝ(t / msPerMinute)) modulo MinutesPerHour)
  262. return static_cast<u8>(modulo(floor(t / MS_PER_MINUTE), MINUTES_PER_HOUR));
  263. }
  264. // SecFromTime(t), https://tc39.es/ecma262/#eqn-SecFromTime
  265. u8 sec_from_time(double t)
  266. {
  267. // 𝔽(floor(ℝ(t / msPerSecond)) modulo SecondsPerMinute)
  268. return static_cast<u8>(modulo(floor(t / MS_PER_SECOND), SECONDS_PER_MINUTE));
  269. }
  270. // msFromTime(t), https://tc39.es/ecma262/#eqn-msFromTime
  271. u16 ms_from_time(double t)
  272. {
  273. // 𝔽(ℝ(t) modulo msPerSecond)
  274. return static_cast<u16>(modulo(t, MS_PER_SECOND));
  275. }
  276. // 21.4.1.6 Week Day, https://tc39.es/ecma262/#sec-week-day
  277. u8 week_day(double t)
  278. {
  279. // 𝔽(ℝ(Day(t) + 4𝔽) modulo 7)
  280. return static_cast<u8>(modulo(day(t) + 4, 7));
  281. }
  282. // 21.4.1.11 MakeTime ( hour, min, sec, ms ), https://tc39.es/ecma262/#sec-maketime
  283. Value make_time(GlobalObject& global_object, Value hour, Value min, Value sec, Value ms)
  284. {
  285. // 1. If hour is not finite or min is not finite or sec is not finite or ms is not finite, return NaN.
  286. if (!hour.is_finite_number() || !min.is_finite_number() || !sec.is_finite_number() || !ms.is_finite_number())
  287. return js_nan();
  288. // 2. Let h be 𝔽(! ToIntegerOrInfinity(hour)).
  289. auto h = MUST(hour.to_integer_or_infinity(global_object));
  290. // 3. Let m be 𝔽(! ToIntegerOrInfinity(min)).
  291. auto m = MUST(min.to_integer_or_infinity(global_object));
  292. // 4. Let s be 𝔽(! ToIntegerOrInfinity(sec)).
  293. auto s = MUST(sec.to_integer_or_infinity(global_object));
  294. // 5. Let milli be 𝔽(! ToIntegerOrInfinity(ms)).
  295. auto milli = MUST(ms.to_integer_or_infinity(global_object));
  296. // 6. Let t be ((h * msPerHour + m * msPerMinute) + s * msPerSecond) + milli, performing the arithmetic according to IEEE 754-2019 rules (that is, as if using the ECMAScript operators * and +).
  297. // NOTE: C++ arithmetic abides by IEEE 754 rules
  298. auto t = ((h * MS_PER_HOUR + m * MS_PER_MINUTE) + s * MS_PER_SECOND) + milli;
  299. // 7. Return t.
  300. return Value(t);
  301. }
  302. // Day(t), https://tc39.es/ecma262/#eqn-Day
  303. double day(double time_value)
  304. {
  305. return floor(time_value / MS_PER_DAY);
  306. }
  307. // 21.4.1.12 MakeDay ( year, month, date ), https://tc39.es/ecma262/#sec-makeday
  308. Value make_day(GlobalObject& global_object, Value year, Value month, Value date)
  309. {
  310. // 1. If year is not finite or month is not finite or date is not finite, return NaN.
  311. if (!year.is_finite_number() || !month.is_finite_number() || !date.is_finite_number())
  312. return js_nan();
  313. // 2. Let y be 𝔽(! ToIntegerOrInfinity(year)).
  314. auto y = MUST(year.to_integer_or_infinity(global_object));
  315. // 3. Let m be 𝔽(! ToIntegerOrInfinity(month)).
  316. auto m = MUST(month.to_integer_or_infinity(global_object));
  317. // 4. Let dt be 𝔽(! ToIntegerOrInfinity(date)).
  318. auto dt = MUST(date.to_integer_or_infinity(global_object));
  319. // 5. Let ym be y + 𝔽(floor(ℝ(m) / 12)).
  320. auto ym = y + floor(m / 12);
  321. // 6. If ym is not finite, return NaN.
  322. if (!Value(ym).is_finite_number())
  323. return js_nan();
  324. // 7. Let mn be 𝔽(ℝ(m) modulo 12).
  325. auto mn = modulo(m, 12);
  326. // 8. Find a finite time value t such that YearFromTime(t) is ym and MonthFromTime(t) is mn and DateFromTime(t) is 1𝔽; but if this is not possible (because some argument is out of range), return NaN.
  327. if (!AK::is_within_range<int>(ym) || !AK::is_within_range<int>(mn + 1))
  328. return js_nan();
  329. auto t = days_since_epoch(static_cast<int>(ym), static_cast<int>(mn) + 1, 1) * MS_PER_DAY;
  330. // 9. Return Day(t) + dt - 1𝔽.
  331. return Value(day(static_cast<double>(t)) + dt - 1);
  332. }
  333. // 21.4.1.13 MakeDate ( day, time ), https://tc39.es/ecma262/#sec-makedate
  334. Value make_date(Value day, Value time)
  335. {
  336. // 1. If day is not finite or time is not finite, return NaN.
  337. if (!day.is_finite_number() || !time.is_finite_number())
  338. return js_nan();
  339. // 2. Let tv be day × msPerDay + time.
  340. auto tv = Value(day.as_double() * MS_PER_DAY + time.as_double());
  341. // 3. If tv is not finite, return NaN.
  342. if (!tv.is_finite_number())
  343. return js_nan();
  344. // 4. Return tv.
  345. return tv;
  346. }
  347. // 21.4.1.14 TimeClip ( time ), https://tc39.es/ecma262/#sec-timeclip
  348. Value time_clip(GlobalObject& global_object, Value time)
  349. {
  350. // 1. If time is not finite, return NaN.
  351. if (!time.is_finite_number())
  352. return js_nan();
  353. // 2. If abs(ℝ(time)) > 8.64 × 10^15, return NaN.
  354. if (fabs(time.as_double()) > 8.64E15)
  355. return js_nan();
  356. // 3. Return 𝔽(! ToIntegerOrInfinity(time)).
  357. return Value(MUST(time.to_integer_or_infinity(global_object)));
  358. }
  359. }