Passed
Push — master ( 1dbab0...58c7cb )
by Serhii
02:41 queued 50s
created

timeago.parseStringInputIntoTime   A

Complexity

Conditions 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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