util/time: fix duration parsing #220
@ -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
|
||||||
for i, r := range s {
|
loop:
|
||||||
switch r {
|
for i, r := range s {
|
||||||
case 'd':
|
switch r {
|
||||||
n, err := strconv.Atoi(s[idx:i])
|
case 's', 'm':
|
||||||
if err != nil {
|
p = i
|
||||||
return 0, errors.New("time: invalid duration " + s)
|
break loop
|
||||||
}
|
case 'h':
|
||||||
s[idx:i] = fmt.Sprintf("%d", n*24)
|
d, err := strconv.Atoi(s[p:i])
|
||||||
default:
|
if err != nil {
|
||||||
sb.WriteRune(r)
|
return 0, errors.New("time: invalid duration " + s)
|
||||||
}
|
}
|
||||||
}
|
hours += d
|
||||||
*/
|
p = i + 1
|
||||||
var td time.Duration
|
case 'd':
|
||||||
var err error
|
d, err := strconv.Atoi(s[p:i])
|
||||||
switch s[len(s)-1] {
|
if err != nil {
|
||||||
case 's', 'm', 'h':
|
return 0, errors.New("time: invalid duration " + s)
|
||||||
td, err = time.ParseDuration(s)
|
}
|
||||||
case 'd':
|
hours += d * 24
|
||||||
if td, err = time.ParseDuration(s[:len(s)-1] + "h"); err == nil {
|
p = i + 1
|
||||||
td *= 24
|
case 'y':
|
||||||
}
|
n, err := strconv.Atoi(s[p:i])
|
||||||
case 'y':
|
if err != nil {
|
||||||
if td, err = time.ParseDuration(s[:len(s)-1] + "h"); err == nil {
|
return 0, errors.New("time: invalid duration " + s)
|
||||||
year := time.Date(time.Now().Year(), time.December, 31, 0, 0, 0, 0, time.Local)
|
}
|
||||||
days := year.YearDay()
|
var d int
|
||||||
td *= 24 * time.Duration(days)
|
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