1
|
|
|
package timeago |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"strconv" |
5
|
|
|
"strings" |
6
|
|
|
"time" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
// Take coverts given datetime into `x time ago` format. |
10
|
|
|
// For displaying `Online` word if date interval within |
11
|
|
|
// 60 seconds, add `|online` flag to the datetime string. |
12
|
|
|
// Format must be [year-month-day hours:minutes:seconds} |
13
|
|
|
func Take(datetime string) string { |
14
|
1 |
|
option, hasOption := getOption(&datetime) |
15
|
|
|
|
16
|
1 |
|
loc, _ := time.LoadLocation(location) |
17
|
1 |
|
parsedTime, _ := time.ParseInLocation("2006-01-02 15:04:05", datetime, loc) |
18
|
1 |
|
seconds := int(time.Now().In(loc).Sub(parsedTime).Seconds()) |
19
|
|
|
|
20
|
1 |
|
switch { |
21
|
|
|
case seconds < 0 && option == "online": |
22
|
1 |
|
return trans("online") |
23
|
|
|
case seconds < 0: |
24
|
1 |
|
return getWords("seconds", 0) |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
return calculateTheResult(seconds, hasOption, option) |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
func calculateTheResult(seconds int, hasOption bool, option string) string { |
31
|
1 |
|
minutes := seconds / 60 |
32
|
1 |
|
hours := seconds / 3600 |
33
|
1 |
|
days := seconds / 86400 |
34
|
1 |
|
weeks := seconds / 604800 |
35
|
1 |
|
months := seconds / 2629440 |
36
|
1 |
|
years := seconds / 31553280 |
37
|
|
|
|
38
|
1 |
|
switch { |
39
|
|
|
case hasOption && option == "online" && seconds < 60: |
40
|
1 |
|
return trans("online") |
41
|
|
|
case seconds < 60: |
42
|
1 |
|
return getWords("seconds", seconds) |
43
|
|
|
case minutes < 60: |
44
|
1 |
|
return getWords("minutes", minutes) |
45
|
|
|
case hours < 24: |
46
|
1 |
|
return getWords("hours", hours) |
47
|
|
|
case days < 7: |
48
|
1 |
|
return getWords("days", days) |
49
|
|
|
case weeks < 4: |
50
|
1 |
|
return getWords("weeks", weeks) |
51
|
|
|
case months < 12: |
52
|
1 |
|
return getWords("months", months) |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
return getWords("years", years) |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
func getLastNumber(num int) int { |
59
|
1 |
|
numStr := strconv.Itoa(num) |
60
|
1 |
|
result, _ := strconv.Atoi(numStr[len(numStr)-1:]) |
61
|
|
|
|
62
|
1 |
|
return result |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
func getWords(timeKind string, num int) string { |
66
|
1 |
|
lastNum := getLastNumber(num) |
67
|
1 |
|
var index int |
68
|
|
|
|
69
|
1 |
|
switch { |
70
|
|
|
case lastNum == 1 && num == 11: |
71
|
1 |
|
index = 2 |
72
|
1 |
|
break |
73
|
|
|
case lastNum == 1: |
74
|
1 |
|
index = 0 |
75
|
1 |
|
break |
76
|
|
|
case lastNum > 1 && lastNum < 5: |
77
|
1 |
|
index = 1 |
78
|
1 |
|
break |
79
|
|
|
default: |
80
|
1 |
|
index = 2 |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
timeTrans := getTimeTranslations() |
84
|
|
|
|
85
|
1 |
|
return strconv.Itoa(num) + " " + timeTrans[timeKind][index] + " " + trans("ago") |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// getTimeTranslations returns array of translations for different |
89
|
|
|
// cases. For example `1 second` must not have `s` at the end |
90
|
|
|
// but `2 seconds` requires `s`. So this method keeps all |
91
|
|
|
// possible options for the translated word. |
92
|
|
|
func getTimeTranslations() map[string][]string { |
93
|
1 |
|
return map[string][]string{ |
94
|
|
|
"seconds": {trans("second"), trans("seconds"), trans("seconds2")}, |
95
|
|
|
"minutes": {trans("minute"), trans("minutes"), trans("minutes2")}, |
96
|
|
|
"hours": {trans("hour"), trans("hours"), trans("hours2")}, |
97
|
|
|
"days": {trans("day"), trans("days"), trans("days2")}, |
98
|
|
|
"weeks": {trans("week"), trans("weeks"), trans("weeks2")}, |
99
|
|
|
"months": {trans("month"), trans("months"), trans("months2")}, |
100
|
|
|
"years": {trans("year"), trans("years"), trans("years2")}, |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
func getOption(datetime *string) (string, bool) { |
105
|
1 |
|
date := *datetime |
106
|
1 |
|
spittedDateString := strings.Split(date, "|") |
107
|
|
|
|
108
|
1 |
|
if len(spittedDateString) > 1 { |
109
|
1 |
|
*datetime = spittedDateString[0] |
110
|
1 |
|
return spittedDateString[1], true |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
return "", false |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
func trans(key string) string { |
117
|
1 |
|
if language == "ru" { |
118
|
1 |
|
return getRussian()[key] |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
return getEnglish()[key] |
122
|
|
|
} |
123
|
|
|
|