Browse Source

fix(nocodb): revise logic for datetime in mapAliasToColumn

pull/5642/head
Wing-Kam Wong 2 years ago
parent
commit
cb06626af3
  1. 32
      packages/nocodb/src/models/Model.ts

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

@ -468,8 +468,6 @@ export default class Model implements TableType {
} }
if (col.uidt === UITypes.DateTime && dayjs(val).isValid()) { if (col.uidt === UITypes.DateTime && dayjs(val).isValid()) {
const { isMySQL, isSqlite, isMssql, isPg } = clientMeta; const { isMySQL, isSqlite, isMssql, isPg } = clientMeta;
if (base.is_meta) {
const d = new Date(val);
if (isMySQL) { if (isMySQL) {
// from UI - convert to local time (e.g. UTC+8) // from UI - convert to local time (e.g. UTC+8)
// e.g. 2023-05-16T12:00:00.000Z -> 2023-05-16 20:00:00 // e.g. 2023-05-16T12:00:00.000Z -> 2023-05-16 20:00:00
@ -490,42 +488,12 @@ export default class Model implements TableType {
val = dayjs(val).format('YYYY-MM-DD HH:mm:ssZ'); val = dayjs(val).format('YYYY-MM-DD HH:mm:ssZ');
} 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
.utc(val)
.utcOffset(d.getTimezoneOffset(), false)
.format('YYYY-MM-DD HH:mm:ssZ');
}
} else {
// External DB
if (isMySQL) {
// assuming local time = UTC+8
// e.g. 2022-01-01 20:00:00+08:00 -> 2022-01-01 20:00:00
// e.g. 2022-01-01 20:00:00 -> 2022-01-01 20:00:00
// e.g. 2022-01-01 20:00:00Z -> 2022-01-02 04:00:00
// e.g. 2022-01-01 20:00:00+00:00 -> 2022-01-02 04:00:00
val = dayjs(val).format('YYYY-MM-DD HH:mm:ss');
} else if (isSqlite) {
val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ss');
} else if (isMssql) {
if (val.slice(-1) === 'Z') {
// from UI
val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ');
} else {
// from API
if (val.indexOf('+') === -1) {
// no timezone info - considered as UTC
val = dayjs(val).utc(true).format('YYYY-MM-DD HH:mm:ssZ');
} else {
// timezone info found - convert to UTC
val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ'); val = 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 20:00:00+08:00
val = dayjs(val).format('YYYY-MM-DD HH:mm:ssZ'); val = dayjs(val).format('YYYY-MM-DD HH:mm:ssZ');
} }
} }
}
insertObj[sanitize(col.column_name)] = val; insertObj[sanitize(col.column_name)] = val;
} }
} }

Loading…
Cancel
Save