@@ -8,59 +8,17 @@ import (
8
8
"strings"
9
9
)
10
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 (durationVal any ) string {
11
+ // SecToHours converts an amount of seconds to a human-readable hours string.
12
+ // This is stable for planning and managing timesheets.
13
+ // Here it only supports hours and minutes, because a work day could contain 6 or 7 or 8 hours.
14
+ func SecToHours (durationVal any ) string {
19
15
duration , _ := ToInt64 (durationVal )
20
-
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
-
36
- // The following three variables are calculated without depending
37
- // on the previous calculated variables.
38
- hours := (duration / 3600 ) % 24
16
+ hours := duration / 3600
39
17
minutes := (duration / 60 ) % 60
40
- seconds := duration % 60
41
18
42
- // Extract only the relevant information of the time
43
- // If the time is greater than a year, it makes no sense to display seconds.
44
- switch {
45
- case years > 0 :
46
- formattedTime = formatTime (years , "year" , formattedTime )
47
- formattedTime = formatTime (months , "month" , formattedTime )
48
- case months > 0 :
49
- formattedTime = formatTime (months , "month" , formattedTime )
50
- formattedTime = formatTime (weeks , "week" , formattedTime )
51
- case weeks > 0 :
52
- formattedTime = formatTime (weeks , "week" , formattedTime )
53
- formattedTime = formatTime (days , "day" , formattedTime )
54
- case days > 0 :
55
- formattedTime = formatTime (days , "day" , formattedTime )
56
- formattedTime = formatTime (hours , "hour" , formattedTime )
57
- case hours > 0 :
58
- formattedTime = formatTime (hours , "hour" , formattedTime )
59
- formattedTime = formatTime (minutes , "minute" , formattedTime )
60
- default :
61
- formattedTime = formatTime (minutes , "minute" , formattedTime )
62
- formattedTime = formatTime (seconds , "second" , formattedTime )
63
- }
19
+ formattedTime := ""
20
+ formattedTime = formatTime (hours , "hour" , formattedTime )
21
+ formattedTime = formatTime (minutes , "minute" , formattedTime )
64
22
65
23
// The formatTime() function always appends a space at the end. This will be trimmed
66
24
return strings .TrimRight (formattedTime , " " )
@@ -76,6 +34,5 @@ func formatTime(value int64, name, formattedTime string) string {
76
34
} else if value > 1 {
77
35
formattedTime = fmt .Sprintf ("%s%d %ss " , formattedTime , value , name )
78
36
}
79
-
80
37
return formattedTime
81
38
}
0 commit comments