Now.plainDateTimeISO.js 3.6 KB

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