-
Notifications
You must be signed in to change notification settings - Fork 0
/
gotime_new.go
140 lines (120 loc) · 3.27 KB
/
gotime_new.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package gotime
import (
"time"
)
type GoTime struct {
Location *time.Location
}
//获取当前时间 年-月-日 时:分:秒
func (g *GoTime) Now() string {
return g.NowTime().Format(TT)
}
//获取当前时间戳
func (g *GoTime) NowUnix() int64 {
return g.NowTime().Unix()
}
//获取当前时间Time
func (g *GoTime) NowTime() time.Time {
return time.Now().In(g.Location)
}
//获取年月日
func (g *GoTime) Ymd() string {
return g.NowTime().Format(YMD)
}
//获取时分秒
func (g *GoTime) Hms() string {
return g.NowTime().Format(HMS)
}
//获取当天的开始时间, eg: 2018-01-01 00:00:00
func (g *GoTime) Start(day int) string {
now := g.NowTime().AddDate(0, 0, day)
tm := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, g.Location)
return tm.Format(TT)
}
//获取当天的结束时间, eg: 2018-01-01 23:59:59
func (g *GoTime) End(day int) string {
now := g.NowTime().AddDate(0, 0, day)
tm := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, g.Location)
return tm.Format(TT)
}
//当前时间 减去 多少秒
func (g *GoTime) Before(beforeSecond int64) string {
return time.Unix(g.NowUnix()-beforeSecond, 0).Format(TT)
}
//当前时间 加上 多少秒
func (g *GoTime) Next(beforeSecond int64) string {
return time.Unix(g.NowUnix()+beforeSecond, 0).Format(TT)
}
//2006-01-02T15:04:05Z07:00 转 时间戳
func (g *GoTime) RfcToUnix(s string) int64 { //转化所需模板
tm, err := time.ParseInLocation(time.RFC3339, s, g.Location) //使用模板在对应时区转化为time.time类型
if err != nil {
return int64(0)
}
return tm.Unix()
}
//2006-01-02 15:04:05 转 时间戳
func (g *GoTime) ToUnix(s string) int64 {
t, _ := time.ParseInLocation(TT, s, g.Location)
return t.Unix()
}
//获取RFC3339格式
func (g *GoTime) GetRFC3339() string {
return g.NowTime().Format(time.RFC3339)
}
//转换成RFC3339格式
func (g *GoTime) ToRFC3339(s string) string {
tm, err := time.ParseInLocation(TT, s, g.Location)
if err != nil {
return ""
}
return tm.Format(time.RFC3339)
}
//将RFC3339格式转成正常格式
func (g *GoTime) RFC3339To(s string) string {
t, err := time.ParseInLocation(RFC3339, s, g.Location)
if err != nil {
return "0000-00-00 00:00:00"
}
return t.Format(TT)
}
//获取格式化的数据
func (g *GoTime) GetFormat(format string) string {
return g.Format(g.NowTime(), format)
}
// Format time.Time struct to string
// MM - month - 01
// M - month - 1, single bit
// DD - day - 02
// D - day 2
// YYYY - year - 2006
// YY - year - 06
// HH - 24 hours - 03
// H - 24 hours - 3
// hh - 12 hours - 03
// h - 12 hours - 3
// mm - minute - 04
// m - minute - 4
// ss - second - 05
// s - second = 5
func (g *GoTime) Format(t time.Time, format string) string {
return Format(t, format)
}
//将格式化为一天的零点
func (g *GoTime) ParseStart(s, fromFormat string) (result string, err error) {
tt, err := time.Parse(fromFormat, s)
if err != nil {
return
}
tt = time.Date(tt.Year(), tt.Month(), tt.Day(), 0, 0, 0, 0, g.Location)
return tt.Format(TT), nil
}
//格式化为一天的最后一刻
func (g *GoTime) ParseEnd(s, fromFormat string) (result string, err error) {
tt, err := time.Parse(fromFormat, s)
if err != nil {
return
}
tt = time.Date(tt.Year(), tt.Month(), tt.Day(), 23, 59, 59, 0, g.Location)
return tt.Format(TT), nil
}