Skip to content

Commit a15720b

Browse files
xinaumpvl
authored andcommitted
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>
1 parent a7eda13 commit a15720b

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

pkg/time/duration.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ func Duration(s string) (bool, error) {
5050
return true, nil
5151
}
5252

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+
5363
// ParseDuration reports the nanoseconds represented by a duration string.
5464
//
5565
// A duration string is a possibly signed sequence of

pkg/time/duration_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ func TestDuration(t *testing.T) {
4848
}
4949
}
5050

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+
5171
func TestParseDuration(t *testing.T) {
5272
valid := []struct {
5373
in string

pkg/time/pkg.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/time/testdata/duration.txtar

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+

0 commit comments

Comments
 (0)