Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fixed constant CPU use and typing lag in workspaces with many Markdown notes. Dependency warnings now reuse the notes already loaded for the sidebars and rescan only edited files; reopening every note after each edit made VS Code close and reopen them in a loop. Unrelated setting changes no longer reload every task file, sidebar and badge refreshes reuse unchanged scans, and the new `todo.refreshDelay` setting controls how long these refreshes wait after an edit. [#306](https://github.com/vycdev/vscode-todo-plus-two/issues/306)
- Encoded terminal periods in generated relative file links so filenames ending in a period survive link parsing and archive moves.
- Kept literal `@due(...)` examples inside other tag arguments out of the Due sidebar and due-date colors in Todo files; a real due tag later on the same task still applies. Due examples in inline code are no longer colored either.
- Rejected impossible calendar dates in time estimates written with a timezone or fractional seconds, and in `@due` timestamps with fractional seconds, instead of silently rolling them into another month.

### Version 5.4.0

Expand Down
11 changes: 11 additions & 0 deletions src/utils/due.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ const Due = {
'MM/DD/YY',
];

// The formats have no fractional seconds. Validate the rest of the timestamp the
// same way rather than letting natural-date parsing roll impossible days over.
const fraction =
/^(\d{4}[-/]\d{1,2}[-/]\d{1,2}[ T]\d{1,2}:\d{2}:\d{2})\.(\d+)((?:[Zz]|[+-]\d{2}(?::?\d{2})?)?)$/.exec(
value
);
if (fraction) {
const date = Due.parse(fraction[1] + fraction[3]);
return date && new Date(date.getTime() + Math.floor(Number(`0.${fraction[2]}`) * 1000));
}

const parsed = moment(value, formats, true);

if (parsed.isValid()) {
Expand Down
6 changes: 4 additions & 2 deletions src/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,11 @@ const Time = {
if (/^\s*\d+\s*$/.test(to)) return 0;

// Sugar normalizes impossible calendar dates (e.g. February 30) into another
// month; reject those explicitly rather than silently changing a deadline.
// month, even for zoned timestamps; reject those explicitly.
const calendar =
/^(\d{4})([-/])(\d{1,2})\2(\d{1,2})(?:[ T](\d{1,2}):(\d{2})(?::(\d{2}))?)?$/.exec(to);
/^(\d{4})([-/])(\d{1,2})\2(\d{1,2})(?:[ T](\d{1,2}):(\d{2})(?::(\d{2})(?:\.\d+)?)?(?:[Zz]|[+-]\d{2}(?::?\d{2})?)?)?$/.exec(
to
);
if (
calendar &&
!moment([
Expand Down
21 changes: 21 additions & 0 deletions test/due.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ describe('Due utilities', () => {
}
});

it('validates timestamps with fractional seconds like other timestamps', () => {
for (const value of [
'2021-02-29T09:00:00.000Z',
'2021-02-30 09:00:00.5',
'2021-04-31T09:00:00.123+02:00',
'2021-02-28T25:00:00.000Z',
'2021-02-28T09:00:00.000+24:00',
]) {
expect(Due.parse(value)).to.equal(undefined, value);
}
for (const [value, expected] of [
['2021-02-28T09:00:00.000Z', '2021-02-28T09:00:00.000Z'],
['2021-02-28T09:00:00.5Z', '2021-02-28T09:00:00.500Z'],
['2021-02-28T09:00:00.123456z', '2021-02-28T09:00:00.123Z'],
['2021-02-28T09:00:00.123+0200', '2021-02-28T07:00:00.123Z'],
['2024/2/29 9:00:00.250-03', '2024-02-29T12:00:00.250Z'],
]) {
expect(Due.parse(value)?.toISOString()).to.equal(expected, value);
}
});

it('still accepts unzoned dates near the end of a month', () => {
for (const day of [24, 29, 30, 31]) {
expect(Due.parse(`2026-07-${day}`)?.getDate()).to.equal(day);
Expand Down
14 changes: 14 additions & 0 deletions test/time.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ describe('Time utilities', () => {
expect(Time.durationSeconds('2020/02/29', from)).to.equal(59 * 24 * 3600);
});

it('rejects impossible zoned calendar estimates instead of rolling them into March', () => {
const from = new Date('2020-01-01T00:00:00Z');

expect(Time.durationSeconds('2020-02-30T09:00:00Z', from)).to.equal(0);
expect(Time.diffSeconds('2021-02-29T09:00:00+02:00', from)).to.equal(0);
expect(Time.durationSeconds('2020-02-29T09:00:00Z', from)).to.equal(5130000);
expect(Time.durationSeconds('2020-02-29T09:00:00+02:00', from)).to.equal(5122800);

for (const zone of ['.000Z', 'z', '+0200', '+02', '.5+02:00']) {
expect(Time.durationSeconds(`2020-02-30T09:00:00${zone}`, from)).to.equal(0, zone);
expect(Time.durationSeconds(`2020-02-29T09:00:00${zone}`, from)).to.be.above(5000000, zone);
}
});

it('validates unpadded calendar estimates without rejecting valid leap dates', () => {
const from = new Date(2020, 0, 1);

Expand Down
Loading