Skip to content

Commit f67e4f4

Browse files
authored
Fix DateTime with ZoneId for years between 00-99 (#992)
The error was happening because Date.UTC factory doesn't treats dates between 00-99 as 1900-1999. Removing the usage of this factory makes the code slightly faster while resolves the issue.
1 parent 18675df commit f67e4f4

File tree

7 files changed

+53
-17
lines changed

7 files changed

+53
-17
lines changed

packages/bolt-connection/src/bolt/bolt-protocol-v5x0.utc.transformer.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -172,22 +172,17 @@ function getTimeInZoneId (timeZoneId, epochSecond, nano) {
172172
era: 'narrow'
173173
})
174174

175-
const l = epochSecondAndNanoToLocalDateTime(epochSecond, nano)
176-
const utc = Date.UTC(
177-
int(l.year).toNumber(),
178-
int(l.month).toNumber() - 1,
179-
int(l.day).toNumber(),
180-
int(l.hour).toNumber(),
181-
int(l.minute).toNumber(),
182-
int(l.second).toNumber()
183-
)
175+
const utc = int(epochSecond)
176+
.multiply(1000)
177+
.add(int(nano).div(1_000_000))
178+
.toNumber()
184179

185180
const formattedUtcParts = formatter.formatToParts(utc)
186181

187182
const localDateTime = formattedUtcParts.reduce((obj, currentValue) => {
188183
if (currentValue.type === 'era') {
189184
obj.adjustEra =
190-
currentValue.value.toLocaleUpperCase() === 'B'
185+
currentValue.value.toUpperCase() === 'B'
191186
? year => year.subtract(1).negate() // 1BC equals to year 0 in astronomical year numbering
192187
: identity
193188
} else if (currentValue.type !== 'literal') {

packages/bolt-connection/test/bolt/bolt-protocol-v4x3.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,10 @@ describe('#unit BoltProtocolV4x3', () => {
813813
[
814814
'DateWithWithZoneId / Min Date',
815815
new DateTime(-99_999, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Samoa')
816+
],
817+
[
818+
'DateWithWithZoneId / Ambiguous date between 00 and 99',
819+
new DateTime(50, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Samoa')
816820
]
817821
])('should pack and unpack DateTimeWithZoneId and without offset (%s)', (_, object) => {
818822
const packable = protocol.packable(object)

packages/bolt-connection/test/bolt/bolt-protocol-v4x4.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,10 @@ describe('#unit BoltProtocolV4x4', () => {
846846
[
847847
'DateWithWithZoneId / Min Date',
848848
new DateTime(-99_999, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Samoa')
849+
],
850+
[
851+
'DateWithWithZoneId / Ambiguous date between 00 and 99',
852+
new DateTime(50, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Samoa')
849853
]
850854
])('should pack and unpack DateTimeWithZoneId and without offset (%s)', (_, object) => {
851855
const packable = protocol.packable(object)

packages/bolt-connection/test/bolt/bolt-protocol-v5x0.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,10 @@ describe('#unit BoltProtocolV5x0', () => {
539539
[
540540
'DateWithWithZoneId / Min Date',
541541
new DateTime(-99_999, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Samoa')
542+
],
543+
[
544+
'DateWithWithZoneId / Ambiguous date between 00 and 99',
545+
new DateTime(50, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Samoa')
542546
]
543547
])('should pack and unpack DateTimeWithZoneId and without offset (%s)', (_, object) => {
544548
const buffer = alloc(256)

packages/neo4j-driver/test/examples.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ describe('#integration examples', () => {
708708
it('service unavailable example', done => {
709709
const console = consoleOverride
710710
const consoleLoggedMsg = consoleOverridePromise
711-
const uri = `bolt://${sharedNeo4j.hostname}:7688` // wrong port
711+
const uri = `bolt://${sharedNeo4j.hostname}:7686` // wrong port
712712
const password = 'wrongPassword'
713713

714714
// tag::service-unavailable[]

packages/neo4j-driver/test/internal/transaction-executor.test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ const LOCKS_TERMINATED_ERROR =
3535
'Neo.TransientError.Transaction.LockClientStopped'
3636
const OOM_ERROR = 'Neo.DatabaseError.General.OutOfMemoryError'
3737

38-
// Not exactly integration tests but annoyingly slow for being a unit tests.
39-
describe('#integration TransactionExecutor', () => {
38+
describe('#unit TransactionExecutor', () => {
4039
it('should retry until database error happens', async () => {
4140
await testNoRetryOnUnknownError(
4241
[

packages/neo4j-driver/test/temporal-types.test.js

+34-4
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,24 @@ describe('#integration temporal-types', () => {
14391439
expect(records.length).toEqual(1)
14401440

14411441
const value = records[0].get(0)
1442-
expect(value).toEqual(expectedValue)
1442+
1443+
if (
1444+
expectedValue.timeZoneId != null &&
1445+
value.timeZoneOffsetSeconds != null &&
1446+
neo4j.isDateTime(value) &&
1447+
neo4j.isDateTime(expectedValue)) {
1448+
expect(value).toEqual(jasmine.objectContaining({
1449+
year: expectedValue.year,
1450+
month: expectedValue.month,
1451+
day: expectedValue.day,
1452+
hour: expectedValue.hour,
1453+
second: expectedValue.second,
1454+
nanosecond: expectedValue.nanosecond,
1455+
timeZoneId: expectedValue.timeZoneId
1456+
}))
1457+
} else {
1458+
expect(value).toEqual(expectedValue)
1459+
}
14431460
} finally {
14441461
await session.close()
14451462
}
@@ -1457,10 +1474,23 @@ describe('#integration temporal-types', () => {
14571474
const receivedValue = records[0].get(0)
14581475
// Amend test to ignore timeZoneOffsetInSeconds returned by the
14591476
// new servers in ZonedDateTime with the utc fix
1460-
if (value.timeZoneId != null && receivedValue.timeZoneOffsetSeconds != null) {
1461-
receivedValue.timeZoneOffsetInSeconds = undefined
1477+
if (
1478+
value.timeZoneId != null &&
1479+
receivedValue.timeZoneOffsetSeconds != null &&
1480+
neo4j.isDateTime(value) &&
1481+
neo4j.isDateTime(receivedValue)) {
1482+
expect(receivedValue).toEqual(jasmine.objectContaining({
1483+
year: value.year,
1484+
month: value.month,
1485+
day: value.day,
1486+
hour: value.hour,
1487+
second: value.second,
1488+
nanosecond: value.nanosecond,
1489+
timeZoneId: value.timeZoneId
1490+
}))
1491+
} else {
1492+
expect(receivedValue).toEqual(value)
14621493
}
1463-
expect(receivedValue).toEqual(value)
14641494
}
14651495

14661496
async function testDurationToString (values) {

0 commit comments

Comments
 (0)