github.com/blend/go-sdk@v1.20220411.3/timeutil/format_duration.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package timeutil
     9  
    10  import (
    11  	"fmt"
    12  	"time"
    13  )
    14  
    15  // FormatDuration formats a duration to it's nearest major increment.
    16  func FormatDuration(d time.Duration) string {
    17  	if d >= time.Hour {
    18  		return fmt.Sprintf("%dh", d.Round(time.Hour)/time.Hour)
    19  	}
    20  	if d >= time.Minute {
    21  		return fmt.Sprintf("%dm", d.Round(time.Minute)/time.Minute)
    22  	}
    23  	if d >= time.Second {
    24  		return fmt.Sprintf("%ds", d.Round(time.Second)/time.Second)
    25  	}
    26  	if d >= time.Millisecond {
    27  		return fmt.Sprintf("%dms", d.Round(time.Millisecond)/time.Millisecond)
    28  	}
    29  	if d >= time.Microsecond {
    30  		return fmt.Sprint(d.Round(time.Microsecond))
    31  	}
    32  	return fmt.Sprint(d)
    33  }