Skip to content

Commit

Permalink
Fix handling of standard dates with zero month
Browse files Browse the repository at this point in the history
Standard dates have zero-based month. Neo4j temporal types have
1-based month. Conversion from standard date with zero month was not
handled correctly and resulted in zero month in neo4j temporal types.
  • Loading branch information
lutovich committed Jun 21, 2018
1 parent 3063f5c commit dd1187d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/v1/temporal-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class Date {

return new Date(
standardDate.getFullYear(),
standardDate.getMonth(),
standardDate.getMonth() + 1,
standardDate.getDate());
}

Expand Down Expand Up @@ -273,7 +273,7 @@ export class LocalDateTime {

return new LocalDateTime(
standardDate.getFullYear(),
standardDate.getMonth(),
standardDate.getMonth() + 1,
standardDate.getDate(),
standardDate.getHours(),
standardDate.getMinutes(),
Expand Down Expand Up @@ -342,7 +342,7 @@ export class DateTime {

return new DateTime(
standardDate.getFullYear(),
standardDate.getMonth(),
standardDate.getMonth() + 1,
standardDate.getDate(),
standardDate.getHours(),
standardDate.getMinutes(),
Expand Down
55 changes: 51 additions & 4 deletions test/v1/temporal-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,8 @@ describe('temporal-types', () => {
testStandardDateToNeo4jDateConversion(new Date(1351, 4, 7));
testStandardDateToNeo4jDateConversion(new Date(3841, 1, 19));
testStandardDateToNeo4jDateConversion(new Date(2222, 3, 29));

testStandardDateToNeo4jDateConversion(new Date(1567, 0, 29));
});

it('should fail to convert invalid standard Date to neo4j Date', () => {
Expand All @@ -753,6 +755,9 @@ describe('temporal-types', () => {
testStandardDateToLocalDateTimeConversion(new Date(1922, 1, 22, 23, 23, 45, 123), 456789);

testStandardDateToLocalDateTimeConversion(new Date(1999, 1, 1, 10, 10, 10), neo4j.int(999));

testStandardDateToLocalDateTimeConversion(new Date(2192, 0, 17, 20, 30, 40));
testStandardDateToLocalDateTimeConversion(new Date(2239, 0, 9, 1, 2, 3), 4);
});

it('should fail to convert invalid standard Date to neo4j LocalDateTime', () => {
Expand Down Expand Up @@ -782,6 +787,9 @@ describe('temporal-types', () => {

testStandardDateToDateTimeConversion(new Date(1922, 1, 22, 23, 23, 45, 123), 456789);
testStandardDateToDateTimeConversion(new Date(1999, 1, 1, 10, 10, 10), neo4j.int(999));

testStandardDateToDateTimeConversion(new Date(1899, 0, 7, 7, 7, 7, 7));
testStandardDateToDateTimeConversion(new Date(2005, 0, 1, 2, 3, 4, 5), 100);
});

it('should fail to convert invalid standard Date to neo4j DateTime', () => {
Expand All @@ -800,6 +808,45 @@ describe('temporal-types', () => {
expect(() => DateTime.fromStandardDate(new Date(), [1])).toThrowError(TypeError);
});

it('should send and receive neo4j Date created from standard Date with zero month', done => {
if (neo4jDoesNotSupportTemporalTypes(done)) {
return;
}

// return numbers and not integers to simplify the equality comparison
session = driverWithNativeNumbers.session();

const standardDate = new Date(2000, 0, 1);
const neo4jDate = neo4j.types.Date.fromStandardDate(standardDate);
testSendReceiveTemporalValue(neo4jDate, done);
});

it('should send and receive neo4j LocalDateTime created from standard Date with zero month', done => {
if (neo4jDoesNotSupportTemporalTypes(done)) {
return;
}

// return numbers and not integers to simplify the equality comparison
session = driverWithNativeNumbers.session();

const standardDate = new Date(2121, 0, 7, 10, 20, 30, 40);
const neo4jLocalDateTime = neo4j.types.LocalDateTime.fromStandardDate(standardDate);
testSendReceiveTemporalValue(neo4jLocalDateTime, done);
});

it('should send and receive neo4j DateTime created from standard Date with zero month', done => {
if (neo4jDoesNotSupportTemporalTypes(done)) {
return;
}

// return numbers and not integers to simplify the equality comparison
session = driverWithNativeNumbers.session();

const standardDate = new Date(1756, 0, 29, 23, 15, 59, 12);
const neo4jDateTime = neo4j.types.DateTime.fromStandardDate(standardDate);
testSendReceiveTemporalValue(neo4jDateTime, done);
});

function testSendAndReceiveRandomTemporalValues(valueGenerator, done) {
const asyncFunction = (index, callback) => {
const next = () => callback();
Expand Down Expand Up @@ -1002,20 +1049,20 @@ describe('temporal-types', () => {

function testStandardDateToNeo4jDateConversion(date) {
const converted = neo4j.types.Date.fromStandardDate(date);
const expected = new neo4j.types.Date(date.getFullYear(), date.getMonth(), date.getDate());
const expected = new neo4j.types.Date(date.getFullYear(), date.getMonth() + 1, date.getDate());
expect(converted).toEqual(expected);
}

function testStandardDateToLocalDateTimeConversion(date, nanosecond) {
const converted = neo4j.types.LocalDateTime.fromStandardDate(date, nanosecond);
const expected = new neo4j.types.LocalDateTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(),
totalNanoseconds(date, nanosecond));
const expected = new neo4j.types.LocalDateTime(date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(),
date.getSeconds(), totalNanoseconds(date, nanosecond));
expect(converted).toEqual(expected);
}

function testStandardDateToDateTimeConversion(date, nanosecond) {
const converted = neo4j.types.DateTime.fromStandardDate(date, nanosecond);
const expected = new neo4j.types.DateTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(),
const expected = new neo4j.types.DateTime(date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(),
totalNanoseconds(date, nanosecond), date.getTimezoneOffset() * 60);
expect(converted).toEqual(expected);
}
Expand Down

0 comments on commit dd1187d

Please sign in to comment.