feat: to_char 图表时间字段时区问题 close #747 (#750)

Co-authored-by: lyx <2027667395@qq.com>
Reviewed-on: daoyoucloud/tachycode#750
Co-authored-by: hello@lv <2256334253@qq.com>
Co-committed-by: hello@lv <2256334253@qq.com>
This commit is contained in:
hello@lv 2024-04-17 16:50:51 +08:00 committed by sealday
parent 3570696595
commit f3671cf697
3 changed files with 15 additions and 5 deletions

View File

@ -0,0 +1,5 @@
---
"@hera/plugin-rental": patch
---
图表时区加加上当前时区

View File

@ -1,4 +1,4 @@
export const dateFormatFn = (sequelize: any, dialect: string, field: string, format: string) => {
export const dateFormatFn = (sequelize: any, dialect: string, field: string, format: string, timezone: string) => {
switch (dialect) {
case 'sqlite':
format = format
@ -21,7 +21,12 @@ export const dateFormatFn = (sequelize: any, dialect: string, field: string, for
return sequelize.fn('date_format', sequelize.col(field), format);
case 'postgres':
format = format.replace(/hh/g, 'HH24').replace(/mm/g, 'MI').replace(/ss/g, 'SS');
return sequelize.fn('to_char', sequelize.col(field), format);
// return sequelize.fn('to_char', sequelize.col(field), format);
return sequelize.fn(
'to_char',
sequelize.literal(`(${field} AT TIME ZONE 'UTC') AT TIME ZONE '${timezone}'`),
format,
);
default:
return sequelize.col(field);
}
@ -37,13 +42,13 @@ export const formatFn = (sequelize: any, dialect: string, field: string, format:
}
};
export const formatter = (sequelize: any, type: string, field: string, format: string) => {
export const formatter = (sequelize: any, type: string, field: string, format: string, timezone: string) => {
const dialect = sequelize.getDialect();
switch (type) {
case 'date':
case 'datetime':
case 'time':
return dateFormatFn(sequelize, dialect, field, format);
return dateFormatFn(sequelize, dialect, field, format, timezone);
default:
return formatFn(sequelize, dialect, field, format);
}

View File

@ -127,7 +127,7 @@ export const parseBuilder = async (ctx: Context, next: Next) => {
const attribute = [];
const col = sequelize.col(field);
if (format) {
attribute.push(formatter(sequelize, type, field, format));
attribute.push(formatter(sequelize, type, field, format, ctx.get('x-timezone')));
} else {
attribute.push(col);
}