"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
fs: fix parsing of times and durations of the form "YYYY-MM-DD HH:MM:SS"
Nick Craig-Wood16039b350da4
diff --git a/fs/parseduration.go b/fs/parseduration.goindex 23e749446..fe7d09986 100644--- a/fs/parseduration.go+++ b/fs/parseduration.go@@ -126,7 +126,7 @@ func parseDurationFromNow(age string, getNow func() time.Time) (d time.Duration,// ParseDuration parses a duration string. Accept ms|s|m|h|d|w|M|y suffixes. Defaults to second if not providedfunc ParseDuration(age string) (time.Duration, error) {- return parseDurationFromNow(age, time.Now)+ return parseDurationFromNow(age, timeNowFunc)}// ReadableString parses d into a human-readable duration.@@ -216,7 +216,7 @@ func (d *Duration) UnmarshalJSON(in []byte) error {// Scan implements the fmt.Scanner interfacefunc (d *Duration) Scan(s fmt.ScanState, ch rune) error {- token, err := s.Token(true, nil)+ token, err := s.Token(true, func(rune) bool { return true })if err != nil {return err}diff --git a/fs/parseduration_test.go b/fs/parseduration_test.goindex f0efba4bd..4a5dbb16c 100644--- a/fs/parseduration_test.go+++ b/fs/parseduration_test.go@@ -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) {diff --git a/fs/parsetime.go b/fs/parsetime.goindex 145cfa0de..7e762451d 100644--- a/fs/parsetime.go+++ b/fs/parsetime.go@@ -83,7 +83,7 @@ func (t *Time) UnmarshalJSON(in []byte) error {// Scan implements the fmt.Scanner interfacefunc (t *Time) Scan(s fmt.ScanState, ch rune) error {- token, err := s.Token(true, nil)+ token, err := s.Token(true, func(rune) bool { return true })if err != nil {return err}diff --git a/fs/parsetime_test.go b/fs/parsetime_test.goindex 5784b971a..21d34c1ee 100644--- a/fs/parsetime_test.go+++ b/fs/parsetime_test.go@@ -93,15 +93,23 @@ func TestTimeScan(t *testing.T) {timeNowFunc = func() time.Time { return now }defer func() { timeNowFunc = oldTimeNowFunc }()- var v1, v2, v3, v4, v5 Time- n, err := fmt.Sscan(" 17m -12h 0 off 2022-03-26T17:48:19Z ", &v1, &v2, &v3, &v4, &v5)- require.NoError(t, err)- assert.Equal(t, 5, n)- assert.Equal(t, Time(now.Add(-17*time.Minute)), v1)- assert.Equal(t, Time(now.Add(12*time.Hour)), v2)- assert.Equal(t, Time(now), v3)- assert.Equal(t, Time(time.Time{}), v4)- assert.Equal(t, Time(time.Date(2022, 03, 26, 17, 48, 19, 0, time.UTC)), v5)+ for _, test := range []struct {+ in string+ want Time+ }{+ {"17m", Time(now.Add(-17 * time.Minute))},+ {"-12h", Time(now.Add(12 * time.Hour))},+ {"0", Time(now)},+ {"off", Time(time.Time{})},+ {"2022-03-26T17:48:19Z", Time(time.Date(2022, 03, 26, 17, 48, 19, 0, time.UTC))},+ {"2022-03-26 17:48:19", Time(time.Date(2022, 03, 26, 17, 48, 19, 0, time.Local))},+ } {+ var got Time+ n, err := fmt.Sscan(test.in, &got)+ require.NoError(t, err)+ assert.Equal(t, 1, n)+ assert.Equal(t, test.want, got)+ }}func TestParseTimeUnmarshalJSON(t *testing.T) {