Skip to content

Fix Zoned DateTime support for dates before common era #986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import v4x4 from './bolt-protocol-v4x4.transformer'
import {
epochSecondAndNanoToLocalDateTime
} from './temporal-factory'
import { identity } from '../lang/functional'

const {
temporalUtil: {
Expand Down Expand Up @@ -89,8 +90,8 @@ function createDateTimeWithZoneIdTransformer (config, logger) {

if (value.timeZoneOffsetSeconds == null) {
logger.warn('DateTime objects without "timeZoneOffsetSeconds" property ' +
'are prune to bugs related to ambiguous times. For instance, ' +
'2022-10-30T2:30:00[Europe/Berlin] could be GMT+1 or GMT+2.')
'are prune to bugs related to ambiguous times. For instance, ' +
'2022-10-30T2:30:00[Europe/Berlin] could be GMT+1 or GMT+2.')
}
const utc = epochSecond.subtract(offset)

Expand Down Expand Up @@ -167,7 +168,8 @@ function getTimeInZoneId (timeZoneId, epochSecond, nano) {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: false
hour12: false,
era: 'narrow'
})

const l = epochSecondAndNanoToLocalDateTime(epochSecond, nano)
Expand All @@ -183,12 +185,19 @@ function getTimeInZoneId (timeZoneId, epochSecond, nano) {
const formattedUtcParts = formatter.formatToParts(utc)

const localDateTime = formattedUtcParts.reduce((obj, currentValue) => {
if (currentValue.type !== 'literal') {
if (currentValue.type === 'era') {
obj.adjustEra =
currentValue.value.toLocaleUpperCase() === 'B'
? year => year.subtract(1).negate() // 1BC equals to year 0 in astronomical year numbering
: identity
} else if (currentValue.type !== 'literal') {
obj[currentValue.type] = int(currentValue.value)
}
return obj
}, {})

localDateTime.year = localDateTime.adjustEra(localDateTime.year)

const epochInTimeZone = localDateTimeToEpochSecond(
localDateTime.year,
localDateTime.month,
Expand Down
12 changes: 12 additions & 0 deletions packages/bolt-connection/test/bolt/bolt-protocol-v4x3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,18 @@ describe('#unit BoltProtocolV4x3', () => {
[
'DateTimeWithZoneId / Sao Paulo just 1 after turn winter time',
new DateTime(2019, 2, 18, 1, 0, 0, 183_000_000, undefined, 'America/Sao_Paulo')
],
[
'DateWithWithZoneId / Berlin before common era',
new DateTime(-2020, 6, 15, 4, 30, 0, 183_000_000, undefined, 'Europe/Berlin')
],
[
'DateWithWithZoneId / Max Date',
new DateTime(99_999, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Kiritimati')
],
[
'DateWithWithZoneId / Min Date',
new DateTime(-99_999, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Samoa')
]
])('should pack and unpack DateTimeWithZoneId and without offset (%s)', (_, object) => {
const packable = protocol.packable(object)
Expand Down
12 changes: 12 additions & 0 deletions packages/bolt-connection/test/bolt/bolt-protocol-v4x4.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,18 @@ describe('#unit BoltProtocolV4x4', () => {
[
'DateTimeWithZoneId / Sao Paulo just 1 after turn winter time',
new DateTime(2019, 2, 18, 1, 0, 0, 183_000_000, undefined, 'America/Sao_Paulo')
],
[
'DateWithWithZoneId / Berlin before common era',
new DateTime(-2020, 6, 15, 4, 30, 0, 183_000_000, undefined, 'Europe/Berlin')
],
[
'DateWithWithZoneId / Max Date',
new DateTime(99_999, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Kiritimati')
],
[
'DateWithWithZoneId / Min Date',
new DateTime(-99_999, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Samoa')
]
])('should pack and unpack DateTimeWithZoneId and without offset (%s)', (_, object) => {
const packable = protocol.packable(object)
Expand Down
12 changes: 12 additions & 0 deletions packages/bolt-connection/test/bolt/bolt-protocol-v5x0.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,18 @@ describe('#unit BoltProtocolV5x0', () => {
[
'DateTimeWithZoneId / Istanbul',
new DateTime(2020, 6, 15, 4, 30, 0, 183_000_000, undefined, 'Pacific/Honolulu')
],
[
'DateWithWithZoneId / Berlin before common era',
new DateTime(-2020, 6, 15, 4, 30, 0, 183_000_000, undefined, 'Europe/Berlin')
],
[
'DateWithWithZoneId / Max Date',
new DateTime(99_999, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Kiritimati')
],
[
'DateWithWithZoneId / Min Date',
new DateTime(-99_999, 12, 31, 23, 59, 59, 999_999_999, undefined, 'Pacific/Samoa')
]
])('should pack and unpack DateTimeWithZoneId and without offset (%s)', (_, object) => {
const buffer = alloc(256)
Expand Down
2 changes: 1 addition & 1 deletion packages/neo4j-driver/test/temporal-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const MAX_TEMPORAL_ARRAY_LENGTH = 1000
*/
const MAX_DURATION_COMPONENT = 3000000000000
const MAX_NANO_OF_SECOND = 999999999
const MAX_YEAR = 999999999
const MAX_YEAR = 99_999
const MIN_YEAR = -MAX_YEAR
const MAX_TIME_ZONE_OFFSET = 64800
const MIN_TIME_ZONE_OFFSET = -MAX_TIME_ZONE_OFFSET
Expand Down