Skip to content

Commit

Permalink
fix: date range check (#1600)
Browse files Browse the repository at this point in the history
* test: date range check

* change datetime range check

* change datetime2 range check

* change datetimeoffset range check

* remove unused constants

* remove todo comments

* smalldatetime range check

* remove unused variables
  • Loading branch information
mShan0 committed Feb 9, 2024
1 parent e30d927 commit 1c88d76
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 38 deletions.
12 changes: 5 additions & 7 deletions src/data-types/date.ts
Expand Up @@ -7,9 +7,6 @@ const EPOCH_DATE = LocalDate.ofYearDay(1, 1);
const NULL_LENGTH = Buffer.from([0x00]);
const DATA_LENGTH = Buffer.from([0x03]);

const MIN_DATE = new globalDate('January 1, 0001');
const MAX_DATE = new globalDate('December 31, 9999');

const Date: DataType = {
id: 0x28,
type: 'DATEN',
Expand Down Expand Up @@ -63,13 +60,14 @@ const Date: DataType = {

value = value as Date;

// TODO: check date range: January 1, 0001, through December 31, 9999
// : time range: 00:00:00 through 23:59:59.997
let year;
if (options && options.useUTC) {
value = new globalDate(value.toUTCString());
year = value.getUTCFullYear();
} else {
year = value.getFullYear();
}

if (value < MIN_DATE || value > MAX_DATE) {
if (year < 1 || year > 9999) {
throw new TypeError('Out of range.');
}

Expand Down
12 changes: 5 additions & 7 deletions src/data-types/datetime.ts
Expand Up @@ -6,9 +6,6 @@ const EPOCH_DATE = LocalDate.ofYearDay(1900, 1);
const NULL_LENGTH = Buffer.from([0x00]);
const DATA_LENGTH = Buffer.from([0x08]);

const MIN_DATE = new Date('January 1, 1753');
const MAX_DATE = new Date('December 31, 9999');

const DateTime: DataType = {
id: 0x3D,
type: 'DATETIME',
Expand Down Expand Up @@ -86,13 +83,14 @@ const DateTime: DataType = {

value = value as Date;

// TODO: check date range: January 1, 1753, through December 31, 9999
// : time range: 00:00:00 through 23:59:59.997
let year;
if (options && options.useUTC) {
value = new Date(value.toUTCString());
year = value.getUTCFullYear();
} else {
year = value.getFullYear();
}

if (value < MIN_DATE || value > MAX_DATE) {
if (year < 1753 || year > 9999) {
throw new TypeError('Out of range.');
}

Expand Down
12 changes: 5 additions & 7 deletions src/data-types/datetime2.ts
Expand Up @@ -5,9 +5,6 @@ import WritableTrackingBuffer from '../tracking-buffer/writable-tracking-buffer'
const EPOCH_DATE = LocalDate.ofYearDay(1, 1);
const NULL_LENGTH = Buffer.from([0x00]);

const MIN_DATE = new Date('January 1, 0001');
const MAX_DATE = new Date('December 31, 9999');

const DateTime2: DataType & { resolveScale: NonNullable<DataType['resolveScale']> } = {
id: 0x2A,
type: 'DATETIME2N',
Expand Down Expand Up @@ -116,13 +113,14 @@ const DateTime2: DataType & { resolveScale: NonNullable<DataType['resolveScale']

value = value as Date;

// TODO: check date range: January 1, 0001, through December 31, 9999
// : time range: 00:00:00 through 23:59:59.997
let year;
if (options && options.useUTC) {
value = new Date(value.toUTCString());
year = value.getUTCFullYear();
} else {
year = value.getFullYear();
}

if (value < MIN_DATE || value > MAX_DATE) {
if (year < 1 || year > 9999) {
throw new TypeError('Out of range.');
}

Expand Down
12 changes: 5 additions & 7 deletions src/data-types/datetimeoffset.ts
Expand Up @@ -5,9 +5,6 @@ import WritableTrackingBuffer from '../tracking-buffer/writable-tracking-buffer'
const EPOCH_DATE = LocalDate.ofYearDay(1, 1);
const NULL_LENGTH = Buffer.from([0x00]);

const MIN_DATE = new Date('January 1, 0001');
const MAX_DATE = new Date('December 31, 9999');

const DateTimeOffset: DataType & { resolveScale: NonNullable<DataType['resolveScale']> } = {
id: 0x2B,
type: 'DATETIMEOFFSETN',
Expand Down Expand Up @@ -106,13 +103,14 @@ const DateTimeOffset: DataType & { resolveScale: NonNullable<DataType['resolveSc

value = value as Date;

// TODO: check date range: January 1, 0001, through December 31, 9999
// : time range: 00:00:00 through 23:59:59.997
let year;
if (options && options.useUTC) {
value = new Date(value.toUTCString());
year = value.getUTCFullYear();
} else {
year = value.getFullYear();
}

if (value < MIN_DATE || value > MAX_DATE) {
if (year < 1 || year > 9999) {
throw new TypeError('Out of range.');
}

Expand Down
21 changes: 16 additions & 5 deletions src/data-types/smalldatetime.ts
Expand Up @@ -4,9 +4,6 @@ import DateTimeN from './datetimen';
const EPOCH_DATE = new Date(1900, 0, 1);
const UTC_EPOCH_DATE = new Date(Date.UTC(1900, 0, 1));

const MIN_DATE = new Date(1900, 1, 1);
const MAX_DATE = new Date(2079, 5, 6, 23, 59, 59, 0);

const DATA_LENGTH = Buffer.from([0x04]);
const NULL_LENGTH = Buffer.from([0x00]);

Expand Down Expand Up @@ -65,14 +62,28 @@ const SmallDateTime: DataType = {

value = value as Date;

let year, month, date;
if (options && options.useUTC) {
value = new Date(value.toUTCString());
year = value.getUTCFullYear();
month = value.getUTCMonth();
date = value.getUTCDate();
} else {
year = value.getFullYear();
month = value.getMonth();
date = value.getDate();
}

if (value < MIN_DATE || value > MAX_DATE) {
if (year < 1900 || year > 2079) {
throw new TypeError('Out of range.');
}

if (year === 2079) {
// Month is 0-indexed, i.e. Jan = 0, Dec = 11
if (month > 4 || (month === 4 && date > 6)) {
throw new TypeError('Out of range.');
}
}

if (isNaN(value)) {
throw new TypeError('Invalid date.');
}
Expand Down
24 changes: 19 additions & 5 deletions test/unit/data-type.js
Expand Up @@ -215,7 +215,9 @@ describe('Date', function() {
describe('.validate', function() {
it('returns a TypeError for dates that are out of range', function() {
assert.throws(() => {
TYPES.Date.validate(new Date('Dec 31 2000'));
const testDate = new Date();
testDate.setFullYear(0);
TYPES.Date.validate(testDate);
}, TypeError, 'Out of range.');

assert.throws(() => {
Expand Down Expand Up @@ -320,7 +322,9 @@ describe('DateTime2', function() {
describe('.validate', function() {
it('returns a TypeError for dates that are out of range', function() {
assert.throws(() => {
TYPES.DateTime2.validate(new Date('Dec 31, 2000'));
const testDate = new Date();
testDate.setFullYear(0);
TYPES.DateTime2.validate(testDate);
}, TypeError, 'Out of range.');

assert.throws(() => {
Expand Down Expand Up @@ -385,7 +389,9 @@ describe('DateTimeOffset', function() {
describe('.validate', function() {
it('returns a TypeError for dates that are out of range', function() {
assert.throws(() => {
TYPES.DateTimeOffset.validate(new Date('Dec 31, 2000'));
const testDate = new Date();
testDate.setFullYear(0);
TYPES.DateTimeOffset.validate(testDate);
}, TypeError, 'Out of range.');

assert.throws(() => {
Expand Down Expand Up @@ -742,7 +748,7 @@ describe('Money', function() {
});
});

describe.only('.validate', function() {
describe('.validate', function() {
it('throws Invalid number error for NaN input', function() {
assert.throws(() => {
TYPES.TinyInt.validate('string');
Expand Down Expand Up @@ -1067,7 +1073,15 @@ describe('SmallDateTime', function() {
}, TypeError, 'Out of range.');

assert.throws(() => {
TYPES.SmallDateTime.validate(new Date('June 7, 2079'));
TYPES.SmallDateTime.validate(new Date('May 7, 2079'));
}, TypeError, 'Out of range.');

assert.throws(() => {
TYPES.SmallDateTime.validate(new Date('Jan 1, 2080'));
}, TypeError, 'Out of range.');

assert.throws(() => {
TYPES.SmallDateTime.validate(new Date('June 1, 2079'));
}, TypeError, 'Out of range.');
});
});
Expand Down

0 comments on commit 1c88d76

Please sign in to comment.