Skip to content

Commit 63fd97e

Browse files
committed
Fix timezone offset calculation
1 parent d5fa5b6 commit 63fd97e

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

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

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ function getTimeInZoneId (timeZoneId, epochSecond, nano) {
167167
hour: 'numeric',
168168
minute: 'numeric',
169169
second: 'numeric',
170-
hour12: false,
171-
timeZoneName: 'short'
170+
hour12: false
172171
})
173172

174173
const l = epochSecondAndNanoToLocalDateTime(epochSecond, nano)
@@ -184,29 +183,23 @@ function getTimeInZoneId (timeZoneId, epochSecond, nano) {
184183
const formattedUtcParts = formatter.formatToParts(utc)
185184

186185
const localDateTime = formattedUtcParts.reduce((obj, currentValue) => {
187-
if (currentValue.type === 'timeZoneName') {
188-
const parts = currentValue.value.replace('GMT', '').split(':')
189-
const divisor = 60
190-
const { offset } = parts.reduce((state, value) => {
191-
const part = int(value)
192-
const signal = part.isPositive() ? int(1) : int(-1)
193-
194-
const offset = part.multiply(state.factor).add(state.offset)
195-
const factor = state.factor.div(divisor).multiply(signal)
196-
197-
return {
198-
offset,
199-
factor
200-
}
201-
}, { factor: int(60 * 60), offset: int(0) })
202-
203-
obj.timeZoneOffsetSeconds = offset
204-
} else if (currentValue.type !== 'literal') {
186+
if (currentValue.type !== 'literal') {
205187
obj[currentValue.type] = int(currentValue.value)
206188
}
207189
return obj
208190
}, {})
209191

192+
const epochInTimeZone = localDateTimeToEpochSecond(
193+
localDateTime.year,
194+
localDateTime.month,
195+
localDateTime.day,
196+
localDateTime.hour,
197+
localDateTime.minute,
198+
localDateTime.second,
199+
localDateTime.nanosecond
200+
)
201+
202+
localDateTime.timeZoneOffsetSeconds = epochInTimeZone.subtract(epochSecond)
210203
localDateTime.hour = localDateTime.hour.modulo(24)
211204

212205
return localDateTime

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ describe('#unit BoltProtocolV5x0', () => {
412412
'DateTimeWithZoneOffset',
413413
new DateTime(2022, 6, 14, 15, 21, 18, 183_000_000, 120 * 60)
414414
],
415+
[
416+
'DateTimeWithZoneOffset / 1978',
417+
new DateTime(1978, 12, 16, 10, 5, 59, 128000987, -150 * 60)
418+
],
415419
[
416420
'DateTimeWithZoneId / Berlin 2:30 CET',
417421
new DateTime(2022, 10, 30, 2, 30, 0, 183_000_000, 2 * 60 * 60, 'Europe/Berlin')
@@ -515,6 +519,14 @@ describe('#unit BoltProtocolV5x0', () => {
515519
[
516520
'DateTimeWithZoneId / Sao Paulo just 1 after turn winter time',
517521
new DateTime(2019, 2, 18, 1, 0, 0, 183_000_000, undefined, 'America/Sao_Paulo')
522+
],
523+
[
524+
'DateTimeWithZoneId / Istanbul',
525+
new DateTime(1978, 12, 16, 12, 35, 59, 128000987, undefined, 'Europe/Istanbul')
526+
],
527+
[
528+
'DateTimeWithZoneId / Istanbul',
529+
new DateTime(2020, 6, 15, 4, 30, 0, 183_000_000, undefined, 'Pacific/Honolulu')
518530
]
519531
])('should pack and unpack DateTimeWithZoneId and without offset (%s)', (_, object) => {
520532
const buffer = alloc(256)
@@ -824,6 +836,13 @@ describe('#unit BoltProtocolV5x0', () => {
824836
]),
825837
new DateTime(2022, 6, 14, 15, 21, 18, 183_000_000, 120 * 60)
826838
],
839+
[
840+
'DateTimeWithZoneOffset / 1978',
841+
new structure.Structure(0x49, [
842+
282659759, 128000987, -150 * 60
843+
]),
844+
new DateTime(1978, 12, 16, 10, 5, 59, 128000987, -150 * 60)
845+
],
827846
[
828847
'DateTimeWithZoneId',
829848
new structure.Structure(0x69, [
@@ -837,6 +856,13 @@ describe('#unit BoltProtocolV5x0', () => {
837856
1655212878, 183_000_000, 'Australia/Eucla'
838857
]),
839858
new DateTime(2022, 6, 14, 22, 6, 18, 183_000_000, 8 * 60 * 60 + 45 * 60, 'Australia/Eucla')
859+
],
860+
[
861+
'DateTimeWithZoneId / Honolulu',
862+
new structure.Structure(0x69, [
863+
1592231400, 183_000_000, 'Pacific/Honolulu'
864+
]),
865+
new DateTime(2020, 6, 15, 4, 30, 0, 183_000_000, -10 * 60 * 60, 'Pacific/Honolulu')
840866
]
841867
])('should unpack spatial types and temporal types (%s)', (_, struct, object) => {
842868
const buffer = alloc(256)

0 commit comments

Comments
 (0)