Skip to content

Commit

Permalink
parser: add more tolerance for errors in date format
Browse files Browse the repository at this point in the history
  • Loading branch information
SantriptaSharma committed Oct 26, 2023
1 parent d5615af commit 5429ffe
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/utils/parse-dining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,26 @@ async function ParseXlsx(path: string, year: number): Promise<MessMenu[]>
Dinner: menu.getRows(38, 11).map(row => row.values) as xl.CellValue[][]
}

const firstDate = menu.getCell(2, 2).value as string;
const startString = firstDate.trim().replace(/(\d)((rd)|(st)|(th)|(nd))/g, "$1") + ` ${year}`;
const startTimestamp = Date.parse(startString);
let startTimestamp;

let dateCell = menu.getCell(2, 2);

if (typeof dateCell.value == "object") {
startTimestamp = (dateCell.value as Date).getTime();
} else {
let firstDate = menu.getCell(2, 2).value as string;
firstDate = firstDate.trim().replace(/(\d)((rd)|(st)|(th)|(nd))/g, "$1");
firstDate = /(\d{1,2})-?([A-Za-z]+)/.exec(firstDate).slice(1, 3).join("-") + ` ${year}`;
startTimestamp = Date.parse(firstDate);
}

if (Number.isNaN(startTimestamp))
{
throw new Error("Invalid date");
}

const startDate = new Date(startTimestamp);
startDate.setFullYear(new Date().getFullYear());
console.log(startDate.toDateString());
const endDate = new Date(startDate);
endDate.setDate(startDate.getDate() + 6)

Expand Down

0 comments on commit 5429ffe

Please sign in to comment.