-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: existing "Z" token is now "ZZ" adds "Z" token style (#52)
* Support for the timezone token "ZZ" and changing the timezone format style. * Fix timezone offset parsing and applyOffset function
- Loading branch information
1 parent
5874536
commit d42a114
Showing
13 changed files
with
191 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { describe, expect, it } from "vitest" | ||
import { getOffsetFormat } from "../common" | ||
|
||
describe("getOffsetFormat", () => { | ||
it("should return 'Z' for format 'YYYY-MM-DDTHH:mm:ssZ'", () => { | ||
expect(getOffsetFormat("YYYY-MM-DDTHH:mm:ssZ")).toBe("Z") | ||
}) | ||
|
||
it("should return 'ZZ' for format 'YYYY-MM-DDTHH:mm:ssZZ'", () => { | ||
expect(getOffsetFormat("YYYY-MM-DDTHH:mm:ssZZ")).toBe("ZZ") | ||
}) | ||
|
||
it("should return 'Z' for formats 'full', 'long', 'medium', and 'short'", () => { | ||
expect(getOffsetFormat("full")).toBe("Z") | ||
expect(getOffsetFormat("long")).toBe("Z") | ||
expect(getOffsetFormat("medium")).toBe("Z") | ||
expect(getOffsetFormat("short")).toBe("Z") | ||
}) | ||
|
||
it("should return 'Z' for formats { date: 'full', time: 'full' }, { date: 'full' }, and { time: 'full' }", () => { | ||
expect(getOffsetFormat({ date: "full", time: "full" })).toBe("Z") | ||
expect(getOffsetFormat({ date: "full" })).toBe("Z") | ||
expect(getOffsetFormat({ time: "full" })).toBe("Z") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
import { date } from "./date" | ||
import { offsetToMins } from "./common" | ||
import { TimezoneToken, fixedLengthByOffset, offsetToMins } from "./common" | ||
import type { DateInput } from "./types" | ||
|
||
/** | ||
* Apply a given offset to a date, returning a new date with the offset | ||
* applied by adding or subtracting the given number of minutes. | ||
* @param dateInput - The date to apply the offset to. | ||
* @param offset - The offset to apply in the +-HHmm format. | ||
* @param offset - The offset to apply in the +-HHmm or +-HH:mm format. | ||
*/ | ||
export function applyOffset(dateInput: DateInput, offset = "+0000"): Date { | ||
export function applyOffset(dateInput: DateInput, offset = "+00:00"): Date { | ||
const d = date(dateInput) | ||
const timeDiffInMins = offsetToMins(offset) | ||
const token = ((): TimezoneToken => { | ||
switch (fixedLengthByOffset(offset)) { | ||
case 5: | ||
return "ZZ" | ||
case 6: | ||
return "Z" | ||
} | ||
})() | ||
const timeDiffInMins = offsetToMins(offset, token) | ||
return new Date(d.getTime() + timeDiffInMins * 1000 * 60) | ||
} |
Oops, something went wrong.