Skip to content

Commit 612819b

Browse files
committed
feat: add other date formula
1 parent 6153108 commit 612819b

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

packages/formula/src/formula.constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export const FORMULA_FUNCTIONS: FormulaFunction[] = [
4040
"YEAR",
4141
"MONTH",
4242
"DAY",
43+
"HOUR",
44+
"MINUTE",
45+
"SECOND",
46+
"WEEKDAY",
4347

4448
// 逻辑运算
4549
"AND",

packages/formula/src/formula/formula.registry.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,19 @@ globalFormulaRegistry.register("DAY", [["date"]], "number", "Returns the day of
376376
["DAY('2024-01-01')", 1],
377377
["DAY({{field1}})", undefined],
378378
])
379+
globalFormulaRegistry.register("HOUR", [["date"]], "number", "Returns the hour of a date.", [
380+
["HOUR('2024-01-01 01:00:00')", 1],
381+
["HOUR({{field1}})", undefined],
382+
])
383+
globalFormulaRegistry.register("MINUTE", [["date"]], "number", "Returns the minute of a date.", [
384+
["MINUTE('2024-01-01 01:00:00')", 0],
385+
["MINUTE({{field1}})", undefined],
386+
])
387+
globalFormulaRegistry.register("SECOND", [["date"]], "number", "Returns the second of a date.", [
388+
["SECOND('2024-01-01 01:00:00')", 0],
389+
["SECOND({{field1}})", undefined],
390+
])
391+
globalFormulaRegistry.register("WEEKDAY", [["date"]], "number", "Returns the weekday of a date.", [
392+
["WEEKDAY('2024-01-01')", 2],
393+
["WEEKDAY({{field1}})", undefined],
394+
])

packages/formula/src/formula/formula.type.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ export type FormulaFunction =
4141
| "DAY"
4242
// | "NOW"
4343
// | "TODAY"
44-
// | "HOUR"
45-
// | "MINUTE"
46-
// | "SECOND"
47-
// | "WEEKDAY"
44+
| "HOUR"
45+
| "MINUTE"
46+
| "SECOND"
47+
| "WEEKDAY"
4848
// | "DATE"
4949

5050
// 逻辑运算

packages/persistence/src/underlying/underlying-formula.visitor.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,22 @@ export class UnderlyingFormulaVisitor extends FormulaParserVisitor<string> {
239239
const args = this.arguments(ctx)
240240
return `CAST(strftime('%d', ${args[0]}/1000, 'unixepoch') AS INTEGER)`
241241
})
242+
.with("HOUR", () => {
243+
const args = this.arguments(ctx)
244+
return `CAST(strftime('%H', ${args[0]}/1000, 'unixepoch') AS INTEGER)`
245+
})
246+
.with("MINUTE", () => {
247+
const args = this.arguments(ctx)
248+
return `CAST(strftime('%M', ${args[0]}/1000, 'unixepoch') AS INTEGER)`
249+
})
250+
.with("SECOND", () => {
251+
const args = this.arguments(ctx)
252+
return `CAST(strftime('%S', ${args[0]}/1000, 'unixepoch') AS INTEGER)`
253+
})
254+
.with("WEEKDAY", () => {
255+
const args = this.arguments(ctx)
256+
return `CAST(strftime('%w', ${args[0]}/1000, 'unixepoch') AS INTEGER)`
257+
})
242258
.with("RECORD_ID", () => {
243259
return ID_TYPE
244260
})

packages/template/src/templates/test.base.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,34 @@
593593
"option": {
594594
"fn": "DAY({{date1}})"
595595
}
596+
},
597+
"Hour": {
598+
"id": "hour",
599+
"type": "formula",
600+
"option": {
601+
"fn": "HOUR({{date1}})"
602+
}
603+
},
604+
"Minute": {
605+
"id": "minute",
606+
"type": "formula",
607+
"option": {
608+
"fn": "MINUTE({{date1}})"
609+
}
610+
},
611+
"Second": {
612+
"id": "second",
613+
"type": "formula",
614+
"option": {
615+
"fn": "SECOND({{date1}})"
616+
}
617+
},
618+
"Weekday": {
619+
"id": "weekday",
620+
"type": "formula",
621+
"option": {
622+
"fn": "WEEKDAY({{date1}})"
623+
}
596624
}
597625
},
598626
"records": [

0 commit comments

Comments
 (0)