-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
65 lines (56 loc) · 1.45 KB
/
time.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
package rice
import (
"strconv"
"strings"
"time"
)
func TodayZeroTime() time.Time {
now := time.Now()
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
}
func TodayZeroTimestamp() int64 {
now := time.Now()
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()
}
func ZeroTime(tm time.Time) time.Time {
return time.Date(tm.Year(), tm.Month(), tm.Day(), 0, 0, 0, 0, time.Local)
}
func ZeroTimestamp(ts int64) int64 {
tm := time.Unix(ts, 0)
return time.Date(tm.Year(), tm.Month(), tm.Day(), 0, 0, 0, 0, time.Local).Unix()
}
// CountWeek 获取当前日期为当月第几周
func CountWeek(TimeFormat string) int {
t, _ := time.ParseInLocation("2006-01-02 15:04:05", TimeFormat, time.Local)
month := t.Month()
year := t.Year()
days := 0
if month != 2 {
if month == 4 || month == 6 || month == 9 || month == 11 {
days = 30
} else {
days = 31
}
} else {
if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
days = 29
} else {
days = 28
}
}
week := 1
for i := 1; i <= days; i++ {
dayString := strconv.Itoa(i)
if i < 10 {
dayString = "0" + dayString
}
dateString := strings.Split(TimeFormat, "-")[0] + "-" + strings.Split(TimeFormat, "-")[1] + "-" + dayString + " 18:30:50"
t1, _ := time.ParseInLocation("2006-01-02 15:04:05", dateString, time.Local)
if t.YearDay() > t1.YearDay() {
if t1.Weekday().String() == "Sunday" {
week++
}
}
}
return week
}