mirror of
https://github.com/rclone/rclone.git
synced 2024-11-07 09:04:52 +01:00
fs: Implement Scan method for SizeSuffix and Duration
This commit is contained in:
parent
028f8a69d3
commit
1c80e84f8a
@ -1,6 +1,8 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -17,6 +19,13 @@ func (d Duration) String() string {
|
||||
if d == DurationOff {
|
||||
return "off"
|
||||
}
|
||||
for i := len(ageSuffixes) - 2; i >= 0; i-- {
|
||||
ageSuffix := &ageSuffixes[i]
|
||||
if math.Abs(float64(d)) >= float64(ageSuffix.Multiplier) {
|
||||
timeUnits := float64(d) / float64(ageSuffix.Multiplier)
|
||||
return strconv.FormatFloat(timeUnits, 'f', -1, 64) + ageSuffix.Suffix
|
||||
}
|
||||
}
|
||||
return time.Duration(d).String()
|
||||
}
|
||||
|
||||
@ -30,10 +39,6 @@ var ageSuffixes = []struct {
|
||||
Suffix string
|
||||
Multiplier time.Duration
|
||||
}{
|
||||
{Suffix: "ms", Multiplier: time.Millisecond},
|
||||
{Suffix: "s", Multiplier: time.Second},
|
||||
{Suffix: "m", Multiplier: time.Minute},
|
||||
{Suffix: "h", Multiplier: time.Hour},
|
||||
{Suffix: "d", Multiplier: time.Hour * 24},
|
||||
{Suffix: "w", Multiplier: time.Hour * 24 * 7},
|
||||
{Suffix: "M", Multiplier: time.Hour * 24 * 30},
|
||||
@ -51,6 +56,12 @@ func ParseDuration(age string) (time.Duration, error) {
|
||||
return time.Duration(DurationOff), nil
|
||||
}
|
||||
|
||||
// Attempt to parse as a time.Duration first
|
||||
d, err := time.ParseDuration(age)
|
||||
if err == nil {
|
||||
return d, nil
|
||||
}
|
||||
|
||||
for _, ageSuffix := range ageSuffixes {
|
||||
if strings.HasSuffix(age, ageSuffix.Suffix) {
|
||||
numberString := age[:len(age)-len(ageSuffix.Suffix)]
|
||||
@ -81,3 +92,12 @@ func (d *Duration) Set(s string) error {
|
||||
func (d Duration) Type() string {
|
||||
return "duration"
|
||||
}
|
||||
|
||||
// Scan implements the fmt.Scanner interface
|
||||
func (d *Duration) Scan(s fmt.ScanState, ch rune) error {
|
||||
token, err := s.Token(true, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return d.Set(string(token))
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -23,6 +24,7 @@ func TestParseDuration(t *testing.T) {
|
||||
{"1ms", time.Millisecond, false},
|
||||
{"1s", time.Second, false},
|
||||
{"1m", time.Minute, false},
|
||||
{"1.5m", (3 * time.Minute) / 2, false},
|
||||
{"1h", time.Hour, false},
|
||||
{"1d", time.Hour * 24, false},
|
||||
{"1w", time.Hour * 24 * 7, false},
|
||||
@ -33,6 +35,7 @@ func TestParseDuration(t *testing.T) {
|
||||
{"1.s", time.Second, false},
|
||||
{"1x", 0, true},
|
||||
{"off", time.Duration(DurationOff), false},
|
||||
{"1h2m3s", time.Hour + 2*time.Minute + 3*time.Second, false},
|
||||
} {
|
||||
duration, err := ParseDuration(test.in)
|
||||
if test.err {
|
||||
@ -52,9 +55,39 @@ func TestDurationString(t *testing.T) {
|
||||
{time.Duration(0), "0s"},
|
||||
{time.Second, "1s"},
|
||||
{time.Minute, "1m0s"},
|
||||
{time.Millisecond, "1ms"},
|
||||
{time.Second, "1s"},
|
||||
{(3 * time.Minute) / 2, "1m30s"},
|
||||
{time.Hour, "1h0m0s"},
|
||||
{time.Hour * 24, "1d"},
|
||||
{time.Hour * 24 * 7, "1w"},
|
||||
{time.Hour * 24 * 30, "1M"},
|
||||
{time.Hour * 24 * 365, "1y"},
|
||||
{time.Hour * 24 * 365 * 3 / 2, "1.5y"},
|
||||
{-time.Second, "-1s"},
|
||||
{time.Second, "1s"},
|
||||
{time.Duration(DurationOff), "off"},
|
||||
{time.Hour + 2*time.Minute + 3*time.Second, "1h2m3s"},
|
||||
{time.Hour * 24, "1d"},
|
||||
{time.Hour * 24 * 7, "1w"},
|
||||
{time.Hour * 24 * 30, "1M"},
|
||||
{time.Hour * 24 * 365, "1y"},
|
||||
{time.Hour * 24 * 365 * 3 / 2, "1.5y"},
|
||||
{-time.Hour * 24 * 365 * 3 / 2, "-1.5y"},
|
||||
} {
|
||||
got := Duration(test.in).String()
|
||||
assert.Equal(t, test.want, got)
|
||||
// Test the reverse
|
||||
reverse, err := ParseDuration(test.want)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, test.in, reverse)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDurationScan(t *testing.T) {
|
||||
var v Duration
|
||||
n, err := fmt.Sscan(" 17m ", &v)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, n)
|
||||
assert.Equal(t, Duration(17*60*time.Second), v)
|
||||
}
|
||||
|
@ -110,3 +110,12 @@ func (x *SizeSuffix) Set(s string) error {
|
||||
func (x *SizeSuffix) Type() string {
|
||||
return "int64"
|
||||
}
|
||||
|
||||
// Scan implements the fmt.Scanner interface
|
||||
func (x *SizeSuffix) Scan(s fmt.ScanState, ch rune) error {
|
||||
token, err := s.Token(true, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return x.Set(string(token))
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
@ -93,3 +94,11 @@ func TestSizeSuffixSet(t *testing.T) {
|
||||
assert.Equal(t, test.want, int64(ss))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSizeSuffixScan(t *testing.T) {
|
||||
var v SizeSuffix
|
||||
n, err := fmt.Sscan(" 17M ", &v)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, n)
|
||||
assert.Equal(t, SizeSuffix(17<<20), v)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user