Now.plainDateTime.js 4.2 KB

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