Dates.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Adam Hodgen <ant1441@gmail.com>
  4. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  5. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  6. * Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
  7. *
  8. * SPDX-License-Identifier: BSD-2-Clause
  9. */
  10. #include <AK/Time.h>
  11. #include <LibWeb/HTML/Dates.h>
  12. namespace Web::HTML {
  13. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#week-number-of-the-last-day
  14. u32 week_number_of_the_last_day(u64 year)
  15. {
  16. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#weeks
  17. // NOTE: A year is considered to have 53 weeks if either of the following conditions are satisfied:
  18. // - January 1 of that year is a Thursday.
  19. // - January 1 of that year is a Wednesday and the year is divisible by 400, or divisible by 4, but not 100.
  20. // Note: Gauss's algorithm for determining the day of the week with D = 1, and M = 0
  21. // https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Gauss's_algorithm
  22. u8 day_of_week = (1 + 5 * ((year - 1) % 4) + 4 * ((year - 1) % 100) + 6 * ((year - 1) % 400)) % 7;
  23. if (day_of_week == 4 || (day_of_week == 3 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))))
  24. return 53;
  25. return 52;
  26. }
  27. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-week-string
  28. bool is_valid_week_string(StringView value)
  29. {
  30. // A string is a valid week string representing a week-year year and week week if it consists of the following components in the given order:
  31. // 1. Four or more ASCII digits, representing year, where year > 0
  32. // 2. A U+002D HYPHEN-MINUS character (-)
  33. // 3. A U+0057 LATIN CAPITAL LETTER W character (W)
  34. // 4. Two ASCII digits, representing the week week, in the range 1 ≤ week ≤ maxweek, where maxweek is the week number of the last day of week-year year
  35. auto parts = value.split_view('-');
  36. if (parts.size() != 2)
  37. return false;
  38. if (parts[0].length() < 4)
  39. return false;
  40. for (auto digit : parts[0])
  41. if (!is_ascii_digit(digit))
  42. return false;
  43. if (parts[1].length() != 3)
  44. return false;
  45. if (!parts[1].starts_with('W'))
  46. return false;
  47. if (!is_ascii_digit(parts[1][1]))
  48. return false;
  49. if (!is_ascii_digit(parts[1][2]))
  50. return false;
  51. u64 year = 0;
  52. for (auto d : parts[0]) {
  53. year *= 10;
  54. year += parse_ascii_digit(d);
  55. }
  56. auto week = (parse_ascii_digit(parts[1][1]) * 10) + parse_ascii_digit(parts[1][2]);
  57. return week >= 1 && week <= week_number_of_the_last_day(year);
  58. }
  59. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-month-string
  60. bool is_valid_month_string(StringView value)
  61. {
  62. // A string is a valid month string representing a year year and month month if it consists of the following components in the given order:
  63. // 1. Four or more ASCII digits, representing year, where year > 0
  64. // 2. A U+002D HYPHEN-MINUS character (-)
  65. // 3. Two ASCII digits, representing the month month, in the range 1 ≤ month ≤ 12
  66. auto parts = value.split_view('-');
  67. if (parts.size() != 2)
  68. return false;
  69. if (parts[0].length() < 4)
  70. return false;
  71. for (auto digit : parts[0])
  72. if (!is_ascii_digit(digit))
  73. return false;
  74. if (parts[1].length() != 2)
  75. return false;
  76. if (!is_ascii_digit(parts[1][0]))
  77. return false;
  78. if (!is_ascii_digit(parts[1][1]))
  79. return false;
  80. auto month = (parse_ascii_digit(parts[1][0]) * 10) + parse_ascii_digit(parts[1][1]);
  81. return month >= 1 && month <= 12;
  82. }
  83. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
  84. bool is_valid_date_string(StringView value)
  85. {
  86. // A string is a valid date string representing a year year, month month, and day day if it consists of the following components in the given order:
  87. // 1. A valid month string, representing year and month
  88. // 2. A U+002D HYPHEN-MINUS character (-)
  89. // 3. Two ASCII digits, representing day, in the range 1 ≤ day ≤ maxday where maxday is the number of days in the month month and year year
  90. auto parts = value.split_view('-');
  91. if (parts.size() != 3)
  92. return false;
  93. if (!is_valid_month_string(ByteString::formatted("{}-{}", parts[0], parts[1])))
  94. return false;
  95. if (parts[2].length() != 2)
  96. return false;
  97. i64 year = 0;
  98. for (auto d : parts[0]) {
  99. year *= 10;
  100. year += parse_ascii_digit(d);
  101. }
  102. auto month = (parse_ascii_digit(parts[1][0]) * 10) + parse_ascii_digit(parts[1][1]);
  103. i64 day = (parse_ascii_digit(parts[2][0]) * 10) + parse_ascii_digit(parts[2][1]);
  104. return day >= 1 && day <= AK::days_in_month(year, month);
  105. }
  106. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-local-date-and-time-string
  107. bool is_valid_local_date_and_time_string(StringView value)
  108. {
  109. auto parts_split_by_T = value.split_view('T');
  110. if (parts_split_by_T.size() == 2)
  111. return is_valid_date_string(parts_split_by_T[0]) && is_valid_time_string(parts_split_by_T[1]);
  112. auto parts_split_by_space = value.split_view(' ');
  113. if (parts_split_by_space.size() == 2)
  114. return is_valid_date_string(parts_split_by_space[0]) && is_valid_time_string(parts_split_by_space[1]);
  115. return false;
  116. }
  117. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-normalised-local-date-and-time-string
  118. String normalize_local_date_and_time_string(String const& value)
  119. {
  120. VERIFY(value.count(" "sv) == 1);
  121. return MUST(value.replace(" "sv, "T"sv, ReplaceMode::FirstOnly));
  122. }
  123. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-time-string
  124. bool is_valid_time_string(StringView value)
  125. {
  126. // A string is a valid time string representing an hour hour, a minute minute, and a second second if it consists of the following components in the given order:
  127. // 1. Two ASCII digits, representing hour, in the range 0 ≤ hour ≤ 23
  128. // 2. A U+003A COLON character (:)
  129. // 3. Two ASCII digits, representing minute, in the range 0 ≤ minute ≤ 59
  130. // 4. If second is nonzero, or optionally if second is zero:
  131. // 1. A U+003A COLON character (:)
  132. // 2. Two ASCII digits, representing the integer part of second, in the range 0 ≤ s ≤ 59
  133. // 3. If second is not an integer, or optionally if second is an integer:
  134. // 1. A U+002E FULL STOP character (.)
  135. // 2. One, two, or three ASCII digits, representing the fractional part of second
  136. auto parts = value.split_view(':');
  137. if (parts.size() != 2 && parts.size() != 3)
  138. return false;
  139. if (parts[0].length() != 2)
  140. return false;
  141. auto hour = (parse_ascii_digit(parts[0][0]) * 10) + parse_ascii_digit(parts[0][1]);
  142. if (hour > 23)
  143. return false;
  144. if (parts[1].length() != 2)
  145. return false;
  146. auto minute = (parse_ascii_digit(parts[1][0]) * 10) + parse_ascii_digit(parts[1][1]);
  147. if (minute > 59)
  148. return false;
  149. if (parts.size() == 2)
  150. return true;
  151. if (parts[2].length() < 2)
  152. return false;
  153. auto second = (parse_ascii_digit(parts[2][0]) * 10) + parse_ascii_digit(parts[2][1]);
  154. if (second > 59)
  155. return false;
  156. if (parts[2].length() == 2)
  157. return true;
  158. auto second_parts = parts[2].split_view('.');
  159. if (second_parts.size() != 2)
  160. return false;
  161. if (second_parts[1].length() < 1 || second_parts[1].length() > 3)
  162. return false;
  163. for (auto digit : second_parts[1])
  164. if (!is_ascii_digit(digit))
  165. return false;
  166. return true;
  167. }
  168. }