TimeZone.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. describe("errors", () => {
  2. test("called without new", () => {
  3. expect(() => {
  4. Temporal.TimeZone();
  5. }).toThrowWithMessage(TypeError, "Temporal.TimeZone constructor must be called with 'new'");
  6. });
  7. test("Invalid time zone name", () => {
  8. expect(() => {
  9. new Temporal.TimeZone("foo");
  10. }).toThrowWithMessage(RangeError, "Invalid time zone name 'foo'");
  11. });
  12. test("Invalid numeric UTC offset", () => {
  13. // FIXME: Error message should probably say '...name or UTC offset ...' :^)
  14. expect(() => {
  15. new Temporal.TimeZone("0123456");
  16. }).toThrowWithMessage(RangeError, "Invalid time zone name '0123456'");
  17. expect(() => {
  18. new Temporal.TimeZone("23:59:59.9999999999");
  19. }).toThrowWithMessage(RangeError, "Invalid time zone name '23:59:59.9999999999'");
  20. });
  21. });
  22. describe("normal behavior", () => {
  23. test("length is 1", () => {
  24. expect(Temporal.TimeZone).toHaveLength(1);
  25. });
  26. test("basic functionality", () => {
  27. const timeZone = new Temporal.TimeZone("UTC");
  28. expect(timeZone.id).toBe("UTC");
  29. expect(typeof timeZone).toBe("object");
  30. expect(timeZone).toBeInstanceOf(Temporal.TimeZone);
  31. expect(Object.getPrototypeOf(timeZone)).toBe(Temporal.TimeZone.prototype);
  32. });
  33. test("canonicalizes time zone name", () => {
  34. const values = [
  35. ["UTC", "UTC"],
  36. ["Utc", "UTC"],
  37. ["utc", "UTC"],
  38. ["uTc", "UTC"],
  39. ["GMT", "UTC"],
  40. ["Etc/UTC", "UTC"],
  41. ["Etc/GMT", "UTC"],
  42. ["Etc/GMT+12", "Etc/GMT+12"],
  43. ["Etc/GMT-12", "Etc/GMT-12"],
  44. ["Europe/London", "Europe/London"],
  45. ["Europe/Isle_of_Man", "Europe/London"],
  46. ];
  47. for (const [arg, expected] of values) {
  48. expect(new Temporal.TimeZone(arg).id).toBe(expected);
  49. }
  50. });
  51. test("numeric UTC offset", () => {
  52. const signs = [
  53. ["+", "+"],
  54. ["-", "-"],
  55. ["\u2212", "-"],
  56. ];
  57. const values = [
  58. ["01", "01:00"],
  59. ["0123", "01:23"],
  60. ["012345", "01:23:45"],
  61. ["012345.6", "01:23:45.6"],
  62. ["012345.123", "01:23:45.123"],
  63. ["012345.123456789", "01:23:45.123456789"],
  64. ["012345,6", "01:23:45.6"],
  65. ["012345,123", "01:23:45.123"],
  66. ["012345,123456789", "01:23:45.123456789"],
  67. ["01:23", "01:23"],
  68. ["01:23:45", "01:23:45"],
  69. ["01:23:45.6", "01:23:45.6"],
  70. ["01:23:45.123", "01:23:45.123"],
  71. ["01:23:45.123456789", "01:23:45.123456789"],
  72. ["01:23:45,6", "01:23:45.6"],
  73. ["01:23:45,123", "01:23:45.123"],
  74. ["01:23:45,123456789", "01:23:45.123456789"],
  75. ["23:59:59.999999999", "23:59:59.999999999"],
  76. ];
  77. for (const [sign, expectedSign] of signs) {
  78. for (const [offset, expectedOffset] of values) {
  79. expect(new Temporal.TimeZone(`${sign}${offset}`).id).toBe(
  80. `${expectedSign}${expectedOffset}`
  81. );
  82. }
  83. }
  84. });
  85. });