Passed
Push — master ( c9d870...ccc043 )
by Serhii
02:31 queued 49s
created

timeago.optionIsEnabled   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 3
nop 1
1
package timeago
2
3
import (
4
	"log"
5
	"math"
6
	"strconv"
7
	"time"
8
)
9
10
var cachedJsonResults = map[string]lang{}
11
var globalOptions = []string{}
12
13
// Parse coverts given datetime into `x time ago` format.
14
// First argument can have 3 types:
15
// 1. int (Unix timestamp)
16
// 2. time.Time (Type from Go time package)
17
// 3. string (Datetime string in format 'YYYY-MM-DD HH:MM:SS')
18
func Parse(datetime interface{}, options ...string) string {
19 1
	var input time.Time
20
21 1
	switch date := datetime.(type) {
22
	case int:
23 1
		input = parseTimestampToTime(date)
24
	case string:
25
		if config.Location == "" {
26
			parsedTime, _ := time.Parse("2006-01-02 15:04:05", date)
27
			input = parsedTime
28
		} else {
29
			location, err := time.LoadLocation(config.Location)
30
31
			if err != nil {
32
				log.Fatalf("Error in timeago package: %v", err)
33
			}
34
35
			parsedTime, _ := time.ParseInLocation("2006-01-02 15:04:05", date, location)
36
			input = parsedTime
37
		}
38
	default:
39 1
		input = datetime.(time.Time)
40
	}
41
42 1
	globalOptions = options
43
44 1
	return process(input)
45
}
46
47
func process(datetime time.Time) string {
48 1
	now := time.Now()
49
50 1
	if config.Location != "" {
51
		location, err := time.LoadLocation(config.Location)
52
53
		if err != nil {
54
			log.Fatalf("Location error in timeago package: %v\n", err)
55
		} else {
56
			now = now.In(location)
57
		}
58
	}
59
60 1
	seconds := now.Sub(datetime).Seconds()
61
62 1
	if seconds < 0 {
63 1
		enableOption("upcoming")
64 1
		seconds = math.Abs(seconds)
65
	}
66
67 1
	switch {
68
	case seconds < 60 && optionIsEnabled("online"):
69
		return trans().Online
70
	case seconds < 0:
71
		return getWords("seconds", 0)
72
	}
73
74 1
	return calculateTheResult(int(seconds))
75
}
76
77
func calculateTheResult(seconds int) string {
78 1
	minutes, hours, days, weeks, months, years := getTimeCalculations(float64(seconds))
79
80 1
	switch {
81
	case optionIsEnabled("online") && seconds < 60:
82
		return trans().Online
83
	case optionIsEnabled("justNow") && seconds < 60:
84
		return trans().JustNow
85
	case seconds < 60:
86
		return getWords("seconds", seconds)
87
	case minutes < 60:
88 1
		return getWords("minutes", minutes)
89
	case hours < 24:
90 1
		return getWords("hours", hours)
91
	case days < 7:
92 1
		return getWords("days", days)
93
	case weeks < 4:
94
		return getWords("weeks", weeks)
95
	case months < 12:
96
		if months == 0 {
97
			months = 1
98
		}
99
100
		return getWords("months", months)
101
	}
102
103
	return getWords("years", years)
104
}
105
106
func getTimeCalculations(seconds float64) (int, int, int, int, int, int) {
107 1
	minutes := math.Round(seconds / 60)
108 1
	hours := math.Round(seconds / 3600)
109 1
	days := math.Round(seconds / 86400)
110 1
	weeks := math.Round(seconds / 604800)
111 1
	months := math.Round(seconds / 2629440)
112 1
	years := math.Round(seconds / 31553280)
113
114 1
	return int(minutes), int(hours), int(days), int(weeks), int(months), int(years)
115
}
116
117
// getWords decides rather the word must be singular or plural,
118
// and depending on the result it adds the correct word after
119
// the time number
120
func getWords(timeKind string, num int) string {
121 1
	form := getLanguageForm(num)
122 1
	time := getTimeTranslations()
123
124 1
	translation := time[timeKind][form]
125 1
	result := strconv.Itoa(num) + " " + translation
126
127 1
	if optionIsEnabled("noSuffix") || optionIsEnabled("upcoming") {
128 1
		return result
129
	}
130
131 1
	return result + " " + trans().Ago
132
}
133
134
func optionIsEnabled(searchOption string) bool {
135 1
	for _, option := range globalOptions {
136 1
		if option == searchOption {
137 1
			return true
138
		}
139
	}
140
141 1
	return false
142
}
143
144
func enableOption(option string) {
145 1
	globalOptions = append(globalOptions, option)
146
}
147