1
|
|
|
package tests |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"time" |
5
|
|
|
) |
6
|
|
|
|
7
|
|
|
const ( |
8
|
|
|
second time.Duration = time.Second |
9
|
|
|
minute time.Duration = time.Minute |
10
|
|
|
hour time.Duration = time.Hour |
11
|
|
|
day time.Duration = hour * 24 |
12
|
|
|
week time.Duration = day * 7 |
13
|
|
|
// we cannot add month and year because months and years cannot have static values |
14
|
|
|
) |
15
|
|
|
|
16
|
|
|
func subTime(duration time.Duration) time.Time { |
17
|
1 |
|
return time.Now().Add(-duration) |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
func subSeconds(duration time.Duration) time.Time { |
21
|
1 |
|
return subTime(second * duration) |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
func subMinutes(duration time.Duration) time.Time { |
25
|
1 |
|
return subTime(minute * duration) |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
func subHours(duration time.Duration) time.Time { |
29
|
1 |
|
return subTime(hour * duration) |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
func subDays(duration time.Duration) time.Time { |
33
|
1 |
|
return subTime(day * duration) |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
func subWeeks(duration time.Duration) time.Time { |
37
|
1 |
|
return subTime(week * duration) |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
func subMonths(duration time.Duration) time.Time { |
41
|
1 |
|
return subTime(duration * getDaysInMonth() * day) |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
func subYears(duration time.Duration) time.Time { |
45
|
1 |
|
return subTime(duration * getDaysInYear() * day) |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
func getDaysInYear() time.Duration { |
49
|
1 |
|
lastDayOfTheYear := time.Date(time.Now().Year(), 12, 31, 0, 0, 0, 0, time.UTC) |
50
|
1 |
|
return time.Duration(lastDayOfTheYear.YearDay()) |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
func getDaysInMonth() time.Duration { |
54
|
1 |
|
return time.Duration(getLastDayOfMonth(time.Now()).Day()) |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
func getLastDayOfMonth(date time.Time) time.Time { |
58
|
1 |
|
return date.AddDate(0, 1, -date.Day()) |
59
|
|
|
} |
60
|
|
|
|