PlainTime.from.js 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. describe("correct behavior", () => {
  2. test("length is 1", () => {
  3. expect(Temporal.PlainTime.from).toHaveLength(1);
  4. });
  5. test("PlainTime instance argument", () => {
  6. const plainTime = new Temporal.PlainTime(18, 45, 37, 1, 2, 3);
  7. const createdPlainTime = Temporal.PlainTime.from(plainTime);
  8. expect(createdPlainTime.hour).toBe(18);
  9. expect(createdPlainTime.minute).toBe(45);
  10. expect(createdPlainTime.second).toBe(37);
  11. expect(createdPlainTime.millisecond).toBe(1);
  12. expect(createdPlainTime.microsecond).toBe(2);
  13. expect(createdPlainTime.nanosecond).toBe(3);
  14. });
  15. test("PlainDateTime instance argument", () => {
  16. const plainDateTime = new Temporal.PlainDateTime(2021, 8, 27, 18, 45, 37, 1, 2, 3);
  17. const createdPlainTime = Temporal.PlainTime.from(plainDateTime);
  18. expect(createdPlainTime.hour).toBe(18);
  19. expect(createdPlainTime.minute).toBe(45);
  20. expect(createdPlainTime.second).toBe(37);
  21. expect(createdPlainTime.millisecond).toBe(1);
  22. expect(createdPlainTime.microsecond).toBe(2);
  23. expect(createdPlainTime.nanosecond).toBe(3);
  24. });
  25. test("ZonedDateTime instance argument", () => {
  26. const timeZone = new Temporal.TimeZone("UTC");
  27. const zonedDateTime = new Temporal.ZonedDateTime(1627318123456789000n, timeZone);
  28. const createdPlainTime = Temporal.PlainTime.from(zonedDateTime);
  29. expect(createdPlainTime.hour).toBe(16);
  30. expect(createdPlainTime.minute).toBe(48);
  31. expect(createdPlainTime.second).toBe(43);
  32. expect(createdPlainTime.millisecond).toBe(456);
  33. expect(createdPlainTime.microsecond).toBe(789);
  34. expect(createdPlainTime.nanosecond).toBe(0);
  35. });
  36. test("PlainTime string argument", () => {
  37. const createdPlainTime = Temporal.PlainTime.from("2021-08-27T18:44:11");
  38. expect(createdPlainTime.hour).toBe(18);
  39. expect(createdPlainTime.minute).toBe(44);
  40. expect(createdPlainTime.second).toBe(11);
  41. expect(createdPlainTime.millisecond).toBe(0);
  42. expect(createdPlainTime.microsecond).toBe(0);
  43. expect(createdPlainTime.nanosecond).toBe(0);
  44. });
  45. });
  46. describe("errors", () => {
  47. test("custom time zone doesn't have a getOffsetNanosecondsFor function", () => {
  48. const zonedDateTime = new Temporal.ZonedDateTime(0n, {});
  49. expect(() => {
  50. Temporal.PlainTime.from(zonedDateTime);
  51. }).toThrowWithMessage(TypeError, "getOffsetNanosecondsFor is undefined");
  52. });
  53. test("string must not contain a UTC designator", () => {
  54. expect(() => {
  55. Temporal.PlainTime.from("2021-07-06T23:42:01Z");
  56. }).toThrowWithMessage(
  57. RangeError,
  58. "Invalid time string '2021-07-06T23:42:01Z': must not contain a UTC designator"
  59. );
  60. });
  61. test("extended year must not be negative zero", () => {
  62. expect(() => {
  63. Temporal.PlainTime.from("-000000-01-01T00:00:00");
  64. }).toThrowWithMessage(RangeError, "Invalid time string '-000000-01-01T00:00:00'");
  65. expect(() => {
  66. Temporal.PlainTime.from("−000000-01-01T00:00:00"); // U+2212
  67. }).toThrowWithMessage(RangeError, "Invalid time string '−000000-01-01T00:00:00'");
  68. });
  69. test("ambiguous string must contain a time designator", () => {
  70. const values = [
  71. // YYYY-MM or HHMM-UU
  72. "2021-12",
  73. // MMDD or HHMM
  74. "1214",
  75. "0229",
  76. "1130",
  77. // MM-DD or HH-UU
  78. "12-14",
  79. // YYYYMM or HHMMSS
  80. "202112",
  81. ];
  82. for (const value of values) {
  83. expect(() => {
  84. Temporal.PlainTime.from(value);
  85. }).toThrowWithMessage(RangeError, `Invalid time string '${value}'`);
  86. // Doesn't throw
  87. Temporal.PlainTime.from(`T${value}`);
  88. }
  89. });
  90. });