code.gitea.io/gitea@v1.19.3/modules/util/sec_to_time.go (about)

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