github.com/oam-dev/kubevela@v1.9.11/references/cli/top/utils/time.go (about) 1 /* 2 Copyright 2022 The KubeVela 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 utils 18 19 import ( 20 "fmt" 21 "strconv" 22 "strings" 23 "time" 24 ) 25 26 // UnknownTime represent the time can't successfully be formatted 27 const UnknownTime = "UNKNOWN" 28 29 // TimeFormat format time data of `time.Duration` type to string type 30 func TimeFormat(t time.Duration) string { 31 timeStr := t.String() 32 // time < 1s 33 if t.Seconds() < 1 { 34 return timeStr 35 } 36 // cut in the place of decimal point 37 removeDecimal := strings.Split(timeStr, ".") 38 if len(removeDecimal) > 1 { 39 removeDecimal[0] += "s" 40 } 41 // cut in the place of 'h' 42 convertHourToDay := strings.Split(removeDecimal[0], "h") 43 if len(convertHourToDay) > 1 { 44 // hour num 45 hour, err := strconv.Atoi(convertHourToDay[0]) 46 if err != nil { 47 return UnknownTime 48 } 49 if hour > 24 { 50 return fmt.Sprintf("%dd%dh%s", hour/24, hour%24, convertHourToDay[1]) 51 } 52 } 53 return removeDecimal[0] 54 55 }