/
time.go
69 lines (57 loc) · 1.14 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
package utilz
import (
"errors"
"time"
)
const (
DateFormat = "2006-01-02"
TimeFormat = "2006-01-02 15:04:05"
)
var ErrFormatNotSupport = errors.New("format not support")
func Now() string {
return time.Now().Format(TimeFormat)
}
func Today() string {
return time.Now().Format(DateFormat)
}
func Yesterday() string {
return time.Now().AddDate(0, 0, -1).Format(DateFormat)
}
func Time() int64 {
return time.Now().Unix()
}
func ParseTime(s string) (time.Time, error) {
t, err := time.ParseInLocation(TimeFormat, s, time.Local)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation(DateFormat, s, time.Local)
if err == nil {
return t, nil
}
return time.Time{}, ErrFormatNotSupport
}
func FormatDate(t time.Time) string {
return t.Format(DateFormat)
}
func FormatTime(t time.Time) string {
return t.Format(TimeFormat)
}
func TimeMax(times ...time.Time) time.Time {
result := times[0]
for _, t := range times {
if t.After(result) {
result = t
}
}
return result
}
func TimeMin(times ...time.Time) time.Time {
result := times[0]
for _, t := range times {
if t.Before(result) {
result = t
}
}
return result
}