Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

fs: fix durations like 1d12h being rejected

Rohit Beherad8794f819279
diff --git a/docs/content/docs.md b/docs/content/docs.md
index 22103b356..e06a54aad 100644
--- a/docs/content/docs.md
+++ b/docs/content/docs.md
@@ -844,7 +844,7 @@ as a number of seconds. The following suffixes are valid:
- `M` - Months
- `y` - Years
-Examples: "10", "300ms", "-1.5h" or "2h45m".
+Examples: "10", "300ms", "-1.5h", "2h45m" or "1d12h".
### Size options
diff --git a/fs/parseduration.go b/fs/parseduration.go
index ab16f6dbe..2c03132f4 100644
--- a/fs/parseduration.go
+++ b/fs/parseduration.go
@@ -69,6 +69,63 @@ func parseDurationSuffixes(age string) (time.Duration, error) {
return time.Duration(period), nil
}
+// parseDurationSequence parses a possibly signed sequence of numbers each
+// with a unit, eg "1d12h", where a unit is one of the ageSuffixes or one
+// accepted by time.ParseDuration.
+func parseDurationSequence(age string) (time.Duration, error) {
+ s := age
+ sign := 1.0
+ if s != "" && (s[0] == '-' || s[0] == '+') {
+ if s[0] == '-' {
+ sign = -1
+ }
+ s = s[1:]
+ }
+ if s == "" {
+ return 0, fmt.Errorf("invalid duration %q", age)
+ }
+ isNumber := func(c byte) bool { return c == '.' || (c >= '0' && c <= '9') }
+ var total float64
+ for s != "" {
+ i := 0
+ for i < len(s) && isNumber(s[i]) {
+ i++
+ }
+ j := i
+ for j < len(s) && !isNumber(s[j]) {
+ j++
+ }
+ number, unit := s[:i], s[i:j]
+ s = s[j:]
+ if number == "" || unit == "" {
+ return 0, fmt.Errorf("invalid duration %q", age)
+ }
+ value, err := strconv.ParseFloat(number, 64)
+ if err != nil {
+ return 0, fmt.Errorf("invalid duration %q: %w", age, err)
+ }
+ found := false
+ for _, ageSuffix := range ageSuffixes {
+ if ageSuffix.Suffix != "" && unit == ageSuffix.Suffix {
+ total += value * float64(ageSuffix.Multiplier)
+ found = true
+ break
+ }
+ }
+ if !found {
+ d, err := time.ParseDuration(number + unit)
+ if err != nil {
+ return 0, fmt.Errorf("invalid duration %q: %w", age, err)
+ }
+ total += float64(d)
+ }
+ }
+ if total >= math.MaxInt64 {
+ return 0, fmt.Errorf("duration %q is too long", age)
+ }
+ return time.Duration(sign * total), nil
+}
+
// time formats to try parsing ages as - in order
var timeFormats = []string{
time.RFC3339,
@@ -117,6 +174,11 @@ func parseDurationFromNow(age string, getNow func() time.Time) (d time.Duration,
return d, nil
}
+ d, err = parseDurationSequence(age)
+ if err == nil {
+ return d, nil
+ }
+
d, err = parseDurationDates(age, getNow())
if err == nil {
return d, nil
diff --git a/fs/parseduration_test.go b/fs/parseduration_test.go
index fa2230f75..2b08fe32a 100644
--- a/fs/parseduration_test.go
+++ b/fs/parseduration_test.go
@@ -45,6 +45,16 @@ func TestParseDuration(t *testing.T) {
{"1x", 0, true},
{"off", time.Duration(DurationOff), false},
{"1h2m3s", time.Hour + 2*time.Minute + 3*time.Second, false},
+ {"1d12h", time.Hour * 36, false},
+ {"1w2d", time.Hour * 24 * 9, false},
+ {"1y1d", time.Hour * 24 * 366, false},
+ {"1.5d12h", time.Hour * 48, false},
+ {"-1d12h", -time.Hour * 36, false},
+ {"1d2h3m4s", time.Hour*26 + 3*time.Minute + 4*time.Second, false},
+ {"1d12", 0, true},
+ {"1d12x", 0, true},
+ {"1d-2h", 0, true},
+ {"300y1d", 0, true},
{"2001-02-03", now.Sub(time.Date(2001, 2, 3, 0, 0, 0, 0, time.Local)), false},
{"2001-02-03 10:11:12", now.Sub(time.Date(2001, 2, 3, 10, 11, 12, 0, time.Local)), false},
{"2001-08-03 10:11:12", now.Sub(time.Date(2001, 8, 3, 10, 11, 12, 0, time.Local)), false},