Now.plainDateTimeISO.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. describe("correct behavior", () => {
  2. test("length is 0", () => {
  3. expect(Temporal.Now.plainDateTimeISO).toHaveLength(0);
  4. });
  5. test("basic functionality", () => {
  6. const plainDateTime = Temporal.Now.plainDateTimeISO();
  7. expect(plainDateTime).toBeInstanceOf(Temporal.PlainDateTime);
  8. expect(plainDateTime.calendar.id).toBe("iso8601");
  9. });
  10. const plainDateTimeToEpochSeconds = plainDateTime =>
  11. (plainDateTime.year - 1970) * 31_556_952 +
  12. plainDateTime.dayOfYear * 86_400 +
  13. plainDateTime.hour * 3_600 +
  14. plainDateTime.minute * 60 +
  15. plainDateTime.second +
  16. plainDateTime.millisecond / 1_000 +
  17. plainDateTime.microsecond / 1_000_000 +
  18. plainDateTime.nanosecond / 1_000_000_000;
  19. let timeZoneTested = false;
  20. // Note: We test both positive and negative timezones because one might cross a year boundary.
  21. // Since a year does not have a fixed amount of seconds because it can be a leap year,
  22. // we cannot have a correct constant for seconds per year which is always correct.
  23. // However, by assuming years are at least 2 days long we can simply try the positive
  24. // and negative timezones and skip one if we jump the year. To ensure at least one is
  25. // tested we have the timeZoneTested which is only set to true if one of the tests passed.
  26. // FIXME: The custom time zone tests are disabled due to being flaky. See:
  27. // https://github.com/SerenityOS/serenity/issues/20806
  28. test.skip("custom time zone (positive)", () => {
  29. const timeZone = {
  30. getOffsetNanosecondsFor() {
  31. return 86399999999999;
  32. },
  33. };
  34. const [plainDateTime, plainDateTimeWithOffset] = withinSameSecond(() => {
  35. return [Temporal.Now.plainDateTimeISO("UTC"), Temporal.Now.plainDateTimeISO(timeZone)];
  36. });
  37. if (plainDateTime.year !== plainDateTimeWithOffset.year) return;
  38. const differenceSeconds =
  39. plainDateTimeToEpochSeconds(plainDateTimeWithOffset) -
  40. plainDateTimeToEpochSeconds(plainDateTime);
  41. expect(Math.floor(differenceSeconds)).toBe(86400);
  42. timeZoneTested = true;
  43. });
  44. test.skip("custom time zone (negative)", () => {
  45. const timeZone = {
  46. getOffsetNanosecondsFor() {
  47. return -86399999999999;
  48. },
  49. };
  50. const [plainDateTime, plainDateTimeWithOffset] = withinSameSecond(() => {
  51. return [Temporal.Now.plainDateTimeISO("UTC"), Temporal.Now.plainDateTimeISO(timeZone)];
  52. });
  53. if (plainDateTime.year !== plainDateTimeWithOffset.year) return;
  54. const differenceSeconds =
  55. plainDateTimeToEpochSeconds(plainDateTimeWithOffset) -
  56. plainDateTimeToEpochSeconds(plainDateTime);
  57. expect(Math.floor(differenceSeconds)).toBe(-86400);
  58. timeZoneTested = true;
  59. });
  60. test.skip("custom time zone test was executed", () => {
  61. expect(timeZoneTested).toBeTrue();
  62. });
  63. test("cannot have a time zone with more than a day", () => {
  64. [86400000000000, -86400000000000, 86400000000001, 86400000000002].forEach(offset => {
  65. const timeZone = {
  66. getOffsetNanosecondsFor() {
  67. return offset;
  68. },
  69. };
  70. expect(() => Temporal.Now.plainDateTimeISO(timeZone)).toThrowWithMessage(
  71. RangeError,
  72. "Invalid offset nanoseconds value, must be in range -86400 * 10^9 + 1 to 86400 * 10^9 - 1"
  73. );
  74. });
  75. });
  76. });
  77. describe("errors", () => {
  78. test("custom time zone doesn't have a getOffsetNanosecondsFor function", () => {
  79. expect(() => {
  80. Temporal.Now.plainDateTimeISO({});
  81. }).toThrowWithMessage(TypeError, "getOffsetNanosecondsFor is undefined");
  82. });
  83. });