Skip to content

Commit 42e7c4d

Browse files
committed
Feat/display duration in log table
1 parent 5c969a5 commit 42e7c4d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/components/record/LogTable.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ async function LogTable({ logs }: Props) {
1818
<th>Rating</th>
1919
<th className="hidden sm:table-cell">elapsed</th>
2020
<th className="hidden sm:table-cell">scheduled</th>
21+
<th className="hidden sm:table-cell">duration</th>
2122
</tr>
2223
</thead>
2324
<tbody>
@@ -29,6 +30,7 @@ async function LogTable({ logs }: Props) {
2930
<td>{log.grade}</td>
3031
<td className="hidden sm:table-cell">{log.elapsed_days}</td>
3132
<td className="hidden sm:table-cell">{log.scheduled_days}</td>
33+
<td className="hidden sm:table-cell">{log.duration > 0 ? durationFormat(log.duration) : '/'}</td>
3234
</tr>
3335
))}
3436
</tbody>
@@ -38,3 +40,30 @@ async function LogTable({ logs }: Props) {
3840
}
3941

4042
export default LogTable;
43+
44+
45+
function durationFormat(duration: number) {
46+
const division = [60, 60, 24]
47+
const char = ['s', 'm', 'h', 'd']
48+
if (duration <= 0) {
49+
return '/';
50+
}
51+
const split_duration = [];
52+
for (let c of division) {
53+
split_duration.push(duration % c);
54+
duration = Math.floor(duration / c);
55+
if (duration === 0) {
56+
break;
57+
}
58+
}
59+
if (duration > 0) {
60+
split_duration.push(duration);
61+
}
62+
const res = [];
63+
for (let i = split_duration.length - 1; i >= 0; i--) {
64+
if (split_duration[i] > 0) {
65+
res.push(`${split_duration[i]}${char[i]}`)
66+
}
67+
}
68+
return res.join('');
69+
}

0 commit comments

Comments
 (0)