Now.plainDateTime.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. test("custom time zone positive", () => {
  28. const calendar = new Temporal.Calendar("iso8601");
  29. const timeZone = {
  30. getOffsetNanosecondsFor() {
  31. return 86399999999999;
  32. },
  33. };
  34. const [plainDateTime, plainDateTimeWithOffset] = withinSameSecond(() => {
  35. return [
  36. Temporal.Now.plainDateTime(calendar, "UTC"),
  37. Temporal.Now.plainDateTime(calendar, timeZone),
  38. ];
  39. });
  40. if (plainDateTime.year !== plainDateTimeWithOffset.year) return;
  41. const differenceSeconds =
  42. plainDateTimeToEpochSeconds(plainDateTimeWithOffset) -
  43. plainDateTimeToEpochSeconds(plainDateTime);
  44. expect(Math.floor(differenceSeconds)).toBe(86400);
  45. timeZoneTested = true;
  46. });
  47. test("custom time zone negative", () => {
  48. const calendar = new Temporal.Calendar("iso8601");
  49. const timeZone = {
  50. getOffsetNanosecondsFor() {
  51. return -86399999999999;
  52. },
  53. };
  54. const [plainDateTime, plainDateTimeWithOffset] = withinSameSecond(() => {
  55. return [
  56. Temporal.Now.plainDateTime(calendar, "UTC"),
  57. Temporal.Now.plainDateTime(calendar, timeZone),
  58. ];
  59. });
  60. if (plainDateTime.year !== plainDateTimeWithOffset.year) return;
  61. const differenceSeconds =
  62. plainDateTimeToEpochSeconds(plainDateTimeWithOffset) -
  63. plainDateTimeToEpochSeconds(plainDateTime);
  64. expect(Math.floor(differenceSeconds)).toBe(-86400);
  65. timeZoneTested = true;
  66. });
  67. expect(timeZoneTested).toBeTrue();
  68. test("cannot have a time zone with more than a day", () => {
  69. [86400000000000, -86400000000000, 86400000000001, 86400000000002].forEach(offset => {
  70. const calendar = new Temporal.Calendar("iso8601");
  71. const timeZone = {
  72. getOffsetNanosecondsFor() {
  73. return offset;
  74. },
  75. };
  76. expect(() => Temporal.Now.plainDateTime(calendar, timeZone)).toThrowWithMessage(
  77. RangeError,
  78. "Invalid offset nanoseconds value, must be in range -86400 * 10^9 + 1 to 86400 * 10^9 - 1"
  79. );
  80. });
  81. });
  82. });
  83. describe("errors", () => {
  84. test("custom time zone doesn't have a getOffsetNanosecondsFor function", () => {
  85. expect(() => {
  86. Temporal.Now.plainDateTime({}, {});
  87. }).toThrowWithMessage(TypeError, "null is not a function");
  88. });
  89. });