mirror of
https://github.com/rclone/rclone.git
synced 2025-08-19 01:46:31 +02:00
fs: fix parsing of times and durations of the form "YYYY-MM-DD HH:MM:SS"
Before this fix, the parsing code gave an error like this parsing "2022-08-02 07:00:00" as fs.Time failed: expected newline This was due to the Scan call failing to read all the data. This patch fixes that, and redoes the tests
This commit is contained in:
@@ -145,11 +145,28 @@ func TestDurationReadableString(t *testing.T) {
|
||||
}
|
||||
|
||||
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)
|
||||
now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)
|
||||
oldTimeNowFunc := timeNowFunc
|
||||
timeNowFunc = func() time.Time { return now }
|
||||
defer func() { timeNowFunc = oldTimeNowFunc }()
|
||||
|
||||
for _, test := range []struct {
|
||||
in string
|
||||
want Duration
|
||||
}{
|
||||
{"17m", Duration(17 * time.Minute)},
|
||||
{"-12h", Duration(-12 * time.Hour)},
|
||||
{"0", Duration(0)},
|
||||
{"off", DurationOff},
|
||||
{"2022-03-26T17:48:19Z", Duration(now.Sub(time.Date(2022, 03, 26, 17, 48, 19, 0, time.UTC)))},
|
||||
{"2022-03-26 17:48:19", Duration(now.Sub(time.Date(2022, 03, 26, 17, 48, 19, 0, time.Local)))},
|
||||
} {
|
||||
var got Duration
|
||||
n, err := fmt.Sscan(test.in, &got)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, n)
|
||||
assert.Equal(t, test.want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseUnmarshalJSON(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user