2019-08-15 17:46:21 +03:00
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2019-08-15 17:46:21 +03:00
package timeutil
import (
"strings"
"time"
2022-06-26 17:19:22 +03:00
"code.gitea.io/gitea/modules/translation"
2019-08-15 17:46:21 +03:00
)
// Seconds-based time units
const (
Minute = 60
Hour = 60 * Minute
Day = 24 * Hour
Week = 7 * Day
Month = 30 * Day
Year = 12 * Month
)
2022-06-26 17:19:22 +03:00
func computeTimeDiffFloor ( diff int64 , lang translation . Locale ) ( int64 , string ) {
2023-04-22 21:53:00 +03:00
var diffStr string
2019-08-15 17:46:21 +03:00
switch {
case diff <= 0 :
diff = 0
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.now" )
2019-08-15 17:46:21 +03:00
case diff < 2 :
diff = 0
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.1s" )
2019-08-15 17:46:21 +03:00
case diff < 1 * Minute :
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.seconds" , diff )
2019-08-15 17:46:21 +03:00
diff = 0
case diff < 2 * Minute :
diff -= 1 * Minute
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.1m" )
2019-08-15 17:46:21 +03:00
case diff < 1 * Hour :
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.minutes" , diff / Minute )
2019-08-15 17:46:21 +03:00
diff -= diff / Minute * Minute
case diff < 2 * Hour :
diff -= 1 * Hour
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.1h" )
2019-08-15 17:46:21 +03:00
case diff < 1 * Day :
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.hours" , diff / Hour )
2019-08-15 17:46:21 +03:00
diff -= diff / Hour * Hour
case diff < 2 * Day :
diff -= 1 * Day
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.1d" )
2019-08-15 17:46:21 +03:00
case diff < 1 * Week :
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.days" , diff / Day )
2019-08-15 17:46:21 +03:00
diff -= diff / Day * Day
case diff < 2 * Week :
diff -= 1 * Week
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.1w" )
2019-08-15 17:46:21 +03:00
case diff < 1 * Month :
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.weeks" , diff / Week )
2019-08-15 17:46:21 +03:00
diff -= diff / Week * Week
case diff < 2 * Month :
diff -= 1 * Month
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.1mon" )
2019-08-15 17:46:21 +03:00
case diff < 1 * Year :
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.months" , diff / Month )
2019-08-15 17:46:21 +03:00
diff -= diff / Month * Month
case diff < 2 * Year :
diff -= 1 * Year
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.1y" )
2019-08-15 17:46:21 +03:00
default :
2024-02-15 00:48:45 +03:00
diffStr = lang . TrString ( "tool.years" , diff / Year )
2019-08-15 17:46:21 +03:00
diff -= ( diff / Year ) * Year
}
return diff , diffStr
}
2024-11-04 14:30:00 +03:00
// MinutesToFriendly returns a user-friendly string with number of minutes
2019-08-15 17:46:21 +03:00
// converted to hours and minutes.
2022-06-26 17:19:22 +03:00
func MinutesToFriendly ( minutes int , lang translation . Locale ) string {
2019-08-15 17:46:21 +03:00
duration := time . Duration ( minutes ) * time . Minute
2024-11-04 14:30:00 +03:00
return timeSincePro ( time . Now ( ) . Add ( - duration ) , time . Now ( ) , lang )
2019-08-15 17:46:21 +03:00
}
2022-06-26 17:19:22 +03:00
func timeSincePro ( then , now time . Time , lang translation . Locale ) string {
2019-08-15 17:46:21 +03:00
diff := now . Unix ( ) - then . Unix ( )
if then . After ( now ) {
2024-02-15 00:48:45 +03:00
return lang . TrString ( "tool.future" )
2019-08-15 17:46:21 +03:00
}
if diff == 0 {
2024-02-15 00:48:45 +03:00
return lang . TrString ( "tool.now" )
2019-08-15 17:46:21 +03:00
}
var timeStr , diffStr string
for {
if diff == 0 {
break
}
2021-01-28 15:29:22 +03:00
diff , diffStr = computeTimeDiffFloor ( diff , lang )
2019-08-15 17:46:21 +03:00
timeStr += ", " + diffStr
}
return strings . TrimPrefix ( timeStr , ", " )
}