Skip to content

Commit

Permalink
Change FormatDuration to be consistent across language implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
xtineskim committed Aug 21, 2024
1 parent e6abec2 commit b7ca574
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions pkg/utils/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func FormatDuration(duration time.Duration) (string, error) {
Returns: string or error if duration cannot be expressed as a GEP-2257 Duration format.
*/

if duration == 0 {
return "0s", nil
}
Expand All @@ -86,37 +87,36 @@ func FormatDuration(duration time.Duration) (string, error) {
return "", errors.New("Cannot express sub-milliseconds precision in GEP-2257")
}

// Golang's time.Duration allows for floating point seconds instead of converting to ms
durationMilliseconds := duration.Milliseconds()
output := ""
seconds := int(duration.Seconds())

// calculating the hours
hours := int(seconds / 3600)

var ms int64
if durationMilliseconds%1000 != 0 {
ms = durationMilliseconds % 1000
durationMilliseconds -= ms
duration = time.Millisecond * time.Duration(durationMilliseconds)
if hours > 0 {
output += fmt.Sprintf("%dh", hours)
seconds -= hours * 3600
}

durationString := duration.String()
if ms > 0 {
durationString += fmt.Sprintf("%dms", ms)
// calculating the minutes
minutes := int(seconds / 60)

if minutes > 0 {
output += fmt.Sprintf("%dm", minutes)
seconds -= minutes * 60
}

// trim the 0 values from the string (for example, 30m0s should result in 30m)
// going to have a regexp that finds the index of the time units with 0, then appropriately trim those away
temp := reSplit.FindAll([]byte(durationString), -1)
res := ""
for _, t := range temp {
if t[0] != '0' {
res += string(t)
} else {
continue
}
if seconds > 0 {
output += fmt.Sprintf("%ds", seconds)
}

// check if there are floating number points
if !re.MatchString(res) {
return "", errors.New("Invalid duration format")
// Golang's time.Duration allows for floating point seconds instead of converting to ms
durationMilliseconds := durationMicroseconds / 1000

ms := durationMilliseconds % 1000
if ms != 0 {
output += fmt.Sprintf("%dms", ms)
}

return res, nil
return output, nil
}

0 comments on commit b7ca574

Please sign in to comment.