Skip to content

Commit 255ca6d

Browse files
committed
Consider precision in successor/predecessor of date
1 parent b7ed8e6 commit 255ca6d

4 files changed

Lines changed: 59 additions & 8 deletions

File tree

examples/browser/cql4browsers.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,8 +1488,11 @@ class Date extends AbstractDate {
14881488
copy() {
14891489
return new Date(this.year, this.month, this.day);
14901490
}
1491-
successor() {
1492-
if (this.day != null) {
1491+
successor(precision) {
1492+
if (precision) {
1493+
return this.add(1, precision);
1494+
}
1495+
else if (this.day != null) {
14931496
return this.add(1, Date.Unit.DAY);
14941497
}
14951498
else if (this.month != null) {
@@ -1499,8 +1502,11 @@ class Date extends AbstractDate {
14991502
return this.add(1, Date.Unit.YEAR);
15001503
}
15011504
}
1502-
predecessor() {
1503-
if (this.day != null) {
1505+
predecessor(precision) {
1506+
if (precision) {
1507+
return this.add(-1, precision);
1508+
}
1509+
else if (this.day != null) {
15041510
return this.add(-1, Date.Unit.DAY);
15051511
}
15061512
else if (this.month != null) {

src/datatypes/datetime.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,8 +1090,10 @@ export class Date extends AbstractDate {
10901090
return new Date(this.year, this.month, this.day);
10911091
}
10921092

1093-
successor() {
1094-
if (this.day != null) {
1093+
successor(precision?: string) {
1094+
if (precision) {
1095+
return this.add(1, precision);
1096+
} else if (this.day != null) {
10951097
return this.add(1, Date.Unit.DAY);
10961098
} else if (this.month != null) {
10971099
return this.add(1, Date.Unit.MONTH);
@@ -1100,8 +1102,10 @@ export class Date extends AbstractDate {
11001102
}
11011103
}
11021104

1103-
predecessor() {
1104-
if (this.day != null) {
1105+
predecessor(precision?: string) {
1106+
if (precision) {
1107+
return this.add(-1, precision);
1108+
} else if (this.day != null) {
11051109
return this.add(-1, Date.Unit.DAY);
11061110
} else if (this.month != null) {
11071111
return this.add(-1, Date.Unit.MONTH);

test/datatypes/date-test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,36 @@ describe('Date.add', () => {
196196
});
197197
});
198198

199+
describe('Date.successor', () => {
200+
it('should use the specified precision', () => {
201+
const date = Date.parse('2000-06-15');
202+
date.successor(Date.Unit.YEAR).should.eql(Date.parse('2001-06-15'));
203+
date.successor(Date.Unit.MONTH).should.eql(Date.parse('2000-07-15'));
204+
date.successor(Date.Unit.DAY).should.eql(Date.parse('2000-06-16'));
205+
});
206+
207+
it("should use the date's finest precision when precision is not specified", () => {
208+
Date.parse('2000-06-15').successor().should.eql(Date.parse('2000-06-16'));
209+
Date.parse('2000-06').successor().should.eql(Date.parse('2000-07'));
210+
Date.parse('2000').successor().should.eql(Date.parse('2001'));
211+
});
212+
});
213+
214+
describe('Date.predecessor', () => {
215+
it('should use the specified precision', () => {
216+
const date = Date.parse('2000-06-15');
217+
date.predecessor(Date.Unit.YEAR).should.eql(Date.parse('1999-06-15'));
218+
date.predecessor(Date.Unit.MONTH).should.eql(Date.parse('2000-05-15'));
219+
date.predecessor(Date.Unit.DAY).should.eql(Date.parse('2000-06-14'));
220+
});
221+
222+
it("should use the date's finest precision when precision is not specified", () => {
223+
Date.parse('2000-06-15').predecessor().should.eql(Date.parse('2000-06-14'));
224+
Date.parse('2000-06').predecessor().should.eql(Date.parse('2000-05'));
225+
Date.parse('2000').predecessor().should.eql(Date.parse('1999'));
226+
});
227+
});
228+
199229
describe('Date.differenceBetween', () => {
200230
it('should return null if passed a non-Date object', () => {
201231
const a = Date.parse('2018-01-23');

test/datatypes/interval-test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,17 @@ describe('Interval', () => {
384384
});
385385
});
386386

387+
describe('DateInterval', () => {
388+
it('should use the specified precision for meets comparisons', () => {
389+
const january = new Interval(Date.parse('2020-01-01'), Date.parse('2020-01-15'));
390+
const february = new Interval(Date.parse('2020-02-15'), Date.parse('2020-02-28'));
391+
392+
january.meets(february, Date.Unit.MONTH).should.be.true();
393+
january.meetsBefore(february, Date.Unit.MONTH).should.be.true();
394+
february.meetsAfter(january, Date.Unit.MONTH).should.be.true();
395+
});
396+
});
397+
387398
describe('DateTimeInterval', () => {
388399
describe('contains', () => {
389400
let d: any;

0 commit comments

Comments
 (0)