Skip to content

Commit

Permalink
change datetime2 range check
Browse files Browse the repository at this point in the history
  • Loading branch information
mShan0 committed Feb 7, 2024
1 parent 9452e06 commit e49cdf9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/data-types/datetime2.ts
Original file line number Diff line number Diff line change
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 @@ -118,11 +115,14 @@ const DateTime2: DataType & { resolveScale: NonNullable<DataType['resolveScale']

// 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
4 changes: 3 additions & 1 deletion test/unit/data-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,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

0 comments on commit e49cdf9

Please sign in to comment.