🎨 Add specific time switch to database date field https://github.com/siyuan-note/siyuan/issues/9486

This commit is contained in:
Daniel 2023-10-27 21:16:54 +08:00
parent ee50814189
commit eb80232771
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 27 additions and 7 deletions

View file

@ -364,6 +364,7 @@ type ValueDate struct {
Content int64 `json:"content"`
IsNotEmpty bool `json:"isNotEmpty"`
HasEndDate bool `json:"hasEndDate"`
IsNotTime bool `json:"isNotTime"`
Content2 int64 `json:"content2"`
IsNotEmpty2 bool `json:"isNotEmpty2"`
FormattedContent string `json:"formattedContent"`
@ -376,10 +377,21 @@ const (
DateFormatDuration DateFormat = "duration"
)
func NewFormattedValueDate(content, content2 int64, format DateFormat) (ret *ValueDate) {
formatted := time.UnixMilli(content).Format("2006-01-02 15:04")
func NewFormattedValueDate(content, content2 int64, format DateFormat, isNotTime bool) (ret *ValueDate) {
var formatted string
if isNotTime {
formatted = time.UnixMilli(content).Format("2006-01-02")
} else {
formatted = time.UnixMilli(content).Format("2006-01-02 15:04")
}
if 0 < content2 {
formatted += " → " + time.UnixMilli(content2).Format("2006-01-02 15:04")
var formattedContent2 string
if isNotTime {
formattedContent2 = time.UnixMilli(content2).Format("2006-01-02")
} else {
formattedContent2 = time.UnixMilli(content2).Format("2006-01-02 15:04")
}
formatted += " → " + formattedContent2
}
switch format {
case DateFormatNone:
@ -392,6 +404,7 @@ func NewFormattedValueDate(content, content2 int64, format DateFormat) (ret *Val
Content: content,
Content2: content2,
HasEndDate: false,
IsNotTime: true,
FormattedContent: formatted,
}
return

View file

@ -1123,43 +1123,50 @@ func (table *Table) calcColDate(col *TableColumn, colIndex int) {
}
case CalcOperatorEarliest:
earliest := int64(0)
var isNotTime bool
for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Date && row.Cells[colIndex].Value.Date.IsNotEmpty {
if 0 == earliest || earliest > row.Cells[colIndex].Value.Date.Content {
earliest = row.Cells[colIndex].Value.Date.Content
isNotTime = row.Cells[colIndex].Value.Date.IsNotTime
}
}
}
if 0 != earliest {
col.Calc.Result = &Value{Date: NewFormattedValueDate(earliest, 0, DateFormatNone)}
col.Calc.Result = &Value{Date: NewFormattedValueDate(earliest, 0, DateFormatNone, isNotTime)}
}
case CalcOperatorLatest:
latest := int64(0)
var isNotTime bool
for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Date && row.Cells[colIndex].Value.Date.IsNotEmpty {
if 0 == latest || latest < row.Cells[colIndex].Value.Date.Content {
latest = row.Cells[colIndex].Value.Date.Content
isNotTime = row.Cells[colIndex].Value.Date.IsNotTime
}
}
}
if 0 != latest {
col.Calc.Result = &Value{Date: NewFormattedValueDate(latest, 0, DateFormatNone)}
col.Calc.Result = &Value{Date: NewFormattedValueDate(latest, 0, DateFormatNone, isNotTime)}
}
case CalcOperatorRange:
earliest := int64(0)
latest := int64(0)
var isNotTime bool
for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Date && row.Cells[colIndex].Value.Date.IsNotEmpty {
if 0 == earliest || earliest > row.Cells[colIndex].Value.Date.Content {
earliest = row.Cells[colIndex].Value.Date.Content
isNotTime = row.Cells[colIndex].Value.Date.IsNotTime
}
if 0 == latest || latest < row.Cells[colIndex].Value.Date.Content {
latest = row.Cells[colIndex].Value.Date.Content
isNotTime = row.Cells[colIndex].Value.Date.IsNotTime
}
}
}
if 0 != earliest && 0 != latest {
col.Calc.Result = &Value{Date: NewFormattedValueDate(earliest, latest, DateFormatDuration)}
col.Calc.Result = &Value{Date: NewFormattedValueDate(earliest, latest, DateFormatDuration, isNotTime)}
}
}
}

View file

@ -1946,7 +1946,7 @@ func exportTree(tree *parse.Tree, wysiwyg, expandKaTexMacros, keepFold bool,
if nil != cell.Value {
if av.KeyTypeDate == cell.Value.Type {
if nil != cell.Value.Date {
cell.Value.Date = av.NewFormattedValueDate(cell.Value.Date.Content, cell.Value.Date.Content2, av.DateFormatNone)
cell.Value.Date = av.NewFormattedValueDate(cell.Value.Date.Content, cell.Value.Date.Content2, av.DateFormatNone, cell.Value.Date.IsNotTime)
}
} else if av.KeyTypeCreated == cell.Value.Type {
if nil != cell.Value.Created {