Skip to content

Commit

Permalink
[timeutil] MiniDuration improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Aug 14, 2024
1 parent bc15648 commit 3ce834a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
8 changes: 4 additions & 4 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ updates:
target-branch: "develop"
schedule:
interval: "daily"
timezone: "Europe/London"
time: "03:00"
timezone: "Etc/UTC"
time: "07:00"
labels:
- "PR • MAINTENANCE"
assignees:
Expand All @@ -26,8 +26,8 @@ updates:
target-branch: "develop"
schedule:
interval: "daily"
timezone: "Europe/London"
time: "04:00"
timezone: "Etc/UTC"
time: "08:00"
labels:
- "PR • MAINTENANCE"
assignees:
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
## Changelog

### [13.3.2](https://kaos.sh/ek/13.3.3)
### [13.3.4](https://kaos.sh/ek/13.3.4)

- `[timeutil]` Added support of minutes, hours and days to `MiniDuration`
- `[timeutil]` Added separator customization to `MiniDuration`

### [13.3.3](https://kaos.sh/ek/13.3.3)

- `[support/apps]` Fixed Docker version extraction
- `[support]` Improved output of large list of IPv4 and IPv6 addresses
Expand Down
15 changes: 15 additions & 0 deletions timeutil/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ func ExampleShortDuration() {
// 0:03.215
}

func ExampleMiniDuration() {
fmt.Println(MiniDuration(36 * time.Hour))
fmt.Println(MiniDuration(18 * time.Second))
fmt.Println(MiniDuration(time.Second / 125))

// You can remove or change separator
fmt.Println(MiniDuration(time.Second/2000, ""))

// Output:
// 2 d
// 18 s
// 8 ms
// 500μs
}

func ExampleDurationToSeconds() {
fmt.Println(DurationToSeconds(2500 * time.Millisecond))

Expand Down
42 changes: 33 additions & 9 deletions timeutil/timeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func PrettyDuration(d any) string {
}

if dur != 0 && dur < time.Second {
return getPrettyShortDuration(dur)
return getPrettyShortDuration(dur, " ")
}

return getPrettyLongDuration(dur)
Expand Down Expand Up @@ -94,9 +94,15 @@ func ShortDuration(d any, highPrecision ...bool) string {
return getShortDuration(dur, false)
}

// MiniDuration returns formatted value for short durations (e.g. s/ms/us/ns)
func MiniDuration(d time.Duration) string {
return getPrettyShortDuration(d)
// MiniDuration returns formatted value of duration (d/hr/m/s/ms/us/ns)
func MiniDuration(d time.Duration, separator ...string) string {
sep := " "

if len(separator) != 0 {
sep = separator[0]
}

return getPrettyShortDuration(d, sep)
}

// Format returns formatted date as a string
Expand Down Expand Up @@ -763,28 +769,46 @@ func getPrettySimpleDuration(dur time.Duration) string {

// codebeat:enable[BLOCK_NESTING]

func getPrettyShortDuration(d time.Duration) string {
func getPrettyShortDuration(d time.Duration, separator string) string {
switch {
case d >= 24*time.Hour:
return fmt.Sprintf(
"%.0g"+separator+"d",
formatFloat(float64(d)/float64(24*time.Hour)),
)

case d >= time.Hour:
return fmt.Sprintf(
"%.2g"+separator+"h",
formatFloat(float64(d)/float64(time.Hour)),
)

case d >= time.Minute:
return fmt.Sprintf(
"%.2g"+separator+"m",
formatFloat(float64(d)/float64(time.Minute)),
)

case d >= time.Second:
return fmt.Sprintf(
"%g s",
"%g"+separator+"s",
formatFloat(float64(d)/float64(time.Second)),
)

case d >= time.Millisecond:
return fmt.Sprintf(
"%g ms",
"%g"+separator+"ms",
formatFloat(float64(d)/float64(time.Millisecond)),
)

case d >= time.Microsecond:
return fmt.Sprintf(
"%g μs",
"%g"+separator+"μs",
formatFloat(float64(d)/float64(time.Microsecond)),
)

default:
return fmt.Sprintf("%d ns", d.Nanoseconds())
return fmt.Sprintf("%d"+separator+"ns", d.Nanoseconds())
}
}

Expand Down
4 changes: 4 additions & 0 deletions timeutil/timeutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ func (s *TimeUtilSuite) TestShortDuration(c *C) {
}

func (s *TimeUtilSuite) TestMiniDuration(c *C) {
c.Assert(MiniDuration(89*time.Hour, ""), Equals, "4d")
c.Assert(MiniDuration(time.Duration(0)), Equals, "0 ns")
c.Assert(MiniDuration(89*time.Hour), Equals, "4 d")
c.Assert(MiniDuration(15*time.Hour), Equals, "15 h")
c.Assert(MiniDuration(80*time.Second), Equals, "1.3 m")
c.Assert(MiniDuration(3*time.Second), Equals, "3 s")
c.Assert(MiniDuration(3*time.Millisecond), Equals, "3 ms")
c.Assert(MiniDuration(3*time.Microsecond), Equals, "3 μs")
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ package ek
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "13.3.3"
const VERSION = "13.4.0"

0 comments on commit 3ce834a

Please sign in to comment.