Browse Source

fix(nocodb): mysql write & read date time logic

pull/5642/head
Wing-Kam Wong 2 years ago
parent
commit
50b85c9710
  1. 11
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 31
      packages/nocodb/src/models/Model.ts

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

@ -3234,7 +3234,18 @@ class BaseModelSqlv2 {
continue;
}
}
let keepLocalTime = true;
if (this.isMySQL) {
if (!isXcdbBase) {
if (d[col.title].indexOf('+') > -1) {
// timezone info found -> skip
continue;
}
}
}
if (d[col.title] instanceof Date) {
// e.g. MSSQL
// Wed May 10 2023 17:47:46 GMT+0800 (Hong Kong Standard Time)

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

@ -458,18 +458,22 @@ export default class Model implements TableType {
if (base.is_meta) {
const d = new Date(val);
if (isMySQL) {
// MySQL converts TIMESTAMP values from the current time zone to UTC for storage.
// Hence, we need to convert it to local time
if (val.slice(-1) === 'Z') {
// from UI
// e.g. 2023-05-02 08:09:43Z
// e.g. 2023-05-02 08:09:43Z -> 2023-05-10 18:45:30
val = dayjs(val).format('YYYY-MM-DD HH:mm:ss');
} else {
// from API
// e.g. 2021-01-01 04:00:00+04:00
// e.g. 2023-05-10 18:45:30+08:00 -> 2023-05-10 18:45:30
console.log('before1', val);
val = dayjs
.utc(val)
.local()
.utcOffset(d.getTimezoneOffset(), true)
.format('YYYY-MM-DD HH:mm:ss');
console.log('after1', val);
}
} else if (isSqlite) {
// e.g. 2023-05-10T10:38:50.000Z -> 2023-05-10 10:38:50
@ -494,9 +498,26 @@ export default class Model implements TableType {
}
} else {
// External DB - convert to utc
val = dayjs(val)
.utc()
.format(isMySQL ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD HH:mm:ssZ');
if (isMySQL) {
if (val.slice(-1) === 'Z') {
// from UI
val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ss');
} else {
// from API
if (val.indexOf('+') === -1) {
// no timezone info - considered as UTC
val = dayjs(val).format('YYYY-MM-DD HH:mm:ss');
if (val.slice(-1) !== 'Z') {
val += 'Z';
}
} else {
// timezone info found - convert to UTC
val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ss');
}
}
} else {
val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ');
}
}
}
insertObj[sanitize(col.column_name)] = val;

Loading…
Cancel
Save