mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 10:19:50 +00:00
Enhance shortHumanDuration to handle more situation
Allow 2 seconds deviation of current time. For time more than 2 seconds from current time, output as '<invalid>'.
This commit is contained in:
parent
501594f8c7
commit
c20ed0344a
@ -361,7 +361,13 @@ func podHostString(host, ip string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func shortHumanDuration(d time.Duration) string {
|
func shortHumanDuration(d time.Duration) string {
|
||||||
if seconds := int(d.Seconds()); seconds < 60 {
|
// Allow deviation no more than 2 seconds(excluded) to tolerate machine time
|
||||||
|
// inconsistence, it can be considered as almost now.
|
||||||
|
if seconds := int(d.Seconds()); seconds < -1 {
|
||||||
|
return fmt.Sprintf("<invalid>")
|
||||||
|
} else if seconds < 0 {
|
||||||
|
return fmt.Sprintf("0s")
|
||||||
|
} else if seconds < 60 {
|
||||||
return fmt.Sprintf("%ds", seconds)
|
return fmt.Sprintf("%ds", seconds)
|
||||||
} else if minutes := int(d.Minutes()); minutes < 60 {
|
} else if minutes := int(d.Minutes()); minutes < 60 {
|
||||||
return fmt.Sprintf("%dm", minutes)
|
return fmt.Sprintf("%dm", minutes)
|
||||||
|
@ -1104,6 +1104,8 @@ type stringTestList []struct {
|
|||||||
|
|
||||||
func TestTranslateTimestamp(t *testing.T) {
|
func TestTranslateTimestamp(t *testing.T) {
|
||||||
tl := stringTestList{
|
tl := stringTestList{
|
||||||
|
{"a while from now", translateTimestamp(util.Time{Time: time.Now().Add(2.1e9)}), "<invalid>"},
|
||||||
|
{"almost now", translateTimestamp(util.Time{Time: time.Now().Add(1.9e9)}), "0s"},
|
||||||
{"now", translateTimestamp(util.Time{Time: time.Now()}), "0s"},
|
{"now", translateTimestamp(util.Time{Time: time.Now()}), "0s"},
|
||||||
{"unknown", translateTimestamp(util.Time{}), "<unknown>"},
|
{"unknown", translateTimestamp(util.Time{}), "<unknown>"},
|
||||||
{"30 seconds ago", translateTimestamp(util.Time{Time: time.Now().Add(-3e10)}), "30s"},
|
{"30 seconds ago", translateTimestamp(util.Time{Time: time.Now().Add(-3e10)}), "30s"},
|
||||||
|
Loading…
Reference in New Issue
Block a user