-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
133 additions
and
7 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,57 @@ | ||
import { describe, it, expect, beforeAll, afterAll } from "vitest"; | ||
|
||
import { formatHourDisplay } from "./formatHourDisplay"; | ||
|
||
const originalTimezone = process.env.TZ; | ||
|
||
// Test cases in different timezones | ||
const timezones = ["UTC", "America/New_York", "Asia/Tokyo", "Europe/London"]; | ||
|
||
describe("Time formatting functions", () => { | ||
describe("formatHourDisplay", () => { | ||
for (const timezone of timezones) { | ||
describe(`in ${timezone} timezone`, () => { | ||
beforeAll(() => { | ||
process.env.TZ = timezone; | ||
}); | ||
|
||
afterAll(() => { | ||
process.env.TZ = originalTimezone; | ||
}); | ||
|
||
it("should format midnight UTC correctly", () => { | ||
const result = formatHourDisplay("2024-01-01", "00"); | ||
expect(result).toMatch(/^\d{2}:00$/); | ||
}); | ||
|
||
it("should format noon UTC correctly", () => { | ||
const result = formatHourDisplay("2024-01-01", "12"); | ||
expect(result).toMatch(/^\d{2}:00$/); | ||
}); | ||
|
||
it("should format evening UTC correctly", () => { | ||
const result = formatHourDisplay("2024-01-01", "20"); | ||
expect(result).toMatch(/^\d{2}:00$/); | ||
}); | ||
|
||
it("should handle single-digit hours correctly", () => { | ||
const result = formatHourDisplay("2024-01-01", "05"); | ||
expect(result).toMatch(/^\d{2}:00$/); | ||
}); | ||
|
||
it("should handle date transitions correctly", () => { | ||
const result = formatHourDisplay("2024-01-01", "23"); | ||
expect(result).toMatch(/^\d{2}:00$/); | ||
}); | ||
}); | ||
} | ||
|
||
it("should handle invalid date strings gracefully", () => { | ||
expect(() => formatHourDisplay("invalid-date", "12")).toThrow(); | ||
}); | ||
|
||
it("should handle invalid hour strings gracefully", () => { | ||
expect(() => formatHourDisplay("2024-01-01", "25")).toThrow(); | ||
}); | ||
}); | ||
}); |
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,8 @@ | ||
import { format } from "date-fns"; | ||
|
||
export const formatHourDisplay = (dateStr: string, hourStr: string) => { | ||
const fullDateTime = `${dateStr}T${hourStr}:00:00Z`; | ||
const date = new Date(fullDateTime); | ||
|
||
return format(date, "HH:00"); | ||
}; |
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,60 @@ | ||
import { afterAll, beforeAll, describe, expect, it } from "vitest"; | ||
|
||
import { formatTime } from "./formatTime"; | ||
|
||
const originalTimezone = process.env.TZ; | ||
const timezones = ["UTC", "America/New_York", "Asia/Tokyo", "Europe/London"]; | ||
|
||
describe("formatTime", () => { | ||
for (const timezone of timezones) { | ||
describe(`in ${timezone} timezone`, () => { | ||
beforeAll(() => { | ||
process.env.TZ = timezone; | ||
}); | ||
|
||
afterAll(() => { | ||
process.env.TZ = originalTimezone; | ||
}); | ||
|
||
it("should format midnight correctly", () => { | ||
const midnight = new Date("2024-01-01T00:00:00Z").getTime(); | ||
const result = formatTime(midnight); | ||
expect(result).toMatch(/^\d{2}:\d{2}:\d{2}$/); | ||
}); | ||
|
||
it("should format noon correctly", () => { | ||
const noon = new Date("2024-01-01T12:00:00Z").getTime(); | ||
const result = formatTime(noon); | ||
expect(result).toMatch(/^\d{2}:\d{2}:\d{2}$/); | ||
}); | ||
|
||
it("should format time with seconds correctly", () => { | ||
const timeWithSeconds = new Date("2024-01-01T12:34:56Z").getTime(); | ||
const result = formatTime(timeWithSeconds); | ||
expect(result).toMatch(/^\d{2}:\d{2}:\d{2}$/); | ||
}); | ||
|
||
it("should handle millisecond precision", () => { | ||
const timeWithMs = new Date("2024-01-01T12:34:56.789Z").getTime(); | ||
const result = formatTime(timeWithMs); | ||
expect(result).toMatch(/^\d{2}:\d{2}:\d{2}$/); | ||
}); | ||
}); | ||
} | ||
|
||
it("should handle invalid timestamps gracefully", () => { | ||
expect(() => formatTime(NaN)).toThrow(); | ||
}); | ||
|
||
it("should handle very large timestamps", () => { | ||
const farFuture = new Date("2100-01-01T00:00:00Z").getTime(); | ||
const result = formatTime(farFuture); | ||
expect(result).toMatch(/^\d{2}:\d{2}:\d{2}$/); | ||
}); | ||
|
||
it("should handle very small timestamps", () => { | ||
const farPast = new Date("1900-01-01T00:00:00Z").getTime(); | ||
const result = formatTime(farPast); | ||
expect(result).toMatch(/^\d{2}:\d{2}:\d{2}$/); | ||
}); | ||
}); |
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,7 @@ | ||
import { format } from "date-fns"; | ||
|
||
export const formatTime = (timestamp: number) => { | ||
const date = new Date(timestamp); | ||
|
||
return format(date, "HH:mm:ss"); | ||
}; |