/
time.go
172 lines (147 loc) · 4.32 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* @Author: calmwu
* @Date: 2017-09-18 10:36:09
* @Last Modified by: calmwu
* @Last Modified time: 2018-05-17 11:23:51
* @Comment:
*/
package base
import (
"fmt"
"strconv"
"time"
)
func GetDate() string {
now := time.Now()
year, month, day := now.Date()
return fmt.Sprintf("%d%02d%02d", year, month, day)
}
func GetDateByLocation(location *time.Location) string {
now := time.Now().In(location)
year, month, day := now.Date()
return fmt.Sprintf("%d%02d%02d", year, month, day)
}
func GetTimeByTz(tz string) (*time.Time, error) {
localtion, err := time.LoadLocation(tz)
if err != nil {
return nil, err
}
now := time.Now().In(localtion)
return &now, nil
}
func GetDateNum(location *time.Location) int {
var dateName string
if location == nil {
dateName = GetDate()
} else {
dateName = GetDateByLocation(location)
}
dateNum, _ := strconv.ParseInt(dateName, 10, 32)
return int(dateNum)
}
func GetDateNum2(now *time.Time) int {
year, month, day := now.Date()
dateName := fmt.Sprintf("%d%02d%02d", year, month, day)
dateNum, _ := strconv.ParseInt(dateName, 10, 32)
return int(dateNum)
}
func GetDateHour() string {
now := time.Now()
year, month, day := now.Date()
return fmt.Sprintf("%d%02d%02d%02d", year, month, day, now.Hour())
}
func GetTimeStampMs() string {
return time.Now().Format("2006-01-02 15:04:05.000")
}
func GetTimeStampSec() string {
return time.Now().Format("2006-01-02 15:04:05")
}
// const (
// ANSIC = "Mon Jan _2 15:04:05 2006"
// UnixDate = "Mon Jan _2 15:04:05 MST 2006"
// RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
// RFC822 = "02 Jan 06 15:04 MST"
// // RFC822 with numeric zone RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
// RFC822Z = "02 Jan 06 15:04 -0700"
// RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
// // RFC1123 with numeric zone RFC3339 = "2006-01-02T15:04:05Z07:00"
// RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700"
// RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
// // Handy time stamps. Stamp = "Jan _2 15:04:05"
// Kitchen = "3:04PM"
// StampMilli = "Jan _2 15:04:05.000"
// StampMicro = "Jan _2 15:04:05.000000"
// StampNano = "Jan _2 15:04:05.000000000"
// )
func TimeName(t time.Time) string {
return t.Format("2006-01-02 15:04:05 -0700")
}
func PrintPerformTimeConsuming(funcName string, startTime time.Time) {
timeConumeSeconds := time.Now().Sub(startTime).Seconds()
GLog.Debug("function[%s] using [%f] seconds", funcName, timeConumeSeconds)
}
func GetDayEndTimeLocal() time.Time {
year, month, day := time.Now().Date()
endTime := time.Date(year, month, day, 23, 59, 59, 0, time.Local)
return endTime
}
func GetDayEndTimeUtc() time.Time {
year, month, day := time.Now().UTC().Date()
endTime := time.Date(year, month, day, 23, 59, 59, 0, time.UTC)
return endTime
}
func GetNextDayStartTimeLocal() time.Time {
year, month, day := time.Now().Date()
startTime := time.Date(year, month, day+1, 0, 0, 0, 0, time.Local)
return startTime
}
func GetNextDayStartTimeUtc() time.Time {
year, month, day := time.Now().UTC().Date()
startTime := time.Date(year, month, day+1, 0, 0, 0, 0, time.UTC)
return startTime
}
func GetNextDayStartTimeByLocation(location *time.Location) time.Time {
year, month, day := time.Now().In(location).Date()
startTime := time.Date(year, month, day+1, 0, 0, 0, 0, location)
return startTime
}
func GetWeekName(location *time.Location) int {
if location == nil {
location = time.Local
}
year, week := time.Now().In(location).ISOWeek()
weekName, _ := strconv.ParseInt(fmt.Sprintf("%d%02d", year, week), 10, 32)
return int(weekName)
}
func GetWeekDay(location *time.Location) int32 {
if location == nil {
location = time.Local
}
return int32(time.Now().In(location).Weekday())
}
func GetMonthName(location *time.Location) int {
if location == nil {
location = time.Local
}
year, month, _ := time.Now().In(location).Date()
monthName, _ := strconv.ParseInt(fmt.Sprintf("%d%02d", year, month), 10, 32)
return int(monthName)
}
// 计算某年某月的天数
func GetMonthlyDayCount(year int, month int) int {
var days int
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
}
}
return days
}