/
dtutil.go
129 lines (101 loc) · 2.3 KB
/
dtutil.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
package utils
import (
"fmt"
"time"
"strings"
)
func GoStdTime()string{
return "2006-01-02 15:04:05"
}
func GoStdUnixDate()string{
return "Mon Jan _2 15:04:05 MST 2006"
}
func GoStdRubyDate()string{
return "Mon Jan 02 15:04:05 -0700 2006"
}
func GetTmStr(tm time.Time,format string)(string){
patterns := []string{
"y","2006",
"m","01",
"d","02",
"Y","2006",
"M","01",
"D","02",
"h","03", //12小时制
"H","15", //24小时制
"i","04",
"s","05",
"t","pm",
"T","PM",
}
return convStr(tm,format,patterns)
}
func GetTmShortStr(tm time.Time,format string)(string){
patterns := []string{
"y","06",
"m","1",
"d","2",
"Y","06",
"M","1",
"D","2",
"h","3", //12小时制
"H","15", //24小时制
"i","4",
"s","5",
"t","pm",
"T","PM",
}
return convStr(tm,format,patterns)
}
func convStr(tm time.Time,format string,patterns []string)(string){
replacer := strings.NewReplacer(patterns...)
strfmt := replacer.Replace(format)
return tm.Format(strfmt)
}
func GetLocaltimeStr()(string){
now := time.Now().Local()
year,mon,day := now.Date()
hour,min,sec := now.Clock()
zone,_ := now.Zone()
return fmt.Sprintf("%d-%d-%d %02d:%02d:%02d %s",year,mon,day,hour,min,sec,zone)
}
func GetGmtimeStr()(string){
now := time.Now()
year,mon,day := now.UTC().Date()
hour,min,sec := now.UTC().Clock()
zone,_ := now.UTC().Zone()
return fmt.Sprintf("%d-%d-%d %02d:%02d:%02d %s",year,mon,day,hour,min,sec,zone)
}
func GetUnixTimeStr(ut int64,format string)(string){
t := time.Unix(ut,0)
return GetTmStr(t,format)
}
func GetUnixTimeShortStr(ut int64,format string)(string){
t := time.Unix(ut,0)
return GetTmShortStr(t,format)
}
func Greatest(arr []time.Time)(time.Time){
var temp time.Time
for _,at := range arr {
if temp.Before(at) {
temp = at
}
}
return temp;
}
type TimeSlice []time.Time
func (s TimeSlice) Len() int {
return len(s)
}
func (s TimeSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s TimeSlice) Less(i, j int) bool {
if s[i].IsZero() {
return false
}
if s[j].IsZero() {
return true
}
return s[i].Before(s[j])
}