Compare commits

..

4 Commits

Author SHA1 Message Date
75173560e3 Merge pull request 'util/time: ParseDuration fix' (#222) from timefix into v3
Reviewed-on: #222
2023-05-29 14:04:41 +03:00
9b3bccd1f1 util/time: ParseDuration fix
All checks were successful
lint / lint (pull_request) Successful in 1m0s
pr / test (pull_request) Successful in 58s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-05-29 14:02:06 +03:00
ce125b77c1 Merge pull request 'util/time: fix duration parsing' (#219) from timefeature into v3
Reviewed-on: #219
2023-05-27 23:55:51 +03:00
2ee8d4ed46 util/time: fix duration parsing
Some checks failed
lint / lint (pull_request) Successful in 59s
pr / test (pull_request) Failing after 1m0s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-05-27 23:55:08 +03:00
2 changed files with 47 additions and 35 deletions

View File

@@ -2,7 +2,9 @@ package time
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"strconv"
"time" "time"
) )
@@ -13,39 +15,42 @@ 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 { break loop
return 0, errors.New("time: invalid duration " + s) case 'h':
} d, err := strconv.Atoi(s[p:i])
s[idx:i] = fmt.Sprintf("%d", n*24) if err != nil {
default: return 0, errors.New("time: invalid duration " + s)
sb.WriteRune(r)
} }
} 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) {
@@ -62,7 +67,7 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
*d = Duration(time.Duration(value)) *d = Duration(time.Duration(value))
return nil return nil
case string: case string:
dv, err := time.ParseDuration(value) dv, err := ParseDuration(value)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -23,27 +23,34 @@ func TestUnmarshalJSON(t *testing.T) {
TTL Duration `json:"ttl"` TTL Duration `json:"ttl"`
} }
v := &str{} v := &str{}
var err error
err := json.Unmarshal([]byte(`{"ttl":"10ms"}`), v) err = json.Unmarshal([]byte(`{"ttl":"10ms"}`), v)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} else if v.TTL != 10000000 { } else if v.TTL != 10000000 {
t.Fatalf("invalid duration %v != 10000000", v.TTL) t.Fatalf("invalid duration %v != 10000000", v.TTL)
} }
err = json.Unmarshal([]byte(`{"ttl":"1y"}`), v)
if err != nil {
t.Fatal(err)
} else if v.TTL != 31536000000000000 {
t.Fatalf("invalid duration %v != 31536000000000000", v.TTL)
}
} }
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)