英文:
get time and date using cron expression in golang
问题
我目前正在寻找一个解决方案,已经有一段时间了,
我有这个 cron 表达式
time := '0 3,10,16,22 * * ?'
我需要将其解析为日期并进行比较以获得结果。
我的目标是从 time 变量中获取时间数据并进行比较,
如果时间不在 00:00 和 00:06 之间,它将返回布尔值 false。
我知道可以使用 if 语句进行比较,但是,
如何解析这个 cron 表达式并将其转换为日期的解决方案尚未找到。
我已经阅读了一段时间的 godoc 中的 cron 包,但还没有找到,也许我漏掉了什么?
非常感谢任何解决方案或建议!
英文:
i'm currently looking for a solution for sometimes now,
i have this cron expression
time := '0 3,10,16,22 * * ?'
and i need to parse this into date and compare it to get a result
what my goal is to get time data from the time var and compare it,
if the time is not in between 00:00 and 00:06 it will return bool false
i understand for comparison i can use if clause but,
how to parse this cron expression and turn it into date solution were not found yet.
i've been reading cron package in godoc for sometimes and dont find it yet maybe i'm missing something?
any kind of solution or input were appreciated thanks!
答案1
得分: 1
你可以使用cronexpr
包来实现,该包来自aptible/supercronic
:
import "github.com/aptible/supercronic/cronexpr"
import "time"
nextTime := cronexpr.MustParse("0 3,10,16,22 * * ?").Next(time.Now())
现在你已经得到了下一个时间,你可以检查它是否在00:00到00:06之间。
英文:
You could use the package cronexpr
from aptible/supercronic
:
import "github.com/aptible/supercronic/cronexpr"
import "time"
nextTime := cronexpr.MustParse("0 3,10,16,22 * * ?").Next(time.Now())
Now that you have the next time, you can check if it is between 00:00 and 00:06.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论