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:
Nick Craig-Wood
2022-08-05 15:23:44 +01:00
parent 28925414b8
commit 274eca148c
4 changed files with 42 additions and 17 deletions

View File

@@ -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) {