Passed
Push — master ( 510e0f...ad64d3 )
by Serhii
02:43 queued 01:09
created

tests.subYears   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
crap 1
nop 1
1
package tests
2
3
import (
4
	"time"
5
6
	"github.com/SerhiiCho/timeago"
7
)
8
9
const (
10
	second time.Duration = time.Second
11
	minute time.Duration = time.Minute
12
	hour   time.Duration = time.Hour
13
	day    time.Duration = hour * 24
14
	week   time.Duration = day * 7
15
	// we cannot add month and year because months and years cannot have static values
16
)
17
18
func subTime(duration time.Duration) time.Time {
19 1
	return time.Now().Add(-duration)
20
}
21
22
func subSeconds(duration time.Duration) time.Time {
23 1
	return subTime(second * duration)
24
}
25
26
func subMinutes(duration time.Duration) time.Time {
27 1
	return subTime(minute * duration)
28
}
29
30
func subHours(duration time.Duration) time.Time {
31 1
	return subTime(hour * duration)
32
}
33
34
func subDays(duration time.Duration) time.Time {
35 1
	return subTime(day * duration)
36
}
37
38
func subWeeks(duration time.Duration) time.Time {
39 1
	return subTime(week * duration)
40
}
41
42
func subMonths(duration time.Duration) time.Time {
43 1
	return subTime(duration * getDaysInMonth() * day)
44
}
45
46
func subYears(duration time.Duration) time.Time {
47 1
	return subTime(duration * getDaysInYear() * day)
48
}
49
50
func getDaysInYear() time.Duration {
51 1
	lastDayOfTheYear := time.Date(time.Now().Year(), 12, 31, 0, 0, 0, 0, time.UTC)
52 1
	return time.Duration(lastDayOfTheYear.YearDay())
53
}
54
55
func getDaysInMonth() time.Duration {
56 1
	return time.Duration(getLastDayOfMonth(time.Now()).Day())
57
}
58
59
func getLastDayOfMonth(date time.Time) time.Time {
60 1
	return date.AddDate(0, 1, -date.Day())
61
}
62
63
func setup() {
64 1
	timeago.SetConfig(timeago.Config{
65
		Location: "Europe/Kiev",
66
	})
67
}
68