Browse Source

fix(nocodb): revise logic for datetime in mapAliasToColumn

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

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

@ -468,62 +468,30 @@ export default class Model implements TableType {
}
if (col.uidt === UITypes.DateTime && dayjs(val).isValid()) {
const { isMySQL, isSqlite, isMssql, isPg } = clientMeta;
if (base.is_meta) {
const d = new Date(val);
if (isMySQL) {
// from UI - convert to local time (e.g. UTC+8)
// e.g. 2023-05-16T12:00:00.000Z -> 2023-05-16 20:00:00
// from API - convert to local time (e.g. UTC+8)
// e.g. 2023-05-10 18:45:30+08:00 -> 2023-05-10 18:45:30
// if timezone info is not found - considered as local time
// e.g. 2022-01-01 20:00:00 -> 2022-01-01 20:00:00
// if timezone info is found - convert to local time (e.g. UTC+8)
// 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
// e.g. 2022-01-01 20:00:00+08:00 -> 2022-01-01 20:00:00
val = dayjs(val).format('YYYY-MM-DD HH:mm:ss');
} else if (isSqlite) {
// e.g. 2022-01-01T10:00:00.000Z -> 2022-01-01 10:00:00
val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ss');
} else if (isPg) {
// 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');
} else if (isMssql) {
// 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');
}
if (isMySQL) {
// from UI - convert to local time (e.g. UTC+8)
// e.g. 2023-05-16T12:00:00.000Z -> 2023-05-16 20:00:00
// from API - convert to local time (e.g. UTC+8)
// e.g. 2023-05-10 18:45:30+08:00 -> 2023-05-10 18:45:30
// if timezone info is not found - considered as local time
// e.g. 2022-01-01 20:00:00 -> 2022-01-01 20:00:00
// if timezone info is found - convert to local time (e.g. UTC+8)
// 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
// e.g. 2022-01-01 20:00:00+08:00 -> 2022-01-01 20:00:00
val = dayjs(val).format('YYYY-MM-DD HH:mm:ss');
} else if (isSqlite) {
// e.g. 2022-01-01T10:00:00.000Z -> 2022-01-01 10:00:00
val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ss');
} else if (isPg) {
// 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');
} else if (isMssql) {
// 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');
} 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');
}
}
} else {
// 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');
}
// 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');
}
}
insertObj[sanitize(col.column_name)] = val;

Loading…
Cancel
Save