Skip to content

Commit 4bc2ce4

Browse files
feat: adds tzDate fn, tz format option
1 parent db1cb3c commit 4bc2ce4

36 files changed

Lines changed: 1531 additions & 1338 deletions

src/__tests__/addDay.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { describe, it, expect } from "vitest"
2+
import { addDay } from "../addDay"
3+
process.env.TZ = "America/New_York"
4+
5+
describe("addDay", () => {
6+
it("gets the next day at the beginning of the month", () => {
7+
expect(addDay("2022-01-01").toISOString()).toBe("2022-01-02T05:00:00.000Z")
8+
})
9+
it("gets the next day at the end of the year", () => {
10+
expect(addDay("2022-12-31").toISOString()).toBe("2023-01-01T05:00:00.000Z")
11+
})
12+
})

src/__tests__/addHour.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, it, expect } from "vitest"
2+
import { addHour } from "../addHour"
3+
process.env.TZ = "America/New_York"
4+
5+
describe("addHour", () => {
6+
it("can increment a normal hour", () => {
7+
expect(addHour("2022-01-01T00:00:00Z").toISOString()).toBe(
8+
"2022-01-01T01:00:00.000Z"
9+
)
10+
})
11+
it("can increment the last hours of the day into a new day", () => {
12+
expect(addHour("2022-01-01T23:11:00Z", 3).toISOString()).toBe(
13+
"2022-01-02T02:11:00.000Z"
14+
)
15+
})
16+
})

src/__tests__/addMinute.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, it, expect } from "vitest"
2+
import { addMinute } from "../addMinute"
3+
process.env.TZ = "America/New_York"
4+
5+
describe("addMinute", () => {
6+
it("can increment a normal hour", () => {
7+
expect(addMinute("2022-01-01T00:00:00Z").toISOString()).toBe(
8+
"2022-01-01T00:01:00.000Z"
9+
)
10+
})
11+
it("can increment the last hours of the day into a new day", () => {
12+
expect(addMinute("2022-01-01T23:11:00Z", 181).toISOString()).toBe(
13+
"2022-01-02T02:12:00.000Z"
14+
)
15+
})
16+
})

src/__tests__/addMonth.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, it, expect } from "vitest"
2+
import { addMonth } from "../addMonth"
3+
process.env.TZ = "America/New_York"
4+
5+
describe("addMonth", () => {
6+
it("gets the next month on the first", () => {
7+
expect(addMonth("2022-01-01").toISOString()).toBe(
8+
"2022-02-01T05:00:00.000Z"
9+
)
10+
})
11+
it("can overflow a month month when the next month has fewer days", () => {
12+
expect(addMonth("2000-01-31", 1, true).toISOString()).toBe(
13+
"2000-03-02T05:00:00.000Z"
14+
)
15+
})
16+
it("goe to the same day of the month on the next month", () => {
17+
expect(addMonth("2000-06-04").toISOString()).toBe(
18+
"2000-07-04T04:00:00.000Z"
19+
)
20+
})
21+
22+
it("can add multiple months by passing a second argument", () => {
23+
expect(addMonth("2000-01-01", 2).toISOString()).toBe(
24+
"2000-03-01T05:00:00.000Z"
25+
)
26+
})
27+
28+
it("can add years months by passing a second argument", () => {
29+
expect(addMonth("2000-01-01", 25).toISOString()).toBe(
30+
"2002-02-01T05:00:00.000Z"
31+
)
32+
})
33+
it("can prevent month overflow with third argument", () => {
34+
expect(addMonth("2020-01-31", 1, false).toISOString()).toBe(
35+
"2020-02-29T05:00:00.000Z"
36+
)
37+
})
38+
})

src/__tests__/addSecond.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, it, expect } from "vitest"
2+
import { addSecond } from "../addSecond"
3+
process.env.TZ = "America/New_York"
4+
5+
describe("addSecond", () => {
6+
it("can increment a normal hour", () => {
7+
expect(addSecond("2022-01-01T00:00:00Z").toISOString()).toBe(
8+
"2022-01-01T00:00:01.000Z"
9+
)
10+
})
11+
it("can increment the last hours of the day into a new day", () => {
12+
expect(addSecond("2022-01-01T23:11:00Z", 3600 * 3 + 1).toISOString()).toBe(
13+
"2022-01-02T02:11:01.000Z"
14+
)
15+
})
16+
})

src/__tests__/addYear.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, it, expect } from "vitest"
2+
import { addYear } from "../addYear"
3+
process.env.TZ = "America/New_York"
4+
5+
describe("addYear", () => {
6+
it("can add a year to a Date object by default", () => {
7+
const d = new Date("2000-12-17T12:00:00")
8+
const h: number = d.getTimezoneOffset() / 60
9+
expect(addYear(d).toISOString()).toBe(`2001-12-17T${12 + h}:00:00.000Z`)
10+
})
11+
it("can subtract a year to a Date", () => {
12+
const d = new Date("2000-12-17T12:00:00")
13+
const h: number = d.getTimezoneOffset() / 60
14+
expect(addYear(d, -1).toISOString()).toBe(`1999-12-17T${12 + h}:00:00.000Z`)
15+
})
16+
it("can overflow the day of the month on leap year", () => {
17+
expect(addYear("2000-02-29").toISOString()).toBe("2001-02-28T05:00:00.000Z")
18+
})
19+
})

src/__tests__/applyOffset.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, it, expect } from "vitest"
2+
import { applyOffset } from "../applyOffset"
3+
process.env.TZ = "America/New_York"
4+
5+
describe("applyOffset", () => {
6+
it("can apply a negative offset to a date", () => {
7+
expect(applyOffset("2023-02-22T00:00:00Z", "-0500").toISOString()).toBe(
8+
"2023-02-21T19:00:00.000Z"
9+
)
10+
})
11+
12+
it("can apply a positive offset to a date", () => {
13+
expect(applyOffset("2023-04-13T10:15:00", "+0200").toISOString()).toBe(
14+
"2023-04-13T16:15:00.000Z"
15+
)
16+
})
17+
})

src/__tests__/date.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, it, expect } from "vitest"
2+
import { date } from "../date"
3+
4+
process.env.TZ = "America/New_York"
5+
describe("date", () => {
6+
it("qualifies and re-timezones a date", () => {
7+
expect(date("2022-01-22 00:00:00").toISOString()).toBe(
8+
"2022-01-22T05:00:00.000Z"
9+
)
10+
})
11+
it("accepts a time with a timezone offset", () => {
12+
expect(date("2022-01-22T00:00-0300").toISOString()).toBe(
13+
"2022-01-22T03:00:00.000Z"
14+
)
15+
})
16+
})

src/__tests__/dayEnd.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { describe, it, expect } from "vitest"
2+
import { dayEnd } from "../dayEnd"
3+
process.env.TZ = "America/New_York"
4+
5+
describe("dayEnd", () => {
6+
it("can become the start of the day", () => {
7+
expect(dayEnd("2023-02-22T12:00:00Z").toISOString()).toBe(
8+
"2023-02-23T04:59:59.999Z"
9+
)
10+
})
11+
})

src/__tests__/dayOfYear.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { describe, it, expect } from "vitest"
2+
import { dayOfYear } from "../dayOfYear"
3+
process.env.TZ = "America/New_York"
4+
5+
describe("dayOfYear", () => {
6+
it("can find the number of days in a year", () => {
7+
expect(dayOfYear("2023-08-01")).toBe(213)
8+
})
9+
it("can find the number of days in a year", () => {
10+
expect(dayOfYear("2020-08-01")).toBe(214)
11+
})
12+
})

0 commit comments

Comments
 (0)