go - 使用默认时间调用 func Golang
问题描述
我的 func 开始。然后在 2 分钟后再次运行。如何在凌晨 3 点再次运行?我用户时间。打勾
func doEvery(d time.Duration, f func(time.Time)) {
for x := range time.Tick(d) {
f(x)
}
}
func RunUpdateTimeTable(respond chan<- string) {
respond <- "Start get timetables info service."
updateTimeTables(time.Now())
doEvery(2*time.Minute, updateTimeTables)
}
解决方案
这里是
func doAtEvery3am(f func(time.Time)) {
for {
// compute duration until 3am local time
t := time.Now()
t = time.Date(t.Year(), t.Month(), t.Day(), 3, 0, 0, 0, time.Local)
d := time.Until(t)
if d <= 0 {
d = time.Until(t.Add(24*time.Hour))
}
// wait duration and call f
f(<-time.After(d))
}
}
注意:我们在执行后重新计算持续时间到凌晨 3 点,f
因为执行持续时间f
是未知的。
推荐阅读
- json - 如何将存储在s3中的地图作为文件读取为spark中的json?
- swift - SwiftUI - “[Color]”类型的值没有“已识别”成员
- linq - 从一个表中求和“金额”并显示在另一个表中
- elasticsearch - Elasticsearch:如何使用 OR 条件查询数组字段?
- r - 从 451 个观测值的数据框中构建时间序列对象,这些观测值不规则地间隔 3 个变量
- php - 如何在 php 中设置连续的 kafka 消费者,而不会出现连接超时
- python - 通过 exec() 使用其他变量名称中的变量
- c++ - 如何使用OpenGL渲染到一个QMainWindow的两个QWidget?
- javascript - TypeError:store.getState 不是函数 React-redux
- awk - 使用 awk 删除重复值和排序值