Skip to content

Commit 7525da3

Browse files
committed
Fix Zoned DateTime support for dates before common era (#986)
Years previous the common era were being treated as positive numbers causing the dates appear as positive numbers and breaking the auto-zone offset detection for zoned date times which were not created with the offset.
1 parent 99a58d2 commit 7525da3

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ export function packDateTime (value, packer) {
262262
hour: 'numeric',
263263
minute: 'numeric',
264264
second: 'numeric',
265-
hour12: false
265+
hour12: false,
266+
era: 'narrow'
266267
})
267268

268269
const l = epochSecondAndNanoToLocalDateTime(epochSecond, nano)
@@ -278,12 +279,19 @@ export function packDateTime (value, packer) {
278279
const formattedUtcParts = formatter.formatToParts(utc)
279280

280281
const localDateTime = formattedUtcParts.reduce((obj, currentValue) => {
281-
if (currentValue.type !== 'literal') {
282+
if (currentValue.type === 'era') {
283+
obj.adjustEra =
284+
currentValue.value.toLocaleUpperCase() === 'B'
285+
? year => year.subtract(1).negate() // 1BC equals to year 0 in astronomical year numbering
286+
: year => year
287+
} else if (currentValue.type !== 'literal') {
282288
obj[currentValue.type] = int(currentValue.value)
283289
}
284290
return obj
285291
}, {})
286292

293+
localDateTime.year = localDateTime.adjustEra(localDateTime.year)
294+
287295
const epochInTimeZone = localDateTimeToEpochSecond(
288296
localDateTime.year,
289297
localDateTime.month,

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

+12
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,18 @@ describe('#unit PackStreamV2', () => {
276276
[
277277
'DateTimeWithZoneId / Sao Paulo just 1 after turn winter time',
278278
new DateTime(2019, 2, 18, 1, 0, 0, 183_000_000, undefined, 'America/Sao_Paulo')
279+
],
280+
[
281+
'DateWithWithZoneId / Berlin before common era',
282+
new DateTime(-2020, 6, 15, 4, 30, 0, 183_000_000, undefined, 'Europe/Berlin')
283+
],
284+
[
285+
'DateWithWithZoneId / Max Date',
286+
new DateTime(99_999, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Kiritimati')
287+
],
288+
[
289+
'DateWithWithZoneId / Min Date',
290+
new DateTime(-99_999, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Samoa')
279291
]
280292
])('should pack and unpack DateTimeWithZoneId and without offset (%s)', (_, object) => {
281293
const unpacked = packAndUnpack(object, { disableLosslessIntegers: true, useUtc: true})

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const MAX_TEMPORAL_ARRAY_LENGTH = 1000
3939
*/
4040
const MAX_DURATION_COMPONENT = 3000000000000
4141
const MAX_NANO_OF_SECOND = 999999999
42-
const MAX_YEAR = 999999999
42+
const MAX_YEAR = 99_999
4343
const MIN_YEAR = -MAX_YEAR
4444
const MAX_TIME_ZONE_OFFSET = 64800
4545
const MIN_TIME_ZONE_OFFSET = -MAX_TIME_ZONE_OFFSET

0 commit comments

Comments
 (0)