Add day format support for duration comparison

This commit is contained in:
Elouan Martinet 2020-11-16 18:16:11 +01:00
parent c23e21cb41
commit 2150942876

View File

@ -3,6 +3,7 @@ package core
import (
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"
@ -186,7 +187,15 @@ func sanitizeAndResolve(list []string, result *Result) []string {
func sanitizeAndResolveNumerical(list []string, result *Result) []int64 {
var sanitizedNumbers []int64
sanitizedList := sanitizeAndResolve(list, result)
dayRegex := regexp.MustCompile(`^\s*(\d+)d\s*$`)
for _, element := range sanitizedList {
if matches := dayRegex.FindStringSubmatch(element); matches != nil {
if days, err := strconv.ParseInt(matches[1], 10, 64); err == nil {
duration := time.Duration(days * int64(time.Hour * 24))
sanitizedNumbers = append(sanitizedNumbers, duration.Milliseconds())
continue
}
}
if duration, err := time.ParseDuration(element); duration != 0 && err == nil {
sanitizedNumbers = append(sanitizedNumbers, duration.Milliseconds())
} else if number, err := strconv.ParseInt(element, 10, 64); err != nil {