Browse Source

feat(nocodb): handle mssql database timezone

pull/5505/head
Wing-Kam Wong 2 years ago
parent
commit
38edbe6e19
  1. 12
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 10
      packages/nocodb/src/models/Model.ts

12
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -1647,6 +1647,18 @@ class BaseModelSqlv2 {
.wrap('(', ')'); .wrap('(', ')');
break; break;
} }
} else if (this.isMssql) {
// if there is no timezone info, convert to database timezone, then convert to UTC
if (column.dt !== 'datetime2') {
const col = `${sanitize(alias || this.model.table_name)}.${
column.column_name
}`;
res[sanitize(column.title || column.column_name)] =
this.dbDriver.raw(
`SWITCHOFFSET(??, DATEPART(TZOFFSET, ??)) AT TIME ZONE 'UTC'`,
[col, col],
);
}
} }
res[sanitize(column.title || column.column_name)] = sanitize( res[sanitize(column.title || column.column_name)] = sanitize(
`${alias || this.model.table_name}.${column.column_name}`, `${alias || this.model.table_name}.${column.column_name}`,

10
packages/nocodb/src/models/Model.ts

@ -506,10 +506,14 @@ export default class Model implements TableType {
]); ]);
} else if (isMssql) { } else if (isMssql) {
// e.g. 2023-05-10T08:49:32.000Z -> 2023-05-10 08:49:32-08:00 // e.g. 2023-05-10T08:49:32.000Z -> 2023-05-10 08:49:32-08:00
val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ'); // then convert to db timezone
val = knex.raw(
`SWITCHOFFSET(? AT TIME ZONE 'UTC', SYSDATETIMEOFFSET()`,
[dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ')],
);
} else { } else {
// e.g. 2023-01-01T12:00:00.000Z -> 2023-01-01 20:00:00+08:00 // e.g. 2023-01-01T12:00:00.000Z -> 2023-01-01 12:00:00+00:00
val = dayjs(val).format('YYYY-MM-DD HH:mm:ssZ'); val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ');
} }
} }
insertObj[sanitize(col.column_name)] = val; insertObj[sanitize(col.column_name)] = val;

Loading…
Cancel
Save