Date.cpp 16 KB

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