Skip to content

Commit

Permalink
fix(dse): closest paper and duration ago
Browse files Browse the repository at this point in the history
  • Loading branch information
sdip15fa committed Apr 18, 2024
1 parent c4840cc commit a62e6b0
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions commands/dse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,18 @@ const timetable: { [key: string]: string } = {

// Function to calculate duration before the start of an examination paper
function timeUntilExam(paper: string) {
// If no paper is specified, find the paper with the earliest starting time

// If no paper is specified, find the paper with the closest starting time
if (!paper?.trim()) {
paper = "Chinese 1";
let earliestTime = Infinity;
let earliestPaper = "";
for (const [key, value] of Object.entries(timetable)) {
const startTime = new Date(value);
if (startTime < new Date() && startTime.getTime() < earliestTime) {
earliestTime = startTime.getTime();
earliestPaper = key;
}
}
paper = earliestPaper;
}

paper = paper
Expand All @@ -80,18 +88,24 @@ function timeUntilExam(paper: string) {
if (!match) {
return null;
}

// Calculate the duration until the start of the examination paper
const currentTime = new Date();
const startTime = new Date(timetable[match]);
const duration = startTime.getTime() - currentTime.getTime();
const isPast = duration < 0;

// Format the duration using humanize-duration
const formattedDuration = humanizeDuration(duration, {
const formattedDuration = humanizeDuration(Math.abs(duration), {
units: ["d", "h", "m"],
round: true,
});

return `Time until HKDSE *${paper}* examination: ${formattedDuration}`;
if (isPast) {
return `HKDSE *${paper}* examination was: ${formattedDuration} ago`;
} else {
return `Time until HKDSE *${paper}* examination: ${formattedDuration}`;
}
}

const execute = async (client: Client, msg: Message, args: string[]) => {
Expand All @@ -115,7 +129,7 @@ const command: Command = {
isDependent: false,
help: `Command: !dse [paper]
Not supplying the paper will give the earliest paper.
Not supplying the paper will give the closest paper in time.
*Available papers:*
${Object.keys(timetable).join("\n")}`,
Expand Down

0 comments on commit a62e6b0

Please sign in to comment.