this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

pkg/time: add FormatDuration

issues: #1469

This change adds a new function to format a duration represented as an
integer in nanoseconds to a compact string representation. The string
representation is used in some applications configuration, I.e.
Prometheus.

It's possible to use CUE's string interpolation to provide a valid
configuration by dividing the nanoseconds by the smallest supported
duration multiplier and appending it's suffix. I.e.
"\(nanoseconds/time.Second)s"`.

But this has the downside that human readers of the resulting
configuration have to convert a duration to a more readable format
before being able to interpret the value inside as part of complete
configuration.

The implementation for this new function is the same as the String()
method of Go's time.Duration type of the standard library.

Signed-off-by: xinau <felix@ehrenpfort.de>
Change-Id: Ibfe455166ebc5f5b8f55f7360cefba1a05eac390
Signed-off-by: xinau <felix@ehrenpfort.de>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/531327
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>

authored by

xinau and committed by
Marcel van Lohuizen
a15720bc a7eda136

+83
+10
pkg/time/duration.go
··· 50 50 return true, nil 51 51 } 52 52 53 + // FormatDuration converts nanoseconds to a string representing the duration in 54 + // the form "72h3m0.5s". 55 + // 56 + // Leading zero units are omitted. As a special case, durations less than 57 + // one second use a smaller unit (milli-, micro-, or nanoseconds) to ensure 58 + // that the leading digit is non-zero. The zero duration formats as 0s. 59 + func FormatDuration(d int64) string { 60 + return time.Duration(d).String() 61 + } 62 + 53 63 // ParseDuration reports the nanoseconds represented by a duration string. 54 64 // 55 65 // A duration string is a possibly signed sequence of
+20
pkg/time/duration_test.go
··· 48 48 } 49 49 } 50 50 51 + func TestFormatDuration(t *testing.T) { 52 + valid := []struct { 53 + in int64 54 + out string 55 + }{ 56 + {3*Hour + 2*Minute, "3h2m0s"}, 57 + {5 * Second, "5s"}, 58 + {600 * Millisecond, "600ms"}, 59 + } 60 + 61 + for _, tc := range valid { 62 + t.Run(tc.out, func(t *testing.T) { 63 + s := FormatDuration(tc.in) 64 + if s != tc.out { 65 + t.Fatalf("got %v; want %v", s, tc.out) 66 + } 67 + }) 68 + } 69 + } 70 + 51 71 func TestParseDuration(t *testing.T) { 52 72 valid := []struct { 53 73 in string
+12
pkg/time/pkg.go
··· 48 48 } 49 49 }, 50 50 }, { 51 + Name: "FormatDuration", 52 + Params: []internal.Param{ 53 + {Kind: adt.IntKind}, 54 + }, 55 + Result: adt.StringKind, 56 + Func: func(c *internal.CallCtxt) { 57 + d := c.Int64(0) 58 + if c.Do() { 59 + c.Ret = FormatDuration(d) 60 + } 61 + }, 62 + }, { 51 63 Name: "ParseDuration", 52 64 Params: []internal.Param{ 53 65 {Kind: adt.StringKind},
+41
pkg/time/testdata/duration.txtar
··· 1 + // Copyright 2022 CUE Authors 2 + // 3 + // Licensed under the Apache License, Version 2.0 (the "License"); 4 + // you may not use this file except in compliance with the License. 5 + // You may obtain a copy of the License at 6 + // 7 + // http://www.apache.org/licenses/LICENSE-2.0 8 + // 9 + // Unless required by applicable law or agreed to in writing, software 10 + // distributed under the License is distributed on an "AS IS" BASIS, 11 + // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 + // See the License for the specific language governing permissions and 13 + // limitations under the License. 14 + 15 + 16 + -- in.cue -- 17 + import "time" 18 + 19 + FormatDuration: [...{ 20 + arg: int 21 + out: time.FormatDuration(arg) 22 + }] 23 + 24 + FormatDuration: [ 25 + {arg: 10920000000000}, 26 + {arg: 5000000000}, 27 + {arg: 600000000}, 28 + ] 29 + 30 + -- out/time -- 31 + FormatDuration: [{ 32 + arg: 10920000000000 33 + out: "3h2m0s" 34 + }, { 35 + arg: 5000000000 36 + out: "5s" 37 + }, { 38 + arg: 600000000 39 + out: "600ms" 40 + }] 41 +