From 998ffe52551a741c2540d8ca2957573c203677ec Mon Sep 17 00:00:00 2001 From: James Elliott Date: Mon, 8 May 2023 15:57:11 +1000 Subject: [PATCH] refactor: strip word and from duration (#5412) Signed-off-by: James Elliott --- .../content/en/configuration/prologue/common.md | 17 ++++++++++------- internal/utils/time.go | 2 ++ internal/utils/time_test.go | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/docs/content/en/configuration/prologue/common.md b/docs/content/en/configuration/prologue/common.md index 5d6162dc3..06455d23d 100644 --- a/docs/content/en/configuration/prologue/common.md +++ b/docs/content/en/configuration/prologue/common.md @@ -28,11 +28,14 @@ If you supply an integer, it is considered a representation of seconds. If you s blocks of quantities and units (number followed by a unit letter). For example `5h` indicates a quantity of 5 units of `h`. -The following is ignored: +The following is ignored or stripped from the input: - all spaces - leading zeros + - the word `and` -While you can use multiple of these blocks in combination, we suggest keeping it simple and use a single value. +While you can use multiple of these blocks in combination, we suggest keeping it simple and use a single value. In +addition it's important to note that the format while somewhat human readable still requires you closely follow the +expected formats. #### Unit Legend @@ -52,11 +55,11 @@ v4.38.0 or newer. #### Examples -| Desired Value | Configuration Examples | -|:---------------------:|:-------------------------------------:| -| 1 hour and 30 minutes | `90m` or `1h30m` or `5400` or `5400s` | -| 1 day | `1d` or `24h` or `86400` or `86400s` | -| 10 hours | `10h` or `600m` or `9h60m` or `36000` | +| Desired Value | Configuration Examples (Short) | Configuration Examples (Long) | +|:---------------------:|:-------------------------------------:|:---------------------------------------:| +| 1 hour and 30 minutes | `90m` or `1h30m` or `5400` or `5400s` | `1 hour and 30 mninutes` | +| 1 day | `1d` or `24h` or `86400` or `86400s` | `1 day` | +| 10 hours | `10h` or `600m` or `9h60m` or `36000` | `10 hours` | ### Address diff --git a/internal/utils/time.go b/internal/utils/time.go index c044f899a..19884784a 100644 --- a/internal/utils/time.go +++ b/internal/utils/time.go @@ -13,6 +13,8 @@ func StandardizeDurationString(input string) (output string, err error) { return "0s", nil } + input = strings.ReplaceAll(input, "and", "") + matches := reDurationStandard.FindAllStringSubmatch(strings.ReplaceAll(input, " ", ""), -1) if len(matches) == 0 { diff --git a/internal/utils/time_test.go b/internal/utils/time_test.go index 39b011fa6..d1e1b6681 100644 --- a/internal/utils/time_test.go +++ b/internal/utils/time_test.go @@ -109,6 +109,23 @@ func TestParseDurationString(t *testing.T) { } } +func TestStandardizeDurationString(t *testing.T) { + var ( + actual string + err error + ) + + actual, err = StandardizeDurationString("1 hour and 20 minutes") + + assert.NoError(t, err) + assert.Equal(t, "1h20m", actual) + + actual, err = StandardizeDurationString("1 hour and 20 minutes") + + assert.NoError(t, err) + assert.Equal(t, "1h20m", actual) +} + func TestParseDurationString_ShouldNotParseDurationStringWithOutOfOrderQuantitiesAndUnits(t *testing.T) { duration, err := ParseDurationString("h1")