Skip to content

Commit

Permalink
feat: add date subscract formula
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Nov 8, 2024
1 parent fd98d30 commit 835f7c2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/formula/src/formula.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const FORMULA_FUNCTIONS: FormulaFunction[] = [

// 日期时间
"DATE_ADD",
"DATE_SUBTRACT",

// 逻辑运算
"AND",
Expand Down
15 changes: 15 additions & 0 deletions packages/formula/src/formula/formula.registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,18 @@ globalFormulaRegistry.register("DATE_ADD", [["date", "number", "string"]], "date
["DATE_ADD('2024-01-01', 1, 'second')", "2024-01-01 00:00:01"],
["DATE_ADD({{field1}}, 1, 'day')", undefined],
])
globalFormulaRegistry.register(
"DATE_SUBTRACT",
[["date", "number", "string"]],
"date",
"Subtracts a number of date from date",
[
["DATE_SUBTRACT('2024-01-01', 1, 'day')", "2023-12-31"],
["DATE_SUBTRACT('2024-01-01', 1, 'month')", "2023-12-01"],
["DATE_SUBTRACT('2024-01-01', 1, 'year')", "2023-01-01"],
["DATE_SUBTRACT('2024-01-01', 1, 'hour')", "2023-12-31 23:00:00"],
["DATE_SUBTRACT('2024-01-01', 1, 'minute')", "2023-12-31 23:59:00"],
["DATE_SUBTRACT('2024-01-01', 1, 'second')", "2023-12-31 23:59:59"],
["DATE_SUBTRACT({{field1}}, 1, 'day')", undefined],
],
)
1 change: 1 addition & 0 deletions packages/formula/src/formula/formula.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type FormulaFunction =

// 日期时间
| "DATE_ADD"
| "DATE_SUBTRACT"
// | "NOW"
// | "TODAY"
// | "YEAR"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ export class UnderlyingFormulaVisitor extends FormulaParserVisitor<string> {
// args[2] 是单位 ('year', 'month', 'day', 'hour', 'minute', 'second')
return `datetime(${args[0]}/1000, 'unixepoch', '+' || ${args[1]} || ' ' || ${args[2]})`
})
.with("DATE_SUBTRACT", () => {
const args = this.arguments(ctx)
return `datetime(${args[0]}/1000, 'unixepoch', '-' || ${args[1]} || ' ' || ${args[2]})`
})
.with("RECORD_ID", () => {
return ID_TYPE
})
Expand Down
49 changes: 48 additions & 1 deletion packages/template/src/templates/test.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,55 @@
"option": {
"fn": "DATE_ADD({{date1}}, 1, 'second')"
}
},
"DateSubtract1": {
"id": "dateSubtract1",
"type": "formula",
"option": {
"fn": "DATE_SUBTRACT({{date1}}, 1, 'day')"
}
},
"DateSubtract2": {
"id": "dateSubtract2",
"type": "formula",
"option": {
"fn": "DATE_SUBTRACT({{date1}}, 1, 'month')"
}
},
"DateSubtract3": {
"id": "dateSubtract3",
"type": "formula",
"option": {
"fn": "DATE_SUBTRACT({{date1}}, 1, 'year')"
}
},
"DateSubtract4": {
"id": "dateSubtract4",
"type": "formula",
"option": {
"fn": "DATE_SUBTRACT({{date1}}, 1, 'hour')"
}
},
"DateSubtract5": {
"id": "dateSubtract5",
"type": "formula",
"option": {
"fn": "DATE_SUBTRACT({{date1}}, 1, 'minute')"
}
},
"DateSubtract6": {
"id": "dateSubtract6",
"type": "formula",
"option": {
"fn": "DATE_SUBTRACT({{date1}}, 1, 'second')"
}
}
}
},
"records": [
{
"Date1": "2024-01-01"
}
]
}
}
}
Expand Down

0 comments on commit 835f7c2

Please sign in to comment.