Date.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/Heap/Heap.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. // YearFromTime(t), https://tc39.es/ecma262/#eqn-YearFromTime
  183. i32 year_from_time(double t)
  184. {
  185. // the largest integral Number y (closest to +∞) such that TimeFromYear(y) ≤ t
  186. return static_cast<i32>(t / (365.0 * MS_PER_DAY) + 1970);
  187. }
  188. // InLeapYear(t), https://tc39.es/ecma262/#eqn-InLeapYear
  189. bool in_leap_year(double t)
  190. {
  191. // +0𝔽 if DaysInYear(YearFromTime(t)) = 365𝔽
  192. // 1𝔽 if DaysInYear(YearFromTime(t)) = 366𝔽
  193. return days_in_year(year_from_time(t)) == 366;
  194. }
  195. // MonthFromTime(t), https://tc39.es/ecma262/#eqn-MonthFromTime
  196. u8 month_from_time(double t)
  197. {
  198. auto in_leap_year = JS::in_leap_year(t);
  199. auto day_within_year = JS::day_within_year(t);
  200. // +0𝔽 if +0𝔽 ≤ DayWithinYear(t) < 31𝔽
  201. if (day_within_year < 31)
  202. return 0;
  203. // 1𝔽 if 31𝔽 ≤ DayWithinYear(t) < 59𝔽 + InLeapYear(t)
  204. if (31 <= day_within_year && day_within_year < 59 + in_leap_year)
  205. return 1;
  206. // 2𝔽 if 59𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 90𝔽 + InLeapYear(t)
  207. if (59 + in_leap_year <= day_within_year && day_within_year < 90 + in_leap_year)
  208. return 2;
  209. // 3𝔽 if 90𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 120𝔽 + InLeapYear(t)
  210. if (90 + in_leap_year <= day_within_year && day_within_year < 120 + in_leap_year)
  211. return 3;
  212. // 4𝔽 if 120𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 151𝔽 + InLeapYear(t)
  213. if (120 + in_leap_year <= day_within_year && day_within_year < 151 + in_leap_year)
  214. return 4;
  215. // 5𝔽 if 151𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 181𝔽 + InLeapYear(t)
  216. if (151 + in_leap_year <= day_within_year && day_within_year < 181 + in_leap_year)
  217. return 5;
  218. // 6𝔽 if 181𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 212𝔽 + InLeapYear(t)
  219. if (181 + in_leap_year <= day_within_year && day_within_year < 212 + in_leap_year)
  220. return 6;
  221. // 7𝔽 if 212𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 243𝔽 + InLeapYear(t)
  222. if (212 + in_leap_year <= day_within_year && day_within_year < 243 + in_leap_year)
  223. return 7;
  224. // 8𝔽 if 243𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 273𝔽 + InLeapYear(t)
  225. if (243 + in_leap_year <= day_within_year && day_within_year < 273 + in_leap_year)
  226. return 8;
  227. // 9𝔽 if 273𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 304𝔽 + InLeapYear(t)
  228. if (273 + in_leap_year <= day_within_year && day_within_year < 304 + in_leap_year)
  229. return 9;
  230. // 10𝔽 if 304𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 334𝔽 + InLeapYear(t)
  231. if (304 + in_leap_year <= day_within_year && day_within_year < 334 + in_leap_year)
  232. return 10;
  233. // 11𝔽 if 334𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 365𝔽 + InLeapYear(t)
  234. if (334 + in_leap_year <= day_within_year && day_within_year < 365 + in_leap_year)
  235. return 11;
  236. VERIFY_NOT_REACHED();
  237. }
  238. // HourFromTime(t), https://tc39.es/ecma262/#eqn-HourFromTime
  239. u8 hour_from_time(double t)
  240. {
  241. // 𝔽(floor(ℝ(t / msPerHour)) modulo HoursPerDay)
  242. return static_cast<u8>(fmod(floor(t / MS_PER_HOUR), HOURS_PER_DAY));
  243. }
  244. // MinFromTime(t), https://tc39.es/ecma262/#eqn-MinFromTime
  245. u8 min_from_time(double t)
  246. {
  247. // 𝔽(floor(ℝ(t / msPerMinute)) modulo MinutesPerHour)
  248. return static_cast<u8>(fmod(floor(t / MS_PER_MINUTE), MINUTES_PER_HOUR));
  249. }
  250. // SecFromTime(t), https://tc39.es/ecma262/#eqn-SecFromTime
  251. u8 sec_from_time(double t)
  252. {
  253. // 𝔽(floor(ℝ(t / msPerSecond)) modulo SecondsPerMinute)
  254. return static_cast<u8>(fmod(t / MS_PER_SECOND, SECONDS_PER_MINUTE));
  255. }
  256. // msFromTime(t), https://tc39.es/ecma262/#eqn-msFromTime
  257. u16 ms_from_time(double t)
  258. {
  259. // 𝔽(ℝ(t) modulo msPerSecond)
  260. return static_cast<u16>(fmod(t, MS_PER_SECOND));
  261. }
  262. // 21.4.1.11 MakeTime ( hour, min, sec, ms ), https://tc39.es/ecma262/#sec-maketime
  263. Value make_time(GlobalObject& global_object, Value hour, Value min, Value sec, Value ms)
  264. {
  265. // 1. If hour is not finite or min is not finite or sec is not finite or ms is not finite, return NaN.
  266. if (!hour.is_finite_number() || !min.is_finite_number() || !sec.is_finite_number() || !ms.is_finite_number())
  267. return js_nan();
  268. // 2. Let h be 𝔽(! ToIntegerOrInfinity(hour)).
  269. auto h = hour.to_integer_or_infinity(global_object);
  270. // 3. Let m be 𝔽(! ToIntegerOrInfinity(min)).
  271. auto m = min.to_integer_or_infinity(global_object);
  272. // 4. Let s be 𝔽(! ToIntegerOrInfinity(sec)).
  273. auto s = sec.to_integer_or_infinity(global_object);
  274. // 5. Let milli be 𝔽(! ToIntegerOrInfinity(ms)).
  275. auto milli = ms.to_integer_or_infinity(global_object);
  276. // 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 +).
  277. // NOTE: C++ arithmetic abides by IEEE 754 rules
  278. auto t = ((h * MS_PER_HOUR + m * MS_PER_MINUTE) + s * MS_PER_SECOND) + milli;
  279. // 7. Return t.
  280. return Value(t);
  281. }
  282. // Day(t), https://tc39.es/ecma262/#eqn-Day
  283. double day(double time_value)
  284. {
  285. return floor(time_value / MS_PER_DAY);
  286. }
  287. // 21.4.1.12 MakeDay ( year, month, date ), https://tc39.es/ecma262/#sec-makeday
  288. Value make_day(GlobalObject& global_object, Value year, Value month, Value date)
  289. {
  290. // 1. If year is not finite or month is not finite or date is not finite, return NaN.
  291. if (!year.is_finite_number() || !month.is_finite_number() || !date.is_finite_number())
  292. return js_nan();
  293. // 2. Let y be 𝔽(! ToIntegerOrInfinity(year)).
  294. auto y = year.to_integer_or_infinity(global_object);
  295. // 3. Let m be 𝔽(! ToIntegerOrInfinity(month)).
  296. auto m = month.to_integer_or_infinity(global_object);
  297. // 4. Let dt be 𝔽(! ToIntegerOrInfinity(date)).
  298. auto dt = date.to_integer_or_infinity(global_object);
  299. // 5. Let ym be y + 𝔽(floor(ℝ(m) / 12)).
  300. auto ym = Value(y + floor(m / 12));
  301. // 6. If ym is not finite, return NaN.
  302. if (!ym.is_finite_number())
  303. return js_nan();
  304. // 7. Let mn be 𝔽(ℝ(m) modulo 12).
  305. // NOTE: This calculation has no side-effects and is unused, so we omit it
  306. // 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.
  307. auto t = Core::DateTime::create(y, m + 1, 0).timestamp() * 1000;
  308. // 9. Return Day(t) + dt - 1𝔽.
  309. return Value(day(t) + dt - 1);
  310. }
  311. // 21.4.1.13 MakeDate ( day, time ), https://tc39.es/ecma262/#sec-makedate
  312. Value make_date(Value day, Value time)
  313. {
  314. // 1. If day is not finite or time is not finite, return NaN.
  315. if (!day.is_finite_number() || !time.is_finite_number())
  316. return js_nan();
  317. // 2. Let tv be day × msPerDay + time.
  318. auto tv = Value(day.as_double() * MS_PER_DAY + time.as_double());
  319. // 3. If tv is not finite, return NaN.
  320. if (!tv.is_finite_number())
  321. return js_nan();
  322. // 4. Return tv.
  323. return tv;
  324. }
  325. }