summaryrefslogtreecommitdiff
path: root/src/utils/datetime.test.ts
blob: 5f0749dcfcef82a6e3e6f6e595bf6d34c7b2fd87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { assertEquals, assertMatch } from "@std/assert";
import { describe, it } from "@std/testing/bdd";
import { toIso8601Full, toIso8601FullUTC } from "./datetime.ts";

describe("toIso8601Full", () => {
  it("formats current local time with offset", () => {
    const date = new Date();
    const result = toIso8601Full(date);

    assertMatch(
      result,
      /^[+-]\d{6}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}(Z|[+-]\d{2}:\d{2})$/,
    );
  });

  it("handles dates before year 0 (BC)", () => {
    const date = new Date(-2000, 0, 1, 0, 0, 0, 0);
    const result = toIso8601Full(date);

    assertMatch(result, /^-\d{6}-01-01T00:00:00\.000(Z|[+-]\d{2}:\d{2})$/);
  });

  it("pads components correctly", () => {
    const date = new Date(7, 0, 2, 3, 4, 5, 6);
    const result = toIso8601Full(date);

    assertMatch(result, /^\+001907-01-02T03:04:05\.006(Z|[+-]\d{2}:\d{2})$/);
  });

  it("handles positive and negative timezone offsets", () => {
    const date = new Date("2025-06-17T12:00:00Z");
    const result = toIso8601Full(date);

    assertMatch(
      result,
      /^[+-]\d{6}-06-17T\d{2}:\d{2}:\d{2}\.\d{3}(Z|[+-]\d{2}:\d{2})$/,
    );
  });
});

describe("toIso8601FullUTC", () => {
  it("always formats in UTC with 'Z'", () => {
    const date = new Date(Date.UTC(2025, 11, 31, 23, 59, 59, 999));
    const result = toIso8601FullUTC(date);

    assertEquals(result, "+002025-12-31T23:59:59.999Z");
  });

  it("pads milliseconds and components correctly", () => {
    const date = new Date(Date.UTC(7, 0, 2, 3, 4, 5, 6));
    const result = toIso8601FullUTC(date);

    assertEquals(result, "+001907-01-02T03:04:05.006Z");
  });

  it("handles BC dates (negative years)", () => {
    const date = new Date(Date.UTC(-44, 2, 15, 12, 0, 0, 0));
    const result = toIso8601FullUTC(date);

    assertMatch(result, /^-\d{6}-03-15T12:00:00\.000Z$/);
  });
});