github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/utils/format.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package utils
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  )
    21  
    22  const (
    23  	B  = 1
    24  	KB = 1 << 10
    25  	MB = 1 << 20
    26  	GB = 1 << 30
    27  	TB = 1 << 40
    28  	EB = 1 << 50
    29  )
    30  
    31  func FmtSize(size uint64) string {
    32  	if size < KB {
    33  		return fmt.Sprintf("%dB", size)
    34  	} else if size < MB {
    35  		return fmt.Sprintf("%d.%03dKB", size>>10, size%KB)
    36  	} else if size < GB {
    37  		return fmt.Sprintf("%d.%03dMB", size>>20, (size>>10)%KB)
    38  	} else if size < TB {
    39  		return fmt.Sprintf("%d.%03dGB", size>>30, (size>>20)%KB)
    40  	} else if size < EB {
    41  		return fmt.Sprintf("%d.%03dTB", size>>40, (size>>30)%KB)
    42  	} else {
    43  		return fmt.Sprintf("%d.%03dEB", size>>50, (size>>40)%KB)
    44  	}
    45  }
    46  
    47  func FmtUnixMillTime(ms int64) string {
    48  	return time.UnixMilli(ms).Format(time.DateTime)
    49  }
    50  
    51  func FmtUnixTime(s int64) string {
    52  	return time.Unix(s, 0).Format(time.DateTime)
    53  }