-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
83 lines (69 loc) · 1.71 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
package util
import (
"fmt"
"time"
)
var LocalCN, _ = time.LoadLocation("Asia/Shanghai")
var LocalGMT, _ = time.LoadLocation("GMT")
var UnixZero = time.Unix(0, 0)
func TrimTimeToDate(t time.Time) time.Time {
ymd := t.In(LocalCN).Format("2006-01-02")
day, _ := time.ParseInLocation("2006-01-02", ymd, LocalCN)
return day
}
func TrimUnixMillisecondToDate(unixMillisecond int64) time.Time {
return TrimTimeToDate(UnixMillisecondToTime(unixMillisecond))
}
func GetUnixMillisecond(time time.Time) int64 {
millisecond := time.UnixNano() / 1e6
if millisecond < 0 {
return 0
}
return millisecond
}
func GetNowUnixMillisecond() int64 {
return GetUnixMillisecond(time.Now())
}
func UnixMillisecondToTime(unixMillisecond int64) time.Time {
if unixMillisecond <= 0 {
return UnixZero
}
second := unixMillisecond / 1000
nanoseconds := (unixMillisecond - second*1000) * 1e6
return time.Unix(second, nanoseconds)
}
func IsInvalidTime(time time.Time) bool {
return GetUnixMillisecond(time) == 0
}
func Ptr2Time(t *time.Time) time.Time {
if t == nil {
return UnixZero
}
return *t
}
func Time2Ptr(t time.Time) *time.Time {
if IsInvalidTime(t) {
return nil
}
return &t
}
func Ptr2UnixMillisecond(t *time.Time) int64 {
return GetUnixMillisecond(Ptr2Time(t))
}
func FormatUnixMillisecond(unixMillisecond int64, layout string, local *time.Location) string {
t := UnixMillisecondToTime(unixMillisecond)
if local != nil {
t = t.In(local)
}
return t.Format(layout)
}
func FormatMinutes(minutes uint64) string {
hours := minutes / 60
return fmt.Sprintf("%d:%d", hours, minutes-hours*60)
}
func DurationFallback(duration, fallback time.Duration) time.Duration {
if duration > 0 {
return duration
}
return fallback
}