time.go
3.5 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package utils
import (
"github.com/linmadan/egglib-go/utils/xtime"
"strconv"
"time"
)
func GetDayBegin() time.Time {
t := time.Now()
today, _ := time.Parse("2006-01-02", t.Format("2006-01-02"))
return today
}
func GetDayEnd() time.Time {
t := GetDayBegin()
nextDay := t.AddDate(0, 0, 1)
return nextDay
}
// GetFirstDateOfMonth 获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
func GetFirstDateOfMonth(d time.Time) time.Time {
d = d.AddDate(0, 0, -d.Day()+1)
return GetZeroTime(d)
}
// GetNextMonthFirstDay 获取传入的时间所在月份的最后一天,即某月最后一天的23:59:59。如传入time.Now(), 返回当前月份的最后一天的23:59:59。
func GetNextMonthFirstDay(d time.Time) time.Time {
d = GetFirstDateOfMonth(d).AddDate(0, 1, 0)
return GetZeroTime(d)
}
// GetNextMonthFirstDay 获取传入的时间所在月份的最后一天,即某月最后一天的23:59:59。如传入time.Now(), 返回当前月份的最后一天的23:59:59。
func GetCurrentMonthFirstDay(t time.Time) time.Time {
y, m, _ := t.Date()
result := time.Date(y, m, 1, 0, 0, 0, 0, time.Local)
return result
}
func GetCurrentWeekFirstDay(t time.Time) time.Time {
d := t.Weekday()
result := GetZeroTime(t)
if d == time.Sunday {
result = result.Add(-time.Hour * 24 * 6)
} else {
result = result.Add(-time.Hour * 24 * (time.Duration(d) - 1))
}
return result
}
// GetZeroTime 获取某一天的0点时间
func GetZeroTime(d time.Time) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, time.Local)
}
// GetZeroTime 获取某一天的0点时间
func GetZeroTimeWithLocal(d time.Time, loc *time.Location) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, loc)
}
// GetNextDayZeroTime 获取某一天的23点59分59秒
func GetNextDayZeroTime(d time.Time) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, 1)
}
// TransformTimestampToTime 根据13位时间戳返回日期时间格式时间
func TransformTimestampToTime(timeStamp int64) time.Time {
t := strconv.FormatInt(timeStamp, 10)
tIpartStr := t[0:10]
//tDecpartStr := t[10:13]
ipart, _ := strconv.ParseInt(tIpartStr, 10, 64)
//decpart, _ := strconv.ParseInt(tDecpartStr, 10, 64)
myTime := time.Unix(ipart, 0)
//(myTime)
return time.Date(myTime.Year(), myTime.Month(), myTime.Day(), myTime.Hour(), myTime.Minute(), myTime.Second(), 0, time.Local)
}
func TimeIsZero(d time.Time) bool {
if d.IsZero() {
return true
}
if d.Equal(time.Date(1, 1, 1, 0, 0, 0, 0, time.Local)) {
return true
}
return false
}
func TimeAfterEqual(t1, t2 time.Time) bool {
if t1.After(t2) {
return true
}
if t1.Equal(t2) {
return true
}
return false
}
func TimeBeforeEqual(t1, t2 time.Time) bool {
if t1.Before(t2) {
return true
}
if t1.Equal(t2) {
return true
}
return false
}
type TimeString time.Time
func (timeString *TimeString) UnmarshalJSON(data []byte) error {
if len(data) == 2 {
return nil
}
t, err := xtime.Parse(string(data[1 : len(data)-1]))
if err != nil {
return err
}
*timeString = TimeString(t)
return nil
}
func (timeString TimeString) MarshalJSON() ([]byte, error) {
if xtime.IsZero(time.Time(timeString)) {
return []byte(`""`), nil
}
b := make([]byte, 0)
b = append(b, '"')
b = time.Time(timeString).AppendFormat(b, time.RFC3339)
b = append(b, '"')
return b, nil
}
func (timeString TimeString) Time() time.Time {
return time.Time(timeString)
}