github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/duration/duration.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package duration 18 19 import ( 20 "fmt" 21 "time" 22 ) 23 24 // ShortHumanDuration returns a succint representation of the provided duration 25 // with limited precision for consumption by humans. 26 func ShortHumanDuration(d time.Duration) string { 27 // Allow deviation no more than 2 seconds(excluded) to tolerate machine time 28 // inconsistence, it can be considered as almost now. 29 if seconds := int(d.Seconds()); seconds < -1 { 30 return "<invalid>" 31 } else if seconds < 0 { 32 return "0s" 33 } else if seconds < 60 { 34 return fmt.Sprintf("%ds", seconds) 35 } else if minutes := int(d.Minutes()); minutes < 60 { 36 return fmt.Sprintf("%dm", minutes) 37 } else if hours := int(d.Hours()); hours < 24 { 38 return fmt.Sprintf("%dh", hours) 39 } else if hours < 24*365 { 40 return fmt.Sprintf("%dd", hours/24) 41 } 42 return fmt.Sprintf("%dy", int(d.Hours()/24/365)) 43 } 44 45 // HumanDuration returns a succint representation of the provided duration 46 // with limited precision for consumption by humans. It provides ~2-3 significant 47 // figures of duration. 48 func HumanDuration(d time.Duration) string { 49 // Allow deviation no more than 2 seconds(excluded) to tolerate machine time 50 // inconsistence, it can be considered as almost now. 51 if seconds := int(d.Seconds()); seconds < -1 { 52 return "<invalid>" 53 } else if seconds < 0 { 54 return "0s" 55 } else if seconds < 60*2 { 56 return fmt.Sprintf("%ds", seconds) 57 } 58 minutes := int(d / time.Minute) 59 if minutes < 10 { 60 s := int(d/time.Second) % 60 61 if s == 0 { 62 return fmt.Sprintf("%dm", minutes) 63 } 64 return fmt.Sprintf("%dm%ds", minutes, s) 65 } else if minutes < 60*3 { 66 return fmt.Sprintf("%dm", minutes) 67 } 68 hours := int(d / time.Hour) 69 if hours < 8 { 70 m := int(d/time.Minute) % 60 71 if m == 0 { 72 return fmt.Sprintf("%dh", hours) 73 } 74 return fmt.Sprintf("%dh%dm", hours, m) 75 } else if hours < 48 { 76 return fmt.Sprintf("%dh", hours) 77 } else if hours < 24*8 { 78 h := hours % 24 79 if h == 0 { 80 return fmt.Sprintf("%dd", hours/24) 81 } 82 return fmt.Sprintf("%dd%dh", hours/24, h) 83 } else if hours < 24*365*2 { 84 return fmt.Sprintf("%dd", hours/24) 85 } else if hours < 24*365*8 { 86 dy := int(hours/24) % 365 87 if dy == 0 { 88 return fmt.Sprintf("%dy", hours/24/365) 89 } 90 return fmt.Sprintf("%dy%dd", hours/24/365, dy) 91 } 92 return fmt.Sprintf("%dy", int(hours/24/365)) 93 }