Skip to content

Fix Date Parser Accidentally Parsing Two-Digit Number as Year #4242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
37 changes: 36 additions & 1 deletion src/app/features/tasks/short-syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,35 @@ const CH_DUE = '@';
const ALL_SPECIAL = `(\\${CH_PRO}|\\${CH_TAG}|\\${CH_DUE})`;

const customDateParser = casual.clone();
customDateParser.refiners.push({
refine: (context, results) => {
results.forEach((result) => {
const { refDate, text, start } = result;
const regex =
/[0-9]{1,2}[\/\-\.][0-9]{1,2}( (1[0-9]|0?[1-9]|2[0-3])(:[0-5][0-9])?([AaPp][Mm])?)? ([0-9]{2})/;
const matched = text.match(regex);
// Match 14/5 90, 14.5 90, 14-5 90, 14/5 2:30pm 90,
// 14/4 23:00 90
if (matched) {
if (matched[matched.length - 1]) {
const twoDigits = matched[matched.length - 1];
result.text = text.replace(twoDigits, '');
Copy link
Preview

Copilot AI Apr 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing the two-digit year indiscriminately may remove matching digits elsewhere in the string. Consider targeting the exact location of the match or using a more precise replacement strategy.

Suggested change
result.text = text.replace(twoDigits, '');
const startIndex = matched.index + matched[0].lastIndexOf(twoDigits);
const endIndex = startIndex + twoDigits.length;
result.text = text.slice(0, startIndex) + text.slice(endIndex);

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

}
const current = new Date();
let year = current.getFullYear();
// If the parsed month is smaller than the current month,
// it means the time is for next year. For example, parsed month is March
// and it is currently April
const impliedMonth = start.get('month');
if (impliedMonth && impliedMonth < refDate.getMonth() + 1) {
year += 1;
}
result.start.assign('year', year);
}
});
return results;
},
});

const SHORT_SYNTAX_PROJECT_REG_EX = new RegExp(`\\${CH_PRO}[^${ALL_SPECIAL}]+`, 'gi');
const SHORT_SYNTAX_TAGS_REG_EX = new RegExp(`\\${CH_TAG}[^${ALL_SPECIAL}|\\s]+`, 'gi');
Expand Down Expand Up @@ -285,7 +314,13 @@ const parseScheduledDate = (task: Partial<TaskCopy>, now: Date): DueChanges => {
if (!start.isCertain('hour')) {
hasPlannedTime = false;
}
const inputDate = parsedDateResult.text;
let inputDate = parsedDateResult.text;
// Hacky way to strip short syntax for time estimate that was
// accidentally included in the date parser
// For example: the task is "Task @14/4 90m" and we don't want "90m"
if (inputDate.match(/ [0-9]{1,}m/g)) {
inputDate += 'm';
}
return {
plannedAt,
// Strip out the short syntax for scheduled date and given date
Expand Down
Loading