带有偏移量的时间
This commit is contained in:
parent
165e99f08e
commit
e1c216ffe2
|
@ -0,0 +1,20 @@
|
||||||
|
package time
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// Time 带有偏移量的时间
|
||||||
|
type Time struct {
|
||||||
|
offset time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *Time) SetOffset(offset time.Duration) {
|
||||||
|
slf.offset = offset
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *Time) Now() time.Time {
|
||||||
|
return time.Now().Add(slf.offset)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *Time) Since(t time.Time) time.Duration {
|
||||||
|
return slf.Now().Sub(t)
|
||||||
|
}
|
|
@ -13,6 +13,7 @@ const (
|
||||||
IntervalHour
|
IntervalHour
|
||||||
IntervalMinute
|
IntervalMinute
|
||||||
IntervalSecond
|
IntervalSecond
|
||||||
|
IntervalNow
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -22,6 +23,7 @@ var (
|
||||||
IntervalHour: "小时前",
|
IntervalHour: "小时前",
|
||||||
IntervalMinute: "分钟前",
|
IntervalMinute: "分钟前",
|
||||||
IntervalSecond: "秒钟前",
|
IntervalSecond: "秒钟前",
|
||||||
|
IntervalNow: "刚刚",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -40,7 +42,7 @@ func IntervalFormat(time1, time2 time.Time) string {
|
||||||
cur := time1.Unix()
|
cur := time1.Unix()
|
||||||
ct := cur - time2.Unix()
|
ct := cur - time2.Unix()
|
||||||
if ct <= 0 {
|
if ct <= 0 {
|
||||||
return "刚刚"
|
return intervalFormat[IntervalNow]
|
||||||
}
|
}
|
||||||
var res string
|
var res string
|
||||||
for i := 0; i < len(byTime); i++ {
|
for i := 0; i < len(byTime); i++ {
|
||||||
|
|
Loading…
Reference in New Issue