Skip to content

Commit 8a55f99

Browse files
authored
Fix DateTime with ZoneId for years between 00-99 #992 (#991)
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 29a1b2f commit 8a55f99

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

packages/bolt-connection/src/packstream/packstream-utc.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -266,22 +266,17 @@ export function packDateTime (value, packer) {
266266
era: 'narrow'
267267
})
268268

269-
const l = epochSecondAndNanoToLocalDateTime(epochSecond, nano)
270-
const utc = Date.UTC(
271-
int(l.year).toNumber(),
272-
int(l.month).toNumber() - 1,
273-
int(l.day).toNumber(),
274-
int(l.hour).toNumber(),
275-
int(l.minute).toNumber(),
276-
int(l.second).toNumber()
277-
)
269+
const utc = int(epochSecond)
270+
.multiply(1000)
271+
.add(int(nano).div(1_000_000))
272+
.toNumber()
278273

279274
const formattedUtcParts = formatter.formatToParts(utc)
280275

281276
const localDateTime = formattedUtcParts.reduce((obj, currentValue) => {
282277
if (currentValue.type === 'era') {
283278
obj.adjustEra =
284-
currentValue.value.toLocaleUpperCase() === 'B'
279+
currentValue.value.toUpperCase() === 'B'
285280
? year => year.subtract(1).negate() // 1BC equals to year 0 in astronomical year numbering
286281
: year => year
287282
} else if (currentValue.type !== 'literal') {

packages/bolt-connection/test/packstream/packstream-v2.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ describe('#unit PackStreamV2', () => {
288288
[
289289
'DateWithWithZoneId / Min Date',
290290
new DateTime(-99_999, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Samoa')
291+
],
292+
[
293+
'DateWithWithZoneId / Ambiguous date between 00 and 99',
294+
new DateTime(50, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Samoa')
291295
]
292296
])('should pack and unpack DateTimeWithZoneId and without offset (%s)', (_, object) => {
293297
const unpacked = packAndUnpack(object, { disableLosslessIntegers: true, useUtc: true})

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

+37-2
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,24 @@ describe('#integration temporal-types', () => {
14411441
expect(records.length).toEqual(1)
14421442

14431443
const value = records[0].get(0)
1444-
expect(value).toEqual(expectedValue)
1444+
1445+
if (
1446+
expectedValue.timeZoneId != null &&
1447+
value.timeZoneOffsetSeconds != null &&
1448+
neo4j.isDateTime(value) &&
1449+
neo4j.isDateTime(expectedValue)) {
1450+
expect(value).toEqual(jasmine.objectContaining({
1451+
year: expectedValue.year,
1452+
month: expectedValue.month,
1453+
day: expectedValue.day,
1454+
hour: expectedValue.hour,
1455+
second: expectedValue.second,
1456+
nanosecond: expectedValue.nanosecond,
1457+
timeZoneId: expectedValue.timeZoneId
1458+
}))
1459+
} else {
1460+
expect(value).toEqual(expectedValue)
1461+
}
14451462
} finally {
14461463
await session.close()
14471464
}
@@ -1457,7 +1474,25 @@ describe('#integration temporal-types', () => {
14571474
expect(records.length).toEqual(1)
14581475

14591476
const receivedValue = records[0].get(0)
1460-
expect(receivedValue).toEqual(value)
1477+
// Amend test to ignore timeZoneOffsetInSeconds returned by the
1478+
// new servers in ZonedDateTime with the utc fix
1479+
if (
1480+
value.timeZoneId != null &&
1481+
receivedValue.timeZoneOffsetSeconds != null &&
1482+
neo4j.isDateTime(value) &&
1483+
neo4j.isDateTime(receivedValue)) {
1484+
expect(receivedValue).toEqual(jasmine.objectContaining({
1485+
year: value.year,
1486+
month: value.month,
1487+
day: value.day,
1488+
hour: value.hour,
1489+
second: value.second,
1490+
nanosecond: value.nanosecond,
1491+
timeZoneId: value.timeZoneId
1492+
}))
1493+
} else {
1494+
expect(receivedValue).toEqual(value)
1495+
}
14611496
}
14621497

14631498
async function testDurationToString (values) {

0 commit comments

Comments
 (0)