Skip to content

Commit

Permalink
refactor relativeTime function
Browse files Browse the repository at this point in the history
  • Loading branch information
xkeels committed Apr 3, 2024
1 parent f9db2f4 commit 66f0cba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 48 deletions.
23 changes: 23 additions & 0 deletions src/lib/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,26 @@ export function humanReadableDateOnly(d: Date): string {

return new Intl.DateTimeFormat("en-US", options).format(d);
}

export function relativeTime(fromDate: Date, toDate: Date = new Date()) {
const units = [
{ max: 1000 * 60, name: "second", divisor: 1000 },
{ max: 1000 * 60 * 60, name: "minute", divisor: 1000 * 60 },
{ max: 1000 * 60 * 60 * 24, name: "hour", divisor: 1000 * 60 * 60 },
{
max: 1000 * 60 * 60 * 24 * 30,
name: "day",
divisor: 1000 * 60 * 60 * 24,
},
];

const millis = fromDate.getTime() - toDate.getTime();

const unit = units.find((u) => Math.abs(millis) < u.max);
if (!unit) return "Unknown";

const difference = Math.round(millis / unit.divisor);

const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
return rtf.format(difference, unit.name as any);
}
30 changes: 5 additions & 25 deletions src/routes/[id]/training/queues/[queueId]/data-table.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,10 @@
import { addPagination } from "svelte-headless-table/plugins";
import { Button } from "$lib/components/ui/button";
import type { TrainingQueueMembership } from "@prisma/client";
import { relativeTime } from "$lib/date";
export let members: TrainingQueueMembership[];
const timeString = (joinedAt: Date): string => {
const now = new Date();
const diff = now.getTime() - joinedAt.getTime();
const sec = Math.floor(diff / 1000);
const min = Math.floor(sec / 60);
const hr = Math.floor(min / 60);
const day = Math.floor(hr / 24);
if (day > 0) {
return `${day} day${day > 1 ? "s" : ""}`;
} else if (hr > 0) {
return `${hr} hour${hr > 1 ? "s" : ""}`;
} else if (min > 0) {
return `${min} minute${min > 1 ? "s" : ""}`;
} else {
return `${sec} second${sec > 1 ? "s" : ""}`;
}
};
const table = createTable(readable(members), {
page: addPagination(),
});
Expand All @@ -50,13 +30,13 @@
}),
table.column({
accessor: (member: TrainingQueueMembership) =>
member.joinedAt.toDateString(),
header: "Joined at",
relativeTime(member.joinedAt),
header: "Joined queue",
}),
table.column({
accessor: (member: TrainingQueueMembership) =>
timeString(member.joinedAt),
header: "Waiting time",
member.joinedAt.toDateString(),
header: "Join date",
}),
table.column({
accessor: (member: any) => member.user.id,
Expand Down
24 changes: 1 addition & 23 deletions src/routes/admin/connections/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as Table from "$lib/components/ui/table";
import type { PageData } from "./$types";
import type { Connection, User } from "@prisma/client";
import { relativeTime } from "$lib/date";
export let data: PageData;
Expand Down Expand Up @@ -42,29 +43,6 @@
const { headerRows, pageRows, tableAttrs, tableBodyAttrs } =
table.createViewModel(columns);
function relativeTime(fromDate: Date, toDate: Date = new Date()) {
const units = [
{ max: 1000 * 60, name: "second", divisor: 1000 },
{ max: 1000 * 60 * 60, name: "minute", divisor: 1000 * 60 },
{ max: 1000 * 60 * 60 * 24, name: "hour", divisor: 1000 * 60 * 60 },
{
max: 1000 * 60 * 60 * 24 * 30,
name: "day",
divisor: 1000 * 60 * 60 * 24,
},
];
const millis = fromDate.getTime() - toDate.getTime();
const unit = units.find((u) => Math.abs(millis) < u.max);
if (!unit) return "Unknown";
const difference = Math.round(millis / unit.divisor);
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
return rtf.format(difference, unit.name as any);
}
</script>

<div class="flex items-center justify-between">
Expand Down

0 comments on commit 66f0cba

Please sign in to comment.