1
|
|
|
package timeago |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"math" |
5
|
|
|
"strconv" |
6
|
|
|
"time" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
var cachedJsonResults = map[string]Lang{} |
10
|
|
|
var globalOptions = []string{} |
11
|
|
|
|
12
|
|
|
// Parse coverts given datetime into `x time ago` format. |
13
|
|
|
// First argument can have 3 types: |
14
|
|
|
// 1. int (Unix timestamp) |
15
|
|
|
// 2. time.Time (Type from Go time package) |
16
|
|
|
// 3. string (Datetime string in format 'YYYY-MM-DD HH:MM:SS') |
17
|
|
|
func Parse(datetime interface{}, options ...string) string { |
18
|
1 |
|
var datetimeStr string |
19
|
|
|
|
20
|
1 |
|
switch date := datetime.(type) { |
21
|
|
|
case int: |
22
|
1 |
|
datetimeStr = parseTimestampToString(date) |
23
|
|
|
case time.Time: |
24
|
1 |
|
datetimeStr = date.Format("2006-01-02 15:04:05") |
25
|
|
|
default: |
26
|
|
|
datetimeStr = datetime.(string) |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
globalOptions = options |
30
|
|
|
|
31
|
1 |
|
return process(datetimeStr) |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
func process(datetime string) string { |
35
|
1 |
|
loc, _ := time.LoadLocation(config.Location) |
36
|
1 |
|
parsedTime, _ := time.ParseInLocation("2006-01-02 15:04:05", datetime, loc) |
37
|
|
|
|
38
|
1 |
|
seconds := int(time.Now().In(loc).Sub(parsedTime).Seconds()) |
39
|
|
|
|
40
|
1 |
|
switch { |
41
|
|
|
case seconds < 60 && optionIsEnabled("online"): |
42
|
|
|
return trans().Online |
43
|
|
|
case seconds < 0: |
44
|
|
|
return getWords("seconds", 0) |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
return calculateTheResult(seconds) |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
func calculateTheResult(seconds int) string { |
51
|
1 |
|
minutes, hours, days, weeks, months, years := getTimeCalculations(float64(seconds)) |
52
|
|
|
|
53
|
1 |
|
switch { |
54
|
|
|
case optionIsEnabled("online") && seconds < 60: |
55
|
|
|
return trans().Online |
56
|
|
|
case seconds < 60: |
57
|
|
|
return getWords("seconds", seconds) |
58
|
|
|
case minutes < 60: |
59
|
1 |
|
return getWords("minutes", minutes) |
60
|
|
|
case hours < 24: |
61
|
1 |
|
return getWords("hours", hours) |
62
|
|
|
case days < 7: |
63
|
1 |
|
return getWords("days", days) |
64
|
|
|
case weeks < 4: |
65
|
|
|
return getWords("weeks", weeks) |
66
|
|
|
case months < 12: |
67
|
|
|
if months == 0 { |
68
|
|
|
months = 1 |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return getWords("months", months) |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return getWords("years", years) |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
func getTimeCalculations(seconds float64) (int, int, int, int, int, int) { |
78
|
1 |
|
minutes := math.Round(seconds / 60) |
79
|
1 |
|
hours := math.Round(seconds / 3600) |
80
|
1 |
|
days := math.Round(seconds / 86400) |
81
|
1 |
|
weeks := math.Round(seconds / 604800) |
82
|
1 |
|
months := math.Round(seconds / 2629440) |
83
|
1 |
|
years := math.Round(seconds / 31553280) |
84
|
|
|
|
85
|
1 |
|
return int(minutes), int(hours), int(days), int(weeks), int(months), int(years) |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// getWords decides rather the word must be singular or plural, |
89
|
|
|
// and depending on the result it adds the correct word after |
90
|
|
|
// the time number |
91
|
|
|
func getWords(timeKind string, num int) string { |
92
|
1 |
|
form := getLanguageForm(num) |
93
|
1 |
|
time := getTimeTranslations() |
94
|
|
|
|
95
|
1 |
|
translation := time[timeKind][form] |
96
|
|
|
|
97
|
1 |
|
return strconv.Itoa(num) + " " + translation + " " + trans().Ago |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Check if option was passed by a Parse function |
101
|
|
|
func optionIsEnabled(searchOption string) bool { |
102
|
1 |
|
for _, option := range globalOptions { |
103
|
|
|
if option == searchOption { |
104
|
|
|
return true |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return false |
109
|
|
|
} |
110
|
|
|
|