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
31 changes: 14 additions & 17 deletions examples/browser/cql4browsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1327,9 +1327,6 @@ class DateTime extends AbstractDate {
toJSON() {
return this.toString();
}
_pad(num) {
return String('0' + num).slice(-2);
}
toString() {
if (this.isTime()) {
return this.toStringTime();
Expand All @@ -1341,13 +1338,13 @@ class DateTime extends AbstractDate {
toStringTime() {
let str = '';
if (this.hour != null) {
str += this._pad(this.hour);
str += String(this.hour).padStart(2, '0');
if (this.minute != null) {
str += ':' + this._pad(this.minute);
str += ':' + String(this.minute).padStart(2, '0');
if (this.second != null) {
str += ':' + this._pad(this.second);
str += ':' + String(this.second).padStart(2, '0');
if (this.millisecond != null) {
str += '.' + String('00' + this.millisecond).slice(-3);
str += '.' + String(this.millisecond).padStart(3, '0');
}
}
}
Expand All @@ -1357,19 +1354,19 @@ class DateTime extends AbstractDate {
toStringDateTime() {
let str = '';
if (this.year != null) {
str += this.year;
str += String(this.year).padStart(4, '0');
if (this.month != null) {
str += '-' + this._pad(this.month);
str += '-' + String(this.month).padStart(2, '0');
if (this.day != null) {
str += '-' + this._pad(this.day);
str += '-' + String(this.day).padStart(2, '0');
if (this.hour != null) {
str += 'T' + this._pad(this.hour);
str += 'T' + String(this.hour).padStart(2, '0');
if (this.minute != null) {
str += ':' + this._pad(this.minute);
str += ':' + String(this.minute).padStart(2, '0');
if (this.second != null) {
str += ':' + this._pad(this.second);
str += ':' + String(this.second).padStart(2, '0');
if (this.millisecond != null) {
str += '.' + String('00' + this.millisecond).slice(-3);
str += '.' + String(this.millisecond).padStart(3, '0');
}
}
}
Expand All @@ -1380,9 +1377,9 @@ class DateTime extends AbstractDate {
if (str.indexOf('T') !== -1 && this.timezoneOffset != null) {
str += this.timezoneOffset < 0 ? '-' : '+';
const offsetHours = Math.floor(Math.abs(this.timezoneOffset));
str += this._pad(offsetHours);
str += String(offsetHours).padStart(2, '0');
const offsetMin = (Math.abs(this.timezoneOffset) - offsetHours) * 60;
str += ':' + this._pad(offsetMin);
str += ':' + String(offsetMin).padStart(2, '0');
}
return str;
}
Expand Down Expand Up @@ -1589,7 +1586,7 @@ class Date extends AbstractDate {
toString() {
let str = '';
if (this.year != null) {
str += this.year.toString();
str += this.year.toString().padStart(4, '0');
if (this.month != null) {
str += '-' + this.month.toString().padStart(2, '0');
if (this.day != null) {
Expand Down
56 changes: 28 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 14 additions & 18 deletions src/datatypes/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,10 +934,6 @@ export class DateTime extends AbstractDate {
return this.toString();
}

_pad(num: number) {
return String('0' + num).slice(-2);
}

toString() {
if (this.isTime()) {
return this.toStringTime();
Expand All @@ -949,13 +945,13 @@ export class DateTime extends AbstractDate {
toStringTime() {
let str = '';
if (this.hour != null) {
str += this._pad(this.hour);
str += String(this.hour).padStart(2, '0');
if (this.minute != null) {
str += ':' + this._pad(this.minute);
str += ':' + String(this.minute).padStart(2, '0');
if (this.second != null) {
str += ':' + this._pad(this.second);
str += ':' + String(this.second).padStart(2, '0');
if (this.millisecond != null) {
str += '.' + String('00' + this.millisecond).slice(-3);
str += '.' + String(this.millisecond).padStart(3, '0');
}
}
}
Expand All @@ -967,19 +963,19 @@ export class DateTime extends AbstractDate {
toStringDateTime() {
let str = '';
if (this.year != null) {
str += this.year;
str += String(this.year).padStart(4, '0');
if (this.month != null) {
str += '-' + this._pad(this.month);
str += '-' + String(this.month).padStart(2, '0');
if (this.day != null) {
str += '-' + this._pad(this.day);
str += '-' + String(this.day).padStart(2, '0');
if (this.hour != null) {
str += 'T' + this._pad(this.hour);
str += 'T' + String(this.hour).padStart(2, '0');
if (this.minute != null) {
str += ':' + this._pad(this.minute);
str += ':' + String(this.minute).padStart(2, '0');
if (this.second != null) {
str += ':' + this._pad(this.second);
str += ':' + String(this.second).padStart(2, '0');
if (this.millisecond != null) {
str += '.' + String('00' + this.millisecond).slice(-3);
str += '.' + String(this.millisecond).padStart(3, '0');
}
}
}
Expand All @@ -991,9 +987,9 @@ export class DateTime extends AbstractDate {
if (str.indexOf('T') !== -1 && this.timezoneOffset != null) {
str += this.timezoneOffset < 0 ? '-' : '+';
const offsetHours = Math.floor(Math.abs(this.timezoneOffset));
str += this._pad(offsetHours);
str += String(offsetHours).padStart(2, '0');
const offsetMin = (Math.abs(this.timezoneOffset) - offsetHours) * 60;
str += ':' + this._pad(offsetMin);
str += ':' + String(offsetMin).padStart(2, '0');
}

return str;
Expand Down Expand Up @@ -1218,7 +1214,7 @@ export class Date extends AbstractDate {
toString() {
let str = '';
if (this.year != null) {
str += this.year.toString();
str += this.year.toString().padStart(4, '0');
if (this.month != null) {
str += '-' + this.month.toString().padStart(2, '0');
if (this.day != null) {
Expand Down
7 changes: 6 additions & 1 deletion test/datatypes/date-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as luxon from 'luxon';
import should from 'should';
import { Date, DateTime } from '../../src/datatypes/datetime';
import { Date, DateTime, MAX_DATE_VALUE, MIN_DATE_VALUE } from '../../src/datatypes/datetime';
import { Uncertainty } from '../../src/datatypes/uncertainty';
import { jsDate } from '../../src/util/util';

Expand Down Expand Up @@ -49,6 +49,11 @@ describe('Date', () => {
d.toString().should.eql('2012-10-25');
});

it('should toString min and max values', () => {
MIN_DATE_VALUE.toString().should.eql('0001-01-01');
MAX_DATE_VALUE.toString().should.eql('9999-12-31');
});

it('should return null when parsing non-string', () => should(Date.parse(20121025)).be.null());

it('should return null when parsing invalid string format', () =>
Expand Down
7 changes: 6 additions & 1 deletion test/datatypes/datetime-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import * as luxon from 'luxon';
import should from 'should';
import { DateTime } from '../../src/datatypes/datetime';
import { DateTime, MAX_DATETIME_VALUE, MIN_DATETIME_VALUE } from '../../src/datatypes/datetime';
import { Uncertainty } from '../../src/datatypes/uncertainty';

const tzDate = function (
Expand Down Expand Up @@ -187,6 +187,11 @@ describe('DateTime', () => {
d.toString().should.eql('2012-10-25T12:55:14.953-05:00');
});

it('should toString min and max values', () => {
MIN_DATETIME_VALUE.toString().should.match(/^0001-01-01T00:00:00\.000([+-]\d\d:\d\d|Z)$/);
MAX_DATETIME_VALUE.toString().should.match(/^9999-12-31T23:59:59\.999([+-]\d\d:\d\d|Z)$/);
});

it('should be null when parsing non-string', () => should(DateTime.parse(20121025)).be.null());

it('should be null when parsing invalid string format', () =>
Expand Down
Loading