fix(filters): try to parse date filter fields of the provided dates are not valid iso dates
continuous-integration/drone/push Build is passing Details

Resolves https://community.vikunja.io/t/due-date-saved-filter-doesnt-seem-to-work/966/12
This commit is contained in:
kolaente 2022-11-10 16:39:44 +01:00
parent 811514855b
commit 3d7605591e
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 13 additions and 2 deletions

View File

@ -24,6 +24,7 @@ import (
"time"
"code.vikunja.io/api/pkg/config"
"github.com/iancoleman/strcase"
"github.com/vectordotdev/go-datemath"
"xorm.io/xorm/schemas"
@ -44,6 +45,9 @@ const (
taskFilterComparatorIn taskFilterComparator = "in"
)
// Guess what you get back if you ask Safari for a rfc 3339 formatted date?
const safariDate = "2006-01-02 15:04"
type taskFilter struct {
field string
value interface{} // Needs to be an interface to be able to hold the field's native value
@ -51,6 +55,14 @@ type taskFilter struct {
isNumeric bool
}
func parseTimeFromUserInput(timeString string) (value time.Time, err error) {
value, err = time.Parse(time.RFC3339, timeString)
if err != nil {
value, err = time.Parse(safariDate, timeString)
}
return value.In(config.GetTimeZone()), err
}
func getTaskFiltersByCollections(c *TaskCollection) (filters []*taskFilter, err error) {
if len(c.FilterByArr) > 0 {
@ -170,8 +182,7 @@ func getValueForField(field reflect.StructField, rawValue string) (value interfa
if err == nil {
value = t.Time(datemath.WithLocation(config.GetTimeZone()))
} else {
value, err = time.Parse(time.RFC3339, rawValue)
value = value.(time.Time).In(config.GetTimeZone())
value, err = parseTimeFromUserInput(rawValue)
}
}
case reflect.Slice: