Skip to content

Commit dd1187d

Browse files
committed
Fix handling of standard dates with zero month
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.
1 parent 3063f5c commit dd1187d

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

src/v1/temporal-types.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export class Date {
214214

215215
return new Date(
216216
standardDate.getFullYear(),
217-
standardDate.getMonth(),
217+
standardDate.getMonth() + 1,
218218
standardDate.getDate());
219219
}
220220

@@ -273,7 +273,7 @@ export class LocalDateTime {
273273

274274
return new LocalDateTime(
275275
standardDate.getFullYear(),
276-
standardDate.getMonth(),
276+
standardDate.getMonth() + 1,
277277
standardDate.getDate(),
278278
standardDate.getHours(),
279279
standardDate.getMinutes(),
@@ -342,7 +342,7 @@ export class DateTime {
342342

343343
return new DateTime(
344344
standardDate.getFullYear(),
345-
standardDate.getMonth(),
345+
standardDate.getMonth() + 1,
346346
standardDate.getDate(),
347347
standardDate.getHours(),
348348
standardDate.getMinutes(),

test/v1/temporal-types.test.js

+51-4
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,8 @@ describe('temporal-types', () => {
728728
testStandardDateToNeo4jDateConversion(new Date(1351, 4, 7));
729729
testStandardDateToNeo4jDateConversion(new Date(3841, 1, 19));
730730
testStandardDateToNeo4jDateConversion(new Date(2222, 3, 29));
731+
732+
testStandardDateToNeo4jDateConversion(new Date(1567, 0, 29));
731733
});
732734

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

755757
testStandardDateToLocalDateTimeConversion(new Date(1999, 1, 1, 10, 10, 10), neo4j.int(999));
758+
759+
testStandardDateToLocalDateTimeConversion(new Date(2192, 0, 17, 20, 30, 40));
760+
testStandardDateToLocalDateTimeConversion(new Date(2239, 0, 9, 1, 2, 3), 4);
756761
});
757762

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

783788
testStandardDateToDateTimeConversion(new Date(1922, 1, 22, 23, 23, 45, 123), 456789);
784789
testStandardDateToDateTimeConversion(new Date(1999, 1, 1, 10, 10, 10), neo4j.int(999));
790+
791+
testStandardDateToDateTimeConversion(new Date(1899, 0, 7, 7, 7, 7, 7));
792+
testStandardDateToDateTimeConversion(new Date(2005, 0, 1, 2, 3, 4, 5), 100);
785793
});
786794

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

811+
it('should send and receive neo4j Date created from standard Date with zero month', done => {
812+
if (neo4jDoesNotSupportTemporalTypes(done)) {
813+
return;
814+
}
815+
816+
// return numbers and not integers to simplify the equality comparison
817+
session = driverWithNativeNumbers.session();
818+
819+
const standardDate = new Date(2000, 0, 1);
820+
const neo4jDate = neo4j.types.Date.fromStandardDate(standardDate);
821+
testSendReceiveTemporalValue(neo4jDate, done);
822+
});
823+
824+
it('should send and receive neo4j LocalDateTime created from standard Date with zero month', done => {
825+
if (neo4jDoesNotSupportTemporalTypes(done)) {
826+
return;
827+
}
828+
829+
// return numbers and not integers to simplify the equality comparison
830+
session = driverWithNativeNumbers.session();
831+
832+
const standardDate = new Date(2121, 0, 7, 10, 20, 30, 40);
833+
const neo4jLocalDateTime = neo4j.types.LocalDateTime.fromStandardDate(standardDate);
834+
testSendReceiveTemporalValue(neo4jLocalDateTime, done);
835+
});
836+
837+
it('should send and receive neo4j DateTime created from standard Date with zero month', done => {
838+
if (neo4jDoesNotSupportTemporalTypes(done)) {
839+
return;
840+
}
841+
842+
// return numbers and not integers to simplify the equality comparison
843+
session = driverWithNativeNumbers.session();
844+
845+
const standardDate = new Date(1756, 0, 29, 23, 15, 59, 12);
846+
const neo4jDateTime = neo4j.types.DateTime.fromStandardDate(standardDate);
847+
testSendReceiveTemporalValue(neo4jDateTime, done);
848+
});
849+
803850
function testSendAndReceiveRandomTemporalValues(valueGenerator, done) {
804851
const asyncFunction = (index, callback) => {
805852
const next = () => callback();
@@ -1002,20 +1049,20 @@ describe('temporal-types', () => {
10021049

10031050
function testStandardDateToNeo4jDateConversion(date) {
10041051
const converted = neo4j.types.Date.fromStandardDate(date);
1005-
const expected = new neo4j.types.Date(date.getFullYear(), date.getMonth(), date.getDate());
1052+
const expected = new neo4j.types.Date(date.getFullYear(), date.getMonth() + 1, date.getDate());
10061053
expect(converted).toEqual(expected);
10071054
}
10081055

10091056
function testStandardDateToLocalDateTimeConversion(date, nanosecond) {
10101057
const converted = neo4j.types.LocalDateTime.fromStandardDate(date, nanosecond);
1011-
const expected = new neo4j.types.LocalDateTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(),
1012-
totalNanoseconds(date, nanosecond));
1058+
const expected = new neo4j.types.LocalDateTime(date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(),
1059+
date.getSeconds(), totalNanoseconds(date, nanosecond));
10131060
expect(converted).toEqual(expected);
10141061
}
10151062

10161063
function testStandardDateToDateTimeConversion(date, nanosecond) {
10171064
const converted = neo4j.types.DateTime.fromStandardDate(date, nanosecond);
1018-
const expected = new neo4j.types.DateTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(),
1065+
const expected = new neo4j.types.DateTime(date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(),
10191066
totalNanoseconds(date, nanosecond), date.getTimezoneOffset() * 60);
10201067
expect(converted).toEqual(expected);
10211068
}

0 commit comments

Comments
 (0)