From 43b4c57d01a21b8544f0f167f6b6c410d9c0f3bb Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sun, 23 Jul 2023 22:15:30 +0800 Subject: [PATCH] :art: Attribute View date column calculate https://github.com/siyuan-note/siyuan/issues/8757 --- kernel/av/av.go | 30 ++++++++++++++++++++++++------ kernel/av/table.go | 6 +++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/kernel/av/av.go b/kernel/av/av.go index 24066d4af..d31461f80 100644 --- a/kernel/av/av.go +++ b/kernel/av/av.go @@ -25,6 +25,7 @@ import ( "path/filepath" "strconv" "strings" + "time" "github.com/88250/gulu" "github.com/88250/lute/ast" @@ -166,6 +167,29 @@ func (number *ValueNumber) FormatNumber() { } } +type ValueDate struct { + Content int64 `json:"content"` + Content2 int64 `json:"content2"` + HasEndDate bool `json:"hasEndDate"` + FormattedContent string `json:"formattedContent"` +} + +type DateFormat string + +const ( + DateFormatNone DateFormat = "" +) + +func NewFormattedValueDate(content int64, format DateFormat) (ret *ValueDate) { + ret = &ValueDate{ + Content: content, + Content2: 0, + HasEndDate: false, + FormattedContent: time.UnixMilli(content).Format("2006-01-02 15:04:05"), + } + return +} + // RoundUp rounds like 12.3416 -> 12.35 func RoundUp(val float64, precision int) float64 { return math.Ceil(val*(math.Pow10(precision))) / math.Pow10(precision) @@ -181,12 +205,6 @@ func Round(val float64, precision int) float64 { return math.Round(val*(math.Pow10(precision))) / math.Pow10(precision) } -type ValueDate struct { - Content int64 `json:"content"` - Content2 int64 `json:"content2"` - HasEndDate bool `json:"hasEndDate"` -} - type ValueSelect struct { Content string `json:"content"` Color string `json:"color"` diff --git a/kernel/av/table.go b/kernel/av/table.go index 5e3947f31..2d43a32e1 100644 --- a/kernel/av/table.go +++ b/kernel/av/table.go @@ -574,7 +574,7 @@ func (table *Table) calcColDate(col *TableColumn, colIndex int) { } } if 0 != earliest { - col.Calc.Result = &Value{Date: &ValueDate{Content: earliest}} + col.Calc.Result = &Value{Date: NewFormattedValueDate(earliest, DateFormatNone)} } case CalcOperatorLatest: latest := int64(0) @@ -586,7 +586,7 @@ func (table *Table) calcColDate(col *TableColumn, colIndex int) { } } if 0 != latest { - col.Calc.Result = &Value{Date: &ValueDate{Content: latest}} + col.Calc.Result = &Value{Date: NewFormattedValueDate(latest, DateFormatNone)} } case CalcOperatorRange: earliest := int64(0) @@ -602,7 +602,7 @@ func (table *Table) calcColDate(col *TableColumn, colIndex int) { } } if 0 != earliest && 0 != latest { - col.Calc.Result = &Value{Date: &ValueDate{Content: latest - earliest}} + col.Calc.Result = &Value{Date: NewFormattedValueDate(latest-earliest, DateFormatNone)} } } }