Skip to content

Commit

Permalink
feat: add other date formula
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Nov 8, 2024
1 parent 6153108 commit 612819b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/formula/src/formula.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export const FORMULA_FUNCTIONS: FormulaFunction[] = [
"YEAR",
"MONTH",
"DAY",
"HOUR",
"MINUTE",
"SECOND",
"WEEKDAY",

// 逻辑运算
"AND",
Expand Down
16 changes: 16 additions & 0 deletions packages/formula/src/formula/formula.registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,19 @@ globalFormulaRegistry.register("DAY", [["date"]], "number", "Returns the day of
["DAY('2024-01-01')", 1],
["DAY({{field1}})", undefined],
])
globalFormulaRegistry.register("HOUR", [["date"]], "number", "Returns the hour of a date.", [
["HOUR('2024-01-01 01:00:00')", 1],
["HOUR({{field1}})", undefined],
])
globalFormulaRegistry.register("MINUTE", [["date"]], "number", "Returns the minute of a date.", [
["MINUTE('2024-01-01 01:00:00')", 0],
["MINUTE({{field1}})", undefined],
])
globalFormulaRegistry.register("SECOND", [["date"]], "number", "Returns the second of a date.", [
["SECOND('2024-01-01 01:00:00')", 0],
["SECOND({{field1}})", undefined],
])
globalFormulaRegistry.register("WEEKDAY", [["date"]], "number", "Returns the weekday of a date.", [
["WEEKDAY('2024-01-01')", 2],
["WEEKDAY({{field1}})", undefined],
])
8 changes: 4 additions & 4 deletions packages/formula/src/formula/formula.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ export type FormulaFunction =
| "DAY"
// | "NOW"
// | "TODAY"
// | "HOUR"
// | "MINUTE"
// | "SECOND"
// | "WEEKDAY"
| "HOUR"
| "MINUTE"
| "SECOND"
| "WEEKDAY"
// | "DATE"

// 逻辑运算
Expand Down
16 changes: 16 additions & 0 deletions packages/persistence/src/underlying/underlying-formula.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,22 @@ export class UnderlyingFormulaVisitor extends FormulaParserVisitor<string> {
const args = this.arguments(ctx)
return `CAST(strftime('%d', ${args[0]}/1000, 'unixepoch') AS INTEGER)`
})
.with("HOUR", () => {
const args = this.arguments(ctx)
return `CAST(strftime('%H', ${args[0]}/1000, 'unixepoch') AS INTEGER)`
})
.with("MINUTE", () => {
const args = this.arguments(ctx)
return `CAST(strftime('%M', ${args[0]}/1000, 'unixepoch') AS INTEGER)`
})
.with("SECOND", () => {
const args = this.arguments(ctx)
return `CAST(strftime('%S', ${args[0]}/1000, 'unixepoch') AS INTEGER)`
})
.with("WEEKDAY", () => {
const args = this.arguments(ctx)
return `CAST(strftime('%w', ${args[0]}/1000, 'unixepoch') AS INTEGER)`
})
.with("RECORD_ID", () => {
return ID_TYPE
})
Expand Down
28 changes: 28 additions & 0 deletions packages/template/src/templates/test.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,34 @@
"option": {
"fn": "DAY({{date1}})"
}
},
"Hour": {
"id": "hour",
"type": "formula",
"option": {
"fn": "HOUR({{date1}})"
}
},
"Minute": {
"id": "minute",
"type": "formula",
"option": {
"fn": "MINUTE({{date1}})"
}
},
"Second": {
"id": "second",
"type": "formula",
"option": {
"fn": "SECOND({{date1}})"
}
},
"Weekday": {
"id": "weekday",
"type": "formula",
"option": {
"fn": "WEEKDAY({{date1}})"
}
}
},
"records": [
Expand Down

0 comments on commit 612819b

Please sign in to comment.