Calendar.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Temporal/Calendar.h>
  7. #include <LibJS/Runtime/Value.h>
  8. namespace JS::Temporal {
  9. // 12.1.30 IsISOLeapYear ( year ), https://tc39.es/proposal-temporal/#sec-temporal-isisoleapyear
  10. bool is_iso_leap_year(i32 year)
  11. {
  12. // 1. Assert: year is an integer.
  13. // 2. If year modulo 4 ≠ 0, return false.
  14. if (year % 4 != 0)
  15. return false;
  16. // 3. If year modulo 400 = 0, return true.
  17. if (year % 400 == 0)
  18. return true;
  19. // 4. If year modulo 100 = 0, return false.
  20. if (year % 100 == 0)
  21. return false;
  22. // 5. Return true.
  23. return true;
  24. }
  25. // 12.1.32 ISODaysInMonth ( year, month ), https://tc39.es/proposal-temporal/#sec-temporal-isodaysinmonth
  26. i32 iso_days_in_month(i32 year, i32 month)
  27. {
  28. // 1. Assert: year is an integer.
  29. // 2. Assert: month is an integer, month ≥ 1, and month ≤ 12.
  30. VERIFY(month >= 1 && month <= 12);
  31. // 3. If month is 1, 3, 5, 7, 8, 10, or 12, return 31.
  32. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
  33. return 31;
  34. // 4. If month is 4, 6, 9, or 11, return 30.
  35. if (month == 4 || month == 6 || month == 9 || month == 11)
  36. return 30;
  37. // 5. If ! IsISOLeapYear(year) is true, return 29.
  38. if (is_iso_leap_year(year))
  39. return 29;
  40. // 6. Return 28.
  41. return 28;
  42. }
  43. }