DateEquations.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Date.h>
  8. #include <LibJS/Runtime/Temporal/DateEquations.h>
  9. namespace JS::Temporal {
  10. // https://tc39.es/proposal-temporal/#eqn-mathematicaldaysinyear
  11. u16 mathematical_days_in_year(i32 year)
  12. {
  13. // MathematicalDaysInYear(y)
  14. // = 365 if ((y) modulo 4) ≠ 0
  15. // = 366 if ((y) modulo 4) = 0 and ((y) modulo 100) ≠ 0
  16. // = 365 if ((y) modulo 100) = 0 and ((y) modulo 400) ≠ 0
  17. // = 366 if ((y) modulo 400) = 0
  18. if (modulo(year, 4) != 0)
  19. return 365;
  20. if (modulo(year, 4) == 0 && modulo(year, 100) != 0)
  21. return 366;
  22. if (modulo(year, 100) == 0 && modulo(year, 400) != 0)
  23. return 365;
  24. if (modulo(year, 400) == 0)
  25. return 366;
  26. VERIFY_NOT_REACHED();
  27. }
  28. // https://tc39.es/proposal-temporal/#eqn-mathematicalinleapyear
  29. u8 mathematical_in_leap_year(double time)
  30. {
  31. // MathematicalInLeapYear(t)
  32. // = 0 if MathematicalDaysInYear(EpochTimeToEpochYear(t)) = 365
  33. // = 1 if MathematicalDaysInYear(EpochTimeToEpochYear(t)) = 366
  34. auto days_in_year = mathematical_days_in_year(epoch_time_to_epoch_year(time));
  35. if (days_in_year == 365)
  36. return 0;
  37. if (days_in_year == 366)
  38. return 1;
  39. VERIFY_NOT_REACHED();
  40. }
  41. // https://tc39.es/proposal-temporal/#eqn-EpochTimeToDayNumber
  42. double epoch_time_to_day_number(double time)
  43. {
  44. // EpochTimeToDayNumber(t) = floor(t / ℝ(msPerDay))
  45. return floor(time / JS::ms_per_day);
  46. }
  47. // https://tc39.es/proposal-temporal/#eqn-epochdaynumberforyear
  48. double epoch_day_number_for_year(double year)
  49. {
  50. // EpochDayNumberForYear(y) = 365 × (y - 1970) + floor((y - 1969) / 4) - floor((y - 1901) / 100) + floor((y - 1601) / 400)
  51. return 365.0 * (year - 1970.0) + floor((year - 1969.0) / 4.0) - floor((year - 1901.0) / 100.0) + floor((year - 1601.0) / 400.0);
  52. }
  53. // https://tc39.es/proposal-temporal/#eqn-epochtimeforyear
  54. double epoch_time_for_year(double year)
  55. {
  56. // EpochTimeForYear(y) = ℝ(msPerDay) × EpochDayNumberForYear(y)
  57. return ms_per_day * epoch_day_number_for_year(year);
  58. }
  59. // https://tc39.es/proposal-temporal/#eqn-epochtimetoepochyear
  60. i32 epoch_time_to_epoch_year(double time)
  61. {
  62. // EpochTimeToEpochYear(t) = the largest integral Number y (closest to +∞) such that EpochTimeForYear(y) ≤ t
  63. return JS::year_from_time(time);
  64. }
  65. // https://tc39.es/proposal-temporal/#eqn-epochtimetodayinyear
  66. u16 epoch_time_to_day_in_year(double time)
  67. {
  68. // EpochTimeToDayInYear(t) = EpochTimeToDayNumber(t) - EpochDayNumberForYear(EpochTimeToEpochYear(t))
  69. return static_cast<u16>(epoch_time_to_day_number(time) - epoch_day_number_for_year(epoch_time_to_epoch_year(time)));
  70. }
  71. // https://tc39.es/proposal-temporal/#eqn-epochtimetomonthinyear
  72. u8 epoch_time_to_month_in_year(double time)
  73. {
  74. auto day_in_year = epoch_time_to_day_in_year(time);
  75. auto in_leap_year = mathematical_in_leap_year(time);
  76. // EpochTimeToMonthInYear(t)
  77. // = 0 if 0 ≤ EpochTimeToDayInYear(t) < 31
  78. // = 1 if 31 ≤ EpochTimeToDayInYear(t) < 59 + MathematicalInLeapYear(t)
  79. // = 2 if 59 + MathematicalInLeapYear(t) ≤ EpochTimeToDayInYear(t) < 90 + MathematicalInLeapYear(t)
  80. // = 3 if 90 + MathematicalInLeapYear(t) ≤ EpochTimeToDayInYear(t) < 120 + MathematicalInLeapYear(t)
  81. // = 4 if 120 + MathematicalInLeapYear(t) ≤ EpochTimeToDayInYear(t) < 151 + MathematicalInLeapYear(t)
  82. // = 5 if 151 + MathematicalInLeapYear(t) ≤ EpochTimeToDayInYear(t) < 181 + MathematicalInLeapYear(t)
  83. // = 6 if 181 + MathematicalInLeapYear(t) ≤ EpochTimeToDayInYear(t) < 212 + MathematicalInLeapYear(t)
  84. // = 7 if 212 + MathematicalInLeapYear(t) ≤ EpochTimeToDayInYear(t) < 243 + MathematicalInLeapYear(t)
  85. // = 8 if 243 + MathematicalInLeapYear(t) ≤ EpochTimeToDayInYear(t) < 273 + MathematicalInLeapYear(t)
  86. // = 9 if 273 + MathematicalInLeapYear(t) ≤ EpochTimeToDayInYear(t) < 304 + MathematicalInLeapYear(t)
  87. // = 10 if 304 + MathematicalInLeapYear(t) ≤ EpochTimeToDayInYear(t) < 334 + MathematicalInLeapYear(t)
  88. // = 11 if 334 + MathematicalInLeapYear(t) ≤ EpochTimeToDayInYear(t) < 365 + MathematicalInLeapYear(t)
  89. if (day_in_year < 31)
  90. return 0;
  91. if (day_in_year >= 31 && day_in_year < 59 + in_leap_year)
  92. return 1;
  93. if (day_in_year >= 59 + in_leap_year && day_in_year < 90 + in_leap_year)
  94. return 2;
  95. if (day_in_year >= 90 + in_leap_year && day_in_year < 120 + in_leap_year)
  96. return 3;
  97. if (day_in_year >= 120 + in_leap_year && day_in_year < 151 + in_leap_year)
  98. return 4;
  99. if (day_in_year >= 151 + in_leap_year && day_in_year < 181 + in_leap_year)
  100. return 5;
  101. if (day_in_year >= 181 + in_leap_year && day_in_year < 212 + in_leap_year)
  102. return 6;
  103. if (day_in_year >= 212 + in_leap_year && day_in_year < 243 + in_leap_year)
  104. return 7;
  105. if (day_in_year >= 243 + in_leap_year && day_in_year < 273 + in_leap_year)
  106. return 8;
  107. if (day_in_year >= 273 + in_leap_year && day_in_year < 304 + in_leap_year)
  108. return 9;
  109. if (day_in_year >= 304 + in_leap_year && day_in_year < 334 + in_leap_year)
  110. return 10;
  111. if (day_in_year >= 334 + in_leap_year && day_in_year < 365 + in_leap_year)
  112. return 11;
  113. VERIFY_NOT_REACHED();
  114. }
  115. // https://tc39.es/proposal-temporal/#eqn-epochtimetoweekday
  116. u8 epoch_time_to_week_day(double time)
  117. {
  118. // EpochTimeToWeekDay(t) = (EpochTimeToDayNumber(t) + 4) modulo 7
  119. return static_cast<u8>(modulo(epoch_time_to_day_number(time) + 4, 7.0));
  120. }
  121. // https://tc39.es/proposal-temporal/#eqn-epochtimetodate
  122. u8 epoch_time_to_date(double time)
  123. {
  124. auto day_in_year = epoch_time_to_day_in_year(time);
  125. auto month_in_year = epoch_time_to_month_in_year(time);
  126. auto in_leap_year = mathematical_in_leap_year(time);
  127. // EpochTimeToDate(t)
  128. // = EpochTimeToDayInYear(t) + 1 if EpochTimeToMonthInYear(t) = 0
  129. // = EpochTimeToDayInYear(t) - 30 if EpochTimeToMonthInYear(t) = 1
  130. // = EpochTimeToDayInYear(t) - 58 - MathematicalInLeapYear(t) if EpochTimeToMonthInYear(t) = 2
  131. // = EpochTimeToDayInYear(t) - 89 - MathematicalInLeapYear(t) if EpochTimeToMonthInYear(t) = 3
  132. // = EpochTimeToDayInYear(t) - 119 - MathematicalInLeapYear(t) if EpochTimeToMonthInYear(t) = 4
  133. // = EpochTimeToDayInYear(t) - 150 - MathematicalInLeapYear(t) if EpochTimeToMonthInYear(t) = 5
  134. // = EpochTimeToDayInYear(t) - 180 - MathematicalInLeapYear(t) if EpochTimeToMonthInYear(t) = 6
  135. // = EpochTimeToDayInYear(t) - 211 - MathematicalInLeapYear(t) if EpochTimeToMonthInYear(t) = 7
  136. // = EpochTimeToDayInYear(t) - 242 - MathematicalInLeapYear(t) if EpochTimeToMonthInYear(t) = 8
  137. // = EpochTimeToDayInYear(t) - 272 - MathematicalInLeapYear(t) if EpochTimeToMonthInYear(t) = 9
  138. // = EpochTimeToDayInYear(t) - 303 - MathematicalInLeapYear(t) if EpochTimeToMonthInYear(t) = 10
  139. // = EpochTimeToDayInYear(t) - 333 - MathematicalInLeapYear(t) if EpochTimeToMonthInYear(t) = 11
  140. if (month_in_year == 0)
  141. return day_in_year + 1;
  142. if (month_in_year == 1)
  143. return day_in_year - 30;
  144. if (month_in_year == 2)
  145. return day_in_year - 58 - in_leap_year;
  146. if (month_in_year == 3)
  147. return day_in_year - 89 - in_leap_year;
  148. if (month_in_year == 4)
  149. return day_in_year - 119 - in_leap_year;
  150. if (month_in_year == 5)
  151. return day_in_year - 150 - in_leap_year;
  152. if (month_in_year == 6)
  153. return day_in_year - 180 - in_leap_year;
  154. if (month_in_year == 7)
  155. return day_in_year - 211 - in_leap_year;
  156. if (month_in_year == 8)
  157. return day_in_year - 242 - in_leap_year;
  158. if (month_in_year == 9)
  159. return day_in_year - 272 - in_leap_year;
  160. if (month_in_year == 10)
  161. return day_in_year - 303 - in_leap_year;
  162. if (month_in_year == 11)
  163. return day_in_year - 333 - in_leap_year;
  164. VERIFY_NOT_REACHED();
  165. }
  166. }