-
Notifications
You must be signed in to change notification settings - Fork 1
/
times.go
118 lines (89 loc) · 2.97 KB
/
times.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package times
import (
"math"
"time"
)
// GetDateOfFirstMondayOfMonth : get date of first monday
func GetDateOfFirstMondayOfMonth(t time.Time) int {
dayFirstDateOfMonth := getDayOfFirstDateOfMonth(t)
if dayFirstDateOfMonth < 2 {
return 2 - dayFirstDateOfMonth
}
return 9 - dayFirstDateOfMonth
}
// GetDateOfFirstTuesdayOfMonth : get date of first tuesday
func GetDateOfFirstTuesdayOfMonth(t time.Time) int {
dayFirstDateOfMonth := getDayOfFirstDateOfMonth(t)
if dayFirstDateOfMonth < 3 {
return 3 - dayFirstDateOfMonth
}
return 10 - dayFirstDateOfMonth
}
// GetDateOfFirstWednesdayOfMonth : get date of first wednesday
func GetDateOfFirstWednesdayOfMonth(t time.Time) int {
dayFirstDateOfMonth := getDayOfFirstDateOfMonth(t)
if dayFirstDateOfMonth < 4 {
return 4 - dayFirstDateOfMonth
}
return 11 - dayFirstDateOfMonth
}
// GetDateOfFirstThursdayOfMonth : get date of first thursday
func GetDateOfFirstThursdayOfMonth(t time.Time) int {
dayFirstDateOfMonth := getDayOfFirstDateOfMonth(t)
if dayFirstDateOfMonth < 5 {
return 5 - dayFirstDateOfMonth
}
return 12 - dayFirstDateOfMonth
}
// GetDateOfFirstFridayOfMonth : get date of first friday
func GetDateOfFirstFridayOfMonth(t time.Time) int {
dayFirstDateOfMonth := getDayOfFirstDateOfMonth(t)
if dayFirstDateOfMonth < 6 {
return 6 - dayFirstDateOfMonth
}
return 13 - dayFirstDateOfMonth
}
// GetDateOfFirstSaturdayOfMonth : get date of first saturday
func GetDateOfFirstSaturdayOfMonth(t time.Time) int {
return 7 - getDayOfFirstDateOfMonth(t)
}
// GetDateOfFirstSaturdayOfMonth : get date of first sunday
func GetDateOfFirstSundayOfMonth(t time.Time) int {
dayFirstDateOfMonth := getDayOfFirstDateOfMonth(t)
if dayFirstDateOfMonth == 0 {
return 1
}
return 8 - dayFirstDateOfMonth
}
// GetNthWeekOfMonth : get {n}th Week of Month
// Regard the start of the week as Sunday
func GetNthWeekOfMonth(t time.Time) int {
date := t.Day()
daysOfFirstSaturdayOfMonth := GetDateOfFirstSaturdayOfMonth(t)
if daysOfFirstSaturdayOfMonth >= date {
return 1
}
return 1 + int(math.Ceil(float64(date-daysOfFirstSaturdayOfMonth)/7))
}
// GetLastTimeOfMonth : get Last TimeObject Of Request Month
func GetLastTimeOfMonth(t time.Time) time.Time {
return getFirstTimeOfMonth(t).AddDate(0, 1, -1)
}
// GetFirstWorkingTimeOfMonth : get First Working TimeObject Of Request Month
func GetFirstWorkingTimeOfMonth(year, month int) time.Time {
firstDateOfMonth := time.Date(year, MonthMapByInt[month], 1, 0, 0, 0, 0, time.UTC)
if firstDateOfMonth.Weekday() == 0 {
return firstDateOfMonth.AddDate(0, 0, 1)
} else if firstDateOfMonth.Weekday() == 6 {
return firstDateOfMonth.AddDate(0, 0, 2)
}
return firstDateOfMonth
}
// getDayOfFirstDateOfMonth : get day of first date
func getDayOfFirstDateOfMonth(t time.Time) int {
return DaysMap[(getFirstTimeOfMonth(t)).Weekday()]
}
// getFirstTimeOfMonth : First time of Month
func getFirstTimeOfMonth(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC)
}