From 7486bfea5bbbcccdf01379c36c2af9fe937c1962 Mon Sep 17 00:00:00 2001 From: plumbeo Date: Wed, 14 Nov 2018 16:28:17 +0100 Subject: [PATCH] Extend custom Duration granularity to hours and add tests --- internal/restic/duration.go | 14 ++++++++++---- internal/restic/duration_test.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/internal/restic/duration.go b/internal/restic/duration.go index 09289849b..ad56bcc81 100644 --- a/internal/restic/duration.go +++ b/internal/restic/duration.go @@ -10,9 +10,9 @@ import ( ) // Duration is similar to time.Duration, except it only supports larger ranges -// like days, months, and years. +// like hours, days, months, and years. type Duration struct { - Days, Months, Years int + Hours, Days, Months, Years int } func (d Duration) String() string { @@ -29,6 +29,10 @@ func (d Duration) String() string { s += fmt.Sprintf("%dd", d.Days) } + if d.Hours != 0 { + s += fmt.Sprintf("%dh", d.Hours) + } + return s } @@ -73,7 +77,7 @@ func nextNumber(input string) (num int, rest string, err error) { } // ParseDuration parses a duration from a string. The format is: -// 6y5m234d +// 6y5m234d37h func ParseDuration(s string) (Duration, error) { var ( d Duration @@ -100,6 +104,8 @@ func ParseDuration(s string) (Duration, error) { d.Months = num case 'd': d.Days = num + case 'h': + d.Hours = num } s = s[1:] @@ -127,5 +133,5 @@ func (d Duration) Type() string { // Zero returns true if the duration is empty (all values are set to zero). func (d Duration) Zero() bool { - return d.Years == 0 && d.Months == 0 && d.Days == 0 + return d.Years == 0 && d.Months == 0 && d.Days == 0 && d.Hours == 0 } diff --git a/internal/restic/duration_test.go b/internal/restic/duration_test.go index 0d5306069..716c00cc9 100644 --- a/internal/restic/duration_test.go +++ b/internal/restic/duration_test.go @@ -13,15 +13,24 @@ func TestNextNumber(t *testing.T) { rest string err bool }{ + { + input: "12h", num: 12, rest: "h", + }, { input: "3d", num: 3, rest: "d", }, + { + input: "4d9h", num: 4, rest: "d9h", + }, { input: "7m5d", num: 7, rest: "m5d", }, { input: "-23y7m5d", num: -23, rest: "y7m5d", }, + { + input: "-13y5m11d12h", num: -13, rest: "y5m11d12h", + }, { input: " 5d", num: 0, rest: " 5d", err: true, }, @@ -55,10 +64,15 @@ func TestParseDuration(t *testing.T) { d Duration output string }{ + {"9h", Duration{Hours: 9}, "9h"}, {"3d", Duration{Days: 3}, "3d"}, + {"4d2h", Duration{Days: 4, Hours: 2}, "4d2h"}, {"7m5d", Duration{Months: 7, Days: 5}, "7m5d"}, + {"6m4d8h", Duration{Months: 6, Days: 4, Hours: 8}, "6m4d8h"}, {"5d7m", Duration{Months: 7, Days: 5}, "7m5d"}, + {"4h3d9m", Duration{Months: 9, Days: 3, Hours: 4}, "9m3d4h"}, {"-7m5d", Duration{Months: -7, Days: 5}, "-7m5d"}, + {"1y4m-5d-3h", Duration{Years: 1, Months: 4, Days: -5, Hours: -3}, "1y4m-5d-3h"}, {"2y7m-5d", Duration{Years: 2, Months: 7, Days: -5}, "2y7m-5d"}, }