github.com/cilium/statedb@v0.3.2/internal/time.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package internal
     5  
     6  import (
     7  	"fmt"
     8  	"time"
     9  )
    10  
    11  func PrettySince(t time.Time) string {
    12  	return PrettyDuration(time.Since(t))
    13  }
    14  
    15  func PrettyDuration(d time.Duration) string {
    16  	ago := float64(d) / float64(time.Microsecond)
    17  
    18  	// micros
    19  	if ago < 1000.0 {
    20  		return fmt.Sprintf("%.1fus", ago)
    21  	}
    22  
    23  	// millis
    24  	ago /= 1000.0
    25  	if ago < 1000.0 {
    26  		return fmt.Sprintf("%.1fms", ago)
    27  	}
    28  	// secs
    29  	ago /= 1000.0
    30  	if ago < 60.0 {
    31  		return fmt.Sprintf("%.1fs", ago)
    32  	}
    33  	// mins
    34  	ago /= 60.0
    35  	if ago < 60.0 {
    36  		return fmt.Sprintf("%.1fm", ago)
    37  	}
    38  	// hours
    39  	ago /= 60.0
    40  	return fmt.Sprintf("%.1fh", ago)
    41  }