|
4 | 4 |
|
5 | 5 | package util
|
6 | 6 |
|
7 |
| -import "fmt" |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | +) |
8 | 11 |
|
9 |
| -// SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s) |
| 12 | +// SecToTime converts an amount of seconds to a human-readable string. E.g. |
| 13 | +// 66s -> 1 minute 6 seconds |
| 14 | +// 52410s -> 14 hours 33 minutes |
| 15 | +// 563418 -> 6 days 12 hours |
| 16 | +// 1563418 -> 2 weeks 4 days |
| 17 | +// 3937125s -> 1 month 2 weeks |
| 18 | +// 45677465s -> 1 year 6 months |
10 | 19 | func SecToTime(duration int64) string {
|
| 20 | + formattedTime := "" |
| 21 | + years := duration / (3600 * 24 * 7 * 4 * 12) |
| 22 | + months := (duration / (3600 * 24 * 30)) % 12 |
| 23 | + weeks := (duration / (3600 * 24 * 7)) % 4 |
| 24 | + days := (duration / (3600 * 24)) % 7 |
| 25 | + hours := (duration / 3600) % 24 |
| 26 | + minutes := (duration / 60) % 60 |
11 | 27 | seconds := duration % 60
|
12 |
| - minutes := (duration / (60)) % 60 |
13 |
| - hours := duration / (60 * 60) % 24 |
14 |
| - days := duration / (60 * 60) / 24 |
15 | 28 |
|
16 |
| - var formattedTime string |
17 |
| - |
18 |
| - if days > 0 { |
19 |
| - formattedTime = fmt.Sprintf("%dd", days) |
20 |
| - } |
21 |
| - if hours > 0 { |
22 |
| - if formattedTime == "" { |
23 |
| - formattedTime = fmt.Sprintf("%dh", hours) |
24 |
| - } else { |
25 |
| - formattedTime = fmt.Sprintf("%s %dh", formattedTime, hours) |
26 |
| - } |
27 |
| - } |
28 |
| - if minutes > 0 { |
29 |
| - if formattedTime == "" { |
30 |
| - formattedTime = fmt.Sprintf("%dm", minutes) |
31 |
| - } else { |
32 |
| - formattedTime = fmt.Sprintf("%s %dm", formattedTime, minutes) |
33 |
| - } |
| 29 | + // Extract only the relevant information of the time |
| 30 | + // If the time is greater than a year, it makes no sense to display seconds. |
| 31 | + switch { |
| 32 | + case years > 0: |
| 33 | + formattedTime = formatTime(years, "year", formattedTime) |
| 34 | + formattedTime = formatTime(months, "month", formattedTime) |
| 35 | + case months > 0: |
| 36 | + formattedTime = formatTime(months, "month", formattedTime) |
| 37 | + formattedTime = formatTime(weeks, "week", formattedTime) |
| 38 | + case weeks > 0: |
| 39 | + formattedTime = formatTime(weeks, "week", formattedTime) |
| 40 | + formattedTime = formatTime(days, "day", formattedTime) |
| 41 | + case days > 0: |
| 42 | + formattedTime = formatTime(days, "day", formattedTime) |
| 43 | + formattedTime = formatTime(hours, "hour", formattedTime) |
| 44 | + case hours > 0: |
| 45 | + formattedTime = formatTime(hours, "hour", formattedTime) |
| 46 | + formattedTime = formatTime(minutes, "minute", formattedTime) |
| 47 | + default: |
| 48 | + formattedTime = formatTime(minutes, "minute", formattedTime) |
| 49 | + formattedTime = formatTime(seconds, "second", formattedTime) |
34 | 50 | }
|
35 |
| - if seconds > 0 { |
36 |
| - if formattedTime == "" { |
37 |
| - formattedTime = fmt.Sprintf("%ds", seconds) |
38 |
| - } else { |
39 |
| - formattedTime = fmt.Sprintf("%s %ds", formattedTime, seconds) |
40 |
| - } |
| 51 | + |
| 52 | + // The formatTime() function always appends a space at the end. This will be trimmed |
| 53 | + return strings.TrimRight(formattedTime, " ") |
| 54 | +} |
| 55 | + |
| 56 | +// formatTime appends the given value to the existing forammattedTime. E.g: |
| 57 | +// formattedTime = "1 year" |
| 58 | +// input: value = 3, name = "month" |
| 59 | +// output will be "1 year 3 months " |
| 60 | +func formatTime(value int64, name, formattedTime string) string { |
| 61 | + if value == 1 { |
| 62 | + formattedTime = fmt.Sprintf("%s1 %s ", formattedTime, name) |
| 63 | + } else if value > 1 { |
| 64 | + formattedTime = fmt.Sprintf("%s%d %ss ", formattedTime, value, name) |
41 | 65 | }
|
42 | 66 |
|
43 | 67 | return formattedTime
|
|
0 commit comments