|
| 1 | +const Firebird = require('../lib'); |
| 2 | +const Config = require('./config'); |
| 3 | +const assert = require('assert'); |
| 4 | + |
| 5 | +const config = Config.default; |
| 6 | + |
| 7 | +function fromCallback(executor) { |
| 8 | + return new Promise((resolve, reject) => { |
| 9 | + executor((err, result) => err ? reject(err) : resolve(result)); |
| 10 | + }); |
| 11 | +} |
| 12 | + |
| 13 | +describe('Timezone Support (Firebird 4.0+)', () => { |
| 14 | + let db; |
| 15 | + let supportsTimezone = false; |
| 16 | + |
| 17 | + beforeAll(async () => { |
| 18 | + try { |
| 19 | + db = await fromCallback(cb => Firebird.attach(config, cb)); |
| 20 | + // Check if server supports timezone types |
| 21 | + await fromCallback(cb => db.query('SELECT CAST('12:00:00 UTC' AS TIME WITH TIME ZONE) FROM RDB$DATABASE', cb)); |
| 22 | + supportsTimezone = true; |
| 23 | + } catch (err) { |
| 24 | + console.warn('Firebird server does not support Timezone types, skipping integration tests.'); |
| 25 | + if (db) { |
| 26 | + await fromCallback(cb => db.detach(cb)); |
| 27 | + db = null; |
| 28 | + } |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + afterAll(async () => { |
| 33 | + if (db) { |
| 34 | + await fromCallback(cb => db.detach(cb)); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + it('should select TIME WITH TIME ZONE and TIMESTAMP WITH TIME ZONE', { skip: !supportsTimezone }, async () => { |
| 39 | + const query = ` |
| 40 | + SELECT |
| 41 | + CAST('12:00:00 UTC' AS TIME WITH TIME ZONE) as T_TZ, |
| 42 | + CAST('2024-01-01 12:00:00 UTC' AS TIMESTAMP WITH TIME ZONE) as TS_TZ |
| 43 | + FROM RDB$DATABASE |
| 44 | + `; |
| 45 | + const rows = await fromCallback(cb => db.query(query, cb)); |
| 46 | + const row = rows[0]; |
| 47 | + |
| 48 | + assert.ok(row.t_tz instanceof Date, 'TIME WITH TIME ZONE should be a Date'); |
| 49 | + assert.ok(row.ts_tz instanceof Date, 'TIMESTAMP WITH TIME ZONE should be a Date'); |
| 50 | + |
| 51 | + // Firebird returns these in UTC, we represent them in local time |
| 52 | + // 12:00:00 UTC should have 12 hours in UTC |
| 53 | + assert.strictEqual(row.t_tz.getUTCHours(), 12); |
| 54 | + assert.strictEqual(row.ts_tz.getUTCFullYear(), 2024); |
| 55 | + assert.strictEqual(row.ts_tz.getUTCHours(), 12); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should round-trip TIME WITH TIME ZONE and TIMESTAMP WITH TIME ZONE', { skip: !supportsTimezone }, async () => { |
| 59 | + const table_sql = 'CREATE TABLE TEST_TZ (ID INT, T_TZ TIME WITH TIME ZONE, TS_TZ TIMESTAMP WITH TIME ZONE)'; |
| 60 | + await fromCallback(cb => db.query(table_sql, cb)); |
| 61 | + |
| 62 | + try { |
| 63 | + const now = new Date(); |
| 64 | + // Reset milliseconds for accurate comparison as Firebird might have different precision |
| 65 | + now.setMilliseconds(0); |
| 66 | + |
| 67 | + await fromCallback(cb => db.query( |
| 68 | + 'INSERT INTO TEST_TZ (ID, T_TZ, TS_TZ) VALUES (?, ?, ?)', |
| 69 | + [1, now, now], |
| 70 | + cb |
| 71 | + )); |
| 72 | + |
| 73 | + const rows = await fromCallback(cb => db.query('SELECT T_TZ, TS_TZ FROM TEST_TZ WHERE ID = 1', cb)); |
| 74 | + const row = rows[0]; |
| 75 | + |
| 76 | + // Compare UTC times to avoid timezone offset issues during comparison |
| 77 | + assert.strictEqual(row.ts_tz.getTime(), now.getTime(), 'Timestamp round-trip mismatch'); |
| 78 | + |
| 79 | + // For TIME, we only compare the time part (hours, minutes, seconds) |
| 80 | + assert.strictEqual(row.t_tz.getUTCHours(), now.getUTCHours()); |
| 81 | + assert.strictEqual(row.t_tz.getUTCMinutes(), now.getUTCMinutes()); |
| 82 | + assert.strictEqual(row.t_tz.getUTCSeconds(), now.getUTCSeconds()); |
| 83 | + |
| 84 | + } finally { |
| 85 | + await fromCallback(cb => db.query('DROP TABLE TEST_TZ', cb)).catch(() => {}); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + it('should handle sessionTimeZone option', { skip: !supportsTimezone }, async () => { |
| 90 | + const utcConfig = Object.assign({}, config, { sessionTimeZone: 'UTC' }); |
| 91 | + const utcDb = await fromCallback(cb => Firebird.attach(utcConfig, cb)); |
| 92 | + try { |
| 93 | + const rows = await fromCallback(cb => utcDb.query('SELECT CURRENT_TIMESTAMP FROM RDB$DATABASE', cb)); |
| 94 | + assert.ok(rows[0].current_timestamp instanceof Date); |
| 95 | + } finally { |
| 96 | + await fromCallback(cb => utcDb.detach(cb)); |
| 97 | + } |
| 98 | + }); |
| 99 | +}); |
0 commit comments