Skip to content

Commit

Permalink
[misc] correct UTC timezone for auto timezone setting
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed May 10, 2023
1 parent f56bd14 commit fab1585
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/connection.js
Expand Up @@ -30,8 +30,8 @@ const { Status } = require('./const/connection_status');
const CommandParameter = require('./command-parameter');
const LruPrepareCache = require('./lru-prepare-cache');

const convertFixedTime = function (tz) {
if (tz === 'Etc/UTC' || tz === 'Z') {
const convertFixedTime = function (tz, conn) {
if (tz === 'UTC' || tz === 'Etc/UTC' || tz === 'Z' || tz === 'Etc/GMT') {
return '+00:00';
} else if (tz.startsWith('Etc/GMT') || tz.startsWith('GMT')) {
let tzdiff;
Expand All @@ -50,7 +50,7 @@ const convertFixedTime = function (tz) {
throw Errors.createFatalError(
`Automatic timezone setting fails. wrong Server timezone '${tz}' conversion to +/-HH:00 conversion.`,
Errors.ER_WRONG_AUTO_TIMEZONE,
this.info
conn.info
);
}
return (negate ? '-' : '+') + (diff >= 10 ? diff : '0' + diff) + ':00';
Expand Down Expand Up @@ -591,7 +591,7 @@ class Connection extends EventEmitter {
for (let j = 0; j < paramLen; j++) {
const val = row[j];
if (
val !== null &&
val != null &&
typeof val === 'object' &&
typeof val.pipe === 'function' &&
typeof val.read === 'function'
Expand All @@ -607,7 +607,7 @@ class Connection extends EventEmitter {
for (let j = 0; j < keys.length; j++) {
const val = row[keys[j]];
if (
val !== null &&
val != null &&
typeof val === 'object' &&
typeof val.pipe === 'function' &&
typeof val.read === 'function'
Expand Down Expand Up @@ -658,24 +658,25 @@ class Connection extends EventEmitter {
* @private
*/
handleTimezone() {
const conn = this;
if (this.opts.timezone === 'local') this.opts.timezone = undefined;
if (this.opts.timezone === 'auto') {
return new Promise(
this.query.bind(this, new CommandParameter('SELECT @@system_time_zone stz, @@time_zone tz'))
).then((res) => {
const serverTimezone = res[0].tz === 'SYSTEM' ? res[0].stz : res[0].tz;
const localTz = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (serverTimezone === localTz || convertFixedTime(serverTimezone) === convertFixedTime(localTz)) {
if (serverTimezone === localTz || convertFixedTime(serverTimezone, conn) === convertFixedTime(localTz, conn)) {
//server timezone is identical to client tz, skipping setting
this.opts.timezone = localTz;
return Promise.resolve();
}
return this._setSessionTimezone(convertFixedTime(localTz));
return this._setSessionTimezone(convertFixedTime(localTz, conn));
});
}

if (this.opts.timezone) {
return this._setSessionTimezone(convertFixedTime(this.opts.timezone));
return this._setSessionTimezone(convertFixedTime(this.opts.timezone, conn));
}
return Promise.resolve();
}
Expand Down

0 comments on commit fab1585

Please sign in to comment.