Date.cpp 15 KB

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