github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/common/format.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package common
    19  
    20  import (
    21  	"fmt"
    22  	"regexp"
    23  	"strings"
    24  	"time"
    25  )
    26  
    27  // PrettyDuration is a pretty printed version of a time.Duration value that cuts
    28  // the unnecessary precision off from the formatted textual representation.
    29  type PrettyDuration time.Duration
    30  
    31  var prettyDurationRe = regexp.MustCompile(`\.[0-9]+`)
    32  
    33  // String implements the Stringer interface, allowing pretty printing of duration
    34  // values rounded to three decimals.
    35  func (d PrettyDuration) String() string {
    36  	label := fmt.Sprintf("%v", time.Duration(d))
    37  	if match := prettyDurationRe.FindString(label); len(match) > 4 {
    38  		label = strings.Replace(label, match, match[:4], 1)
    39  	}
    40  	return label
    41  }
    42  
    43  // PrettyAge is a pretty printed version of a time.Duration value that rounds
    44  // the values up to a single most significant unit, days/weeks/years included.
    45  type PrettyAge time.Time
    46  
    47  // ageUnits is a list of units the age pretty printing uses.
    48  var ageUnits = []struct {
    49  	Size   time.Duration
    50  	Symbol string
    51  }{
    52  	{12 * 30 * 24 * time.Hour, "y"},
    53  	{30 * 24 * time.Hour, "mo"},
    54  	{7 * 24 * time.Hour, "w"},
    55  	{24 * time.Hour, "d"},
    56  	{time.Hour, "h"},
    57  	{time.Minute, "m"},
    58  	{time.Second, "s"},
    59  }
    60  
    61  // String implements the Stringer interface, allowing pretty printing of duration
    62  // values rounded to the most significant time unit.
    63  func (t PrettyAge) String() string {
    64  	// Calculate the time difference and handle the 0 cornercase
    65  	diff := time.Since(time.Time(t))
    66  	if diff < time.Second {
    67  		return "0"
    68  	}
    69  	// Accumulate a precision of 3 components before returning
    70  	result, prec := "", 0
    71  
    72  	for _, unit := range ageUnits {
    73  		if diff > unit.Size {
    74  			result = fmt.Sprintf("%s%d%s", result, diff/unit.Size, unit.Symbol)
    75  			diff %= unit.Size
    76  
    77  			if prec += 1; prec >= 3 {
    78  				break
    79  			}
    80  		}
    81  	}
    82  	return result
    83  }