Skip to content

Commit 6aaab03

Browse files
committed
Fixed timezone issue and also added unit tests to verify
1 parent 099d314 commit 6aaab03

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { convertTimestampToLocaleTime } from "../src/utils/convert-timestamp-to-locale-time";
2+
import { describe, expect, it, test } from "@jest/globals";
3+
4+
describe("convertTimestampToLocaleTime", () => {
5+
test("should convert a valid timestamp string to a locale time string", () => {
6+
const timestamp = "2025-03-28T12:00:00Z";
7+
const result = convertTimestampToLocaleTime(timestamp);
8+
expect(result).toBe("3/28/2025, 8:00:00 AM");
9+
expect(result.length).toBeGreaterThan(0); // Ensures it returns a non-empty string
10+
});
11+
12+
test("should convert a valid numeric timestamp to a locale time string", () => {
13+
const timestamp = 1711622400000; // Equivalent to 2025-03-28T12:00:00Z in milliseconds
14+
const result = convertTimestampToLocaleTime(timestamp);
15+
expect(result).toBe("3/28/2024, 6:40:00 AM");
16+
expect(result.length).toBeGreaterThan(0);
17+
});
18+
19+
test("convert to locale time date is different", () => {
20+
const timestamp = "2025-03-28 02:33:02.589+00";
21+
const result = convertTimestampToLocaleTime(timestamp);
22+
expect(result).toBe("3/27/2025, 10:33:02 PM");
23+
expect(result.length).toBeGreaterThan(0);
24+
});
25+
26+
test("should return 'Invalid Date' for an invalid timestamp", () => {
27+
const timestamp = "invalid";
28+
const result = convertTimestampToLocaleTime(timestamp);
29+
expect(result).toBe("Invalid Date");
30+
});
31+
});

course-matrix/frontend/src/pages/Home/TimetableCard.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from "@/api/timetableApiSlice";
1717
import { Link } from "react-router-dom";
1818
import { TimetableModel } from "@/models/models";
19+
import {convertTimestampToLocaleTime} from "../../utils/convert-timestamp-to-locale-time";
1920

2021
interface TimetableCardProps {
2122
refetch: () => void;
@@ -43,21 +44,14 @@ const TimetableCard = ({
4344
}: TimetableCardProps) => {
4445
const [updateTimetable] = useUpdateTimetableMutation();
4546

46-
const lastEditedDateArray = lastEditedDate
47-
.toISOString()
48-
.split("T")[0]
49-
.split("-");
50-
const lastEditedYear = lastEditedDateArray[0];
51-
const lastEditedMonth = lastEditedDateArray[1];
52-
const lastEditedDay = lastEditedDateArray[2];
53-
const lastEditedDateTimestamp =
54-
lastEditedMonth + "/" + lastEditedDay + "/" + lastEditedYear;
5547

5648
const [timetableCardTitle, setTimetableCardTitle] = useState(title);
5749
const [isEditingTitle, setIsEditingTitle] = useState(false);
5850
const { data } = useGetTimetableQuery(timetableId);
5951
const [toggled, setToggled] = useState(favorite);
6052

53+
54+
6155
const handleSave = async () => {
6256
try {
6357
await updateTimetable({
@@ -161,7 +155,7 @@ const TimetableCard = ({
161155
</CardHeader>
162156
<CardContent className="-mt-3">
163157
<CardDescription className="flex justify-between text-xs">
164-
<div>Last edited {lastEditedDateTimestamp}</div>
158+
<div>Last edited {convertTimestampToLocaleTime(lastEditedDate.toISOString()).split(",")[0]}</div>
165159
<div>Owned by: {owner}</div>
166160
</CardDescription>
167161
</CardContent>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
export function convertTimestampToLocaleTime(timestampz: string | number): string {
4+
const date = new Date(timestampz);
5+
return date.toLocaleString(); // Uses system's default locale
6+
}

0 commit comments

Comments
 (0)