Merge pull request 'util/time: fix duration parsing' (#219) from timefeature into v3
Reviewed-on: #219
This commit is contained in:
commit
ce125b77c1
@ -2,7 +2,9 @@ package time
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,39 +15,43 @@ func ParseDuration(s string) (time.Duration, error) {
|
|||||||
return 0, fmt.Errorf(`time: invalid duration "` + s + `"`)
|
return 0, fmt.Errorf(`time: invalid duration "` + s + `"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
//var sb strings.Builder
|
var p int
|
||||||
/*
|
var hours int
|
||||||
|
loop:
|
||||||
for i, r := range s {
|
for i, r := range s {
|
||||||
switch r {
|
switch r {
|
||||||
case 'd':
|
case 's', 'm':
|
||||||
n, err := strconv.Atoi(s[idx:i])
|
p = i
|
||||||
|
break loop
|
||||||
|
case 'h':
|
||||||
|
d, err := strconv.Atoi(s[p:i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.New("time: invalid duration " + s)
|
return 0, errors.New("time: invalid duration " + s)
|
||||||
}
|
}
|
||||||
s[idx:i] = fmt.Sprintf("%d", n*24)
|
hours += d
|
||||||
default:
|
p = i + 1
|
||||||
sb.WriteRune(r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
var td time.Duration
|
|
||||||
var err error
|
|
||||||
switch s[len(s)-1] {
|
|
||||||
case 's', 'm', 'h':
|
|
||||||
td, err = time.ParseDuration(s)
|
|
||||||
case 'd':
|
case 'd':
|
||||||
if td, err = time.ParseDuration(s[:len(s)-1] + "h"); err == nil {
|
d, err := strconv.Atoi(s[p:i])
|
||||||
td *= 24
|
if err != nil {
|
||||||
|
return 0, errors.New("time: invalid duration " + s)
|
||||||
}
|
}
|
||||||
|
hours += d * 24
|
||||||
|
p = i + 1
|
||||||
case 'y':
|
case 'y':
|
||||||
if td, err = time.ParseDuration(s[:len(s)-1] + "h"); err == nil {
|
n, err := strconv.Atoi(s[p:i])
|
||||||
year := time.Date(time.Now().Year(), time.December, 31, 0, 0, 0, 0, time.Local)
|
if err != nil {
|
||||||
days := year.YearDay()
|
return 0, errors.New("time: invalid duration " + s)
|
||||||
td *= 24 * time.Duration(days)
|
}
|
||||||
|
var d int
|
||||||
|
for j := n - 1; j >= 0; j-- {
|
||||||
|
d += time.Date(time.Now().Year()+j, time.December, 31, 0, 0, 0, 0, time.Local).YearDay()
|
||||||
|
}
|
||||||
|
hours += d * 24
|
||||||
|
p = i + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return td, err
|
return time.ParseDuration(fmt.Sprintf("%dh%s", hours, s[p:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Duration) MarshalJSON() ([]byte, error) {
|
func (d Duration) MarshalJSON() ([]byte, error) {
|
||||||
|
@ -35,15 +35,14 @@ func TestUnmarshalJSON(t *testing.T) {
|
|||||||
func TestParseDuration(t *testing.T) {
|
func TestParseDuration(t *testing.T) {
|
||||||
var td time.Duration
|
var td time.Duration
|
||||||
var err error
|
var err error
|
||||||
t.Skip()
|
|
||||||
td, err = ParseDuration("14d4h")
|
td, err = ParseDuration("14d4h")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("ParseDuration error: %v", err)
|
t.Fatalf("ParseDuration error: %v", err)
|
||||||
}
|
}
|
||||||
if td.String() != "336h0m0s" {
|
if td.String() != "340h0m0s" {
|
||||||
t.Fatalf("ParseDuration 14d != 336h0m0s : %s", td.String())
|
t.Fatalf("ParseDuration 14d != 340h0m0s : %s", td.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
td, err = ParseDuration("1y")
|
td, err = ParseDuration("1y")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("ParseDuration error: %v", err)
|
t.Fatalf("ParseDuration error: %v", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user