Skip to content

Commit

Permalink
feat: add WaitUntilMinute() and WaitUntilSecond()
Browse files Browse the repository at this point in the history
  • Loading branch information
fufuok committed Mar 29, 2024
1 parent b28dd4a commit 6a78827
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ func InitLocation(name string) (*time.Location, bool) {
return loc, true
}

// WaitUntilMinute 等待, 直到 m 分钟
func WaitUntilMinute(m int, t ...time.Time) {
var now time.Time
if len(t) > 0 {
now = t[0]
} else {
now = time.Now()
}
n := m - now.Minute()
if n < 0 {
n += 60
}
time.Sleep(time.Duration(n) * time.Minute)
}

// WaitNextMinute 下一分钟, 对齐时间, 0 秒
func WaitNextMinute(t ...time.Time) {
_ = WaitNextMinuteWithTime(t...)
Expand All @@ -51,6 +66,21 @@ func WaitNextMinuteWithTime(t ...time.Time) (now time.Time) {
return
}

// WaitUntilSecond 等待, 直到 s 秒
func WaitUntilSecond(s int, t ...time.Time) {
var now time.Time
if len(t) > 0 {
now = t[0]
} else {
now = time.Now()
}
n := s - now.Second()
if n < 0 {
n += 60
}
time.Sleep(time.Duration(n) * time.Second)
}

// WaitNextSecond 下一秒, 对齐时间, 0 毫秒 (近似)
func WaitNextSecond(t ...time.Time) {
_ = WaitNextSecondWithTime(t...)
Expand Down

0 comments on commit 6a78827

Please sign in to comment.