Passed
Push — master ( 2df23a...0c8182 )
by Serhii
01:29
created

timeago.go   A

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
cc 26
eloc 78
dl 0
loc 117
rs 10
c 0
b 0
f 0
ccs 47
cts 47
cp 1
crap 26

6 Methods

Rating   Name   Duplication   Size   Complexity  
B timeago.getWords 0 21 7
A timeago.getLastNumber 0 5 1
A timeago.trans 0 6 2
A timeago.getOption 0 10 2
D timeago.Take 0 39 13
A timeago.getTimeTranslations 0 9 1
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
	minutes := seconds / 60
28 1
	hours := seconds / 3600
29 1
	days := seconds / 86400
30 1
	weeks := seconds / 604800
31 1
	months := seconds / 2629440
32 1
	years := seconds / 31553280
33
34 1
	switch {
35
	case hasOption && option == "online" && seconds < 60:
36 1
		return trans("online")
37
	case seconds < 60:
38 1
		return getWords("seconds", seconds)
39
	case minutes < 60:
40 1
		return getWords("minutes", minutes)
41
	case hours < 24:
42 1
		return getWords("hours", hours)
43
	case days < 7:
44 1
		return getWords("days", days)
45
	case weeks < 4:
46 1
		return getWords("weeks", weeks)
47
	case months < 12:
48 1
		return getWords("months", months)
49
	}
50
51 1
	return getWords("years", years)
52
}
53
54
func getLastNumber(num int) int {
55 1
	numStr := strconv.Itoa(num)
56 1
	result, _ := strconv.Atoi(numStr[len(numStr)-1:])
57
58 1
	return result
59
}
60
61
func getWords(timeKind string, num int) string {
62 1
	lastNum := getLastNumber(num)
63 1
	var index int
64
65 1
	switch {
66
	case lastNum == 1 && num == 11:
67 1
		index = 2
68 1
		break
69
	case lastNum == 1:
70 1
		index = 0
71 1
		break
72
	case lastNum > 1 && lastNum < 5:
73 1
		index = 1
74 1
		break
75
	default:
76 1
		index = 2
77
	}
78
79 1
	timeTrans := getTimeTranslations()
80
81 1
	return strconv.Itoa(num) + " " + timeTrans[timeKind][index] + " " + trans("ago")
82
}
83
84
// getTimeTranslations returns array of translations for different
85
// cases. For example `1 second` must not have `s` at the end
86
// but `2 seconds` requires `s`. So this method keeps all
87
// possible options for the translated word.
88
func getTimeTranslations() map[string][]string {
89 1
	return map[string][]string{
90
		"seconds": {trans("second"), trans("seconds"), trans("seconds2")},
91
		"minutes": {trans("minute"), trans("minutes"), trans("minutes2")},
92
		"hours":   {trans("hour"), trans("hours"), trans("hours2")},
93
		"days":    {trans("day"), trans("days"), trans("days2")},
94
		"weeks":   {trans("week"), trans("weeks"), trans("weeks2")},
95
		"months":  {trans("month"), trans("months"), trans("months2")},
96
		"years":   {trans("year"), trans("years"), trans("years2")},
97
	}
98
}
99
100
func getOption(datetime *string) (string, bool) {
101 1
	date := *datetime
102 1
	spittedDateString := strings.Split(date, "|")
103
104 1
	if len(spittedDateString) > 1 {
105 1
		*datetime = spittedDateString[0]
106 1
		return spittedDateString[1], true
107
	}
108
109 1
	return "", false
110
}
111
112
func trans(key string) string {
113 1
	if language == "ru" {
114 1
		return getRussian()[key]
115
	}
116
117 1
	return getEnglish()[key]
118
}
119