Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/stdlib/src/time-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/**
* Converts a start-end time string to a number of minutes since midnight.
* If an end time doesn't specify "p" or "pm", we assume it ends in "am" based on WebSoc logic
* @example parseStartAndEndTimes("11:00-11:50"); // { startTime: 660, endTime: 710 }
* @example parseStartAndEndTimes("12:00-12:50p"); // { startTime: 720, endTime: 770 }
* @example parseStartAndEndTimes("11:00-2:50p"); // { startTime: 660, endTime: 890 }
*
* @example parseStartAndEndTimes("11:00-11:50a"); // { startTime: 660, endTime: 710 }
* If a time passes through midnight, we return number of minutes passed since the midnight of the first day.
* @example parseStartAndEndTimes("11:00-1:50"); // { startTime: 1380, endTime: 1550 }
*
* @param time start-end time string
* @returns startTime and endTime in minutes since midnight
Expand All @@ -18,9 +23,13 @@ export const parseStartAndEndTimes = (time: string) => {
const [endTimeHour, endTimeMinute] = endTimeString.split(":");
endTime = (Number.parseInt(endTimeHour, 10) % 12) * 60 + Number.parseInt(endTimeMinute, 10);
if (endTimeMinute.includes("p")) {
startTime += 12 * 60;
// Ending in PM: if start < end (both PM) shift both by 12 hours, otherwise just shift end
if (startTime < endTime) startTime += 12 * 60;
endTime += 12 * 60;
} else if (startTime > endTime) {
// Ending in AM: if start > end, start is PM and end is next day, so shift times accordingly
startTime += 12 * 60;
endTime += 24 * 60;
}
if (startTime > endTime) startTime -= 12 * 60;
return { startTime, endTime };
};
Loading