Date.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2020, 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 <sys/time.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-msPerSecond
  97. static constexpr double MS_PER_SECOND = 1000;
  98. // https://tc39.es/ecma262/#eqn-msPerMinute
  99. static constexpr double MS_PER_MINUTE = 60000;
  100. // https://tc39.es/ecma262/#eqn-msPerHour
  101. static constexpr double MS_PER_HOUR = 3600000;
  102. // https://tc39.es/ecma262/#eqn-msPerDay
  103. static constexpr double MS_PER_DAY = 86400000;
  104. // 21.4.1.11 MakeTime ( hour, min, sec, ms ), https://tc39.es/ecma262/#sec-maketime
  105. Value make_time(GlobalObject& global_object, Value hour, Value min, Value sec, Value ms)
  106. {
  107. // 1. If hour is not finite or min is not finite or sec is not finite or ms is not finite, return NaN.
  108. if (!hour.is_finite_number() || !min.is_finite_number() || !sec.is_finite_number() || !ms.is_finite_number())
  109. return js_nan();
  110. // 2. Let h be 𝔽(! ToIntegerOrInfinity(hour)).
  111. auto h = hour.to_integer_or_infinity(global_object);
  112. // 3. Let m be 𝔽(! ToIntegerOrInfinity(min)).
  113. auto m = min.to_integer_or_infinity(global_object);
  114. // 4. Let s be 𝔽(! ToIntegerOrInfinity(sec)).
  115. auto s = sec.to_integer_or_infinity(global_object);
  116. // 5. Let milli be 𝔽(! ToIntegerOrInfinity(ms)).
  117. auto milli = ms.to_integer_or_infinity(global_object);
  118. // 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 +).
  119. // NOTE: C++ arithmetic abides by IEEE 754 rules
  120. auto t = ((h * MS_PER_HOUR + m * MS_PER_MINUTE) + s * MS_PER_SECOND) + milli;
  121. // 7. Return t.
  122. return Value(t);
  123. }
  124. // https://tc39.es/ecma262/#eqn-Day
  125. static inline double day(double time_value)
  126. {
  127. return floor(time_value / MS_PER_DAY);
  128. }
  129. // 21.4.1.12 MakeDay ( year, month, date ), https://tc39.es/ecma262/#sec-makeday
  130. Value make_day(GlobalObject& global_object, Value year, Value month, Value date)
  131. {
  132. // 1. If year is not finite or month is not finite or date is not finite, return NaN.
  133. if (!year.is_finite_number() || !month.is_finite_number() || !date.is_finite_number())
  134. return js_nan();
  135. // 2. Let y be 𝔽(! ToIntegerOrInfinity(year)).
  136. auto y = year.to_integer_or_infinity(global_object);
  137. // 3. Let m be 𝔽(! ToIntegerOrInfinity(month)).
  138. auto m = month.to_integer_or_infinity(global_object);
  139. // 4. Let dt be 𝔽(! ToIntegerOrInfinity(date)).
  140. auto dt = date.to_integer_or_infinity(global_object);
  141. // 5. Let ym be y + 𝔽(floor(ℝ(m) / 12)).
  142. auto ym = Value(y + floor(m / 12));
  143. // 6. If ym is not finite, return NaN.
  144. if (!ym.is_finite_number())
  145. return js_nan();
  146. // 7. Let mn be 𝔽(ℝ(m) modulo 12).
  147. // NOTE: This calculation has no side-effects and is unused, so we omit it
  148. // 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.
  149. auto t = Core::DateTime::create(y, m + 1, 0).timestamp() * 1000;
  150. // 9. Return Day(t) + dt - 1𝔽.
  151. return Value(day(t) + dt - 1);
  152. }
  153. // 21.4.1.13 MakeDate ( day, time ), https://tc39.es/ecma262/#sec-makedate
  154. Value make_date(Value day, Value time)
  155. {
  156. // 1. If day is not finite or time is not finite, return NaN.
  157. if (!day.is_finite_number() || !time.is_finite_number())
  158. return js_nan();
  159. // 2. Let tv be day × msPerDay + time.
  160. auto tv = Value(day.as_double() * MS_PER_DAY + time.as_double());
  161. // 3. If tv is not finite, return NaN.
  162. if (!tv.is_finite_number())
  163. return js_nan();
  164. // 4. Return tv.
  165. return tv;
  166. }
  167. }