github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/util/sec_to_time.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package util
     7  
     8  import (
     9  	"fmt"
    10  	"strings"
    11  )
    12  
    13  // SecToTime converts an amount of seconds to a human-readable string. E.g.
    14  // 66s			-> 1 minute 6 seconds
    15  // 52410s		-> 14 hours 33 minutes
    16  // 563418		-> 6 days 12 hours
    17  // 1563418		-> 2 weeks 4 days
    18  // 3937125s     -> 1 month 2 weeks
    19  // 45677465s	-> 1 year 6 months
    20  func SecToTime(duration int64) string {
    21  	formattedTime := ""
    22  
    23  	// The following four variables are calculated by taking
    24  	// into account the previously calculated variables, this avoids
    25  	// pitfalls when using remainders. As that could lead to incorrect
    26  	// results when the calculated number equals the quotient number.
    27  	remainingDays := duration / (60 * 60 * 24)
    28  	years := remainingDays / 365
    29  	remainingDays -= years * 365
    30  	months := remainingDays * 12 / 365
    31  	remainingDays -= months * 365 / 12
    32  	weeks := remainingDays / 7
    33  	remainingDays -= weeks * 7
    34  	days := remainingDays
    35  	hours := (duration / 3600) % 24
    36  	minutes := (duration / 60) % 60
    37  	seconds := duration % 60
    38  
    39  	// Extract only the relevant information of the time
    40  	// If the time is greater than a year, it makes no sense to display seconds.
    41  	switch {
    42  	case years > 0:
    43  		formattedTime = formatTime(years, "year", formattedTime)
    44  		formattedTime = formatTime(months, "month", formattedTime)
    45  	case months > 0:
    46  		formattedTime = formatTime(months, "month", formattedTime)
    47  		formattedTime = formatTime(weeks, "week", formattedTime)
    48  	case weeks > 0:
    49  		formattedTime = formatTime(weeks, "week", formattedTime)
    50  		formattedTime = formatTime(days, "day", formattedTime)
    51  	case days > 0:
    52  		formattedTime = formatTime(days, "day", formattedTime)
    53  		formattedTime = formatTime(hours, "hour", formattedTime)
    54  	case hours > 0:
    55  		formattedTime = formatTime(hours, "hour", formattedTime)
    56  		formattedTime = formatTime(minutes, "minute", formattedTime)
    57  	default:
    58  		formattedTime = formatTime(minutes, "minute", formattedTime)
    59  		formattedTime = formatTime(seconds, "second", formattedTime)
    60  	}
    61  
    62  	// The formatTime() function always appends a space at the end. This will be trimmed
    63  	return strings.TrimRight(formattedTime, " ")
    64  }
    65  
    66  // formatTime appends the given value to the existing forammattedTime. E.g:
    67  // formattedTime = "1 year"
    68  // input: value = 3, name = "month"
    69  // output will be "1 year 3 months "
    70  func formatTime(value int64, name, formattedTime string) string {
    71  	if value == 1 {
    72  		formattedTime = fmt.Sprintf("%s1 %s ", formattedTime, name)
    73  	} else if value > 1 {
    74  		formattedTime = fmt.Sprintf("%s%d %ss ", formattedTime, value, name)
    75  	}
    76  
    77  	return formattedTime
    78  }