dongque8332 2019-05-29 16:12
浏览 78
已采纳

如何为不同时区设置多个克朗

Following my question about cron job and timezone

I would like to know what would be the best way to schedule things at different time zones:

  • Something to run at : 16h30 Tokyo time
  • Then another to run at : 10AM London time
  • Another at 3PM NYC Time , etc etc.

I have multiple cron jobs like those that i need to run and my code from previous post doesn't seem to cut it.

I don't know what is the best way to proceed as it should be independent of the server time, so working with UTC time doesn't really cut it.

package main

import (
    "fmt"
    "github.com/robfig/cron"
    "os"
    "os/signal"
    "time"
)

func helloWorld() {
  fmt.Println("hello world")
}

func main() {
    s, err1 := cron.Parse("30 10 * * *")
    fmt.Println(err1)
    l, err := time.LoadLocation("Asia/Tokyo")
    fmt.Println(err)
    c := cron.NewWithLocation(l)
    c.Schedule(s, cron.FuncJob(helloWorld))
    c.Start()

    sig := make(chan os.Signal)
    signal.Notify(sig, os.Interrupt, os.Kill)
    <-sig
}

After my previous post i've found that you could check the scheduler this way :

    test := c.Entries()
    log.Println(test[0].Schedule)
    log.Println(test[0].Next)
    log.Println(test[0].Prev)

and got as result (the first 2 nils being the err "handling"):

    <nil>
    <nil>
    2019/05/30 00:59:21 &{1073741824 1024 9223372036871553023 9223372041149743102 9223372036854783998 9223372036854775935}
    2019/05/30 00:59:21 2019-05-30 01:10:30 +0900 JST
    2019/05/30 00:59:21 0001-01-01 00:00:00 +0000 UTC

You can see that it's scheduled to run at 01:10:30 Japan time ( JST ) instead of 10:30 AM JST that i expected.

Anybody know what's happening. I'm gonna need to do this for upward of 70 timezones..

  • 写回答

1条回答 默认 最新

  • dpca31461 2019-05-29 19:37
    关注

    As stated in the docs s, err1 := cron.Parse("30 10 * * *") creates a new cron job executed hourly at exactly 10mins and 30sec.

    Because your time was 2019/05/30 00:59:21 the next avaiable slot would be exactly like stated 01:10:30 +0900 JST.

    To create a new job executed daily at 10:30 local time use s, err1 := cron.Parse("0 30 10 * *") instead. For a quick overview use Predefined schedules in the docs, it displays it in a nice table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化